Skip to content

Commit

Permalink
[UI v2] experiment: Adds dark mode decorator for all stories (#16670)
Browse files Browse the repository at this point in the history
  • Loading branch information
devinvillarosa authored Jan 10, 2025
1 parent 11d32be commit aef571f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ui-v2/.storybook/preview-body.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<html class="bg-slate-800">
</html>
2 changes: 2 additions & 0 deletions ui-v2/.storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ModeDecorator } from "@/storybook/utils";
import type { Preview } from "@storybook/react";
import { handlers } from "@tests/utils/handlers";
import { initialize, mswLoader } from "msw-storybook-addon";
Expand All @@ -16,6 +17,7 @@ export default {
},
},
},
decorators: [ModeDecorator],
// Provide the MSW addon loader globally
loaders: [mswLoader],
} satisfies Preview;
1 change: 1 addition & 0 deletions ui-v2/src/storybook/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { routerDecorator } from "./router-decorator";
export { reactQueryDecorator } from "./react-query-decorator";
export { toastDecorator } from "./toast-decorator";
export { ModeDecorator } from "./mode-decorator";
31 changes: 31 additions & 0 deletions ui-v2/src/storybook/utils/mode-decorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { StoryFn } from "@storybook/react";
import { useEffect, useState } from "react";

export const ModeDecorator = (Story: StoryFn) => {
const [isDarkMode, setIsDarkMode] = useState(false);
const toggleMode = () => {
setIsDarkMode((curr) => !curr);
};

// sync external css with state
useEffect(() => {
document.documentElement.classList.toggle("dark", isDarkMode);
}, [isDarkMode]);

return (
<>
<div className="absolute bottom-4 left-4 z-50">
<div className="flex items-center space-x-2">
<Switch id="theme-mode" checked={isDarkMode} onClick={toggleMode} />
<Label htmlFor="theme-mode">
{isDarkMode ? "Light Mode" : "Dark Mode (experimental)"}
</Label>
</div>
</div>
{/** @ts-expect-error Error typing from React 19 types upgrade. Will need to wait for this up be updated */}
<Story />
</>
);
};

0 comments on commit aef571f

Please sign in to comment.