@vueless/storybook 1.0.1-beta.1 → 1.0.1-beta.3
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/.storybook/decorators/vue3SourceDecorator.js +17 -4
- package/.storybook/main.js +1 -3
- package/.storybook/vite.config.js +3 -3
- package/package.json +7 -7
- package/.storybook/addons/storybook-dark-mode/Tool.tsx +0 -270
- package/.storybook/addons/storybook-dark-mode/constants.ts +0 -2
- package/.storybook/addons/storybook-dark-mode/index.tsx +0 -20
- package/.storybook/addons/storybook-dark-mode/preset/manager.tsx +0 -26
|
@@ -100,6 +100,14 @@ export const vue3SourceDecorator = makeDecorator({
|
|
|
100
100
|
},
|
|
101
101
|
});
|
|
102
102
|
|
|
103
|
+
function getModelValue(value) {
|
|
104
|
+
if (value === undefined) return "";
|
|
105
|
+
|
|
106
|
+
return isPrimitive(value)
|
|
107
|
+
? JSON.stringify(value)?.replaceAll('"', "")
|
|
108
|
+
: JSON.stringify(value).replaceAll('"', "'");
|
|
109
|
+
}
|
|
110
|
+
|
|
103
111
|
function preFormat(templateSource, args, argTypes) {
|
|
104
112
|
templateSource = expandVueLoopFromTemplate(templateSource, args, argTypes);
|
|
105
113
|
|
|
@@ -137,9 +145,7 @@ function preFormat(templateSource, args, argTypes) {
|
|
|
137
145
|
// eslint-disable-next-line vue/max-len
|
|
138
146
|
`</template><template v-else-if="slot === 'default' && args['defaultSlot']">{{ args['defaultSlot'] }}</template><template v-else-if="args[slot + 'Slot']">{{ args[slot + 'Slot'] }}</template></template>`;
|
|
139
147
|
|
|
140
|
-
const modelValue =
|
|
141
|
-
? JSON.stringify(args["modelValue"])?.replaceAll('"', "")
|
|
142
|
-
: JSON.stringify(args["modelValue"])?.replaceAll('"', "'");
|
|
148
|
+
const modelValue = getModelValue(args["modelValue"]);
|
|
143
149
|
|
|
144
150
|
templateSource = templateSource
|
|
145
151
|
.replace(/>[\s]+</g, "><")
|
|
@@ -150,12 +156,19 @@ function preFormat(templateSource, args, argTypes) {
|
|
|
150
156
|
new RegExp(`v-model="args\\.modelValue"`, "g"),
|
|
151
157
|
args["modelValue"] ? `v-model="${modelValue}"` : "",
|
|
152
158
|
)
|
|
159
|
+
.replace(/v-model:(\w+)="args\.(\w+)"/g, (_, modelKey, argKey) => {
|
|
160
|
+
const value = args[argKey];
|
|
161
|
+
const formattedVal = getModelValue(value);
|
|
162
|
+
|
|
163
|
+
return formattedVal ? `v-model:${modelKey}="${formattedVal}"` : "";
|
|
164
|
+
})
|
|
153
165
|
.replace(
|
|
154
166
|
/v-bind="args"/g,
|
|
155
167
|
Object.keys(componentArgs)
|
|
156
168
|
.map((key) => " " + propToSource(kebabCase(key), args[key], argTypes[key]))
|
|
157
169
|
.join(""),
|
|
158
|
-
)
|
|
170
|
+
)
|
|
171
|
+
.replace(/:class="args\.wrapperClass"/g, `class="${args.wrapperClass}"`);
|
|
159
172
|
|
|
160
173
|
return templateSource;
|
|
161
174
|
}
|
package/.storybook/main.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { resolve } from "node:path";
|
|
2
|
-
|
|
3
1
|
/** @type { import('@storybook/vue3-vite').StorybookConfig } */
|
|
4
2
|
export default {
|
|
5
3
|
stories: [
|
|
@@ -12,7 +10,7 @@ export default {
|
|
|
12
10
|
addons: [
|
|
13
11
|
"@storybook/addon-docs",
|
|
14
12
|
"@storybook/addon-links",
|
|
15
|
-
|
|
13
|
+
"@vueless/storybook-dark-mode",
|
|
16
14
|
"@storybook/addon-themes",
|
|
17
15
|
],
|
|
18
16
|
framework: {
|
|
@@ -3,19 +3,19 @@ import { defineConfig } from "vite";
|
|
|
3
3
|
// Plugins
|
|
4
4
|
import Vue from "@vitejs/plugin-vue";
|
|
5
5
|
import { Vueless, TailwindCSS } from "vueless/plugin-vite";
|
|
6
|
-
import {
|
|
6
|
+
import { INTERNAL_ENV } from "vueless/constants.js";
|
|
7
7
|
|
|
8
8
|
export default defineConfig({
|
|
9
|
-
plugins: [Vue(), TailwindCSS(), Vueless({ env:
|
|
9
|
+
plugins: [Vue(), TailwindCSS(), Vueless({ env: INTERNAL_ENV })],
|
|
10
10
|
optimizeDeps: {
|
|
11
11
|
include: [
|
|
12
12
|
"cva",
|
|
13
13
|
"tailwind-merge",
|
|
14
|
-
"@tailwindcss/forms",
|
|
15
14
|
"prettier2",
|
|
16
15
|
"prettier2/parser-html",
|
|
17
16
|
"@storybook/addon-docs/blocks",
|
|
18
17
|
"storybook/theming/create",
|
|
18
|
+
"storybook/internal/docs-tools",
|
|
19
19
|
"@storybook/addon-themes",
|
|
20
20
|
"@storybook/vue3-vite",
|
|
21
21
|
],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vueless/storybook",
|
|
3
|
-
"version": "1.0.1-beta.
|
|
3
|
+
"version": "1.0.1-beta.3",
|
|
4
4
|
"description": "Simplifies Storybook configuration for Vueless UI library.",
|
|
5
5
|
"homepage": "https://vueless.com",
|
|
6
6
|
"author": "Johnny Grid",
|
|
@@ -26,17 +26,17 @@
|
|
|
26
26
|
"release:major": "release-it major --ci --npm.publish --git.tag --github.release"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@storybook/addon-docs": "9.0.
|
|
30
|
-
"@storybook/addon-links": "9.0.
|
|
31
|
-
"@storybook/addon-themes": "9.0.
|
|
32
|
-
"@storybook/vue3-vite": "9.0.
|
|
33
|
-
"@vueless/storybook-dark-mode": "^9.0.
|
|
29
|
+
"@storybook/addon-docs": "9.0.18",
|
|
30
|
+
"@storybook/addon-links": "9.0.18",
|
|
31
|
+
"@storybook/addon-themes": "9.0.18",
|
|
32
|
+
"@storybook/vue3-vite": "9.0.18",
|
|
33
|
+
"@vueless/storybook-dark-mode": "^9.0.6",
|
|
34
34
|
"chokidar": "^4.0.3",
|
|
35
35
|
"esbuild": "^0.25.5",
|
|
36
36
|
"globby": "^14.1.0",
|
|
37
37
|
"mkdirp": "^3.0.1",
|
|
38
38
|
"prettier2": "npm:prettier@2.8.8",
|
|
39
|
-
"storybook": "9.0.
|
|
39
|
+
"storybook": "9.0.18",
|
|
40
40
|
"vue-docgen-api": "^4.79.2"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { global } from "@storybook/global";
|
|
3
|
-
import { themes, ThemeVars } from "storybook/theming";
|
|
4
|
-
import { IconButton } from "storybook/internal/components";
|
|
5
|
-
import { MoonIcon, SunIcon } from "@storybook/icons";
|
|
6
|
-
import {
|
|
7
|
-
STORY_CHANGED,
|
|
8
|
-
SET_STORIES,
|
|
9
|
-
DOCS_RENDERED,
|
|
10
|
-
} from "storybook/internal/core-events";
|
|
11
|
-
import { API, useParameter } from "storybook/manager-api";
|
|
12
|
-
import equal from "fast-deep-equal";
|
|
13
|
-
import { DARK_MODE_EVENT_NAME, UPDATE_DARK_MODE_EVENT_NAME } from "./constants";
|
|
14
|
-
|
|
15
|
-
const { document, window } = global as { document: Document; window: Window };
|
|
16
|
-
const modes = ["light", "dark"] as const;
|
|
17
|
-
type Mode = (typeof modes)[number];
|
|
18
|
-
|
|
19
|
-
interface DarkModeStore {
|
|
20
|
-
/** The class target in the preview iframe */
|
|
21
|
-
classTarget: string;
|
|
22
|
-
/** The current mode the storybook is set to */
|
|
23
|
-
current: Mode;
|
|
24
|
-
/** The dark theme for storybook */
|
|
25
|
-
dark: ThemeVars;
|
|
26
|
-
/** The dark class name for the preview iframe */
|
|
27
|
-
darkClass: string | string[];
|
|
28
|
-
/** The light theme for storybook */
|
|
29
|
-
light: ThemeVars;
|
|
30
|
-
/** The light class name for the preview iframe */
|
|
31
|
-
lightClass: string | string[];
|
|
32
|
-
/** Apply mode to iframe */
|
|
33
|
-
stylePreview: boolean;
|
|
34
|
-
/** Persist if the user has set the theme */
|
|
35
|
-
userHasExplicitlySetTheTheme: boolean;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const STORAGE_KEY = "sb-addon-themes-3";
|
|
39
|
-
export const prefersDark = window.matchMedia?.("(prefers-color-scheme: dark)");
|
|
40
|
-
|
|
41
|
-
const defaultParams: Required<Omit<DarkModeStore, "current">> = {
|
|
42
|
-
classTarget: "body",
|
|
43
|
-
dark: themes.dark,
|
|
44
|
-
darkClass: ["dark"],
|
|
45
|
-
light: themes.light,
|
|
46
|
-
lightClass: ["light"],
|
|
47
|
-
stylePreview: false,
|
|
48
|
-
userHasExplicitlySetTheTheme: false,
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/** Persist the dark mode settings in localStorage */
|
|
52
|
-
export const updateStore = (newStore: DarkModeStore) => {
|
|
53
|
-
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(newStore));
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/** Add the light/dark class to an element */
|
|
57
|
-
const toggleDarkClass = (
|
|
58
|
-
el: Element,
|
|
59
|
-
{
|
|
60
|
-
current,
|
|
61
|
-
darkClass = defaultParams.darkClass,
|
|
62
|
-
lightClass = defaultParams.lightClass,
|
|
63
|
-
}: DarkModeStore,
|
|
64
|
-
) => {
|
|
65
|
-
if (current === "dark") {
|
|
66
|
-
el.classList.remove(...arrayify(lightClass));
|
|
67
|
-
el.classList.add(...arrayify(darkClass));
|
|
68
|
-
} else {
|
|
69
|
-
el.classList.remove(...arrayify(darkClass));
|
|
70
|
-
el.classList.add(...arrayify(lightClass));
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
/** Coerce a string to a single item array, or return an array as-is */
|
|
75
|
-
const arrayify = (classes: string | string[]): string[] => {
|
|
76
|
-
const arr: string[] = [];
|
|
77
|
-
return arr.concat(classes).map((item) => item);
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/** Update the preview iframe class */
|
|
81
|
-
const updatePreview = (store: DarkModeStore) => {
|
|
82
|
-
const iframe = document.getElementById(
|
|
83
|
-
"storybook-preview-iframe",
|
|
84
|
-
) as HTMLIFrameElement;
|
|
85
|
-
|
|
86
|
-
if (!iframe) {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const iframeDocument =
|
|
91
|
-
iframe.contentDocument || iframe.contentWindow?.document;
|
|
92
|
-
const target = iframeDocument?.querySelector<HTMLElement>(store.classTarget);
|
|
93
|
-
|
|
94
|
-
if (!target) {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
toggleDarkClass(target, store);
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
/** Update the manager iframe class */
|
|
102
|
-
const updateManager = (store: DarkModeStore) => {
|
|
103
|
-
const manager = document.querySelector(store.classTarget);
|
|
104
|
-
|
|
105
|
-
if (!manager) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
toggleDarkClass(manager, store);
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
/** Update changed dark mode settings and persist to localStorage */
|
|
113
|
-
export const store = (
|
|
114
|
-
userTheme: Partial<DarkModeStore> = {},
|
|
115
|
-
): DarkModeStore => {
|
|
116
|
-
const storedItem = window.localStorage.getItem(STORAGE_KEY);
|
|
117
|
-
|
|
118
|
-
if (typeof storedItem === "string") {
|
|
119
|
-
const stored = JSON.parse(storedItem) as DarkModeStore;
|
|
120
|
-
|
|
121
|
-
if (userTheme) {
|
|
122
|
-
if (userTheme.dark && !equal(stored.dark, userTheme.dark)) {
|
|
123
|
-
stored.dark = userTheme.dark;
|
|
124
|
-
updateStore(stored);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (userTheme.light && !equal(stored.light, userTheme.light)) {
|
|
128
|
-
stored.light = userTheme.light;
|
|
129
|
-
updateStore(stored);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return stored;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return { ...defaultParams, ...userTheme } as DarkModeStore;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
// On initial load, set the dark mode class on the manager
|
|
140
|
-
// This is needed if you're using mostly CSS overrides to styles the storybook
|
|
141
|
-
// Otherwise the default theme is set in src/preset/manager.tsx
|
|
142
|
-
updateManager(store());
|
|
143
|
-
|
|
144
|
-
interface DarkModeProps {
|
|
145
|
-
/** The storybook API */
|
|
146
|
-
api: API;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/** A toolbar icon to toggle between dark and light themes in storybook */
|
|
150
|
-
export function DarkMode({ api }: DarkModeProps) {
|
|
151
|
-
const [isDark, setDark] = React.useState(prefersDark.matches);
|
|
152
|
-
const darkModeParams = useParameter<Partial<DarkModeStore>>("darkMode", {});
|
|
153
|
-
const { current: defaultMode, stylePreview, ...params } = darkModeParams;
|
|
154
|
-
const channel = api.getChannel();
|
|
155
|
-
// Save custom themes on init
|
|
156
|
-
const userHasExplicitlySetTheTheme = React.useMemo(
|
|
157
|
-
() => store(params).userHasExplicitlySetTheTheme,
|
|
158
|
-
[params],
|
|
159
|
-
);
|
|
160
|
-
/** Set the theme in storybook, update the local state, and emit an event */
|
|
161
|
-
const setMode = React.useCallback(
|
|
162
|
-
(mode: Mode) => {
|
|
163
|
-
const currentStore = store();
|
|
164
|
-
api.setOptions({ theme: currentStore[mode] });
|
|
165
|
-
setDark(mode === "dark");
|
|
166
|
-
api.getChannel().emit(DARK_MODE_EVENT_NAME, mode === "dark");
|
|
167
|
-
updateManager(currentStore);
|
|
168
|
-
if (stylePreview) {
|
|
169
|
-
updatePreview(currentStore);
|
|
170
|
-
}
|
|
171
|
-
},
|
|
172
|
-
[api, stylePreview],
|
|
173
|
-
);
|
|
174
|
-
|
|
175
|
-
/** Update the theme settings in localStorage, react, and storybook */
|
|
176
|
-
const updateMode = React.useCallback(
|
|
177
|
-
(mode?: Mode) => {
|
|
178
|
-
const currentStore = store();
|
|
179
|
-
const current =
|
|
180
|
-
mode || (currentStore.current === "dark" ? "light" : "dark");
|
|
181
|
-
updateStore({ ...currentStore, current });
|
|
182
|
-
setMode(current);
|
|
183
|
-
},
|
|
184
|
-
[setMode],
|
|
185
|
-
);
|
|
186
|
-
|
|
187
|
-
/** Update the theme based on the color preference */
|
|
188
|
-
function prefersDarkUpdate(event: MediaQueryListEvent) {
|
|
189
|
-
if (userHasExplicitlySetTheTheme || defaultMode) {
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
updateMode(event.matches ? "dark" : "light");
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/** Render the current theme */
|
|
197
|
-
const renderTheme = React.useCallback(() => {
|
|
198
|
-
const { current = "light" } = store();
|
|
199
|
-
setMode(current);
|
|
200
|
-
}, [setMode]);
|
|
201
|
-
|
|
202
|
-
/** Handle the user event and side effects */
|
|
203
|
-
const handleIconClick = () => {
|
|
204
|
-
updateMode();
|
|
205
|
-
const currentStore = store();
|
|
206
|
-
updateStore({ ...currentStore, userHasExplicitlySetTheTheme: true });
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
/** When storybook params change update the stored themes */
|
|
210
|
-
React.useEffect(() => {
|
|
211
|
-
const currentStore = store();
|
|
212
|
-
// Ensure we use the stores `current` value first to persist
|
|
213
|
-
// themeing between page loads and story changes.
|
|
214
|
-
updateStore({
|
|
215
|
-
...currentStore,
|
|
216
|
-
...darkModeParams,
|
|
217
|
-
current: currentStore.current || darkModeParams.current,
|
|
218
|
-
});
|
|
219
|
-
renderTheme();
|
|
220
|
-
}, [darkModeParams, renderTheme]);
|
|
221
|
-
React.useEffect(() => {
|
|
222
|
-
channel.on(STORY_CHANGED, renderTheme);
|
|
223
|
-
channel.on(SET_STORIES, renderTheme);
|
|
224
|
-
channel.on(DOCS_RENDERED, renderTheme);
|
|
225
|
-
prefersDark.addListener(prefersDarkUpdate);
|
|
226
|
-
return () => {
|
|
227
|
-
channel.removeListener(STORY_CHANGED, renderTheme);
|
|
228
|
-
channel.removeListener(SET_STORIES, renderTheme);
|
|
229
|
-
channel.removeListener(DOCS_RENDERED, renderTheme);
|
|
230
|
-
prefersDark.removeListener(prefersDarkUpdate);
|
|
231
|
-
};
|
|
232
|
-
});
|
|
233
|
-
React.useEffect(() => {
|
|
234
|
-
channel.on(UPDATE_DARK_MODE_EVENT_NAME, updateMode);
|
|
235
|
-
return () => {
|
|
236
|
-
channel.removeListener(UPDATE_DARK_MODE_EVENT_NAME, updateMode);
|
|
237
|
-
};
|
|
238
|
-
});
|
|
239
|
-
// Storybook's first render doesn't have the global user params loaded so we
|
|
240
|
-
// need the effect to run whenever defaultMode is updated
|
|
241
|
-
React.useEffect(() => {
|
|
242
|
-
// If a users has set the mode this is respected
|
|
243
|
-
if (userHasExplicitlySetTheTheme) {
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
if (defaultMode) {
|
|
248
|
-
updateMode(defaultMode);
|
|
249
|
-
} else if (prefersDark.matches) {
|
|
250
|
-
updateMode("dark");
|
|
251
|
-
}
|
|
252
|
-
}, [defaultMode, updateMode, userHasExplicitlySetTheTheme]);
|
|
253
|
-
return (
|
|
254
|
-
<IconButton
|
|
255
|
-
key="dark-mode"
|
|
256
|
-
title={
|
|
257
|
-
isDark ? "Change theme to light mode" : "Change theme to dark mode"
|
|
258
|
-
}
|
|
259
|
-
onClick={handleIconClick}
|
|
260
|
-
>
|
|
261
|
-
{isDark ? (
|
|
262
|
-
<SunIcon aria-hidden="true" />
|
|
263
|
-
) : (
|
|
264
|
-
<MoonIcon aria-hidden="true" />
|
|
265
|
-
)}
|
|
266
|
-
</IconButton>
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
export default DarkMode;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { addons, useState, useEffect } from "storybook/preview-api";
|
|
2
|
-
import { DARK_MODE_EVENT_NAME } from "./constants";
|
|
3
|
-
import { store } from "./Tool";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Returns the current state of storybook's dark-mode
|
|
7
|
-
*/
|
|
8
|
-
export function useDarkMode(): boolean {
|
|
9
|
-
const [isDark, setIsDark] = useState(store().current === "dark");
|
|
10
|
-
|
|
11
|
-
useEffect(() => {
|
|
12
|
-
const chan = addons.getChannel();
|
|
13
|
-
chan.on(DARK_MODE_EVENT_NAME, setIsDark);
|
|
14
|
-
return () => chan.off(DARK_MODE_EVENT_NAME, setIsDark);
|
|
15
|
-
}, []);
|
|
16
|
-
|
|
17
|
-
return isDark;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export * from "./constants";
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { addons } from "storybook/manager-api";
|
|
2
|
-
import { Addon_TypesEnum } from "storybook/internal/types";
|
|
3
|
-
import { themes } from "storybook/theming";
|
|
4
|
-
import * as React from "react";
|
|
5
|
-
|
|
6
|
-
import Tool, { prefersDark, store } from "../Tool";
|
|
7
|
-
|
|
8
|
-
const currentStore = store();
|
|
9
|
-
const currentTheme =
|
|
10
|
-
currentStore.current || (prefersDark.matches && "dark") || "light";
|
|
11
|
-
|
|
12
|
-
addons.setConfig({
|
|
13
|
-
theme: {
|
|
14
|
-
...themes[currentTheme],
|
|
15
|
-
...currentStore[currentTheme],
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
addons.register("storybook/dark-mode", (api) => {
|
|
20
|
-
addons.add("storybook/dark-mode", {
|
|
21
|
-
title: "dark mode",
|
|
22
|
-
type: Addon_TypesEnum.TOOL,
|
|
23
|
-
match: ({ viewMode }) => viewMode === "story" || viewMode === "docs",
|
|
24
|
-
render: () => <Tool api={api} />,
|
|
25
|
-
});
|
|
26
|
-
});
|