How do I make the canvas background color change, when a theme is selected? #25183
Replies: 8 comments 11 replies
-
Thanks for taking the time to reach out to us with your question. We appreciate it 🙏 ! So that you're aware, the styling addon you referred to is officially [deprecated}(https://storybook.js.org/addons/@storybook/addon-styling), and we're encouraging users to use one of the alternatives provided in the disclaimer (ie., Looking forward to hearing from you. Hope you have a great day. Stay safe |
Beta Was this translation helpful? Give feedback.
-
Using Tailwind with add on-theme, my preference is to use |
Beta Was this translation helpful? Give feedback.
-
There is actually a quite easy way to do this, with themes addon alone: export const decorators = [
withThemeByClassName({
themes: {
light: "light",
dark: "dark bg-neutral-900",
},
defaultTheme: "light",
}),
] Just add Tailwind background class as part of your themes css classes list. Be mindful that in Might be worth also disabling backgrounds in your preview config: const preview: Preview = {
parameters: {
backgrounds: { disable: true },
}
} as they will be conflicting with your theme classes. |
Beta Was this translation helpful? Give feedback.
-
For those in tailwindcss land I got it working with this from inspiration from everyone else! parameters: {
backgrounds: {
options: {
"light/dark": {
name: "light/dark",
value: "var(--light-dark)",
},
white: { name: "white", value: "#ffffff" },
"gray-50": { name: "gray-50", value: "#f7f7f7" },
"gray-950": { name: "gray-950", value: "#292929" },
},
},
},
initialGlobals: {
backgrounds: {
value: "light/dark",
},
},
decorators: [
withThemeByClassName({
themes: {
light: "light [--light-dark:#ffffff]", // gray-50
dark: "dark [--light-dark:#292929]", // gray-950
},
defaultTheme: "light",
}), As someone else mentioned- make sure your tailwind config is looking at this file! |
Beta Was this translation helpful? Give feedback.
-
Doesn't work. Interesting that so popular tool can't do so simple and logical thing - just apply dark background in dark mode... |
Beta Was this translation helpful? Give feedback.
-
I was looking for the same solution but couldn’t find anything anywhere. Eventually, I created this, but it’s a bit buggy. I’m not sure if anyone has a better solution.
|
Beta Was this translation helpful? Give feedback.
-
The only way I could reliably find to make it work is by making a decorator like this: export const ThemeSwitchDecorator: Decorator = (Story, context) => {
const { scheme, viewMode } = context.globals;
const isIframe = viewMode === "story";
const darkmodeBackgroundColor = "black";
function WithColor(props: {
children: React.ReactNode;
themeClassName: string;
}) {
return (
<div
style={{
padding: context.viewMode == "docs" ? "30px" : "15px",
backgroundColor:
props.themeClassName === "dark" ? darkmodeBackgroundColor : "white",
width: "100%",
}}
className={`${props.themeClassName}`}
>
{props.children}
</div>
);
}
if (scheme === "light") {
return (
<WithColor themeClassName="light">
<Story />
</WithColor>
);
}
if (scheme === "dark") {
return (
<WithColor themeClassName="dark">
<div
style={{
position: "fixed",
width: "100%",
height: context.viewMode == "docs" ? "100%" : "100vh",
backgroundColor: darkmodeBackgroundColor,
left: 0,
top: 0,
}}
></div>
<Story />
</WithColor>
);
}
return (
<>
<div
style={{
display: "flex",
width: "100%",
height: context.viewMode == "docs" ? "100%" : "100vh",
}}
>
<WithColor themeClassName="light">
<Story />
</WithColor>
<WithColor themeClassName="dark">
<Story />
</WithColor>
</div>
</>
);
}; And also setting the fullscreen parameter in preview: const preview: Preview = {
parameters: {
layout: "fullscreen", // <----- here
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
},
}; |
Beta Was this translation helpful? Give feedback.
-
I'm using Tailwind and CSS variables for dark mode (based on shadcn) and I came up with this solution Screen.Recording.2025-01-21.165312.mp4My @tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
:root {
--background: 0 0% 100%;
...
}
.dark {
--background: 222.2 84% 4.9%;
...
}
} My import type { Config } from 'tailwindcss';
import tailwindCssAnimate from 'tailwindcss-animate';
export default {
darkMode: [`class`],
theme: {
extend: {
colors: {
background: `hsl(var(--background))`, I update the Storybook const preview: Preview = {
...
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
docs: {
canvas: {
// override the canvas background based on the theme
className: `!bg-background`,
},
},
},
}; I also have to add the class to the export default {
...
safelist: [
// allow Storybook to override the canvas background based on the theme
`!bg-background`,
] |
Beta Was this translation helpful? Give feedback.
-
Summary
I'm using Sveltekit and Tailwind, and followed the guide here to add the "theme" selector:
My question is, how can I make the background color of the canvas change to a dark tailwind class, when dark-mode is selected? Thanks 🙏
Additional information
No response
Create a reproduction
No response
Beta Was this translation helpful? Give feedback.
All reactions