1- import type { Preview } from '@storybook-vue/nuxt'
1+ import type { Preview } from '@nuxtjs/storybook'
2+ import { currentLocales } from '../config/i18n'
3+ import { fn } from 'storybook/test'
4+ import { ACCENT_COLORS } from '../shared/utils/constants'
5+
6+ // Stub Nuxt specific globals
7+ globalThis [ '__NUXT_COLOR_MODE__' ] ??= {
8+ preference : 'system' ,
9+ value : 'dark' ,
10+ getColorScheme : fn ( ( ) => 'dark' ) ,
11+ addColorScheme : fn ( ) ,
12+ removeColorScheme : fn ( ) ,
13+ }
14+ globalThis . defineOgImageComponent = fn ( )
215
316const preview : Preview = {
417 parameters : {
@@ -9,6 +22,83 @@ const preview: Preview = {
922 } ,
1023 } ,
1124 } ,
25+ // Provides toolbars to switch things like theming and language
26+ globalTypes : {
27+ locale : {
28+ name : 'Locale' ,
29+ description : 'UI language' ,
30+ defaultValue : 'en' ,
31+ toolbar : {
32+ icon : 'globe' ,
33+ dynamicTitle : true ,
34+ items : [
35+ // English is at the top so it's easier to reset to it
36+ { value : 'en-US' , title : 'English (US)' } ,
37+ ...currentLocales
38+ . filter ( locale => locale . code !== 'en-US' )
39+ . map ( locale => ( { value : locale . code , title : locale . name } ) ) ,
40+ ] ,
41+ } ,
42+ } ,
43+ accentColor : {
44+ name : 'Accent Color' ,
45+ description : 'Accent color' ,
46+ toolbar : {
47+ icon : 'paintbrush' ,
48+ dynamicTitle : true ,
49+ items : [
50+ ...Object . keys ( ACCENT_COLORS . light ) . map ( color => ( { value : color , title : color } ) ) ,
51+ { value : 'clear' , title : 'clear' } ,
52+ ] ,
53+ } ,
54+ } ,
55+ theme : {
56+ name : 'Theme' ,
57+ description : 'Color mode' ,
58+ toolbar : {
59+ icon : 'moon' ,
60+ dynamicTitle : true ,
61+ items : [
62+ { value : 'light' , icon : 'sun' , title : 'Light' } ,
63+ { value : 'dark' , icon : 'moon' , title : 'Dark' } ,
64+ ] ,
65+ } ,
66+ } ,
67+ } ,
68+ decorators : [
69+ ( story , context ) => {
70+ const { locale, theme, accentColor } = context . globals as {
71+ locale : string
72+ theme : string
73+ accentColor ?: string
74+ }
75+
76+ // Set theme from globals
77+ document . documentElement . setAttribute ( 'data-theme' , theme )
78+
79+ // Set accent color from globals
80+ if ( accentColor ) {
81+ document . documentElement . style . setProperty ( '--accent-color' , `var(--swatch-${ accentColor } )` )
82+ } else if ( accentColor === 'clear' ) {
83+ document . documentElement . style . removeProperty ( '--accent-color' )
84+ }
85+
86+ return {
87+ template : '<story />' ,
88+ // Set locale from globals
89+ created ( ) {
90+ if ( this . $i18n ) {
91+ this . $i18n . setLocale ( locale )
92+ }
93+ } ,
94+ updated ( ) {
95+ if ( this . $i18n ) {
96+ this . $i18n . setLocale ( locale )
97+ }
98+ } ,
99+ }
100+ } ,
101+ ] ,
12102}
13103
14104export default preview
0 commit comments