Skip to content

Commit 193187a

Browse files
committed
Replace storybook-react-i18next with our own globals
1 parent 0926c47 commit 193187a

File tree

4 files changed

+79
-123
lines changed

4 files changed

+79
-123
lines changed

frontend/.storybook/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { StorybookConfig } from "@storybook/react-vite";
99
const config: StorybookConfig = {
1010
stories: ["../{src,stories}/**/*.stories.@(js|jsx|ts|tsx)"],
1111

12-
addons: ["storybook-react-i18next", "@storybook/addon-docs"],
12+
addons: ["@storybook/addon-docs"],
1313

1414
framework: "@storybook/react-vite",
1515

frontend/.storybook/preview.tsx

Lines changed: 78 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
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";
138
import { TooltipProvider } from "@vector-im/compound-web";
149
import { initialize, mswLoader } from "msw-storybook-addon";
15-
import { useLayoutEffect } from "react";
10+
import { useEffect, useLayoutEffect } from "react";
11+
import { I18nextProvider } from "react-i18next";
1612
import "../src/shared.css";
1713
import i18n, { setupI18n } from "../src/i18n";
1814
import { DummyRouter } from "../src/test-utils/router";
@@ -31,37 +27,12 @@ initialize(
3127

3228
setupI18n();
3329

34-
export const parameters: Parameters = {
35-
controls: {
36-
matchers: {
37-
color: /(background|color)$/i,
38-
date: /Date$/,
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

6637
const 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+
8981
const 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-
11897
const preview: Preview = {
98+
loaders: [mswLoader],
99+
parameters: {
100+
controls: {
101+
matchers: {
102+
color: /(background|color)$/i,
103+
date: /Date$/,
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

frontend/package-lock.json

Lines changed: 0 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
"postcss-nesting": "^13.0.2",
7474
"rimraf": "^6.0.1",
7575
"storybook": "^9.1.5",
76-
"storybook-react-i18next": "4.0.11",
7776
"tailwindcss": "^3.4.17",
7877
"typescript": "^5.9.2",
7978
"vite": "7.1.7",

0 commit comments

Comments
 (0)