Skip to main content

Composition Patterns

How Dim components nest: useScope, stamped tags from define, children/slots, keyed lists, and .props for object graphs.


useScope​

Register child components so their tags resolve in the parent’s template:

const Child = (props, { html }) => html`<span>${props.label}</span>`;
define({ tag: "my-child", component: Child });

const Parent = (props, { html, useScope }) => {
useScope({ Child }); // uses Child.tag stamped by define()
// or: useScope({ "my-child": Child });

return html`<my-child .props=${{ label: "Hi" }}></my-child>`;
};

define({ tag: "my-parent", component: Parent });

If the tag cannot be resolved (no .tag, non-hyphenated key), Dim logs a warning and skips registration.


Children and slots​

Light-DOM children are captured when the host connects. Prefer composition via nested custom elements and .props for data. renderChildren(string) renders text, not HTML — wrap trusted markup with unsafeHTML:

const { html, unsafeHTML, renderChildren } = Dim;

renderChildren("<b>x</b>"); // text: literally <b>x</b>
html`${unsafeHTML("<b>x</b>")}`; // HTML (trusted only)

See Props & attributes for the attribute vs .props contract.


keyed lists​

keyed is Dim’s own directive (not Lit). Use it when list identity matters across updates:

// Inside component(props, { html, keyed }) — or from Dim
html`
<ul>
${items.map((item) =>
keyed(
item.id,
html`<li>${item.text}</li>`,
),
)}
</ul>
`;

Nested .props​

Pass functions and nested objects through .props — never through HTML attributes (JSON-only, no functions):

html`
<screen-host
.props=${{
viewId,
onBack: () => pop(),
data: { title, body },
}}
></screen-host>
`;

Secrets belong in useStore with { encryptionKey }, not in attributes or .props that end up inspectable in the DOM.


useLazyScope (experimental)​

useLazyScope("remote-widget", fetchModuleAsString());

This evaluates a code string with new Function. Trusted sources only — not safe for untrusted/remote input. Prefer registering components with define / useScope for CDN apps.