@inversestudio/storybook-addon 0.0.0-stage → 0.1.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 +6 -0
- package/README.md +23 -2
- package/package.json +46 -4
- package/src/manager.js +131 -0
- package/src/preset.js +44 -0
- package/src/preview.js +54 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# Changelog — @inversestudio/storybook-addon
|
|
2
|
+
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
- 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.
|
|
6
|
+
- 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,24 @@
|
|
|
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, and where it's used.
|
|
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
|
+
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.
|
|
23
|
+
|
|
24
|
+
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.1.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,131 @@
|
|
|
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
|
+
];
|
|
28
|
+
|
|
29
|
+
const styles = {
|
|
30
|
+
wrap: { padding: "12px 16px", font: "13px/1.5 system-ui, sans-serif" },
|
|
31
|
+
tabs: { display: "flex", gap: 4, marginBottom: 12, flexWrap: "wrap" },
|
|
32
|
+
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 }),
|
|
33
|
+
row: { display: "flex", gap: 8, alignItems: "baseline", margin: "4px 0" },
|
|
34
|
+
muted: { opacity: 0.7 },
|
|
35
|
+
ok: { color: "#15803d", fontWeight: 600 },
|
|
36
|
+
no: { color: "#b45309", fontWeight: 600 },
|
|
37
|
+
list: { margin: "4px 0", paddingLeft: 18 },
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const yes = (v, label) => h("div", { style: styles.row }, h("span", { style: v ? styles.ok : styles.no }, v ? "✓" : "○"), label);
|
|
41
|
+
|
|
42
|
+
function Readiness({ c }) {
|
|
43
|
+
const r = c.readiness || {};
|
|
44
|
+
return h("div", null,
|
|
45
|
+
h("p", { style: r.ready ? styles.ok : styles.no }, r.ready ? "Ready to use" : "Not ready yet"),
|
|
46
|
+
yes(c.status === "stable", `Status: ${c.status || "not set"}`),
|
|
47
|
+
yes(r.contrastOk, "Text and backgrounds pass contrast"),
|
|
48
|
+
r.operable ? yes(r.focusOk, "Shows where keyboard focus is") : null,
|
|
49
|
+
h("div", { style: { ...styles.row, ...styles.muted } }, `${r.states || 0} interaction states designed`),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function Contrast({ c }) {
|
|
54
|
+
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.");
|
|
55
|
+
const pass = c.contrast === "AA" || c.contrast === "AAA";
|
|
56
|
+
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."));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function Spec({ c }) {
|
|
60
|
+
const s = c.spec;
|
|
61
|
+
if (!s) return h("p", { style: styles.muted }, "No design spec for this component yet.");
|
|
62
|
+
return h("div", null,
|
|
63
|
+
s.purpose ? h("p", null, s.purpose) : null,
|
|
64
|
+
s.usage ? h("p", { style: styles.muted }, s.usage) : null,
|
|
65
|
+
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,
|
|
66
|
+
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,
|
|
67
|
+
s.file ? h("p", { style: styles.muted }, `From ${s.file}`) : null,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function Used({ c }) {
|
|
72
|
+
const u = c.usedIn;
|
|
73
|
+
if (!u) return h("p", { style: styles.muted }, "No app's usage recorded yet. `studio consumers` (or an app's `studio release`) records it.");
|
|
74
|
+
return h("div", null,
|
|
75
|
+
u.apps?.length ? h("p", null, `Used in ${u.apps.join(", ")}`) : null,
|
|
76
|
+
u.usage ? h("p", { style: styles.muted }, `Rendered ${u.usage}`) : null,
|
|
77
|
+
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,
|
|
78
|
+
u.composedBy?.length ? h("p", { style: styles.muted }, `Inside ${u.composedBy.join(", ")}`) : null,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function Panel() {
|
|
83
|
+
const [data, setData] = useState(null);
|
|
84
|
+
const [tab, setTab] = useState("readiness");
|
|
85
|
+
const param = useParameter("studio", null);
|
|
86
|
+
const state = useStorybookState();
|
|
87
|
+
const story = state.storyId ? state.index?.[state.storyId] : null;
|
|
88
|
+
useEffect(() => { loadData().then(setData); }, []);
|
|
89
|
+
const name = param?.component || titleName(story?.title);
|
|
90
|
+
if (!data) return h("div", { style: styles.wrap }, h("p", { style: styles.muted }, "Loading studio's data…"));
|
|
91
|
+
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."));
|
|
92
|
+
const c = data.components?.[name];
|
|
93
|
+
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' } }."));
|
|
94
|
+
const Body = { readiness: Readiness, contrast: Contrast, spec: Spec, used: Used }[tab];
|
|
95
|
+
return h("div", { style: styles.wrap },
|
|
96
|
+
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))),
|
|
97
|
+
h(Body, { c }),
|
|
98
|
+
h("p", { style: { ...styles.muted, marginTop: 16, fontSize: 11 } }, `${name} · studio data from ${new Date(data.generatedAt).toLocaleString()}`),
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/*
|
|
103
|
+
* The theme switch in the toolbar: the design system's themes from
|
|
104
|
+
* storybook.json, one click to the next. "Theme: default" leaves the story as
|
|
105
|
+
* the CSS has it (the sheet's :root, or the OS's colour scheme).
|
|
106
|
+
*/
|
|
107
|
+
function ThemeTool() {
|
|
108
|
+
const [themes, setThemes] = useState(null);
|
|
109
|
+
const [globals, updateGlobals] = useGlobals();
|
|
110
|
+
useEffect(() => { loadData().then((d) => setThemes(Array.isArray(d?.themes) ? d.themes : [])); }, []);
|
|
111
|
+
if (!themes || themes.length < 2) return null;
|
|
112
|
+
const current = globals.studioTheme || "";
|
|
113
|
+
const order = ["", ...themes.map((t) => t.id)];
|
|
114
|
+
const next = order[(order.indexOf(current) + 1) % order.length];
|
|
115
|
+
const label = themes.find((t) => t.id === current)?.label || "default";
|
|
116
|
+
return h("button", {
|
|
117
|
+
type: "button",
|
|
118
|
+
title: `Theme: ${label}. Click for ${themes.find((t) => t.id === next)?.label || "default"}.`,
|
|
119
|
+
onClick: () => updateGlobals({ studioTheme: next }),
|
|
120
|
+
style: { border: 0, background: "transparent", color: "inherit", font: "inherit", fontSize: 12, padding: "0 8px", height: 28, cursor: "pointer", alignSelf: "center" },
|
|
121
|
+
}, `Theme: ${label}`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
addons.register(ADDON, () => {
|
|
125
|
+
addons.add(TOOL, { type: types.TOOL, title: "Theme", match: ({ viewMode }) => viewMode === "story" || viewMode === "docs", render: () => h(ThemeTool) });
|
|
126
|
+
addons.add(PANEL, {
|
|
127
|
+
type: types.PANEL,
|
|
128
|
+
title: "Studio",
|
|
129
|
+
render: ({ active }) => h(AddonPanel, { active: !!active }, h(Panel)),
|
|
130
|
+
});
|
|
131
|
+
});
|
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
|
+
];
|