|
| 1 | +import '@fontsource/inter/400.css'; |
| 2 | +import '@fontsource/inter/500.css'; |
| 3 | +import '@fontsource/inter/600.css'; |
| 4 | +import React, { useEffect } from 'react'; |
| 5 | +import type { Preview, Decorator } from '@storybook/react-vite'; |
| 6 | +import { ConfigProvider } from 'antd'; |
| 7 | +import { customTheme } from './manager'; |
| 8 | +import { |
| 9 | + geo2franceLightTheme, |
| 10 | + geo2franceDarkTheme, |
| 11 | + neutralLightTheme, |
| 12 | + neutralDarkTheme, |
| 13 | +} from '../src/theme'; |
| 14 | +import type { ThemeName } from '../src/theme'; |
| 15 | + |
| 16 | +type ThemeMode = 'light' | 'dark'; |
| 17 | +type ThemeKey = `${ThemeName}-${ThemeMode}`; |
| 18 | + |
| 19 | +const THEMES: Record<ThemeKey, object> = { |
| 20 | + 'geo2france-light': geo2franceLightTheme, |
| 21 | + 'geo2france-dark': geo2franceDarkTheme, |
| 22 | + 'neutral-light': neutralLightTheme, |
| 23 | + 'neutral-dark': neutralDarkTheme, |
| 24 | +}; |
| 25 | + |
| 26 | +const BG: Record<ThemeMode, string> = { |
| 27 | + light: '#ffffff', |
| 28 | + dark: '#141414', |
| 29 | +}; |
| 30 | + |
| 31 | +/** |
| 32 | + * Decorator global : enveloppe chaque story dans un ConfigProvider Ant Design |
| 33 | + * correspondant au couple (theme × mode) sélectionné dans la toolbar Storybook. |
| 34 | + * |
| 35 | + * Note : les stories qui contiennent leur propre <DashboardApp> (qui expose son |
| 36 | + * propre <ThemeProvider>) ignoreront ce decorator pour la partie layout — c'est |
| 37 | + * le comportement attendu (ConfigProvider nested with inherit:true). |
| 38 | + */ |
| 39 | +const withTheme: Decorator = (Story, context) => { |
| 40 | + const themeName = (context.globals.themeName as ThemeName) ?? 'geo2france'; |
| 41 | + const themeMode = (context.globals.themeMode as ThemeMode) ?? 'light'; |
| 42 | + const key: ThemeKey = `${themeName}-${themeMode}`; |
| 43 | + const antdTheme = THEMES[key] ?? THEMES['geo2france-light']; |
| 44 | + const bg = BG[themeMode]; |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + document.body.style.backgroundColor = bg; |
| 48 | + return () => { |
| 49 | + document.body.style.backgroundColor = ''; |
| 50 | + }; |
| 51 | + }, [bg]); |
| 52 | + |
| 53 | + return ( |
| 54 | + <div style={{ backgroundColor: bg, minHeight: '100vh' }}> |
| 55 | + <ConfigProvider theme={antdTheme}> |
| 56 | + <Story /> |
| 57 | + </ConfigProvider> |
| 58 | + </div> |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +const preview: Preview = { |
| 63 | + tags: ['autodocs'], |
| 64 | + |
| 65 | + globalTypes: { |
| 66 | + themeName: { |
| 67 | + name: 'Thème', |
| 68 | + defaultValue: 'geo2france', |
| 69 | + toolbar: { |
| 70 | + icon: 'paintbrush', |
| 71 | + items: [ |
| 72 | + { value: 'geo2france', title: 'Géo2France' }, |
| 73 | + { value: 'neutral', title: 'Neutral' }, |
| 74 | + ], |
| 75 | + dynamicTitle: true, |
| 76 | + }, |
| 77 | + }, |
| 78 | + themeMode: { |
| 79 | + name: 'Mode', |
| 80 | + defaultValue: 'light', |
| 81 | + toolbar: { |
| 82 | + icon: 'mirror', |
| 83 | + items: [ |
| 84 | + { value: 'light', title: 'Light', right: '☀️' }, |
| 85 | + { value: 'dark', title: 'Dark', right: '🌙' }, |
| 86 | + ], |
| 87 | + dynamicTitle: true, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + |
| 92 | + decorators: [withTheme], |
| 93 | + |
| 94 | + parameters: { |
| 95 | + codePanel: true, |
| 96 | + docs: { |
| 97 | + theme: customTheme, |
| 98 | + }, |
| 99 | + controls: { |
| 100 | + matchers: { |
| 101 | + color: /(background|color)$/i, |
| 102 | + date: /Date$/i, |
| 103 | + }, |
| 104 | + }, |
| 105 | + options: { |
| 106 | + storySort: { |
| 107 | + order: [ |
| 108 | + 'Documentation', ['Introduction'], |
| 109 | + 'Layout', ['DashboardApp'], |
| 110 | + 'Dataset', |
| 111 | + 'Dataviz', |
| 112 | + 'Controle', |
| 113 | + ], |
| 114 | + }, |
| 115 | + }, |
| 116 | + a11y: { |
| 117 | + // Mode bloquant : les violations d'accessibilité font échouer les tests vitest/CI. |
| 118 | + // Les stories avec des problèmes techniques connus (canvas ECharts) portent |
| 119 | + // leur propre surcharge `parameters.a11y.test = 'todo'` avec un commentaire FIXME. |
| 120 | + test: 'error', |
| 121 | + config: { |
| 122 | + rules: [ |
| 123 | + // FIXME(a11y): La couleur primaire Géo2France (#95c11f) a un ratio de |
| 124 | + // contraste ≈ 2.9:1, en-dessous du seuil WCAG AA (4.5:1 texte normal, |
| 125 | + // 3:1 grand texte). C'est un choix de charte graphique délibéré. |
| 126 | + // À traiter dans un ticket dédié : fix/a11y-geo2france-contrast. |
| 127 | + { id: 'color-contrast', enabled: false }, |
| 128 | + ], |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | +}; |
| 133 | + |
| 134 | +export default preview; |
0 commit comments