From 76d78e1925088c1450878e192cdd375e8431341f Mon Sep 17 00:00:00 2001 From: Buddy Date: Tue, 10 Nov 2026 11:44:27 -0800 Subject: [PATCH] Accept a CSSStyleSheet as css; move example CSS and templates into files css is now string | CSSStyleSheet. A sheet is adopted as-is: into its own shadow root, an enclosing shadow root, or document.adoptedStyleSheets for light DOM at the document level. Strings behave as before. The examples import their styles with `with { type: 'css' }` and their static templates with `with { type: 'text' }`. Safari doesn't support those yet, so vite.config.ts rewrites them into modules Vite builds: a constructed CSSStyleSheet, or the file's text. npm run examples now just runs vite, with root set in the config. Co-Authored-By: Claude Opus 5.5 --- README.md | 6 ++-- examples/color-swatch-picker.css | 29 +++++++++++++++++ examples/color-swatch-picker.ts | 33 ++----------------- examples/hero-banner.css | 50 +++++++++++++++++++++++++++++ examples/hero-banner.ts | 54 +------------------------------- examples/index.html | 4 +-- examples/labeled-field.css | 26 +++++++++++++++ examples/labeled-field.html | 5 +++ examples/labeled-field.ts | 41 ++---------------------- examples/main-context.css | 1 + examples/main-context.ts | 3 +- examples/modules.d.ts | 11 +++++++ examples/secret-note.css | 32 +++++++++++++++++++ examples/secret-note.html | 5 +++ examples/secret-note.ts | 45 +++----------------------- examples/star-rating.css | 26 +++++++++++++++ examples/star-rating.html | 3 ++ examples/star-rating.ts | 37 ++-------------------- oxlint.config.ts | 7 +++++ package.json | 2 +- src/types.ts | 8 +++-- src/web-component.ts | 9 +++--- test/web-component.test.ts | 30 ++++++++++++++++++ vite.config.ts | 42 +++++++++++++++++++++++++ 24 files changed, 299 insertions(+), 210 deletions(-) create mode 100644 examples/color-swatch-picker.css create mode 100644 examples/hero-banner.css create mode 100644 examples/labeled-field.css create mode 100644 examples/labeled-field.html create mode 100644 examples/main-context.css create mode 100644 examples/modules.d.ts create mode 100644 examples/secret-note.css create mode 100644 examples/secret-note.html create mode 100644 examples/star-rating.css create mode 100644 examples/star-rating.html create mode 100644 vite.config.ts diff --git a/README.md b/README.md index 4248235..9573886 100644 --- a/README.md +++ b/README.md @@ -43,8 +43,8 @@ ExampleElement.observedAttributes; // readonly ('user-id' | 'min' | 'is-ready' | - **Props** are stored as values and reflected to attributes; attribute changes decode back. `onPropChange` fires once per real change from either side. - **Attribute names**: an explicit `attribute` is used verbatim; otherwise `kebab(key)`, with `disable-` for `disable()` and `data-` in front when `dataPrefix: true`. - **Codecs**: `string`, `number`, `enable`, `disable`, `choice([...])`, `json()`, `prop({ default, decode, encode })`. -- **Shadow DOM**: `shadow` attaches a root and clones `template` into it; `css` is a shared adopted stylesheet. An existing declarative shadow root is used instead (and `shadow`/`template` are ignored). `root` is the shadow root (even when closed) or the element itself. -- **Light DOM**: `template` is appended on first connect; `css` is added once per element type, as a `