-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy paththeme.tsx
43 lines (36 loc) · 1.18 KB
/
theme.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { ReactRenderer } from "@storybook/react";
import React, { useEffect } from "react";
import { DecoratorFunction } from "storybook/internal/types";
export const themes = [undefined, "light", "dark"] as const;
const applyTheme = (element: HTMLElement, theme: (typeof themes)[number]) => {
element.classList.add("jkl");
element.dataset["theme"] = theme;
};
const clearTheme = (element: HTMLElement) => {
delete element.dataset["theme"];
};
export const themeGlobal = {
description: "Fargetema for eksemplet",
toolbar: {
title: "Fargetema",
icon: "paintbrush",
items: [
{ title: "Automatisk", value: themes[0], icon: "contrast" },
{ title: "Lyst", value: themes[1], icon: "sun" },
{ title: "Mørkt", value: themes[2], icon: "moon" },
],
dynamicTitle: true,
},
};
export const themeDecorator: DecoratorFunction<ReactRenderer, {}> = (
Story,
context,
) => {
useEffect(() => {
const body = window.document.body;
clearTheme(body);
applyTheme(body, context.globals.theme);
return () => clearTheme(body);
}, [context.globals.theme]);
return <Story />;
};