44// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
55// Please see LICENSE files in the repository root for full details.
66
7- import type {
8- ArgTypes ,
9- Decorator ,
10- Parameters ,
11- Preview ,
12- } from "@storybook/react-vite" ;
7+ import type { Decorator , Preview } from "@storybook/react-vite" ;
138import { TooltipProvider } from "@vector-im/compound-web" ;
149import { initialize , mswLoader } from "msw-storybook-addon" ;
15- import { useLayoutEffect } from "react" ;
10+ import { useEffect , useLayoutEffect } from "react" ;
11+ import { I18nextProvider } from "react-i18next" ;
1612import "../src/shared.css" ;
1713import i18n , { setupI18n } from "../src/i18n" ;
1814import { DummyRouter } from "../src/test-utils/router" ;
@@ -31,37 +27,12 @@ initialize(
3127
3228setupI18n ( ) ;
3329
34- export const parameters : Parameters = {
35- controls : {
36- matchers : {
37- color : / ( b a c k g r o u n d | c o l o r ) $ / i,
38- date : / D a t e $ / ,
39- } ,
40- } ,
41- } ;
42-
43- export const globalTypes = {
44- theme : {
45- name : "Theme" ,
46- defaultValue : "system" ,
47- description : "Global theme for components" ,
48- toolbar : {
49- icon : "circlehollow" ,
50- title : "Theme" ,
51- items : [
52- { title : "System" , value : "system" , icon : "browser" } ,
53- { title : "Light" , value : "light" , icon : "sun" } ,
54- { title : "Light (high contrast)" , value : "light-hc" , icon : "sun" } ,
55- { title : "Dark" , value : "dark" , icon : "moon" } ,
56- { title : "Dark (high contrast)" , value : "dark-hc" , icon : "moon" } ,
57- ] ,
58- } ,
59- } ,
60- } satisfies ArgTypes ;
61-
62- const allThemesClasses = globalTypes . theme . toolbar . items . map (
63- ( { value } ) => `cpd-theme-${ value } ` ,
64- ) ;
30+ const allThemesClasses = [
31+ "cpd-theme-light" ,
32+ "cpd-theme-light-hc" ,
33+ "cpd-theme-dark" ,
34+ "cpd-theme-dark-hc" ,
35+ ] ;
6536
6637const ThemeSwitcher : React . FC < {
6738 theme : string ;
@@ -86,6 +57,27 @@ const withThemeProvider: Decorator = (Story, context) => {
8657 ) ;
8758} ;
8859
60+ const LocaleSwitcher : React . FC < {
61+ locale : string ;
62+ } > = ( { locale } ) => {
63+ useEffect ( ( ) => {
64+ i18n . changeLanguage ( locale ) ;
65+ } , [ locale ] ) ;
66+
67+ return null ;
68+ } ;
69+
70+ const withI18nProvider : Decorator = ( Story , context ) => {
71+ return (
72+ < >
73+ < LocaleSwitcher locale = { context . globals . locale } />
74+ < I18nextProvider i18n = { i18n } >
75+ < Story />
76+ </ I18nextProvider >
77+ </ >
78+ ) ;
79+ } ;
80+
8981const withDummyRouter : Decorator = ( Story , _context ) => {
9082 return (
9183 < DummyRouter >
@@ -102,28 +94,58 @@ const withTooltipProvider: Decorator = (Story, _context) => {
10294 ) ;
10395} ;
10496
105- export const decorators : Decorator [ ] = [
106- withThemeProvider ,
107- withDummyRouter ,
108- withTooltipProvider ,
109- ] ;
110-
111- const locales = Object . fromEntries (
112- localazyMetadata . languages . map ( ( { language, name, localizedName } ) => [
113- language ,
114- `${ localizedName } (${ name } )` ,
115- ] ) ,
116- ) ;
117-
11897const preview : Preview = {
98+ loaders : [ mswLoader ] ,
99+ parameters : {
100+ controls : {
101+ matchers : {
102+ color : / ( b a c k g r o u n d | c o l o r ) $ / i,
103+ date : / D a t e $ / ,
104+ } ,
105+ } ,
106+ } ,
107+ decorators : [
108+ withI18nProvider ,
109+ withThemeProvider ,
110+ withDummyRouter ,
111+ withTooltipProvider ,
112+ ] ,
113+ globalTypes : {
114+ theme : {
115+ name : "Theme" ,
116+ description : "Global theme for components" ,
117+ toolbar : {
118+ icon : "circlehollow" ,
119+ title : "Theme" ,
120+ items : [
121+ { title : "System" , value : "system" , icon : "browser" } ,
122+ { title : "Light" , value : "light" , icon : "sun" } ,
123+ { title : "Light (high contrast)" , value : "light-hc" , icon : "sun" } ,
124+ { title : "Dark" , value : "dark" , icon : "moon" } ,
125+ { title : "Dark (high contrast)" , value : "dark-hc" , icon : "moon" } ,
126+ ] ,
127+ } ,
128+ } ,
129+
130+ locale : {
131+ name : "Locale" ,
132+ description : "Locale for the app" ,
133+ toolbar : {
134+ title : "Language" ,
135+ icon : "globe" ,
136+ items : localazyMetadata . languages . map (
137+ ( { language, localizedName, name } ) => ( {
138+ title : `${ localizedName } (${ name } )` ,
139+ value : language ,
140+ } ) ,
141+ ) ,
142+ } ,
143+ } ,
144+ } ,
119145 initialGlobals : {
120146 locale : localazyMetadata . baseLocale ,
121- locales,
122- } ,
123- parameters : {
124- i18n,
147+ theme : "system" ,
125148 } ,
126- loaders : [ mswLoader ] ,
127149 tags : [ "autodocs" ] ,
128150} ;
129151
0 commit comments