Skip to main content

Getting Started

Dim is a React-inspired framework for functional web components. You write components with hooks (useState, useEffect, …) and register them as custom elements. Templates use Dim’s own html tagged template literals — JSX-like syntax, not React JSX and not lit-html.

Apps load Dim from the CDN into index.html. No npm install, no bundler.

Live demos: dim.positive-intentions.com. Written docs live here.


Load from the CDN​

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My Dim app</title>
</head>
<body>
<my-counter></my-counter>

<script src="https://positive-intentions.github.io/dim/lib/dim.global.js"></script>
<script src="./app.js"></script>
</body>
</html>

Use dim.wasm.global.js (or ?impl=wasm in dimui / dim-boilerplate) for the Rust/WASM runtime. See JS vs WASM and Classic scripts.

Serve over HTTP (python3 -m http.server) — file:// will not load the CDN reliably.


Quick example​

In app.js (after the Dim script):

const { define } = Dim;

const Counter = (props, { useState, html, css, useStyle }) => {
const [count, setCount] = useState(0);

useStyle(css`
.counter {
padding: 2rem;
text-align: center;
}
button {
background: #029cfd;
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: 4px;
cursor: pointer;
}
`);

return html`
<div class="counter">
<h2>Count: ${count}</h2>
<button @click="${() => setCount(count + 1)}">Increment</button>
</div>
`;
};

define({ tag: "my-counter", component: Counter });

define() creates a DimElement host (open shadow root), injects hooks and html/css via the second argument, and registers the custom element. Prefer those injected deps over grabbing hooks from Dim at the top of every file.


Template syntax​

Dim templates look like JSX inside a tagged template:

PatternMeaning
html`<button @click=${fn}>`Event listener
html`<input .value=${v}>`Property binding
html`<div class=${cls}>`Attribute
html`${items.map(...)}`Nested templates

Always use a real template literal (html`...`). Plain strings throw. Event handlers use @click, not onclick=.


Next steps​