How to sync withThemeByDataAttribute to global storybook background #24897
Replies: 4 comments 2 replies
-
Having a similar issue, have just started using the new addon-themes with withThemeFromJSXProvider and its working well. But am not sure if there is a way to sync the theme to a background. Am relatively new to storybook, but could not find anything from searching. However the Themes Add-on Page shows an image of the theme changing and also changing the background at the same time. If this is not a currently supported feature would be handy to be able to supply the themes as an object such as below to both label them nicely in the dropdown and provide a background that syncs using the background add-on if being used:
|
Beta Was this translation helpful? Give feedback.
-
The theme of Storybook's manager UI (sidebar, addons, etc.) is completely separate from the theme of the preview (where your stories are shown). Right now there's no way to sync the two. There's a community addon storybook-dark-mode that attempts to do something similar that you could try out. It works independently of addon-themes, so it might not be a perfect solution. |
Beta Was this translation helpful? Give feedback.
-
Having the same question here. I am using Tailwind and am wondering how I can sync the background I am using for the light and dark mode with the story background when switching themes with the addon. Also really confused by the GIF in the readme where it happens automatically. |
Beta Was this translation helpful? Give feedback.
-
Thank you @JReinhold I was able to figure this out from your comment and referring to the source for // in .storybook/preview.js
import { withThemeByClassName } from '@storybook/addon-themes'
import { useEffect } from '@storybook/preview-api'
// ...
export const decorators = [
withThemeByClassName({
themes: {
light: 'light',
dark: 'dark'
},
defaultTheme: 'light'
}),
// Adds dark/light background to preview canvas.
storyFn => {
useEffect(() => {
document.documentElement.classList.add('bg-white', 'dark:bg-black')
}, [])
return storyFn()
}
] It does not sync Storybook's "background" setting. I also had to make sure to remove a |
Beta Was this translation helpful? Give feedback.
-
Ive finally figured out how to toggle my tailwind dark/light configuration via the following
with the only line required in my tailwind config file being
this predictably adds the data-mode onto the story Body tag for my components to derive their theme from. however id also like to somehow sync the overall storybook background to this theme toggle as well. regardless of the order, all im trying to achieve is toggle light or dark where the storybook background is synced to the change and a data attribute is attached to the Body tag for my components to render properly...
Beta Was this translation helpful? Give feedback.
All reactions