Skip to content

Commit 2909a50

Browse files
committed
add toolbar app settings
1 parent bc318bd commit 2909a50

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

.storybook/preview.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,87 @@ const preview: Preview = {
2525
},
2626
},
2727
},
28+
// Provides toolbars to switch things like theming and language
29+
globalTypes: {
30+
locale: {
31+
name: 'Locale',
32+
description: 'UI language',
33+
defaultValue: 'en-US',
34+
toolbar: {
35+
icon: 'globe',
36+
dynamicTitle: true,
37+
items: [
38+
// English is at the top so it's easier to reset to it
39+
{ value: 'en-US', title: 'English (US)' },
40+
...currentLocales
41+
.filter(locale => locale.code !== 'en-US')
42+
.map(locale => ({ value: locale.code, title: locale.name })),
43+
],
44+
},
45+
},
46+
accentColor: {
47+
name: 'Accent Color',
48+
description: 'Accent color',
49+
toolbar: {
50+
icon: 'paintbrush',
51+
dynamicTitle: true,
52+
items: [
53+
...Object.keys(ACCENT_COLORS.light).map(color => ({
54+
value: color,
55+
title: color.charAt(0).toUpperCase() + color.slice(1),
56+
})),
57+
{ value: undefined, title: 'No Accent' },
58+
],
59+
},
60+
},
61+
theme: {
62+
name: 'Theme',
63+
description: 'Color mode',
64+
defaultValue: 'dark',
65+
toolbar: {
66+
icon: 'moon',
67+
dynamicTitle: true,
68+
items: [
69+
{ value: 'light', icon: 'sun', title: 'Light' },
70+
{ value: 'dark', icon: 'moon', title: 'Dark' },
71+
],
72+
},
73+
},
74+
},
75+
decorators: [
76+
(story, context) => {
77+
const { locale, theme, accentColor } = context.globals as {
78+
locale: string
79+
theme: string
80+
accentColor?: string
81+
}
82+
83+
// Set theme from globals
84+
document.documentElement.setAttribute('data-theme', theme)
85+
86+
// Set accent color from globals
87+
if (accentColor) {
88+
document.documentElement.style.setProperty('--accent-color', `var(--swatch-${accentColor})`)
89+
} else {
90+
document.documentElement.style.removeProperty('--accent-color')
91+
}
92+
93+
return {
94+
template: '<story />',
95+
// Set locale from globals
96+
created() {
97+
if (this.$i18n) {
98+
this.$i18n.setLocale(locale)
99+
}
100+
},
101+
updated() {
102+
if (this.$i18n) {
103+
this.$i18n.setLocale(locale)
104+
}
105+
},
106+
}
107+
},
108+
],
28109
}
29110

30111
export default preview

0 commit comments

Comments
 (0)