preview.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import type { Preview, Decorator } from "@storybook/react";
  2. import { useEffect } from "react";
  3. import styles from "./storybook.module.scss";
  4. import { BodyProviders } from "../src/AppShell/body-providers.jsx";
  5. import { sans } from "../src/AppShell/fonts";
  6. import { RootProviders } from "../src/AppShell/index.jsx";
  7. import shellStyles from "../src/AppShell/index.module.scss";
  8. const preview = {
  9. globalTypes: {
  10. theme: {
  11. description: "Theme",
  12. toolbar: {
  13. title: "Theme",
  14. icon: "sun",
  15. items: [
  16. { value: "light", title: "Light", icon: "sun" },
  17. { value: "dark", title: "Dark", icon: "moon" },
  18. ],
  19. dynamicTitle: true,
  20. },
  21. },
  22. background: {
  23. description: "Background",
  24. toolbar: {
  25. title: "Background",
  26. icon: "switchalt",
  27. items: [
  28. { value: "primary", title: "Primary", icon: "switchalt" },
  29. { value: "secondary", title: "Secondary", icon: "contrast" },
  30. ],
  31. dynamicTitle: true,
  32. },
  33. },
  34. },
  35. initialGlobals: {
  36. background: "primary",
  37. theme: "light",
  38. },
  39. parameters: {
  40. layout: "centered",
  41. actions: { argTypesRegex: "^on[A-Z].*" },
  42. nextjs: {
  43. appDirectory: true,
  44. navigation: {
  45. segments: [],
  46. },
  47. },
  48. },
  49. } satisfies Preview;
  50. export default preview;
  51. export const decorators: Decorator[] = [
  52. (Story, { globals, parameters }) => {
  53. useEffect(() => {
  54. document.documentElement.classList.add(
  55. sans.className,
  56. shellStyles.html ?? "",
  57. );
  58. document.body.classList.add(shellStyles.body ?? "");
  59. }, []);
  60. return (
  61. <RootProviders>
  62. {globals.bare ? (
  63. <Story />
  64. ) : (
  65. <BodyProviders
  66. className={styles.contents ?? ""}
  67. {...(isValidTheme(globals.theme) && { theme: globals.theme })}
  68. {...(typeof parameters.layout === "string" && {
  69. "data-layout": parameters.layout,
  70. })}
  71. {...(typeof globals.background === "string" && {
  72. "data-background": globals.background,
  73. })}
  74. >
  75. <Story />
  76. </BodyProviders>
  77. )}
  78. </RootProviders>
  79. );
  80. },
  81. ];
  82. const isValidTheme = (theme: unknown): theme is "light" | "dark" =>
  83. theme === "light" || theme === "dark";