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.
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>): voidRocket 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.
| Field | What it does |
|---|---|
mode | Chooses light DOM, open shadow DOM, or closed shadow DOM. |
ignoredByMorph | Controls incoming morph updates to the host and its children; defaults to false. |
delegatesFocus | Delegates focus into a shadow root; defaults to false. |
formAssociated | Opts into native form participation and automatic submission of a declared value prop. |
formValue | Overrides automatic submission with a derived value, or false for manual control. |
onFormAssociated, onFormReset, onFormDisabled, onFormStateRestore | Handle native form lifecycle callbacks at the top level of the definition. |
props | Defines public props, decoding, defaults, and attribute reflection. |
setup | Runs once per connected instance to create local state, prop observers, timers, host APIs, and cleanup. |
onFirstRender | Runs after the initial render and Datastar apply pass, when refs and rendered DOM exist. |
render | Returns the component DOM as Rocket html or svg. |
renderOnPropChange | Controls whether prop updates trigger rerendering. |
manifest | Adds slot and event metadata to Rocket’s generated component manifest. |
mode #
mode chooses where the component renders.
1mode?: 'open' | 'closed' | 'light'| Value | Mount target | When 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.
| Value | Incoming morph behavior | When to use it |
|---|---|---|
false (default) |