Examples
Runnable Chroma apps to copy from - a scaffolded .chr app, a hooks tour, and a larger single-file app.
Scaffolded .chr app
This is the code chroma new generates. Create it yourself:
chroma new demo --template dynamic
cd demo
chroma devThe counter component, from the template:
// src/components/Counter.chr
import { hookState, e } from "../../pkg/chroma.js";
export function Counter() {
const [count, setCount] = hookState(0);
return e(
"button",
{ onClick: () => setCount(count() + 1) },
"Count: ",
count,
);
}Mounted by the generated App:
// src/App.chr
import { e } from "../pkg/chroma.js";
import { Counter } from "./components/Counter.chr";
export function App() {
return e("main", null, e("h1", null, "Chroma app"), e(Counter));
}Real files in the repository: Counter.chr, App.chr, main.chr. The standard template adds storage.chr.
Filtered list
A search box filtering a list with useMemo and For:
// src/components/TodoList.chr
import { hookState, useMemo, e, For } from "../../pkg/chroma.js";
export function TodoList() {
const [query, setQuery] = hookState("");
const [todos] = hookState([
{ id: 1, text: "Write docs" },
{ id: 2, text: "Ship it" },
]);
const visible = useMemo(
() => todos().filter((t) => t.text.toLowerCase().includes(query().toLowerCase())),
[todos, query],
);
return e(
"section",
null,
e("input", { value: query, onInput: (ev) => setQuery(ev.target.value) }),
e("ul", null, e(For, { each: () => visible(), children: (t) => e("li", null, t.text) })),
);
}More on this pattern: Derived state.
Hooks demo (example/index.html)
Both remaining examples live under Chroma/example/ and run against pkg/chroma.js with no bundler.
A single-file tour of every hook (hookState, useEffect, useLayoutEffect, useMemo, useCallback, useRef, useReducer, createContext / useContext, useDebugValue) plus a Button built with cva (Components). It also defines custom hooks (useCounter below) that only compose the primitives.
// example/index.html (inline <script type="module">)
import {
hookState, render, e, Fragment,
useEffect, useLayoutEffect, useMemo, useCallback,
useRef, useReducer, createContext, useContext, useDebugValue,
cva,
} from "../pkg/chroma.js";
// Reusable component with variants: same Button, different look per props —
// cva resolves the class; the component stays a plain function built with e().
const buttonClass = cva({
base: "btn",
variants: {
variant: { primary: "btn-primary", ghost: "btn-ghost" },
size: { sm: "btn-sm", lg: "btn-lg" },
},
compoundVariants: [
{ variant: "primary", size: "lg", class: "btn-primary-lg" },
],
defaultVariants: { variant: "primary", size: "sm" },
});
function Button(props) {
return e(
"button",
{ class: buttonClass(props), onClick: props.onClick },
props.children,
);
}
// Context: shares [theme, setTheme] with any descendant.
const Theme = createContext(null);
// Custom hook composing hookState + useMemo + useCallback.
function useCounter(initial) {
const [count, setCount] = hookState(initial);
const doubled = useMemo(() => count() * 2, [count]);
const inc = useCallback(() => setCount(count() + 1));
useDebugValue("useCounter");
return { count, doubled, inc };
}
function Counter() {
const { count, doubled, inc } = useCounter(0);
useEffect(() => {
document.title = `Chroma — count ${count()}`;
return () => console.log("cleanup: count was", count());
}, [count]);
return e(
"section", null,
e("h2", null, "hookState + useMemo + useEffect"),
e("button", { onClick: inc }, "Count: ", count),
e("p", null, "Doubled (useMemo): ", doubled),
);
}
function Reducer() {
const [total, dispatch] = useReducer((state, action) => {
switch (action) {
case "add": return state + 1;
case "sub": return state - 1;
case "reset": return 0;
default: return state;
}
}, 0);
return e(
"section", null,
e("h2", null, "useReducer"),
e("button", { onClick: () => dispatch("sub") }, "−"),
e("strong", null, " ", total, " "),
e("button", { onClick: () => dispatch("add") }, "+"),
e("button", { onClick: () => dispatch("reset") }, "reset"),
);
}
function Focusable() {
const inputRef = useRef(null);
// Synchronous, before paint: the node is already in inputRef.current.
useLayoutEffect(() => {
inputRef.current.placeholder = "set by useLayoutEffect";
}, []);
return e(
"section", null,
e("h2", null, "useRef + useLayoutEffect"),
e("input", { ref: inputRef }),
e("button", { onClick: () => inputRef.current.focus() }, "Focus"),
);
}
function ThemeButton() {
const [theme, setTheme] = useContext(Theme);
return e(
"button",
{ onClick: () => setTheme(theme() === "light" ? "dark" : "light") },
"Toggle theme (current: ", () => theme(), ")",
);
}
function Buttons() {
return e(
"section", null,
e("h2", null, "cva — reusable component with variants"),
e(Button, { variant: "primary", size: "sm" }, "Save"),
e(Button, { variant: "ghost", size: "sm" }, "Cancel"),
e(Button, { variant: "primary", size: "lg" }, "Confirm"),
);
}
function App() {
const [theme, setTheme] = hookState("light");
// Provider children go as a function (lazy) so useContext
// sees them with the provider already active.
return e(Theme.Provider, { value: [theme, setTheme] }, () =>
e(
"main",
{ class: theme }, // reactive attribute: a getter as the value
e("h1", null, "Chroma — hooks"),
e(ThemeButton),
e(Counter),
e(Reducer),
e(Focusable),
e(Buttons),
),
);
}
render(App, document.getElementById("app"));Run it from the Chroma/ directory of the repository:
./build.sh
python3 -m http.server 8000
# open http://localhost:8000/example/index.htmlLarger app (example/targapps_kvdb.html)
A larger, single-file CRM-style app (about 1,200 lines) that exercises the full surface together: hookState, useEffect, Fragment, and the two control-flow components, Show and For.
// example/targapps_kvdb.html (inline <script type="module">)
import { hookState, render, e, Fragment, Show, For, useEffect } from "../pkg/chroma.js";It models a login screen plus a dashboard with clients and projects stored in local component state (persisted to localStorage between reloads), and demonstrates:
Showfor gating the dashboard behind a login screen (when: loggedIn,fallback: () => e(LoginScreen)).Forfor rendering the clients and projects tables from arrays kept inhookState, rebuilding rows whenever a new array reference is written (see Control Flow for why reference identity matters).useEffectfor side effects like syncingdocument.titleand driving a confirmation toast.- A modal-driven create/edit flow for clients and projects, with form state tracked in a single
hookStateobject and updated via the updater form (setFormData(f => ({ ...f, ...updates }))). - Plain data-driven UI: no routing library, no state-management library — just signals and functions.
Use this file as a reference for structuring a bigger app: keep data in hookState/useReducer at the top of App, derive views with useMemo, and delegate branching/lists to Show/For instead of hand-rolled conditionals over DOM nodes.