Skip to content

Commit 5349161

Browse files
add next-themes to storybook
1 parent 650dbba commit 5349161

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { DecoratorHelpers } from '@storybook/addon-themes';
2+
import type { ReactRenderer } from '@storybook/react';
3+
import { ThemeProvider, type ThemeProviderProps, useTheme } from 'next-themes';
4+
import { type PropsWithChildren, useEffect } from 'react';
5+
import type { DecoratorFunction } from 'storybook/internal/types';
6+
7+
type ThemeSwitcherProps = PropsWithChildren<{
8+
theme: string;
9+
}>;
10+
11+
const ThemeSwitcher = ({ theme, children }: ThemeSwitcherProps) => {
12+
const { setTheme } = useTheme();
13+
useEffect(() => setTheme(theme), [theme]);
14+
/**
15+
* If you're using tailwind and want your background to be displayed in the preview,
16+
* use <div className="bg-background">{children}</div> instead
17+
*/
18+
return children;
19+
};
20+
21+
type NextThemesDecorator = Omit<ThemeProviderProps, 'defaultTheme' | 'themes'> & {
22+
themes: Record<string, string>;
23+
defaultTheme: string;
24+
};
25+
26+
const { initializeThemeState, pluckThemeFromContext } = DecoratorHelpers;
27+
28+
export const withNextThemes = ({
29+
themes,
30+
defaultTheme,
31+
...props
32+
}: NextThemesDecorator): DecoratorFunction<ReactRenderer> => {
33+
initializeThemeState(Object.keys(themes), defaultTheme);
34+
35+
return (Story, context) => {
36+
const selectedTheme = pluckThemeFromContext(context);
37+
const { themeOverride } = context.parameters.themes ?? {};
38+
const selected = themeOverride ?? selectedTheme ?? defaultTheme;
39+
40+
return (
41+
<ThemeProvider defaultTheme={defaultTheme} {...props}>
42+
<ThemeSwitcher theme={selected}>
43+
<Story />
44+
</ThemeSwitcher>
45+
</ThemeProvider>
46+
);
47+
};
48+
};

docs/storybook/.storybook/preview.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import '@livekit/components-styles';
55
import './tailwind.css';
66

7-
import { withThemeByClassName } from '@storybook/addon-themes';
7+
import { withNextThemes } from './lk-decorators/withNextThemes';
88

99
export const parameters = {
1010
viewMode: 'docs',
@@ -30,11 +30,16 @@ export const globalTypes = {
3030
};
3131

3232
export const decorators = [
33-
withThemeByClassName({
33+
withNextThemes({
3434
themes: {
3535
light: '',
3636
dark: 'dark',
37+
system: 'system',
3738
},
38-
defaultTheme: 'light',
39+
defaultTheme: 'system',
40+
// All next-themes properties can be configured directly here
41+
enableSystem: true,
42+
disableTransitionOnChange: true,
43+
attribute: 'class',
3944
}),
4045
];

docs/storybook/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"babel-loader": "^9.0.0",
3434
"eslint-config-lk-custom": "workspace:*",
3535
"eslint-plugin-storybook": "10.1.4",
36+
"next-themes": "^0.4.6",
3637
"postcss": "^8.5.6",
3738
"storybook": "^10.1.4",
3839
"tailwindcss": "^4.1.17",

pnpm-lock.yaml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)