4
4
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5
5
// Please see LICENSE files in the repository root for full details.
6
6
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" ;
13
8
import { TooltipProvider } from "@vector-im/compound-web" ;
14
9
import { initialize , mswLoader } from "msw-storybook-addon" ;
15
- import { useLayoutEffect } from "react" ;
10
+ import { useEffect , useLayoutEffect } from "react" ;
11
+ import { I18nextProvider } from "react-i18next" ;
16
12
import "../src/shared.css" ;
17
13
import i18n , { setupI18n } from "../src/i18n" ;
18
14
import { DummyRouter } from "../src/test-utils/router" ;
@@ -31,37 +27,12 @@ initialize(
31
27
32
28
setupI18n ( ) ;
33
29
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
+ ] ;
65
36
66
37
const ThemeSwitcher : React . FC < {
67
38
theme : string ;
@@ -86,6 +57,27 @@ const withThemeProvider: Decorator = (Story, context) => {
86
57
) ;
87
58
} ;
88
59
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
+
89
81
const withDummyRouter : Decorator = ( Story , _context ) => {
90
82
return (
91
83
< DummyRouter >
@@ -102,28 +94,58 @@ const withTooltipProvider: Decorator = (Story, _context) => {
102
94
) ;
103
95
} ;
104
96
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
-
118
97
const 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
+ } ,
119
145
initialGlobals : {
120
146
locale : localazyMetadata . baseLocale ,
121
- locales,
122
- } ,
123
- parameters : {
124
- i18n,
147
+ theme : "system" ,
125
148
} ,
126
- loaders : [ mswLoader ] ,
127
149
tags : [ "autodocs" ] ,
128
150
} ;
129
151
0 commit comments