-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpreview.ts
219 lines (198 loc) · 7.82 KB
/
preview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { getIconImportName } from "@sit-onyx/icons";
import type { Preview } from "@storybook/vue3";
import { deepmerge } from "deepmerge-ts";
import { DARK_MODE_EVENT_NAME } from "storybook-dark-mode";
import { DOCS_RENDERED } from "storybook/internal/core-events";
import { addons } from "storybook/internal/preview-api";
import type { ThemeVars } from "storybook/internal/theming";
import { enhanceEventArgTypes } from "./actions";
import { requiredGlobalType, withRequired } from "./required";
import { ONYX_BREAKPOINTS, createTheme, type BrandDetails } from "./theme";
/**
* Creates a default Storybook preview configuration for 'onyx' with the following features:
* - Improved controls (sorting and expanded controls so descriptions etc. are also shown in a single story)
* - Improved Vue-specific code highlighting (e.g. using `@` instead of `v-on:`)
* - Setup for dark mode (including docs page). Requires addon `storybook-dark-mode` to be enabled in .storybook/main.ts file
* - Custom Storybook theme using onyx colors (light and dark mode)
* - Configure viewports / breakpoints as defined by onyx
* - Logs Vue emits as Storybook events
*
* @param overrides Custom preview / overrides, will be deep merged with the default preview.
*
* @example
* ```ts
* // .storybook/preview.ts
* const preview = {
* // you need to destructure here because as of Storybook 7.6
* // it can not statically analyze that the `preview` variable is an object
* ...createPreview({
* // custom preview / overrides
* },
* }),
* };
*
* export default preview;
* ```
*/
export const createPreview = <T extends Preview = Preview>(
overrides?: T,
brandDetails?: BrandDetails,
) => {
const themes = {
light: createTheme("light", brandDetails),
dark: createTheme("dark", brandDetails),
} as const;
const defaultPreview = {
argTypesEnhancers: [enhanceEventArgTypes],
globalTypes: {
...requiredGlobalType,
},
decorators: [withRequired],
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
sort: "requiredFirst",
// needed to also show props/events descriptions etc. when opening a single story
expanded: true,
},
docs: {
// see: https://github.com/hipstersmoothie/storybook-dark-mode/issues/127#issuecomment-840701971
get theme(): ThemeVars {
const isDark = parent.document.body.classList.contains("dark");
if (isDark) {
document.body.classList.remove("light");
document.body.classList.add("dark");
} else {
document.body.classList.remove("dark");
document.body.classList.add("light");
}
return isDark ? themes.dark : themes.light;
},
source: {
language: "html",
/**
* Use a custom transformer for the story source code to better fit to our
* Vue.js code because storybook per default does not render it exactly how
* we want it to look.
* @see https://storybook.js.org/docs/react/api/doc-block-source
*/
transform: sourceCodeTransformer,
},
},
darkMode: {
stylePreview: true,
light: themes.light,
dark: themes.dark,
},
backgrounds: {
// backgrounds are not needed because we have configured the darkMode addon/toggle switch
disable: true,
},
viewport: {
viewports: ONYX_BREAKPOINTS,
},
},
} satisfies Preview;
const channel = addons.getChannel();
// our "workaround" above for dynamically setting the docs theme needs a page-reload after
// the theme has changed to take effect:
channel.once(DOCS_RENDERED, () => {
// the DARK_MODE_EVENT_NAME is emitted once after the docs have been rendered for the initial theme.
// We only want to reload the page on theme changes, that's why we use .once() to add the event listener
// only after the initial dark mode change event has been fired. Otherwise we would get an infinite loop.
channel.once(DARK_MODE_EVENT_NAME, () => {
channel.on(DARK_MODE_EVENT_NAME, () => {
window.location.reload();
});
});
});
return deepmerge<[T, typeof defaultPreview]>(overrides ?? ({} as T), defaultPreview);
};
/**
* Custom transformer for the story source code to support improved source code generation.
* and add imports for all used onyx icons so icon imports are displayed in the source code
* instead of the the raw SVG content.
*
* @see https://storybook.js.org/docs/react/api/doc-block-source
*/
export const sourceCodeTransformer = (originalSourceCode: string): string => {
const RAW_ICONS = import.meta.glob("../node_modules/@sit-onyx/icons/src/assets/*.svg", {
query: "?raw",
import: "default",
eager: true,
});
let code = originalSourceCode;
/**
* Mapping between icon SVG content (key) and icon name (value).
* Needed to display a labelled dropdown list of all available icons.
*/
const ALL_ICONS = Object.entries(RAW_ICONS).reduce<Record<string, string>>(
(obj, [filePath, content]) => {
obj[filePath.split("/").at(-1)!.replace(".svg", "")] = content as string;
return obj;
},
{},
);
const additionalImports: string[] = [];
// add icon imports to the source code for all used onyx icons
Object.entries(ALL_ICONS).forEach(([iconName, iconContent]) => {
const importName = getIconImportName(iconName);
const singleQuotedIconContent = `'${replaceAll(iconContent, '"', "\\'")}'`;
const escapedIconContent = `"${replaceAll(iconContent, '"', '\\"')}"`;
if (code.includes(iconContent)) {
code = code.replace(
new RegExp(` (\\S+)=['"]${escapeRegExp(iconContent)}['"]`),
` :$1="${importName}"`,
);
additionalImports.push(`import ${importName} from "@sit-onyx/icons/${iconName}.svg?raw";`);
} else if (code.includes(singleQuotedIconContent)) {
// support icons inside objects
code = code.replace(singleQuotedIconContent, importName);
additionalImports.push(`import ${importName} from "@sit-onyx/icons/${iconName}.svg?raw";`);
} else if (code.includes(escapedIconContent)) {
// support icons inside objects
code = code.replace(escapedIconContent, importName);
additionalImports.push(`import ${importName} from "@sit-onyx/icons/${iconName}.svg?raw";`);
}
});
// add imports for all used onyx components
// Set is used here to only include unique components if they are used multiple times
const usedOnyxComponents = [
...new Set(Array.from(code.matchAll(/<(Onyx\w+)(?:\s*\/?)/g)).map((match) => match[1])),
].sort();
if (usedOnyxComponents.length > 0) {
additionalImports.unshift(`import { ${usedOnyxComponents.join(", ")} } from "sit-onyx";`);
}
if (additionalImports.length === 0) return code;
if (code.startsWith("<script")) {
const index = code.indexOf("\n");
const hasOtherImports = code.includes("import {");
return (
code.slice(0, index) +
additionalImports.join("\n") +
(!hasOtherImports ? "\n" : "") +
code.slice(index)
);
}
return `<script lang="ts" setup>
${additionalImports.join("\n")}
</script>
${code}`;
};
/**
* Custom String.replaceAll implementation using a RegExp
* because String.replaceAll() is not available in our specified EcmaScript target in tsconfig.json
*/
export const replaceAll = (value: string, searchValue: string | RegExp, replaceValue: string) => {
return value.replace(new RegExp(searchValue, "gi"), replaceValue);
};
/**
* Escapes the given string value to be used in `new RegExp()`.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping
*/
export const escapeRegExp = (string: string) => {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
};