Rocket

Rocket is currently in beta and its API is subject to change.

Overview #

Rocket is Datastar’s web component API. You define a custom element with rocket(tagName, { ... }), describe public props with codecs, put non-DOM instance behavior in setup, use onFirstRender only when work depends on rendered refs or mounted DOM, and return DOM from render.

Rocket is a JavaScript API built around the browser’s custom-element model, with Datastar handling reactivity, local signal scoping, action dispatch, and DOM application.

Bundle #

Rocket is available in the separate datastar-rocket.js bundle. It includes Datastar and Rocket, so load it instead of datastar.js. A matching datastar-rocket.d.ts declaration bundle is available for TypeScript and editor autocomplete.

1<script type="module" src="/path/to/datastar-rocket.js"></script>

Usage #

 1import { rocket } from '/path/to/datastar-rocket.js'
 2
 3rocket('demo-counter', {
 4  mode: 'light',
 5  props: ({ number, string }) => ({
 6    count: number.step(1).min(0),
 7    label: string.trim.default('Counter'),
 8  }),
 9  setup: ({ $$, observeProps, props }) => {
10    $$.count = props.count
11    observeProps(() => {
12      $$.count = props.count
13    }, 'count')
14  },
15  render: ({ html, props: { count, label } }) => {
16    return html`
17      <div class="stack gap-2">
18        <button type="button" data-on:click="$$count += 1">
19          ${label}: <span data-text="$$count"></span>
20        </button>
21        <template data-if="$$count !== ${count}">
22          <button type="button" data-on:click="$$count = ${count}">Reset</button>
23        </template>
24      </div>
25    `
26  },
27})

The result is a real web component. Once defined, you use it like any other custom element.

1<script type="module" src="/path/to/demo-counter.js"></script>
2
3<demo-counter count="5" label="Inventory"></demo-counter>

tag #

tag is the first argument to rocket(...). It must contain a hyphen, must be unique, and becomes the actual HTML tag users place in the page.

1rocket(tag: string, options?: RocketDefinition<Defs>): void
 1rocket('demo-user-card', {
 2  props: ({ string }) => ({
 3    name: string.default('Anonymous'),
 4  }),
 5  render: ({ html, props: { name } }) => {
 6    return html`
 7        <p>${name}</p>
 8    `
 9  },
10})

Rocket registers the element with customElements.define. Re-registering the same tag is ignored, which makes repeated module evaluation safe during development.

Definition #

A Rocket definition describes one custom-element type. Each field controls a specific part of the browser component lifecycle.

FieldWhat it does
modeChooses light DOM, open shadow DOM, or closed shadow DOM.
ignoredByMorphControls incoming morph updates to the host and its children; defaults to false.
delegatesFocusDelegates focus into a shadow root; defaults to false.
formAssociatedOpts into native form participation and automatic submission of a declared value prop.
formValueOverrides automatic submission with a derived value, or false for manual control.
onFormAssociated, onFormReset, onFormDisabled, onFormStateRestoreHandle native form lifecycle callbacks at the top level of the definition.
propsDefines public props, decoding, defaults, and attribute reflection.
setupRuns once per connected instance to create local state, prop observers, timers, host APIs, and cleanup.
onFirstRenderRuns after the initial render and Datastar apply pass, when refs and rendered DOM exist.
renderReturns the component DOM as Rocket html or svg.
renderOnPropChangeControls whether prop updates trigger rerendering.
manifestAdds slot and event metadata to Rocket’s generated component manifest.

mode #

mode chooses where the component renders.

1mode?: 'open' | 'closed' | 'light'
ValueMount targetWhen to use it
'light'The host element itself.Use when the component should participate directly in the page’s DOM and CSS.
'open'An open shadow root.Use when you want style encapsulation but still want element.shadowRoot.
'closed'A closed shadow root.Use when the internal DOM should stay fully encapsulated.

If you omit mode, Rocket uses shadow DOM and defaults to 'open'.

Rocket defaults to 'open' because it gives you native slots and a normal shadow-root debugging surface without giving up the main styling mechanism components should rely on: CSS custom properties. CSS variables pierce the shadow boundary, so the usual pattern is to keep component structure encapsulated while letting parents theme it through --tokens.

ignoredByMorph #

1ignoredByMorph?: boolean | 'children'

Use ignoredByMorph when a component owns DOM that incoming server morphs should preserve. It is a top-level definition option, at the same level as props and render.

ValueIncoming morph behaviorWhen to use it
false (default)