|
| 1 | +import "@opencode-ai/ui/styles" |
| 2 | + |
| 3 | +import { createEffect, onCleanup, onMount } from "solid-js" |
| 4 | +import addonA11y from "@storybook/addon-a11y" |
| 5 | +import addonDocs from "@storybook/addon-docs" |
| 6 | +import { MetaProvider } from "@solidjs/meta" |
| 7 | +import { addons } from "storybook/preview-api" |
| 8 | +import { GLOBALS_UPDATED } from "storybook/internal/core-events" |
| 9 | +import { createJSXDecorator, definePreview } from "storybook-solidjs-vite" |
| 10 | +import { Code } from "@opencode-ai/ui/code" |
| 11 | +import { CodeComponentProvider } from "@opencode-ai/ui/context/code" |
| 12 | +import { DialogProvider } from "@opencode-ai/ui/context/dialog" |
| 13 | +import { DiffComponentProvider } from "@opencode-ai/ui/context/diff" |
| 14 | +import { MarkedProvider } from "@opencode-ai/ui/context/marked" |
| 15 | +import { Diff } from "@opencode-ai/ui/diff" |
| 16 | +import { ThemeProvider, useTheme, type ColorScheme } from "@opencode-ai/ui/theme" |
| 17 | +import { Font } from "@opencode-ai/ui/font" |
| 18 | + |
| 19 | +function resolveScheme(value: unknown): ColorScheme { |
| 20 | + if (value === "light" || value === "dark" || value === "system") return value |
| 21 | + return "system" |
| 22 | +} |
| 23 | + |
| 24 | +const channel = addons.getChannel() |
| 25 | + |
| 26 | +const Scheme = (props: { value?: unknown }) => { |
| 27 | + const theme = useTheme() |
| 28 | + const apply = (value?: unknown) => { |
| 29 | + theme.setColorScheme(resolveScheme(value)) |
| 30 | + } |
| 31 | + createEffect(() => { |
| 32 | + apply(props.value) |
| 33 | + }) |
| 34 | + createEffect(() => { |
| 35 | + const root = document.documentElement |
| 36 | + root.classList.remove("light", "dark") |
| 37 | + root.classList.add(theme.mode()) |
| 38 | + }) |
| 39 | + onMount(() => { |
| 40 | + const handler = (event: { globals?: Record<string, unknown> }) => { |
| 41 | + apply(event.globals?.theme) |
| 42 | + } |
| 43 | + channel.on(GLOBALS_UPDATED, handler) |
| 44 | + onCleanup(() => channel.off(GLOBALS_UPDATED, handler)) |
| 45 | + }) |
| 46 | + return null |
| 47 | +} |
| 48 | + |
| 49 | +const frame = createJSXDecorator((Story, context) => { |
| 50 | + const override = context.parameters?.themes?.themeOverride |
| 51 | + const selected = context.globals?.theme |
| 52 | + const pick = override === "light" || override === "dark" ? override : selected |
| 53 | + const scheme = resolveScheme(pick) |
| 54 | + return ( |
| 55 | + <MetaProvider> |
| 56 | + <Font /> |
| 57 | + <ThemeProvider> |
| 58 | + <Scheme value={scheme} /> |
| 59 | + <DialogProvider> |
| 60 | + <MarkedProvider> |
| 61 | + <DiffComponentProvider component={Diff}> |
| 62 | + <CodeComponentProvider component={Code}> |
| 63 | + <div |
| 64 | + style={{ |
| 65 | + "min-height": "100vh", |
| 66 | + padding: "24px", |
| 67 | + "background-color": "var(--background-base)", |
| 68 | + color: "var(--text-base)", |
| 69 | + }} |
| 70 | + > |
| 71 | + <Story /> |
| 72 | + </div> |
| 73 | + </CodeComponentProvider> |
| 74 | + </DiffComponentProvider> |
| 75 | + </MarkedProvider> |
| 76 | + </DialogProvider> |
| 77 | + </ThemeProvider> |
| 78 | + </MetaProvider> |
| 79 | + ) |
| 80 | +}) |
| 81 | + |
| 82 | +export default definePreview({ |
| 83 | + addons: [addonDocs(), addonA11y()], |
| 84 | + decorators: [frame], |
| 85 | + globalTypes: { |
| 86 | + theme: { |
| 87 | + name: "Theme", |
| 88 | + description: "Global theme", |
| 89 | + defaultValue: "light", |
| 90 | + }, |
| 91 | + }, |
| 92 | + parameters: { |
| 93 | + actions: { |
| 94 | + argTypesRegex: "^on.*", |
| 95 | + }, |
| 96 | + controls: { |
| 97 | + matchers: { |
| 98 | + color: /(background|color)$/i, |
| 99 | + date: /Date$/i, |
| 100 | + }, |
| 101 | + }, |
| 102 | + a11y: { |
| 103 | + test: "todo", |
| 104 | + }, |
| 105 | + }, |
| 106 | +}) |
0 commit comments