preview.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { useEffect } from 'react';
  2. import type { Decorator, Preview } from '@storybook/react-vite';
  3. import { ConfigProvider, theme as antdTheme } from 'antd';
  4. import i18next from 'i18next';
  5. import { initReactI18next } from 'react-i18next';
  6. import enUS from '../../internal/web/translation/en-US.json';
  7. if (!i18next.isInitialized) {
  8. void i18next.use(initReactI18next).init({
  9. lng: 'en-US',
  10. fallbackLng: 'en-US',
  11. resources: { 'en-US': { translation: enUS } },
  12. interpolation: { escapeValue: false, prefix: '{', suffix: '}' },
  13. returnNull: false,
  14. });
  15. }
  16. const withTheme: Decorator = (Story, context) => {
  17. const dark = context.globals.theme === 'dark';
  18. useEffect(() => {
  19. document.documentElement.setAttribute('data-theme', dark ? 'dark' : 'light');
  20. }, [dark]);
  21. return (
  22. <ConfigProvider theme={{ algorithm: dark ? antdTheme.darkAlgorithm : antdTheme.defaultAlgorithm }}>
  23. <div style={{ padding: 24, minWidth: 320 }}>
  24. <Story />
  25. </div>
  26. </ConfigProvider>
  27. );
  28. };
  29. const preview: Preview = {
  30. decorators: [withTheme],
  31. globalTypes: {
  32. theme: {
  33. description: 'Ant Design theme',
  34. defaultValue: 'light',
  35. toolbar: {
  36. title: 'Theme',
  37. icon: 'circlehollow',
  38. items: [
  39. { value: 'light', title: 'Light' },
  40. { value: 'dark', title: 'Dark' },
  41. ],
  42. dynamicTitle: true,
  43. },
  44. },
  45. },
  46. parameters: {
  47. controls: {
  48. matchers: {
  49. color: /(background|color)$/i,
  50. date: /Date$/i,
  51. },
  52. },
  53. },
  54. };
  55. export default preview;