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'
67
8+ import npmxDark from './theme'
9+
710// related: https://github.com/npmx-dev/npmx.dev/blob/1431d24be555bca5e1ae6264434d49ca15173c43/test/nuxt/setup.ts#L12-L26
811// Stub Nuxt specific globals
912// @ts -expect-error - dynamic global name
@@ -17,6 +20,12 @@ globalThis['__NUXT_COLOR_MODE__'] ??= {
1720// @ts -expect-error - dynamic global name
1821globalThis . defineOgImageComponent = fn ( )
1922
23+ // Subscribe to locale changes from storybook-i18n addon (once, outside decorator)
24+ let currentI18nInstance : any = null
25+ addons . getChannel ( ) . on ( 'LOCALE_CHANGED' , ( newLocale : string ) => {
26+ currentI18nInstance ?. setLocale ( newLocale )
27+ } )
28+
2029const preview : Preview = {
2130 parameters : {
2231 controls : {
@@ -25,25 +34,22 @@ const preview: Preview = {
2534 date : / D a t e $ / i,
2635 } ,
2736 } ,
37+ docs : {
38+ theme : npmxDark ,
39+ } ,
40+ } ,
41+ initialGlobals : {
42+ locale : 'en-US' ,
43+ locales : currentLocales . reduce (
44+ ( acc , locale ) => {
45+ acc [ locale . code ] = locale . name
46+ return acc
47+ } ,
48+ { } as Record < string , string > ,
49+ ) ,
2850 } ,
2951 // Provides toolbars to switch things like theming and language
3052 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- } ,
4753 accentColor : {
4854 name : 'Accent Color' ,
4955 description : 'Accent color' ,
@@ -70,9 +76,9 @@ const preview: Preview = {
7076 attributeName : 'data-theme' ,
7177 } ) ,
7278 ( story , context ) => {
73- const { locale, accentColor } = context . globals as {
74- locale : string
79+ const { accentColor, locale } = context . globals as {
7580 accentColor ?: string
81+ locale ?: string
7682 }
7783
7884 // Set accent color from globals
@@ -84,14 +90,12 @@ const preview: Preview = {
8490
8591 return {
8692 template : '<story />' ,
87- // Set locale from globals
8893 created ( ) {
89- if ( this . $i18n ) {
90- this . $i18n . setLocale ( locale )
91- }
92- } ,
93- updated ( ) {
94- if ( this . $i18n ) {
94+ // Store i18n instance for LOCALE_CHANGED events
95+ currentI18nInstance = this . $i18n
96+
97+ // Set initial locale when component is created
98+ if ( locale && this . $i18n ) {
9599 this . $i18n . setLocale ( locale )
96100 }
97101 } ,
0 commit comments