|
| 1 | +import type { Preview } from "@storybook/nextjs"; |
| 2 | +import "../src/global.css"; |
| 3 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 4 | +import { MoonIcon, SunIcon } from "lucide-react"; |
| 5 | +import { Inter as interFont } from "next/font/google"; |
| 6 | +import { ThemeProvider, useTheme } from "next-themes"; |
| 7 | +// biome-ignore lint/style/useImportType: ok |
| 8 | +import React, { useEffect } from "react"; |
| 9 | +import { Toaster } from "sonner"; |
| 10 | +import { Button } from "../src/components/button"; |
| 11 | + |
| 12 | +const queryClient = new QueryClient(); |
| 13 | + |
| 14 | +const fontSans = interFont({ |
| 15 | + display: "swap", |
| 16 | + subsets: ["latin"], |
| 17 | + variable: "--font-sans", |
| 18 | +}); |
| 19 | + |
| 20 | +const customViewports = { |
| 21 | + xs: { |
| 22 | + // Regular sized phones (iphone 15 / 15 pro) |
| 23 | + name: "iPhone", |
| 24 | + styles: { |
| 25 | + width: "390px", |
| 26 | + height: "844px", |
| 27 | + }, |
| 28 | + }, |
| 29 | + sm: { |
| 30 | + // Larger phones (iphone 15 plus / 15 pro max) |
| 31 | + name: "iPhone Plus", |
| 32 | + styles: { |
| 33 | + width: "430px", |
| 34 | + height: "932px", |
| 35 | + }, |
| 36 | + }, |
| 37 | +}; |
| 38 | + |
| 39 | +const preview: Preview = { |
| 40 | + parameters: { |
| 41 | + viewport: { |
| 42 | + viewports: customViewports, |
| 43 | + }, |
| 44 | + controls: { |
| 45 | + matchers: { |
| 46 | + color: /(background|color)$/i, |
| 47 | + date: /Date$/i, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + decorators: [ |
| 52 | + (Story) => { |
| 53 | + return ( |
| 54 | + <ThemeProvider |
| 55 | + attribute="class" |
| 56 | + disableTransitionOnChange |
| 57 | + enableSystem={false} |
| 58 | + defaultTheme="dark" |
| 59 | + > |
| 60 | + <StoryLayout> |
| 61 | + <Story /> |
| 62 | + </StoryLayout> |
| 63 | + </ThemeProvider> |
| 64 | + ); |
| 65 | + }, |
| 66 | + ], |
| 67 | +}; |
| 68 | + |
| 69 | +export default preview; |
| 70 | + |
| 71 | +function StoryLayout(props: { children: React.ReactNode }) { |
| 72 | + const { setTheme, theme } = useTheme(); |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + document.body.className = `font-sans antialiased ${fontSans.variable}`; |
| 76 | + }, []); |
| 77 | + |
| 78 | + return ( |
| 79 | + <QueryClientProvider client={queryClient}> |
| 80 | + <div className="flex min-h-dvh min-w-0 flex-col bg-background text-foreground"> |
| 81 | + <div className="fixed right-0 bottom-0 z-50 flex justify-end gap-2 p-4"> |
| 82 | + <Button |
| 83 | + onClick={() => setTheme(theme === "dark" ? "light" : "dark")} |
| 84 | + size="sm" |
| 85 | + variant="outline" |
| 86 | + className="h-auto w-auto shrink-0 rounded-full p-2" |
| 87 | + > |
| 88 | + {theme === "dark" ? ( |
| 89 | + <MoonIcon className="size-4" /> |
| 90 | + ) : ( |
| 91 | + <SunIcon className="size-4" /> |
| 92 | + )} |
| 93 | + </Button> |
| 94 | + </div> |
| 95 | + |
| 96 | + <div className="flex min-w-0 grow flex-col">{props.children}</div> |
| 97 | + <ToasterSetup /> |
| 98 | + </div> |
| 99 | + </QueryClientProvider> |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +function ToasterSetup() { |
| 104 | + const { theme } = useTheme(); |
| 105 | + return <Toaster richColors theme={theme === "light" ? "light" : "dark"} />; |
| 106 | +} |
0 commit comments