Skip to content

Commit 37791a2

Browse files
zernoniaclaude
andauthored
feat(elements): generate a Custom Elements Manifest at build time (#3150)
* feat(elements): generate a Custom Elements Manifest at build time `pnpm --filter reka-ui build` now writes `dist/custom-elements.json` (schema 2.1.0) from the element registry the runtime uses: the built `rekaElementFamilies` and `getPartMeta()`, so tags, attributes, properties and events are exactly what `defineRekaElements()` defines. Types, descriptions and defaults come from the docs' generated component meta and the component pages. `package.json` points to it with `customElements`, and exports it as `reka-ui/custom-elements.json`. A test checks the generator and the built file against the live registry, per tag. Closes #3080 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: document the custom elements manifest, and that every family is available Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 14c0028 commit 37791a2

6 files changed

Lines changed: 690 additions & 6 deletions

File tree

‎docs/content/docs/guides/custom-elements.md‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Use Reka UI primitives outside Vue, as standard custom elements, from plain HTML
1111

1212
<Callout type="warning" title="Experimental">
1313

14-
`reka-ui/elements` is experimental. It covers a first set of families, and its API may change before it is marked stable.
14+
`reka-ui/elements` is experimental. Its API may change before it is marked stable.
1515

1616
</Callout>
1717

@@ -627,6 +627,51 @@ Element types for the default-prefixed tags are opt-in, through a types-only ent
627627

628628
`reka-ui/elements/global` has no JavaScript: reference it from a `.d.ts` file or with `import type {} from 'reka-ui/elements/global'`, not with a runtime `import`. With a custom prefix, declare your tags from `RekaElementTagNameMap` yourself.
629629

630+
## Custom Elements Manifest
631+
632+
The package ships a [Custom Elements Manifest](https://github.com/webcomponents/custom-elements-manifest) (schema 2.1.0) for editors, documentation tools and linters. `package.json` points to it (`"customElements": "dist/custom-elements.json"`), and it is exported as `reka-ui/custom-elements.json`.
633+
634+
It is generated at build time from the registry `defineRekaElements()` reads, so it lists every element of every family, with the default `reka` prefix:
635+
636+
- **Attributes**, with the value each one takes (`'horizontal' | 'vertical'`, a boolean flag, an ISO 8601 string for a date) and the property it sets.
637+
- **Properties**, with their TypeScript types, including the ones that have no attribute (`handle`, `filter`, …).
638+
- **Events**, typed as `RekaElementEvent<Value, Details>`. The cancelable ones (`before-update-*`, and the ones that carry a DOM event) say so.
639+
- **Descriptions and defaults** from the component's API reference.
640+
641+
The element classes are created by `defineRekaElements()`. Their names in the manifest (`DialogRootElement`, …) identify the tags: they are not exports.
642+
643+
**VS Code.** [`custom-element-vs-code-integration`](https://www.npmjs.com/package/custom-element-vs-code-integration) turns the manifest into HTML custom data, for tag and attribute completion, attribute values and hover docs in HTML files:
644+
645+
```js
646+
// scripts/reka-vscode-data.mjs
647+
import { readFileSync } from 'node:fs'
648+
import { createRequire } from 'node:module'
649+
import { generateVsCodeCustomElementData } from 'custom-element-vs-code-integration'
650+
651+
const require = createRequire(import.meta.url)
652+
const manifest = JSON.parse(readFileSync(require.resolve('reka-ui/custom-elements.json'), 'utf8'))
653+
generateVsCodeCustomElementData(manifest, { outdir: '.vscode', cssFileName: null })
654+
```
655+
656+
```json
657+
// .vscode/settings.json
658+
{
659+
"html.customData": ["./.vscode/vscode.html-custom-data.json"]
660+
}
661+
```
662+
663+
**Storybook.** With the web components framework, give the manifest to `setCustomElementsManifest()`. A story whose `component` is a tag (`component: 'reka-dialog-root'`) gets its attributes, properties and events in the args table, and its description on the docs page:
664+
665+
```ts
666+
// .storybook/preview.ts
667+
import { setCustomElementsManifest } from '@storybook/web-components'
668+
import manifest from 'reka-ui/custom-elements.json'
669+
670+
setCustomElementsManifest(manifest)
671+
```
672+
673+
With a custom prefix, the manifest still names the `reka-` tags.
674+
630675
## React
631676
632677
React 19 sets props as properties on custom elements when one exists, and listens to custom events with `on<event-type>`, in the event's own casing:
@@ -716,7 +761,7 @@ Bind props in camelCase (`:modelValue="checked"`, or `:modelValue.prop` to force
716761
717762
## Limitations
718763
719-
- **Families.** Only the families listed above are available. The rest will follow.
764+
- **Families.** Every public family is available, except `Label` (below). The utility components (`Primitive`, `Presence`, `FocusScope`, `RovingFocusGroup`, `Viewport`) are building blocks for Vue components and have no elements.
720765
- **Parts a custom element can't be.** These parts render a native element whose behaviour a custom element can't take on, so they aren't available:
721766
- `Label`: a `<label>`. Its `for` only labels a control when it's on a native `<label>`. Use a native `<label for>`: it labels Reka form elements, which are form-associated. (`<reka-field-label>` works, through `aria-labelledby`.)
722767
- `AvatarImage`: an `<img>`. A custom element can't display an image source. `<reka-avatar-root>` and `<reka-avatar-fallback>` work.
@@ -734,5 +779,4 @@ Bind props in camelCase (`:modelValue="checked"`, or `:modelValue.prop` to force
734779
- **Temporal.** Date families need a global `Temporal`, like the Vue components.
735780
- **SSR.** Server rendering and declarative shadow DOM are not supported. Elements render on the client after they are defined.
736781
- **Bundler required.** See the [quick start](#quick-start).
737-
- **Custom Elements Manifest.** Not generated yet.
738782
- **Vue version.** Tested on Vue 3.5. With older versions generated ids are not unique between elements, and a warning says so in development.

‎packages/core/package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"unstyled"
3030
],
3131
"sideEffects": false,
32+
"customElements": "dist/custom-elements.json",
3233
"exports": {
3334
".": {
3435
"import": "./dist/index.js"
@@ -51,6 +52,7 @@
5152
"./elements/react": {
5253
"types": "./dist/elements/react.d.ts"
5354
},
55+
"./custom-elements.json": "./dist/custom-elements.json",
5456
"./namespaced": {
5557
"import": "./dist/namespaced/index.mjs"
5658
},
@@ -675,7 +677,7 @@
675677
],
676678
"scripts": {
677679
"build": "pnpm type-check && pnpm build-only",
678-
"build-only": "tsdown && tsx scripts/postbuild.ts",
680+
"build-only": "tsdown && tsx scripts/postbuild.ts && tsx scripts/elements-manifest.ts",
679681
"watch": "tsdown --watch",
680682
"gen:exports": "tsx scripts/sync-exports.ts",
681683
"type-check": "vue-tsc -p tsconfig.check.json --noEmit",

0 commit comments

Comments
 (0)