preview.tsx 1.8 KB

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