11import type { ArgTypes , Preview , Decorator } from "@storybook/react-vite" ;
2+ import { addons } from "storybook/preview-api" ;
23
34import "../res/css/shared.pcss" ;
45import "./preview.css" ;
56import React , { useLayoutEffect } from "react" ;
7+ import { FORCE_RE_RENDER } from "storybook/internal/core-events" ;
8+ import { setLanguage } from "../src/shared-components/i18n" ;
69
710export const globalTypes = {
811 theme : {
912 name : "Theme" ,
10- defaultValue : "system" ,
1113 description : "Global theme for components" ,
1214 toolbar : {
1315 icon : "circlehollow" ,
@@ -21,6 +23,14 @@ export const globalTypes = {
2123 ] ,
2224 } ,
2325 } ,
26+ language : {
27+ name : "Language" ,
28+ description : "Global language for components" ,
29+ } ,
30+ initialGlobals : {
31+ theme : "system" ,
32+ language : "en" ,
33+ } ,
2434} satisfies ArgTypes ;
2535
2636const allThemesClasses = globalTypes . theme . toolbar . items . map ( ( { value } ) => `cpd-theme-${ value } ` ) ;
@@ -48,9 +58,33 @@ const withThemeProvider: Decorator = (Story, context) => {
4858 ) ;
4959} ;
5060
61+ const LanguageSwitcher : React . FC < {
62+ language : string ;
63+ } > = ( { language } ) => {
64+ useLayoutEffect ( ( ) => {
65+ const changeLanguage = async ( language : string ) => {
66+ await setLanguage ( language ) ;
67+ // Force the component to re-render to apply the new language
68+ addons . getChannel ( ) . emit ( FORCE_RE_RENDER ) ;
69+ } ;
70+ changeLanguage ( language ) ;
71+ } , [ language ] ) ;
72+
73+ return null ;
74+ } ;
75+
76+ export const withLanguageProvider : Decorator = ( Story , context ) => {
77+ return (
78+ < >
79+ < LanguageSwitcher language = { context . globals . language } />
80+ < Story />
81+ </ >
82+ ) ;
83+ } ;
84+
5185const preview : Preview = {
5286 tags : [ "autodocs" ] ,
53- decorators : [ withThemeProvider ] ,
87+ decorators : [ withThemeProvider , withLanguageProvider ] ,
5488} ;
5589
5690export default preview ;
0 commit comments