@takazudo/zudo-sg 0.3.6 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +92 -0
  2. package/README.md +13 -2
  3. package/dist/catalog/catalog-thumb.d.ts +19 -0
  4. package/dist/catalog/catalog-thumb.js +11 -0
  5. package/dist/catalog/descriptor-thumb.d.ts +20 -0
  6. package/dist/catalog/descriptor-thumb.js +31 -0
  7. package/dist/catalog/index.d.ts +4 -0
  8. package/dist/catalog/index.js +5 -0
  9. package/dist/code-panel/code-panel.d.ts +8 -1
  10. package/dist/code-panel/code-panel.js +76 -24
  11. package/dist/code-panel/editor-setup.d.ts +5 -0
  12. package/dist/code-panel/editor-setup.js +18 -3
  13. package/dist/code-panel/editor-theme.d.ts +2 -0
  14. package/dist/code-panel/editor-theme.js +54 -0
  15. package/dist/code-panel/source-editor.d.ts +12 -1
  16. package/dist/code-panel/source-editor.js +14 -3
  17. package/dist/config/index.d.ts +54 -5
  18. package/dist/config/index.js +72 -12
  19. package/dist/host-paths.d.ts +2 -0
  20. package/dist/host-paths.js +5 -0
  21. package/dist/plugins/routes.d.ts +60 -11
  22. package/dist/plugins/routes.js +135 -23
  23. package/dist/preview/detail-workbench.d.ts +63 -1
  24. package/dist/preview/detail-workbench.js +112 -22
  25. package/dist/preview/index.d.ts +3 -3
  26. package/dist/preview/index.js +7 -1
  27. package/dist/preview/messages.d.ts +34 -5
  28. package/dist/preview/messages.js +34 -6
  29. package/dist/preview/preview-app.js +22 -10
  30. package/dist/preview/variant-frame.d.ts +50 -2
  31. package/dist/preview/variant-frame.js +64 -23
  32. package/dist/registry/catalog.d.ts +64 -0
  33. package/dist/registry/catalog.js +79 -0
  34. package/dist/registry/descriptors.d.ts +46 -0
  35. package/dist/registry/descriptors.js +148 -0
  36. package/dist/registry/index.d.ts +5 -1
  37. package/dist/registry/index.js +7 -1
  38. package/dist/registry/nav-nodes.d.ts +4 -1
  39. package/dist/registry/nav-nodes.js +4 -1
  40. package/dist/registry/registry.d.ts +15 -0
  41. package/dist/registry/registry.js +18 -9
  42. package/dist/safelist.css +1 -1
  43. package/dist/sg-context.d.ts +29 -0
  44. package/dist/sg-routes.d.ts +49 -1
  45. package/dist/sg-routes.js +15 -1
  46. package/dist/shared/tablist-keyboard.d.ts +14 -0
  47. package/dist/shared/tablist-keyboard.js +19 -0
  48. package/package.json +12 -6
  49. package/routes-src/_preview-app.tsx +2 -2
  50. package/routes-src/_registry.ts +31 -6
  51. package/routes-src/components-index.tsx +15 -8
  52. package/routes-src/components-preview.tsx +5 -0
  53. package/routes-src/components-slug.tsx +63 -31
  54. package/routes-src/tokens.tsx +9 -4
  55. package/styles.css +29 -0
  56. package/virtual-modules.d.ts +6 -0
package/CHANGELOG.md CHANGED
@@ -4,6 +4,98 @@ All notable changes to `@takazudo/zudo-sg` are documented in this file.
4
4
 
5
5
  The format is based on Keep a Changelog, and release notes are generated from the changelog MDX pages.
6
6
 
7
+ ## [0.4.1] - 2026-09-27
8
+
9
+ This release replaces the code panel's fixed dark editor theme with an
10
+ engine-owned theme that meets WCAG AA text contrast, follows the page's light
11
+ or dark scheme, and exposes the editor colors as host-overridable tokens.
12
+
13
+ ### Features
14
+
15
+ - Color the code panel's CodeMirror editors from 16 `--sg-code-*` custom
16
+ properties (background, text, gutter, active line, selection, cursor, and
17
+ nine syntax roles). Their defaults are `light-dark()` pairs chosen so every
18
+ text and syntax color reaches at least 4.5:1 against the surface it sits on,
19
+ so the editor is light on light pages, dark on dark pages, and switches when
20
+ the page theme toggles. Override any token on `:root` to match the host's
21
+ palette.
22
+ - Highlight the active line and its gutter number in the editable Live CSS
23
+ editor. The read-only source view is unchanged.
24
+ - Add an `extensions` option to `SourceEditor` for hosts that mount it
25
+ directly. The engine theme is low precedence, so a host-supplied CodeMirror
26
+ theme wins on conflicting rules. It is read once at mount, like `value`.
27
+
28
+ ### Bug Fixes
29
+
30
+ - The editor gutter and comment text no longer fall below WCAG AA contrast:
31
+ the previous `oneDark` theme drew them at 3.86:1 and stayed dark on light
32
+ pages. `@codemirror/theme-one-dark` is no longer a dependency.
33
+
34
+ ## [0.4.0] - 2026-09-27
35
+
36
+ This release lets a non-Preact host adopt the catalog, nav, search, and
37
+ detail-page chrome while keeping full ownership of the preview document, and
38
+ carries two behavior changes to defaults set by earlier releases. It also
39
+ makes the detail page's code panel keyboard- and screen-reader-operable and
40
+ requires the zdtp release that keeps the preview token panel working across
41
+ SPA navigation.
42
+
43
+ ### Features
44
+
45
+ - Add a second registry source, `registry: { mode: "descriptor", module }`:
46
+ plain-data `StoryDescriptor`/`ThumbnailDescriptor` metadata in place of
47
+ `*.stories.tsx` files with `render` functions, validated at build time with
48
+ named, indexed error messages.
49
+ - Add `externalPreview: { url, trailingSlash? }`, a host-served preview
50
+ document that replaces the in-engine `/components/preview` route. Required
51
+ in descriptor mode; optional in module mode.
52
+ - Version the preview iframe postMessage contract as protocol v1 (`v: 1`,
53
+ legacy messages with no `v` still accepted) and publish it as the
54
+ framework-free leaf module `@takazudo/zudo-sg/preview/messages`, so an
55
+ `externalPreview` document can implement it without depending on Preact.
56
+ - Add `routes.componentsPreview: false` and `routes.tokens: false` opt-outs,
57
+ plus an implied `tokens` opt-out in descriptor mode when no token manifest
58
+ is configured.
59
+ - Extend `DetailWorkbench` with host-controlled props (`selection`, `theme`,
60
+ `previewParams`, `sizing`, `toolbar`) so a foreign host can drive the
61
+ detail-page chrome directly instead of only reaching it through the
62
+ engine's own detail route.
63
+ - Make the code panel accessible: the variant tablist is named
64
+ "Source variant", uses a roving tabindex, and moves and activates the
65
+ selection with the arrow keys, Home and End. The source region is a labelled
66
+ tabpanel, and both CodeMirror editors are named (`Source code for {story}: {variant}`,
67
+ `Live CSS for {story}`). `SourceEditor` gains an optional
68
+ `label` prop, and the regions carry stable `data-sg-code-region` hooks.
69
+
70
+ ### Bug Fixes
71
+
72
+ - Raise the `@takazudo/zdtp` peer floor to `^0.8.5`. It picks up the upstream
73
+ fixes for a panel root left empty by an SPA remount and for a fresh mount
74
+ that briefly persisted a closed state, so the preview token panel keeps
75
+ working across host and engine navigation and reloads.
76
+ - Skip preview posts from an opaque origin.
77
+ - Reserve the external preview slug and align tokens-route detection.
78
+ - Keep the code panel's tab selection and tabpanel labelling on the variant
79
+ actually shown when the stored variant no longer matches a tab.
80
+
81
+ ### Breaking Changes
82
+
83
+ - `withZudoSg()`'s `headerTokenTrigger` option now defaults to whether the
84
+ preview token panel is actually wired (`zdtpApplyProxy.tabsModule` set),
85
+ rather than defaulting to `true` unconditionally. A host that never wired
86
+ the apply proxy's tabs module no longer gets a header button that opened
87
+ nothing; setting `headerTokenTrigger: true` explicitly still forces it on
88
+ (with a one-time console warning when the panel isn't wired).
89
+ - A standalone `DetailWorkbench` (driven directly, outside the engine's own
90
+ detail route) now defaults `toolbar.codePanel` and `toolbar.tokenPanel` to
91
+ `false`. The engine's own detail route is unaffected — it opts both back
92
+ in explicitly — but existing direct consumers of `DetailWorkbench` that
93
+ relied on those controls appearing by default must now pass
94
+ `toolbar: { codePanel: true, tokenPanel: true }`.
95
+
96
+ See [Descriptor mode & external preview](/docs/reference/descriptor-mode) for
97
+ the full contract and a minimal framework-free preview-frame example.
98
+
7
99
  ## [0.3.6] - 2026-09-25
8
100
 
9
101
  This release updates the supported zfb toolchain to 2.20.3 so staged
package/README.md CHANGED
@@ -130,11 +130,11 @@ output as drift and never writes it.
130
130
  ## Installation
131
131
 
132
132
  ```sh
133
- pnpm add @takazudo/zudo-sg@latest @takazudo/zfb@^2.20.3 @takazudo/zfb-md-wasm@^2.20.3 @takazudo/zfb-runtime@^2.20.3 @takazudo/zudo-doc@^5.27.0 @takazudo/zdtp@^0.8.2 diff@^8.0.4 katex@^0.16.38 preact@^10.29.8 preact-render-to-string@^6.6.6 tailwindcss@^4.2.0 zod@^4.3.6
133
+ pnpm add @takazudo/zudo-sg@latest @takazudo/zfb@^2.20.3 @takazudo/zfb-md-wasm@^2.20.3 @takazudo/zfb-runtime@^2.20.3 @takazudo/zudo-doc@^5.27.0 @takazudo/zdtp@^0.8.5 diff@^8.0.4 katex@^0.16.38 preact@^10.29.8 preact-render-to-string@^6.6.6 tailwindcss@^4.2.0 zod@^4.3.6
134
134
  ```
135
135
 
136
136
  The engine peers are `@takazudo/zfb ^2.20.3`, `@takazudo/zudo-doc ^5.27.0`,
137
- `@takazudo/zdtp ^0.8.2`, and `preact ^10.29.8`. `@takazudo/zdtp` is required:
137
+ `@takazudo/zdtp ^0.8.5`, and `preact ^10.29.8`. `@takazudo/zdtp` is required:
138
138
  the injected `/tokens` route imports its dashboard at build time. The
139
139
  `@takazudo/zfb-md-wasm`, `@takazudo/zfb-runtime`, `diff`, `katex`, and `zod`
140
140
  entries are prerequisites inherited from zfb/zudo-doc; zudo-doc's published
@@ -259,6 +259,17 @@ migrate that declaration to `--sg-border` (and use the matching `--sg-*` role fo
259
259
  other chrome color). Bare `--color-border` remains a valid host component token, but it
260
260
  no longer controls zudo-sg chrome.
261
261
 
262
+ The code panel's editor surface has its own sixteen `--sg-code-*` roles (background,
263
+ foreground, gutter, active line, selection, cursor, and the syntax token colors), themed
264
+ the same order-independent way. Their `light-dark()` defaults hold WCAG AA (≥ 4.5:1)
265
+ contrast and follow the host's `color-scheme`; any override must preserve that contrast.
266
+ `SourceEditor`'s `extensions` prop lets a host mounting the editor directly pass its own
267
+ CodeMirror extensions, which win over the engine's theme extension — it's read once at
268
+ mount, so change it by remounting with a new `key`. `CodePanel` doesn't expose this prop,
269
+ since island props must be JSON-serializable. See
270
+ [Tokens and preview CSS](../../src/content/docs/architecture/tokens-and-preview-css.mdx)
271
+ for the full token list and override example.
272
+
262
273
  The zudo-doc theme supplies framework defaults, including the breakpoints
263
274
  needed by responsive sidebar utilities. Its color-token reset precedes your
264
275
  tokens so they survive. Use `theme-no-reset.css` instead to preserve
@@ -0,0 +1,19 @@
1
+ /** @jsxRuntime automatic */
2
+ /** @jsxImportSource preact */
3
+ import type { JSX } from "preact";
4
+ import type { CatalogEntry } from "../registry/catalog.js";
5
+ export interface CatalogThumbProps {
6
+ entry: CatalogEntry;
7
+ /** Site base, forwarded to `DescriptorThumb` to prefix a root-absolute `src`. Ignored for module entries. */
8
+ base?: string;
9
+ }
10
+ /**
11
+ * Catalog tile thumbnail for either registry mode. A module entry renders its
12
+ * story inline through `ComponentThumb`; a descriptor entry renders its
13
+ * `ThumbnailDescriptor` (image, placeholder, or a labelled "missing" note)
14
+ * through `DescriptorThumb`.
15
+ */
16
+ export declare function CatalogThumb({ entry, base }: CatalogThumbProps): JSX.Element;
17
+ export declare namespace CatalogThumb {
18
+ var displayName: string;
19
+ }
@@ -0,0 +1,11 @@
1
+ import { jsx } from "preact/jsx-runtime";
2
+ import { ComponentThumb } from "./component-thumb.js";
3
+ import { DescriptorThumb } from "./descriptor-thumb.js";
4
+ function CatalogThumb({ entry, base }) {
5
+ if (entry.source === "module") return /* @__PURE__ */ jsx(ComponentThumb, { entry: entry.storyEntry });
6
+ return /* @__PURE__ */ jsx(DescriptorThumb, { entry, base });
7
+ }
8
+ CatalogThumb.displayName = "CatalogThumb";
9
+ export {
10
+ CatalogThumb
11
+ };
@@ -0,0 +1,20 @@
1
+ /** @jsxRuntime automatic */
2
+ /** @jsxImportSource preact */
3
+ import type { JSX } from "preact";
4
+ import type { DescriptorCatalogEntry } from "../registry/catalog.js";
5
+ /** Note shown when a descriptor declares no `thumbnail` at all. */
6
+ export declare const DESCRIPTOR_THUMB_MISSING_NOTE = "No thumbnail provided";
7
+ export interface DescriptorThumbProps {
8
+ entry: DescriptorCatalogEntry;
9
+ /** Site base to prefix a root-absolute image `src` with. Defaults to no prefix. */
10
+ base?: string;
11
+ }
12
+ /**
13
+ * One descriptor-mode tile's thumbnail slot. Mirrors `ComponentThumb`'s
14
+ * `aria-hidden` + `inert` wrapper so both registry modes keep decorative
15
+ * snapshots out of the tab order and the accessibility tree the same way.
16
+ */
17
+ export declare function DescriptorThumb({ entry, base }: DescriptorThumbProps): JSX.Element;
18
+ export declare namespace DescriptorThumb {
19
+ var displayName: string;
20
+ }
@@ -0,0 +1,31 @@
1
+ import { jsx } from "preact/jsx-runtime";
2
+ const DESCRIPTOR_THUMB_MISSING_NOTE = "No thumbnail provided";
3
+ function withThumbBase(base, src) {
4
+ if (!src.startsWith("/")) return src;
5
+ return base.replace(/\/+$/, "") + src;
6
+ }
7
+ function DescriptorThumbNote({ note }) {
8
+ return /* @__PURE__ */ jsx("div", { class: "sg-thumb", "data-sg-preview-scope": true, "data-sg-thumb-note": true, "aria-hidden": "true", inert: true, children: /* @__PURE__ */ jsx("p", { class: "sg-thumb-note", children: note }) });
9
+ }
10
+ function DescriptorThumb({ entry, base = "" }) {
11
+ const thumbnail = entry.thumbnail;
12
+ if (!thumbnail) return /* @__PURE__ */ jsx(DescriptorThumbNote, { note: DESCRIPTOR_THUMB_MISSING_NOTE });
13
+ if (thumbnail.kind === "placeholder") return /* @__PURE__ */ jsx(DescriptorThumbNote, { note: thumbnail.note });
14
+ return /* @__PURE__ */ jsx("div", { class: "sg-thumb", "data-sg-preview-scope": true, "data-sg-thumb-image": true, "aria-hidden": "true", inert: true, children: /* @__PURE__ */ jsx(
15
+ "img",
16
+ {
17
+ class: "sg-thumb-img",
18
+ src: withThumbBase(base, thumbnail.src),
19
+ width: thumbnail.width,
20
+ height: thumbnail.height,
21
+ alt: thumbnail.alt ?? "",
22
+ loading: "lazy",
23
+ decoding: "async"
24
+ }
25
+ ) });
26
+ }
27
+ DescriptorThumb.displayName = "DescriptorThumb";
28
+ export {
29
+ DESCRIPTOR_THUMB_MISSING_NOTE,
30
+ DescriptorThumb
31
+ };
@@ -1,4 +1,8 @@
1
1
  export { ATOM_SCALE_CATEGORIES, ComponentThumb, THUMB_OPT_OUTS, THUMB_VIEWPORT_W, THUMB_VIEWPORT_W_ATOM, scopeThumbHtml, thumbIdPrefix, thumbScale, thumbViewportWidth, } from "./component-thumb.js";
2
2
  export type { ComponentThumbProps } from "./component-thumb.js";
3
+ export { DESCRIPTOR_THUMB_MISSING_NOTE, DescriptorThumb } from "./descriptor-thumb.js";
4
+ export type { DescriptorThumbProps } from "./descriptor-thumb.js";
5
+ export { CatalogThumb } from "./catalog-thumb.js";
6
+ export type { CatalogThumbProps } from "./catalog-thumb.js";
3
7
  export { ATTR_TILE_SIZE, DEFAULT_TILE_SIZE, LS_TILE_SIZE, TILE_SIZES, TILE_SIZE_RESTORE_SCRIPT, applyTileSize, isTileSize, readTileSize, } from "./tile-size.js";
4
8
  export type { TileSize, TileSizeOption } from "./tile-size.js";
@@ -9,6 +9,8 @@ import {
9
9
  thumbScale,
10
10
  thumbViewportWidth
11
11
  } from "./component-thumb.js";
12
+ import { DESCRIPTOR_THUMB_MISSING_NOTE, DescriptorThumb } from "./descriptor-thumb.js";
13
+ import { CatalogThumb } from "./catalog-thumb.js";
12
14
  import {
13
15
  ATTR_TILE_SIZE,
14
16
  DEFAULT_TILE_SIZE,
@@ -22,8 +24,11 @@ import {
22
24
  export {
23
25
  ATOM_SCALE_CATEGORIES,
24
26
  ATTR_TILE_SIZE,
27
+ CatalogThumb,
25
28
  ComponentThumb,
26
29
  DEFAULT_TILE_SIZE,
30
+ DESCRIPTOR_THUMB_MISSING_NOTE,
31
+ DescriptorThumb,
27
32
  LS_TILE_SIZE,
28
33
  THUMB_OPT_OUTS,
29
34
  THUMB_VIEWPORT_W,
@@ -10,8 +10,15 @@ export interface CodePanelProps {
10
10
  variants: CodePanelVariant[];
11
11
  /** Base-prefixed preview route URL — the same value DetailWorkbench receives. */
12
12
  previewUrl?: string;
13
+ /**
14
+ * Story slug, used to build stable variant-tab/tabpanel ids that match
15
+ * SSR output (mirrors `DetailWorkbench`'s `tabsId`). Falls back to
16
+ * `useId()` when the host doesn't pass one — still unique per page, just
17
+ * not stable across an SSR/hydration boundary.
18
+ */
19
+ slug?: string;
13
20
  }
14
- declare function CodePanel({ storyTitle, variants, previewUrl, }: CodePanelProps): JSX.Element;
21
+ declare function CodePanel({ storyTitle, variants, previewUrl, slug, }: CodePanelProps): JSX.Element;
15
22
  declare namespace CodePanel {
16
23
  var displayName: string;
17
24
  }
@@ -1,9 +1,10 @@
1
1
  "use client";
2
2
  import { jsx, jsxs } from "preact/jsx-runtime";
3
- import { useRef, useState } from "preact/hooks";
3
+ import { useId, useRef, useState } from "preact/hooks";
4
4
  import SourceEditor from "./source-editor.js";
5
5
  import CopyButton from "./copy-button.js";
6
6
  import { injectCssToAllPreviews } from "./css-injection.js";
7
+ import { handleTablistKeyDown } from "../shared/tablist-keyboard.js";
7
8
  const STARTER_CSS = `/* Live CSS \u2014 edits inject into every preview above.
8
9
  Try overriding a component utility, e.g.:
9
10
  .btn { letter-spacing: 0.04em; } */
@@ -11,48 +12,98 @@ const STARTER_CSS = `/* Live CSS \u2014 edits inject into every preview above.
11
12
  function CodePanel({
12
13
  storyTitle,
13
14
  variants,
14
- previewUrl
15
+ previewUrl,
16
+ slug
15
17
  }) {
16
18
  const [activeVariant, setActiveVariant] = useState(
17
19
  variants[0]?.exportName ?? ""
18
20
  );
19
21
  const debounceRef = useRef(void 0);
20
- const active = variants.find((v) => v.exportName === activeVariant) ?? variants[0];
22
+ const reactId = useId();
23
+ const tabsId = `sg-code-panel-${slug ?? reactId}`;
24
+ const foundIndex = variants.findIndex((v) => v.exportName === activeVariant);
25
+ const activeIndex = foundIndex >= 0 ? foundIndex : 0;
26
+ const active = variants[activeIndex];
27
+ const tabId = (index) => `${tabsId}-tab-${index}`;
28
+ const panelId = `${tabsId}-panel`;
21
29
  function handleCssChange(css) {
22
30
  window.clearTimeout(debounceRef.current);
23
31
  debounceRef.current = window.setTimeout(() => {
24
32
  injectCssToAllPreviews("live", css, previewUrl);
25
33
  }, 250);
26
34
  }
35
+ function onTabKeyDown(e, index) {
36
+ handleTablistKeyDown(
37
+ e,
38
+ index,
39
+ variants.length,
40
+ (next) => {
41
+ const target = variants[next];
42
+ if (target) setActiveVariant(target.exportName);
43
+ },
44
+ (next) => {
45
+ const tablist = e.currentTarget.parentElement;
46
+ tablist?.children[next]?.focus();
47
+ }
48
+ );
49
+ }
27
50
  return /* @__PURE__ */ jsxs("div", { class: "flex h-full flex-col gap-vsp-md p-hsp-md", children: [
28
51
  /* @__PURE__ */ jsxs("div", { children: [
29
52
  /* @__PURE__ */ jsxs("div", { class: "flex items-center justify-between gap-hsp-sm", children: [
30
53
  /* @__PURE__ */ jsx("h2", { class: "text-small font-semibold uppercase tracking-wide text-[color:var(--sg-muted)]", children: "Source" }),
31
54
  active && /* @__PURE__ */ jsx(CopyButton, { text: active.source, label: "Copy source" })
32
55
  ] }),
33
- variants.length > 1 && /* @__PURE__ */ jsx("div", { role: "tablist", class: "mt-vsp-2xs flex flex-wrap gap-hsp-3xs", children: variants.map((v) => /* @__PURE__ */ jsx(
34
- "button",
56
+ variants.length > 1 && /* @__PURE__ */ jsx(
57
+ "div",
35
58
  {
36
- type: "button",
37
- role: "tab",
38
- "aria-selected": v.exportName === activeVariant,
39
- onClick: () => setActiveVariant(v.exportName),
40
- class: "px-hsp-xs py-vsp-3xs text-xs rounded-sm border transition-colors " + (v.exportName === activeVariant ? "border-[color:var(--sg-accent)] bg-[var(--sg-accent)] text-[color:var(--sg-on-accent)]" : "border-[color:var(--sg-border)] text-[color:var(--sg-muted)] hover:text-[color:var(--sg-fg)]"),
41
- children: v.name
42
- },
43
- v.exportName
44
- )) }),
45
- active && /* @__PURE__ */ jsx("div", { class: "mt-vsp-xs", children: /* @__PURE__ */ jsx(
46
- SourceEditor,
59
+ role: "tablist",
60
+ "aria-label": "Source variant",
61
+ class: "mt-vsp-2xs flex flex-wrap gap-hsp-3xs",
62
+ children: variants.map((v, index) => {
63
+ const selected = index === activeIndex;
64
+ return /* @__PURE__ */ jsx(
65
+ "button",
66
+ {
67
+ id: tabId(index),
68
+ type: "button",
69
+ role: "tab",
70
+ "aria-selected": selected,
71
+ "aria-controls": panelId,
72
+ tabIndex: selected ? 0 : -1,
73
+ onClick: () => setActiveVariant(v.exportName),
74
+ onKeyDown: (e) => onTabKeyDown(e, index),
75
+ class: "px-hsp-xs py-vsp-3xs text-xs rounded-sm border transition-colors " + (selected ? "border-[color:var(--sg-accent)] bg-[var(--sg-accent)] text-[color:var(--sg-on-accent)]" : "border-[color:var(--sg-border)] text-[color:var(--sg-muted)] hover:text-[color:var(--sg-fg)]"),
76
+ children: v.name
77
+ },
78
+ v.exportName
79
+ );
80
+ })
81
+ }
82
+ ),
83
+ active && /* @__PURE__ */ jsx(
84
+ "div",
47
85
  {
48
- value: active.source,
49
- language: "tsx",
50
- editable: false
51
- },
52
- active.exportName
53
- ) })
86
+ class: "sg-code-source mt-vsp-xs",
87
+ "data-sg-code-region": "source",
88
+ ...variants.length > 1 ? {
89
+ id: panelId,
90
+ role: "tabpanel",
91
+ "aria-labelledby": tabId(activeIndex)
92
+ } : {},
93
+ children: /* @__PURE__ */ jsx(
94
+ SourceEditor,
95
+ {
96
+ value: active.source,
97
+ language: "tsx",
98
+ editable: false,
99
+ label: `Source code for ${storyTitle}: ${active.name}`
100
+ },
101
+ active.exportName
102
+ )
103
+ }
104
+ )
54
105
  ] }),
55
- /* @__PURE__ */ jsxs("div", { class: "flex min-h-0 flex-1 flex-col", children: [
106
+ /* @__PURE__ */ jsxs("div", { class: "sg-code-live-css flex min-h-0 flex-1 flex-col", "data-sg-code-region": "live-css", children: [
56
107
  /* @__PURE__ */ jsx("h2", { class: "text-small font-semibold uppercase tracking-wide text-[color:var(--sg-muted)]", children: "Live CSS" }),
57
108
  /* @__PURE__ */ jsxs("p", { class: "mt-vsp-3xs text-xs text-[color:var(--sg-muted)]", children: [
58
109
  "Injected into the previews above. ",
@@ -64,7 +115,8 @@ function CodePanel({
64
115
  value: STARTER_CSS,
65
116
  language: "css",
66
117
  editable: true,
67
- onChange: handleCssChange
118
+ onChange: handleCssChange,
119
+ label: `Live CSS for ${storyTitle}`
68
120
  }
69
121
  ) })
70
122
  ] })
@@ -1,10 +1,15 @@
1
1
  import { EditorView } from "@codemirror/view";
2
+ import { type Extension } from "@codemirror/state";
2
3
  export interface CreateEditorOptions {
3
4
  /** "css" enables the CSS language (live-injection buffer); else TS/JSX. */
4
5
  language: "css" | "tsx";
5
6
  /** Whether the editor is editable (false = read-only source view). */
6
7
  editable: boolean;
8
+ /** Accessible name for the editor's `.cm-content` (role="textbox"). */
9
+ label?: string;
7
10
  /** Fired on every document change with the full text. */
8
11
  onChange?: (value: string) => void;
12
+ /** Host extensions appended after the engine's; they win over its `Prec.low` theme. */
13
+ extensions?: Extension[];
9
14
  }
10
15
  export declare function createEditorView(doc: string, parent: HTMLElement, opts: CreateEditorOptions): EditorView;
@@ -1,9 +1,15 @@
1
- import { EditorView, lineNumbers, keymap } from "@codemirror/view";
1
+ import {
2
+ EditorView,
3
+ highlightActiveLine,
4
+ highlightActiveLineGutter,
5
+ lineNumbers,
6
+ keymap
7
+ } from "@codemirror/view";
2
8
  import { EditorState } from "@codemirror/state";
3
9
  import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
4
10
  import { javascript } from "@codemirror/lang-javascript";
5
11
  import { css } from "@codemirror/lang-css";
6
- import { oneDark } from "@codemirror/theme-one-dark";
12
+ import { engineTheme } from "./editor-theme.js";
7
13
  function createEditorView(doc, parent, opts) {
8
14
  const langExtension = opts.language === "css" ? css() : javascript({ jsx: true, typescript: true });
9
15
  const extensions = [
@@ -11,11 +17,17 @@ function createEditorView(doc, parent, opts) {
11
17
  history(),
12
18
  keymap.of([...defaultKeymap, ...historyKeymap]),
13
19
  langExtension,
14
- oneDark,
20
+ engineTheme,
15
21
  EditorView.lineWrapping,
16
22
  EditorState.readOnly.of(!opts.editable),
17
23
  EditorView.editable.of(opts.editable)
18
24
  ];
25
+ if (opts.editable) {
26
+ extensions.push(highlightActiveLine(), highlightActiveLineGutter());
27
+ }
28
+ if (opts.label) {
29
+ extensions.push(EditorView.contentAttributes.of({ "aria-label": opts.label }));
30
+ }
19
31
  if (opts.onChange) {
20
32
  extensions.push(
21
33
  EditorView.updateListener.of((update) => {
@@ -23,6 +35,9 @@ function createEditorView(doc, parent, opts) {
23
35
  })
24
36
  );
25
37
  }
38
+ if (opts.extensions) {
39
+ extensions.push(...opts.extensions);
40
+ }
26
41
  return new EditorView({
27
42
  state: EditorState.create({ doc, extensions }),
28
43
  parent
@@ -0,0 +1,2 @@
1
+ import { type Extension } from "@codemirror/state";
2
+ export declare const engineTheme: Extension;
@@ -0,0 +1,54 @@
1
+ import { Prec } from "@codemirror/state";
2
+ import { EditorView } from "@codemirror/view";
3
+ import { HighlightStyle, syntaxHighlighting } from "@codemirror/language";
4
+ import { tags as t } from "@lezer/highlight";
5
+ const code = (name) => `var(--sg-code-${name})`;
6
+ const selectionBg = { backgroundColor: code("selection-bg") };
7
+ const engineEditorTheme = EditorView.theme({
8
+ "&": {
9
+ color: code("fg"),
10
+ backgroundColor: code("bg")
11
+ },
12
+ ".cm-content": {
13
+ caretColor: code("cursor")
14
+ },
15
+ ".cm-cursor, .cm-dropCursor": {
16
+ borderLeftColor: code("cursor")
17
+ },
18
+ "&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground": selectionBg,
19
+ ".cm-selectionBackground": selectionBg,
20
+ ".cm-content ::selection": selectionBg,
21
+ ".cm-gutters": {
22
+ color: code("gutter-fg"),
23
+ backgroundColor: code("gutter-bg"),
24
+ border: "none"
25
+ },
26
+ ".cm-activeLine": {
27
+ backgroundColor: code("active-line-bg")
28
+ },
29
+ ".cm-activeLineGutter": {
30
+ backgroundColor: code("active-line-bg")
31
+ }
32
+ });
33
+ const engineHighlightStyle = HighlightStyle.define([
34
+ { tag: t.keyword, color: code("keyword") },
35
+ { tag: [t.string, t.special(t.string), t.regexp, t.escape], color: code("string") },
36
+ { tag: [t.number, t.bool, t.null, t.atom, t.unit, t.color], color: code("number") },
37
+ { tag: t.comment, color: code("comment"), fontStyle: "italic" },
38
+ {
39
+ tag: [t.function(t.variableName), t.function(t.propertyName), t.macroName],
40
+ color: code("function")
41
+ },
42
+ { tag: [t.propertyName, t.definition(t.propertyName)], color: code("property") },
43
+ { tag: [t.typeName, t.className, t.namespace], color: code("type") },
44
+ { tag: t.tagName, color: code("tag") },
45
+ { tag: t.attributeName, color: code("attribute") },
46
+ { tag: t.invalid, color: code("keyword") }
47
+ ]);
48
+ const engineTheme = [
49
+ Prec.low(engineEditorTheme),
50
+ Prec.low(syntaxHighlighting(engineHighlightStyle))
51
+ ];
52
+ export {
53
+ engineTheme
54
+ };
@@ -1,8 +1,19 @@
1
1
  import type { JSX } from "preact";
2
+ import type { Extension } from "@codemirror/state";
2
3
  export interface SourceEditorProps {
3
4
  value: string;
4
5
  language: "css" | "tsx";
5
6
  editable?: boolean;
7
+ /** Accessible name for the editor (and its <pre> fallback before it mounts). */
8
+ label?: string;
6
9
  onChange?: (value: string) => void;
10
+ /**
11
+ * Extra CodeMirror extensions (e.g. a host theme), applied after — and so
12
+ * overriding — the engine's own. For direct `SourceEditor` use only: they are
13
+ * not serializable across the `CodePanel` island boundary, so `CodePanel`
14
+ * does not accept them. Initial configuration only, like `value` — change
15
+ * them by remounting with a new `key`.
16
+ */
17
+ extensions?: Extension[];
7
18
  }
8
- export default function SourceEditor({ value, language, editable, onChange, }: SourceEditorProps): JSX.Element;
19
+ export default function SourceEditor({ value, language, editable, label, onChange, extensions, }: SourceEditorProps): JSX.Element;
@@ -5,7 +5,9 @@ function SourceEditor({
5
5
  value,
6
6
  language,
7
7
  editable = false,
8
- onChange
8
+ label,
9
+ onChange,
10
+ extensions
9
11
  }) {
10
12
  const containerRef = useRef(null);
11
13
  const viewRef = useRef(null);
@@ -19,7 +21,9 @@ function SourceEditor({
19
21
  viewRef.current = createEditorView(value, containerRef.current, {
20
22
  language,
21
23
  editable,
22
- onChange: (v) => onChangeRef.current?.(v)
24
+ label,
25
+ onChange: (v) => onChangeRef.current?.(v),
26
+ extensions
23
27
  });
24
28
  setLoaded(true);
25
29
  });
@@ -30,7 +34,14 @@ function SourceEditor({
30
34
  };
31
35
  }, []);
32
36
  return /* @__PURE__ */ jsxs("div", { class: "relative text-small", children: [
33
- !loaded && /* @__PURE__ */ jsx("pre", { class: "m-0 overflow-auto rounded-md bg-[var(--sg-surface-2)] p-hsp-sm text-xs text-[color:var(--sg-muted)]", children: /* @__PURE__ */ jsx("code", { children: value }) }),
37
+ !loaded && /* @__PURE__ */ jsx(
38
+ "pre",
39
+ {
40
+ class: "m-0 overflow-auto rounded-md bg-[var(--sg-surface-2)] p-hsp-sm text-xs text-[color:var(--sg-muted)]",
41
+ "aria-label": label,
42
+ children: /* @__PURE__ */ jsx("code", { children: value })
43
+ }
44
+ ),
34
45
  /* @__PURE__ */ jsx("div", { ref: containerRef, class: loaded ? "" : "hidden" })
35
46
  ] });
36
47
  }