-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(storybook): Theming support for stories (#1151)
- Loading branch information
Mykola Ptushchuk
authored
Oct 30, 2024
1 parent
cbb0b91
commit 82f0d89
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.docs-story { | ||
background: var(--vf-color-background-default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |