Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: legg til mulighet for å velge fargetema i Storybook #4633

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const config: StorybookConfig = {
stories: ["../packages/jokul/**/*.stories.@(ts|tsx)"],
staticDirs: ["../storybook-public"],
addons: [
"@storybook/addon-onboarding",
"@storybook/addon-essentials",
"@chromatic-com/storybook",
"@storybook/addon-interactions",
],
features: {
backgroundsStoryGlobals: true,
},
framework: {
name: "@storybook/react-vite",
options: {},
Expand Down
3 changes: 0 additions & 3 deletions .storybook/preview-body.html

This file was deleted.

33 changes: 33 additions & 0 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,42 @@ import React from "react";
import "../packages/jokul/src/components/card/styles/_index.scss";
import { PropDocs } from "./docs/props/PropDocs.js";
import "./global.scss";
import { themeDecorator, themeGlobal, themes } from "./theme.js";

const preview: Preview = {
globalTypes: {
theme: themeGlobal,
},
initialGlobals: {
theme: themes[0],
backgrounds: { value: "page" },
},
decorators: [themeDecorator],
parameters: {
backgrounds: {
options: {
page: {
name: "Page",
value: "var(--jkl-color-background-page)",
},
pageVariant: {
name: "Page variant",
value: "var(--jkl-color-background-page-variant)",
},
container: {
name: "Container",
value: "var(--jkl-color-background-container)",
},
containerLow: {
name: "Container low",
value: "var(--jkl-color-background-container-low)",
},
containerHigh: {
name: "Container high",
value: "var(--jkl-color-background-container-high)",
},
},
},
options: {
storySort: (a, b) => {
let compareBy = "name";
Expand Down
43 changes: 43 additions & 0 deletions .storybook/theme.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 />;
};
Loading