11import type { Preview } from '@storybook-vue/nuxt'
22import { withThemeByDataAttribute } from '@storybook/addon-themes'
3+ import { addons } from 'storybook/preview-api'
34import { currentLocales } from '../config/i18n'
45import { fn } from 'storybook/test'
56import { ACCENT_COLORS } from '../shared/utils/constants'
7+ import npmxDark from './theme'
68
79// related: https://github.com/npmx-dev/npmx.dev/blob/1431d24be555bca5e1ae6264434d49ca15173c43/test/nuxt/setup.ts#L12-L26
810// Stub Nuxt specific globals
@@ -25,25 +27,22 @@ const preview: Preview = {
2527 date : / D a t e $ / i,
2628 } ,
2729 } ,
30+ docs : {
31+ theme : npmxDark ,
32+ } ,
33+ } ,
34+ initialGlobals : {
35+ locale : 'en-US' ,
36+ locales : currentLocales . reduce (
37+ ( acc , locale ) => {
38+ acc [ locale . code ] = locale . name
39+ return acc
40+ } ,
41+ { } as Record < string , string > ,
42+ ) ,
2843 } ,
2944 // Provides toolbars to switch things like theming and language
3045 globalTypes : {
31- locale : {
32- name : 'Locale' ,
33- description : 'UI language' ,
34- defaultValue : 'en-US' ,
35- toolbar : {
36- icon : 'globe' ,
37- dynamicTitle : true ,
38- items : [
39- // English is at the top so it's easier to reset to it
40- { value : 'en-US' , title : 'English (US)' } ,
41- ...currentLocales
42- . filter ( locale => locale . code !== 'en-US' )
43- . map ( locale => ( { value : locale . code , title : locale . name } ) ) ,
44- ] ,
45- } ,
46- } ,
4746 accentColor : {
4847 name : 'Accent Color' ,
4948 description : 'Accent color' ,
@@ -70,9 +69,9 @@ const preview: Preview = {
7069 attributeName : 'data-theme' ,
7170 } ) ,
7271 ( story , context ) => {
73- const { locale, accentColor } = context . globals as {
74- locale : string
72+ const { accentColor, locale } = context . globals as {
7573 accentColor ?: string
74+ locale ?: string
7675 }
7776
7877 // Set accent color from globals
@@ -82,16 +81,24 @@ const preview: Preview = {
8281 document . documentElement . style . removeProperty ( '--accent-color' )
8382 }
8483
84+ // Store reference to i18n instance for locale changes
85+ let i18nInstance : any = null
86+
87+ // Subscribe to locale changes from storybook-i18n addon
88+ addons . getChannel ( ) . on ( 'LOCALE_CHANGED' , ( newLocale : string ) => {
89+ if ( i18nInstance ) {
90+ i18nInstance . setLocale ( newLocale )
91+ }
92+ } )
93+
8594 return {
8695 template : '<story />' ,
87- // Set locale from globals
8896 created ( ) {
89- if ( this . $i18n ) {
90- this . $i18n . setLocale ( locale )
91- }
92- } ,
93- updated ( ) {
94- if ( this . $i18n ) {
97+ // Store i18n instance for LOCALE_CHANGED events
98+ i18nInstance = this . $i18n
99+
100+ // Set initial locale when component is created
101+ if ( locale && this . $i18n ) {
95102 this . $i18n . setLocale ( locale )
96103 }
97104 } ,
0 commit comments