Skip to content

Commit

Permalink
docs(storybook): Theming support for stories (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykola Ptushchuk authored Oct 30, 2024
1 parent cbb0b91 commit 82f0d89
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .storybook/addons/withTheme.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.docs-story {
background: var(--vf-color-background-default);
}
49 changes: 49 additions & 0 deletions .storybook/addons/withTheme.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { useEffect, useRef } from "react";

import "./withTheme.css";

const THEMES = [
{
value: "is-light",
title: "Theme: Light",
},
{
value: "is-dark",
title: "Theme: Dark",
},
{
value: "is-paper",
title: "Theme: Paper",
},
];

export const themeType = {
theme: {
name: "Theme",
description: "Global theme for components",
defaultValue: THEMES[0].value,
toolbar: {
items: THEMES,
showName: true,
dynamicTitle: true,
},
},
};

export const WithThemeProvider = (Story, context) => {
const theme = context.globals.theme;
const themeRef = useRef(null);

useEffect(() => {
if (themeRef.current !== theme) {
if (themeRef.current) {
document.body.classList.remove(themeRef.current);
}

document.body.classList.add(theme);
themeRef.current = theme;
}
}, [theme]);

return <Story {...context} />;
};
8 changes: 8 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { themes } from "@storybook/theming";
import "vanilla-framework/scss/build.scss";
import { themeType, WithThemeProvider } from "./addons/withTheme";

export const parameters = {
docs: {
theme: themes.vanillaish,
},
backgrounds: {
disable: true,
},
};

export const decorators = [WithThemeProvider];

export const globalTypes = themeType;

0 comments on commit 82f0d89

Please sign in to comment.