Comments (6)
I managed to make theme changeable in toolbar in storybook
in preview.js
export const globalTypes = {
theme: {
name: 'Theme',
description: 'Global theme for components',
defaultValue: 'light',
toolbar: {
items: ['light', 'dark'],
showName: true,
dynamicTitle: true,
},
},
};
and also I added decorator in the same file
export const decorators = [withStyle];
And here is withStyle.tsx
file
import { FC } from 'react';
import { ColorThemeProvider } from 'modules/styleSystem';
export function withStyle(Story: FC, context: any) {
return (
<ColorThemeProvider forcedTheme={context.globals.theme}>
<Story />
</ColorThemeProvider>
);
}
And this is my ColorThemeProvider
import { PropsWithChildren } from 'react';
import { ThemeProvider as NextThemeProvider } from 'next-themes';
export const ColorThemeProvider = ({
forcedTheme,
...props
}: PropsWithChildren<{ forcedTheme?: 'light' | 'dark' }>) => {
return (
<NextThemeProvider themes={['light', 'dark']} {...(forcedTheme ? { forcedTheme } : {})}>
{props.children}
</NextThemeProvider>
);
};
Please let me know if it works for you guys
Screen.Recording.2022-09-02.at.20.28.25.mov
from next-themes.
I am also trying to integrate next-themes
with storybook, using globalTypes
and forcedTheme
also renders light mode only
export const decorators = [
(Story, { globals }) => {
return (
<ThemeProvider attribute="class" forcedTheme={globals.theme}>
<Story />
</ThemeProvider>
);
},
];
export const globalTypes = {
theme: {
name: 'Theme',
defaultValue: 'system',
toolbar: {
icon: 'circlehollow',
items: ['light', 'dark', 'system'],
showName: true,
},
},
};
EDIT:
It does change the theme, but doesn't apply the class attribute, setTheme
is calling to changeTheme
with updateDom
set to false, which in return does not update the class
attribute.
from next-themes.
Storybook is not implemented with Next.js. Since the purpose of this library is a clean implementation of theming in Next.js, I doubt storybook support will be a thing. (Considering CRA and Gatsby are out of scope as well)
from next-themes.
In v0.2 we're not using any Next.js specific code anymore, so I'll think about making the library more generic to support Storybook or other frameworks. Definitely low priority though โย using next-themes with Next.js will always be the focus.
from next-themes.
This worked for me:
// ThemeChanger.tsx
import React from "react";
import { useTheme } from "next-themes";
import { useEffect } from "react";
const ThemeChanger = ({ theme }: { theme: "light" | "dark" }) => {
const { setTheme } = useTheme();
useEffect(() => {
setTheme(theme);
}, [setTheme, theme]);
return null;
};
export default React.memo(ThemeChanger);
// preview.js
import React from 'react';
import { ThemeProvider } from "next-themes";
import ThemeChanger from './ThemeChanger';
export const decorators = [
(Story, { globals }) => (
<ThemeProvider>
<ThemeChanger
theme={
globals?.backgrounds?.value == null ||
globals.backgrounds.value === "#F8F8F8"
? "light"
: "dark"
}
/>
<Story />
</ThemeProvider>
),
];
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
};
from next-themes.
I used a combination of the above.
// ThemeChanger.tsx
import React from "react";
import { useTheme } from "next-themes";
import { useEffect } from "react";
const ThemeChanger = ({ theme }: { theme: "modern" | "dark" | "classic" }) => {
const { setTheme } = useTheme();
useEffect(() => {
console.log(theme, "theme");
setTheme(theme);
}, [setTheme, theme]);
return null;
};
export default React.memo(ThemeChanger);
//preview.js
import "../public/static/global-styles.scss";
import { ThemeProvider } from "next-themes";
import ThemeChanger from "./ThemeChanger";
export const globalTypes = {
theme: {
name: "Theme",
description: "Global theme for components",
defaultValue: "modern",
toolbar: {
items: ["modern", "dark", "classic"],
showName: true,
dynamicTitle: true,
},
},
};
export const decorators = [
(Story, { globals }) => (
<ThemeProvider>
<ThemeChanger theme={globals.theme ? globals.theme : "modern"} />
<Story />
</ThemeProvider>
),
];
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
};
The example from @fede-rodes didn't include a "toolbar switcher" and i grabbed that from @natasha08n example and made some minor changes. Works for me!
from next-themes.
Related Issues (20)
- How can I get a transition between dark and light mode HOT 3
- defaultTheme did't work in the latest Chrome for macOS HOT 7
- Next Themes Flash of White BG when using the basePath HOT 4
- Inconsistent theming when the page loads HOT 3
- light/dark mode by changing class names on html tag HOT 2
- There is a error while setting up ThemeProvider HOT 5
- How to disable syncing themes between tabs HOT 2
- Documentation out of date with Tailwind's new dark-mode API
- Should `resolvedTheme` obey `forcedTheme`? HOT 3
- Allow nested providers HOT 2
- Theme object is injected into the "data-theme" property. HOT 1
- useTheme doesn't work in monorepo ui library. HOT 1
- `TypeError: Cannot read properties of undefined (reading 'addListener')` using `vitest`/`@testing-library/react` HOT 1
- Support for changing both class and data-theme attribute simultaneously HOT 1
- Callback Version of `setTheme` Passes Itself HOT 1
- Possibility to share across sub-domains? HOT 1
- ThemeProvider returns multiple types HOT 3
- setTheme not working HOT 1
- Theme flashing v0.3.0 HOT 6
- Next.js suspense triggered twice with next-themes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-themes.