|
| 1 | +import type { Preview, Decorator } from "@storybook/react-vite"; |
| 2 | +import type { LocaleCode } from "../src/locales"; |
| 3 | +import "../src/styles.css"; |
| 4 | + |
| 5 | +const LOCALE_ITEMS: { value: LocaleCode; title: string }[] = [ |
| 6 | + { value: "en", title: "English" }, |
| 7 | + { value: "es", title: "Español" }, |
| 8 | + { value: "fr", title: "Français" }, |
| 9 | + { value: "de", title: "Deutsch" }, |
| 10 | +]; |
| 11 | + |
| 12 | +type ThemeMode = "light" | "dark" | "auto"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Mirrors ChatWidget's own logic: dark mode is driven by a `data-claudius-dark` |
| 16 | + * ancestor (see styles.css), and "auto" follows the OS `prefers-color-scheme`. |
| 17 | + */ |
| 18 | +function resolveDark(theme: ThemeMode): boolean { |
| 19 | + if (theme === "dark") return true; |
| 20 | + if (theme === "light") return false; |
| 21 | + return ( |
| 22 | + typeof window !== "undefined" && |
| 23 | + typeof window.matchMedia === "function" && |
| 24 | + window.matchMedia("(prefers-color-scheme: dark)").matches |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Wraps every story in the widget's theming context: |
| 30 | + * - `.claudius-root` + `data-claudius-dark` so `--cl-*` tokens resolve exactly |
| 31 | + * as they do in production (light is the default; dark flips the tokens). |
| 32 | + * - `dir` for RTL coverage. |
| 33 | + * - an optional widget-shaped frame for components that normally live inside |
| 34 | + * the chat panel, opted into via `parameters.widgetFrame` ("panel" renders |
| 35 | + * edge-to-edge like the header/input; "messages" pads like the message list). |
| 36 | + */ |
| 37 | +const withWidgetTheme: Decorator = (Story, context) => { |
| 38 | + const theme = (context.globals.theme as ThemeMode) ?? "light"; |
| 39 | + const direction = (context.globals.direction as "ltr" | "rtl") ?? "ltr"; |
| 40 | + const dark = resolveDark(theme); |
| 41 | + const frame = context.parameters.widgetFrame as |
| 42 | + | "panel" |
| 43 | + | "messages" |
| 44 | + | undefined; |
| 45 | + |
| 46 | + let inner = <Story />; |
| 47 | + if (frame === "panel") { |
| 48 | + inner = ( |
| 49 | + <div className="w-[380px] overflow-hidden rounded-claudius-lg bg-claudius-surface font-body shadow-claudius-elevated"> |
| 50 | + {inner} |
| 51 | + </div> |
| 52 | + ); |
| 53 | + } else if (frame === "messages") { |
| 54 | + inner = ( |
| 55 | + <div className="w-[380px] space-y-3 rounded-claudius-lg bg-claudius-surface p-4 font-body shadow-claudius-elevated"> |
| 56 | + {inner} |
| 57 | + </div> |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + return ( |
| 62 | + <div |
| 63 | + className="claudius-root" |
| 64 | + data-claudius-dark={dark ? "true" : "false"} |
| 65 | + dir={direction} |
| 66 | + style={{ |
| 67 | + minHeight: "100vh", |
| 68 | + display: "flex", |
| 69 | + alignItems: frame ? "center" : "stretch", |
| 70 | + justifyContent: frame ? "center" : "stretch", |
| 71 | + padding: frame ? "2rem" : 0, |
| 72 | + background: dark ? "#030712" : "#f1f5f9", |
| 73 | + }} |
| 74 | + > |
| 75 | + {inner} |
| 76 | + </div> |
| 77 | + ); |
| 78 | +}; |
| 79 | + |
| 80 | +const preview: Preview = { |
| 81 | + parameters: { |
| 82 | + layout: "fullscreen", |
| 83 | + controls: { |
| 84 | + matchers: { color: /(background|color)$/i, date: /Date$/i }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + initialGlobals: { |
| 88 | + locale: "en", |
| 89 | + theme: "light", |
| 90 | + direction: "ltr", |
| 91 | + }, |
| 92 | + globalTypes: { |
| 93 | + locale: { |
| 94 | + description: "Widget UI locale", |
| 95 | + toolbar: { |
| 96 | + title: "Locale", |
| 97 | + icon: "globe", |
| 98 | + items: LOCALE_ITEMS, |
| 99 | + dynamicTitle: true, |
| 100 | + }, |
| 101 | + }, |
| 102 | + theme: { |
| 103 | + description: "Color scheme", |
| 104 | + toolbar: { |
| 105 | + title: "Theme", |
| 106 | + icon: "contrast", |
| 107 | + items: [ |
| 108 | + { value: "light", title: "Light", icon: "sun" }, |
| 109 | + { value: "dark", title: "Dark", icon: "moon" }, |
| 110 | + { value: "auto", title: "Auto (OS)", icon: "browser" }, |
| 111 | + ], |
| 112 | + dynamicTitle: true, |
| 113 | + }, |
| 114 | + }, |
| 115 | + direction: { |
| 116 | + description: "Text direction", |
| 117 | + toolbar: { |
| 118 | + title: "Direction", |
| 119 | + icon: "transfer", |
| 120 | + items: [ |
| 121 | + { value: "ltr", title: "LTR" }, |
| 122 | + { value: "rtl", title: "RTL" }, |
| 123 | + ], |
| 124 | + dynamicTitle: true, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + decorators: [withWidgetTheme], |
| 129 | +}; |
| 130 | + |
| 131 | +export default preview; |
0 commit comments