@inversestudio/storybook-addon 0.0.0-stage → 0.2.0
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.
- package/CHANGELOG.md +14 -0
- package/README.md +25 -2
- package/package.json +46 -4
- package/src/manager.js +165 -0
- package/src/preset.js +44 -0
- package/src/preview.js +54 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog — @inversestudio/storybook-addon
|
|
2
|
+
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
## 0.2.0 — 2026-12-01
|
|
6
|
+
|
|
7
|
+
- **History and Activity tabs in the Studio panel.** History lists the component's newest changes: what the commit said, when, who, whether an agent made it, and what kind of change it was (props, variants, tokens), with breaking changes marked. Activity shows its commits in the last year and the latest date, how many of the recent changes agents made and how many people made, and who changed it. Both come from storybook.json, from what `studio health` bakes (studio-kit 0.146.10 or later); before that, the tabs say how to get it. **Test:** packages/studio-kit/test/storybook-addon.smoke.js.
|
|
8
|
+
|
|
9
|
+
- **README: how to turn the History and Activity tabs off.** It now names the kit's `storybook.history: false` setting, which leaves history out of storybook.json; the tabs then say there's no history. Docs only; no add-on code changed. **Test:** packages/studio-kit/test/storybook-addon.smoke.js (the history:false check).
|
|
10
|
+
|
|
11
|
+
## 0.1.0 — 2026-11-30
|
|
12
|
+
|
|
13
|
+
- First version: a "Studio" tab beside every story with Readiness, Contrast, Spec and Used in, read from the `storybook.json` that `studio health` (`studio-showcase-data`) bakes. Storybook 9 and 10. Public; it imports no studio-kit code.
|
|
14
|
+
- A **Theme** switch in the toolbar: the design system's themes from storybook.json, applied to the story the way the token sheet selects them (`[data-theme="dark"]`, `.dark`). Framework-agnostic: the decorator only sets an attribute or class on `<html>`.
|
package/README.md
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @inversestudio/storybook-addon
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
studio's data beside every story, in a **Studio** panel: whether the component is ready, its contrast in every theme, its design spec, where it's used, its history and who changed it.
|
|
4
|
+
|
|
5
|
+
For Storybook 9 and 10.
|
|
6
|
+
|
|
7
|
+
## Set up
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm i -D @inversestudio/storybook-addon
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
// .storybook/main.js
|
|
15
|
+
export default {
|
|
16
|
+
addons: ["@inversestudio/storybook-addon"],
|
|
17
|
+
};
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The panel reads `storybook.json`, which `studio health` writes next to the Showcase's data (`health.dataDir`, `showcase/data` by default). Run it, then start Storybook.
|
|
21
|
+
|
|
22
|
+
**History** and **Activity** come from what `studio health` bakes from git: the component's newest changes (what kind of change, breaking ones marked) and how many were made by agents and how many by people. An agent's commit is one with a coding agent as co-author (`Co-authored-by: Claude …`), a `[bot]` author, or a subject prefix from `health.activity.agentPrefixes`. They need studio-kit 0.146.10 or later. storybook.json ships with a published Storybook; for one the public can open, `"storybook": { "history": false }` in studio.config.json leaves the history out.
|
|
23
|
+
|
|
24
|
+
A story names its component with `parameters: { studio: { component: "Button" } }`. The stories `studio generate` writes do. Without it, the last part of the story's title is used.
|
|
25
|
+
|
|
26
|
+
The toolbar gets a **Theme** switch with the design system's themes, from the same file. It sets the theme the way your token sheet selects it (`[data-theme="dark"]` or `.dark` on `<html>`). "default" leaves the page as your CSS has it.
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inversestudio/storybook-addon",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "studio's data beside every story: readiness, contrast in every theme, the design spec and where a component is used.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"storybook-addon",
|
|
9
|
+
"design-system",
|
|
10
|
+
"studio"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
"./preset": "./src/preset.js",
|
|
14
|
+
"./manager": "./src/manager.js",
|
|
15
|
+
"./preview": "./src/preview.js",
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"README.md",
|
|
21
|
+
"CHANGELOG.md"
|
|
22
|
+
],
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"storybook": "^9.0.0 || ^10.0.0"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"storybook": {
|
|
30
|
+
"displayName": "studio",
|
|
31
|
+
"supportedFrameworks": [
|
|
32
|
+
"react",
|
|
33
|
+
"vue",
|
|
34
|
+
"web-components",
|
|
35
|
+
"marko"
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
"peerDependenciesMeta": {
|
|
39
|
+
"storybook": {
|
|
40
|
+
"optional": true
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/invstd/studio.git",
|
|
46
|
+
"directory": "packages/storybook-addon"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/manager.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The manager (browser): the "Studio" panel beside every story.
|
|
3
|
+
*
|
|
4
|
+
* A story names its component with `parameters.studio.component` (studio's
|
|
5
|
+
* generated stories do); otherwise the last part of its title is used. The
|
|
6
|
+
* data comes from `studio/storybook.json`, which the preset serves.
|
|
7
|
+
*/
|
|
8
|
+
import React, { useEffect, useState } from "react";
|
|
9
|
+
import { addons, types, useGlobals, useParameter, useStorybookState } from "storybook/manager-api";
|
|
10
|
+
import { AddonPanel } from "storybook/internal/components";
|
|
11
|
+
|
|
12
|
+
const ADDON = "inversestudio/storybook-addon";
|
|
13
|
+
const PANEL = `${ADDON}/panel`;
|
|
14
|
+
const TOOL = `${ADDON}/theme`;
|
|
15
|
+
const h = React.createElement;
|
|
16
|
+
|
|
17
|
+
let loaded = null;
|
|
18
|
+
const loadData = () => (loaded ||= fetch(new URL("studio/storybook.json", window.location.href)).then((r) => (r.ok ? r.json() : null)).catch(() => null));
|
|
19
|
+
|
|
20
|
+
const titleName = (title) => String(title || "").split("/").pop().trim();
|
|
21
|
+
|
|
22
|
+
const TABS = [
|
|
23
|
+
["readiness", "Readiness"],
|
|
24
|
+
["contrast", "Contrast"],
|
|
25
|
+
["spec", "Spec"],
|
|
26
|
+
["used", "Used in"],
|
|
27
|
+
["history", "History"],
|
|
28
|
+
["activity", "Activity"],
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
const styles = {
|
|
32
|
+
wrap: { padding: "12px 16px", font: "13px/1.5 system-ui, sans-serif" },
|
|
33
|
+
tabs: { display: "flex", gap: 4, marginBottom: 12, flexWrap: "wrap" },
|
|
34
|
+
tab: (on) => ({ border: "1px solid rgba(128,128,128,.35)", background: on ? "rgba(128,128,128,.18)" : "transparent", color: "inherit", borderRadius: 6, padding: "3px 10px", font: "inherit", cursor: "pointer", fontWeight: on ? 600 : 400 }),
|
|
35
|
+
row: { display: "flex", gap: 8, alignItems: "baseline", margin: "4px 0" },
|
|
36
|
+
muted: { opacity: 0.7 },
|
|
37
|
+
ok: { color: "#15803d", fontWeight: 600 },
|
|
38
|
+
no: { color: "#b45309", fontWeight: 600 },
|
|
39
|
+
list: { margin: "4px 0", paddingLeft: 18 },
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const yes = (v, label) => h("div", { style: styles.row }, h("span", { style: v ? styles.ok : styles.no }, v ? "✓" : "○"), label);
|
|
43
|
+
|
|
44
|
+
function Readiness({ c }) {
|
|
45
|
+
const r = c.readiness || {};
|
|
46
|
+
return h("div", null,
|
|
47
|
+
h("p", { style: r.ready ? styles.ok : styles.no }, r.ready ? "Ready to use" : "Not ready yet"),
|
|
48
|
+
yes(c.status === "stable", `Status: ${c.status || "not set"}`),
|
|
49
|
+
yes(r.contrastOk, "Text and backgrounds pass contrast"),
|
|
50
|
+
r.operable ? yes(r.focusOk, "Shows where keyboard focus is") : null,
|
|
51
|
+
h("div", { style: { ...styles.row, ...styles.muted } }, `${r.states || 0} interaction states designed`),
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function Contrast({ c }) {
|
|
56
|
+
if (!c.contrast) return h("p", { style: styles.muted }, "No contrast grade yet. `studio health` measures it from the design system's token pairs in every theme.");
|
|
57
|
+
const pass = c.contrast === "AA" || c.contrast === "AAA";
|
|
58
|
+
return h("div", null, h("p", null, "Contrast: ", h("span", { style: pass ? styles.ok : styles.no }, c.contrast)), h("p", { style: styles.muted }, "The worst text-on-background pair this component uses, across every theme."));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function Spec({ c }) {
|
|
62
|
+
const s = c.spec;
|
|
63
|
+
if (!s) return h("p", { style: styles.muted }, "No design spec for this component yet.");
|
|
64
|
+
return h("div", null,
|
|
65
|
+
s.purpose ? h("p", null, s.purpose) : null,
|
|
66
|
+
s.usage ? h("p", { style: styles.muted }, s.usage) : null,
|
|
67
|
+
s.statements?.length ? h("div", null, h("strong", null, "What it must do"), h("ul", { style: styles.list }, s.statements.map((t, i) => h("li", { key: i }, t)))) : null,
|
|
68
|
+
s.openQuestions?.length ? h("div", null, h("strong", null, "Open questions"), h("ul", { style: styles.list }, s.openQuestions.map((t, i) => h("li", { key: i }, t)))) : null,
|
|
69
|
+
s.file ? h("p", { style: styles.muted }, `From ${s.file}`) : null,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function Used({ c }) {
|
|
74
|
+
const u = c.usedIn;
|
|
75
|
+
if (!u) return h("p", { style: styles.muted }, "No app's usage recorded yet. `studio consumers` (or an app's `studio release`) records it.");
|
|
76
|
+
return h("div", null,
|
|
77
|
+
u.apps?.length ? h("p", null, `Used in ${u.apps.join(", ")}`) : null,
|
|
78
|
+
u.usage ? h("p", { style: styles.muted }, `Rendered ${u.usage}`) : null,
|
|
79
|
+
u.sites?.length ? h("ul", { style: styles.list }, u.sites.map((s, i) => h("li", { key: i }, typeof s === "string" ? s : s.file || JSON.stringify(s)))) : null,
|
|
80
|
+
u.composedBy?.length ? h("p", { style: styles.muted }, `Inside ${u.composedBy.join(", ")}`) : null,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const day = (iso) => { const d = new Date(iso); return Number.isNaN(+d) ? "" : d.toLocaleDateString(); };
|
|
85
|
+
const NO_HISTORY = "No history yet. `studio health` bakes it from git, then `studio-showcase-data` (or `npm run storybook`) brings it in.";
|
|
86
|
+
const plural = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
|
|
87
|
+
|
|
88
|
+
function History({ c }) {
|
|
89
|
+
const hs = c.history;
|
|
90
|
+
if (!hs?.nodes?.length) return h("p", { style: styles.muted }, NO_HISTORY);
|
|
91
|
+
return h("div", null,
|
|
92
|
+
h("p", { style: styles.muted }, `${plural(hs.commits, "change")}${hs.truncated ? " (the most recent; older ones aren't baked)" : ""}`),
|
|
93
|
+
h("ul", { style: { ...styles.list, listStyle: "none", paddingLeft: 0 } }, hs.nodes.map((n) => h("li", { key: n.sha, style: { margin: "6px 0" } },
|
|
94
|
+
h("div", null, n.subject || "(no message)"),
|
|
95
|
+
h("div", { style: { ...styles.muted, fontSize: 12 } },
|
|
96
|
+
[day(n.when), n.author, n.agent ? "agent" : null, n.sha, n.lane].filter(Boolean).join(" · "),
|
|
97
|
+
n.changes?.total ? ` · ${plural(n.changes.total, "change")}${n.changes.kinds.length ? ` (${n.changes.kinds.join(", ")})` : ""}` : "",
|
|
98
|
+
n.changes?.breaking ? h("span", { style: styles.no }, ` · ${n.changes.breaking} breaking`) : null,
|
|
99
|
+
),
|
|
100
|
+
))),
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function Activity({ c }) {
|
|
105
|
+
const a = c.activity;
|
|
106
|
+
if (!a) return h("p", { style: styles.muted }, NO_HISTORY);
|
|
107
|
+
const people = c.history?.contributors || [];
|
|
108
|
+
return h("div", null,
|
|
109
|
+
a.updates != null ? h("p", null, `${plural(a.updates, "commit")} in the last year${a.lastUpdated ? `, the latest on ${day(a.lastUpdated)}` : ""}.`) : null,
|
|
110
|
+
a.recent ? h("p", null, `Of the last ${plural(a.recent, "change")}: ${a.byAgents} by agents, ${a.byPeople} by people.`) : null,
|
|
111
|
+
people.length ? h("div", null, h("strong", null, "Who changed it"), h("ul", { style: styles.list }, people.map((p) => h("li", { key: p.author }, `${p.author} (${p.commits})`)))) : null,
|
|
112
|
+
h("p", { style: { ...styles.muted, fontSize: 11 } }, "An agent's change: a commit with a coding agent as co-author (Co-authored-by: Claude …), a bot's, or one whose subject starts with a prefix such as [agent] (health.activity.agentPrefixes)."),
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function Panel() {
|
|
117
|
+
const [data, setData] = useState(null);
|
|
118
|
+
const [tab, setTab] = useState("readiness");
|
|
119
|
+
const param = useParameter("studio", null);
|
|
120
|
+
const state = useStorybookState();
|
|
121
|
+
const story = state.storyId ? state.index?.[state.storyId] : null;
|
|
122
|
+
useEffect(() => { loadData().then(setData); }, []);
|
|
123
|
+
const name = param?.component || titleName(story?.title);
|
|
124
|
+
if (!data) return h("div", { style: styles.wrap }, h("p", { style: styles.muted }, "Loading studio's data…"));
|
|
125
|
+
if (data.missing) return h("div", { style: styles.wrap }, h("p", null, "No studio data yet. Run `studio health` in the project, then restart Storybook."));
|
|
126
|
+
const c = data.components?.[name];
|
|
127
|
+
if (!c) return h("div", { style: styles.wrap }, h("p", null, `studio has no component named "${name}".`), h("p", { style: styles.muted }, "Name it in the story: parameters: { studio: { component: 'Name' } }."));
|
|
128
|
+
const Body = { readiness: Readiness, contrast: Contrast, spec: Spec, used: Used, history: History, activity: Activity }[tab];
|
|
129
|
+
return h("div", { style: styles.wrap },
|
|
130
|
+
h("div", { style: styles.tabs }, TABS.map(([id, label]) => h("button", { key: id, type: "button", style: styles.tab(tab === id), onClick: () => setTab(id), "aria-pressed": tab === id }, label))),
|
|
131
|
+
h(Body, { c }),
|
|
132
|
+
h("p", { style: { ...styles.muted, marginTop: 16, fontSize: 11 } }, `${name} · studio data from ${new Date(data.generatedAt).toLocaleString()}`),
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/*
|
|
137
|
+
* The theme switch in the toolbar: the design system's themes from
|
|
138
|
+
* storybook.json, one click to the next. "Theme: default" leaves the story as
|
|
139
|
+
* the CSS has it (the sheet's :root, or the OS's colour scheme).
|
|
140
|
+
*/
|
|
141
|
+
function ThemeTool() {
|
|
142
|
+
const [themes, setThemes] = useState(null);
|
|
143
|
+
const [globals, updateGlobals] = useGlobals();
|
|
144
|
+
useEffect(() => { loadData().then((d) => setThemes(Array.isArray(d?.themes) ? d.themes : [])); }, []);
|
|
145
|
+
if (!themes || themes.length < 2) return null;
|
|
146
|
+
const current = globals.studioTheme || "";
|
|
147
|
+
const order = ["", ...themes.map((t) => t.id)];
|
|
148
|
+
const next = order[(order.indexOf(current) + 1) % order.length];
|
|
149
|
+
const label = themes.find((t) => t.id === current)?.label || "default";
|
|
150
|
+
return h("button", {
|
|
151
|
+
type: "button",
|
|
152
|
+
title: `Theme: ${label}. Click for ${themes.find((t) => t.id === next)?.label || "default"}.`,
|
|
153
|
+
onClick: () => updateGlobals({ studioTheme: next }),
|
|
154
|
+
style: { border: 0, background: "transparent", color: "inherit", font: "inherit", fontSize: 12, padding: "0 8px", height: 28, cursor: "pointer", alignSelf: "center" },
|
|
155
|
+
}, `Theme: ${label}`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
addons.register(ADDON, () => {
|
|
159
|
+
addons.add(TOOL, { type: types.TOOL, title: "Theme", match: ({ viewMode }) => viewMode === "story" || viewMode === "docs", render: () => h(ThemeTool) });
|
|
160
|
+
addons.add(PANEL, {
|
|
161
|
+
type: types.PANEL,
|
|
162
|
+
title: "Studio",
|
|
163
|
+
render: ({ active }) => h(AddonPanel, { active: !!active }, h(Panel)),
|
|
164
|
+
});
|
|
165
|
+
});
|
package/src/preset.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The preset (Node): serves the project's studio data to the panel.
|
|
3
|
+
*
|
|
4
|
+
* studio writes `{health.dataDir}/storybook.json` (default showcase/data) with
|
|
5
|
+
* `studio health`. Only that file is served, from a folder of its own, at
|
|
6
|
+
* `studio/storybook.json`: never the rest of the data folder. It's copied at
|
|
7
|
+
* start, so a published (static) Storybook carries it too.
|
|
8
|
+
*/
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
|
|
12
|
+
/** The data file a project's studio config points at, or null. */
|
|
13
|
+
export function studioDataFile(root = process.cwd()) {
|
|
14
|
+
let dataDir = "showcase/data";
|
|
15
|
+
try {
|
|
16
|
+
const cfg = JSON.parse(fs.readFileSync(path.join(root, "studio.config.json"), "utf8"));
|
|
17
|
+
if (typeof cfg?.health?.dataDir === "string" && cfg.health.dataDir) dataDir = cfg.health.dataDir;
|
|
18
|
+
} catch { /* no studio config: the default */ }
|
|
19
|
+
const base = path.resolve(root);
|
|
20
|
+
const file = path.resolve(base, dataDir, "storybook.json");
|
|
21
|
+
// Only inside the project, by real path too: a symlinked data folder pointing elsewhere is not read.
|
|
22
|
+
const inside = (b, f) => { const rel = path.relative(b, f); return !(rel.startsWith("..") || path.isAbsolute(rel)); };
|
|
23
|
+
if (!inside(base, file) || !fs.existsSync(file)) return null;
|
|
24
|
+
try { return inside(fs.realpathSync(base), fs.realpathSync(file)) ? file : null; } catch { return null; }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A folder holding only the data file (a copy), for staticDirs. */
|
|
28
|
+
export function servedDataDir(root = process.cwd()) {
|
|
29
|
+
const dir = path.resolve(root, "node_modules", ".cache", "studio-storybook");
|
|
30
|
+
fs.mkdirSync(path.join(dir, "studio"), { recursive: true });
|
|
31
|
+
const out = path.join(dir, "studio", "storybook.json");
|
|
32
|
+
const file = studioDataFile(root);
|
|
33
|
+
let text = null;
|
|
34
|
+
try {
|
|
35
|
+
if (file && !fs.lstatSync(file).isSymbolicLink()) text = fs.readFileSync(file, "utf8");
|
|
36
|
+
} catch { /* none */ }
|
|
37
|
+
fs.writeFileSync(out, text || JSON.stringify({ kind: "studio.storybook", version: 1, missing: true, components: {} }));
|
|
38
|
+
return dir;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const staticDirs = async (dirs = [], options = {}) => {
|
|
42
|
+
const root = options.configDir ? path.resolve(options.configDir, "..") : process.cwd();
|
|
43
|
+
return [...(dirs || []), { from: servedDataDir(root), to: "/" }];
|
|
44
|
+
};
|
package/src/preview.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The preview (the story's iframe): puts the story in the theme picked in
|
|
3
|
+
* the toolbar, the way the design system's own CSS selects it.
|
|
4
|
+
*
|
|
5
|
+
* storybook.json lists each theme with its selector, as the token sheet
|
|
6
|
+
* writes it: `[data-theme="dark"]`, `.dark`, `:root, [data-theme="light"]`.
|
|
7
|
+
* The theme is set on <html> with the first attribute or class in that
|
|
8
|
+
* selector; picking another theme takes the previous one off. No framework
|
|
9
|
+
* code: the decorator only touches the document and calls the story.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const GLOBAL = "studioTheme";
|
|
13
|
+
let loaded = null;
|
|
14
|
+
const loadThemes = () =>
|
|
15
|
+
(loaded ||= fetch(new URL("studio/storybook.json", window.location.href))
|
|
16
|
+
.then((r) => (r.ok ? r.json() : null))
|
|
17
|
+
.then((d) => (Array.isArray(d?.themes) ? d.themes : []))
|
|
18
|
+
.catch(() => []));
|
|
19
|
+
|
|
20
|
+
/** What a theme's selector sets: { attr, value } or { className }, or null for `:root` alone. */
|
|
21
|
+
export function themeTarget(selector) {
|
|
22
|
+
for (const part of String(selector || "").split(",").map((s) => s.trim())) {
|
|
23
|
+
const a = /^\[([\w-]+)(?:\s*=\s*["']?([^"'\]]*)["']?)?\]$/.exec(part);
|
|
24
|
+
if (a) return { attr: a[1], value: a[2] ?? "" };
|
|
25
|
+
const c = /^\.([\w-]+)$/.exec(part);
|
|
26
|
+
if (c) return { className: c[1] };
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Put <html> in one theme and out of the others. */
|
|
32
|
+
export function applyTheme(el, themes, id) {
|
|
33
|
+
const targets = themes.map((t) => ({ id: t.id, t: themeTarget(t.selector) })).filter((x) => x.t);
|
|
34
|
+
for (const { t } of targets) {
|
|
35
|
+
if (t.className) el.classList.remove(t.className);
|
|
36
|
+
else if (el.getAttribute(t.attr) !== null) el.removeAttribute(t.attr);
|
|
37
|
+
}
|
|
38
|
+
const pick = targets.find((x) => x.id === id)?.t;
|
|
39
|
+
if (!pick) return;
|
|
40
|
+
if (pick.className) el.classList.add(pick.className);
|
|
41
|
+
else el.setAttribute(pick.attr, pick.value);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const initialGlobals = { [GLOBAL]: "" };
|
|
45
|
+
|
|
46
|
+
export const loaders = [async () => ({ studioThemes: await loadThemes() })];
|
|
47
|
+
|
|
48
|
+
export const decorators = [
|
|
49
|
+
(storyFn, ctx) => {
|
|
50
|
+
const themes = ctx.loaded?.studioThemes || [];
|
|
51
|
+
if (typeof document !== "undefined" && themes.length) applyTheme(document.documentElement, themes, ctx.globals?.[GLOBAL] || "");
|
|
52
|
+
return storyFn();
|
|
53
|
+
},
|
|
54
|
+
];
|