Closed sandranrivas closed 1 year ago
Yep, doesn't work for me either. I had to use useDarkMode()
and just add a manual wrapper.
Not working for me either. @dmackerman would you mind sharing the code for your manual wrapper?
I'm not finding this to even be setting the dark class on the iframe body either when using the default settings. For now I've added the below event listener to the preview-head.html
file that parses the local storage and setts the dark class accordingly.
<script type="text/javascript">
function updateTheme() {
const themeString = localStorage.getItem('sb-addon-themes-3');
if (themeString) {
const theme = JSON.parse(themeString);
if (theme['current'] === 'light') {
document.documentElement.classList.remove('dark');
} else {
document.documentElement.classList.add('dark');
}
}
}
window.addEventListener('storage', (e) => {
updateTheme();
});
</script>
bump on this. The solution by @rpf3 works great but this should be fixed natively in the plugin.
PRs welcome!
@hipstersmoothie I am taking a stab at this but I'm having a hell of a time trying to validate my fix locally. I have a skeleton Storybook project set up and was trying to link the local project but am not seeing any changes reflected, even cosmetic ones to test out the linking. Do you mind me asking what your dev workflow is?
I usually just make changes to files in my node_modules and port over fixes.
If you're linking the project you'll want to make sure that you're running build:watch
so any changes you make are in the files ran by storybook.
Also: I've found that you want to remove the storybook cache while developing storybook adding by running rm -rf node_modules/.cache/storybook/ && yarn storybook
@rpf3 I just merged an example into the repo so you can test there or create another example that showcases the feature so you can debug
Wrapping in a decorator your story using useDarkMode()
seems the right thing to do.
But for me, after upgrading to SB7+Next+Webpack my addons channel is no longer working (so no useDarkMode()
possible) and didn't find for now the origin (ref: https://github.com/hipstersmoothie/storybook-dark-mode/issues/205).
So for me, the quick solution is to use preview-head.html
with:
<script type="text/javascript">
function updateTheme() {
const themeString = localStorage.getItem('sb-addon-themes-3');
if (themeString) {
const theme = JSON.parse(themeString);
const themeValue = theme['current'] === 'light' ? 'light' : 'dark';
document.documentElement.dataset.theme = themeValue;
document.documentElement.dataset.frTheme = themeValue;
document.documentElement.dataset.frScheme = themeValue;
}
}
window.addEventListener('storage', (e) => {
updateTheme();
});
// Call it at start to init correctly
updateTheme();
</script>
(big sorry for this dirty workaround)
still a bug
:rocket: Issue was released in v3.0.0
:rocket:
still not working for me, back to using the preview.js fix
When selecting html like in the documentation shown, The code will only be shown in body.
I can imagine, that the error is on https://github.com/hipstersmoothie/storybook-dark-mode/blob/master/src/Tool.tsx
on line 84.
Instead of:
It should be