Skip to content

Commit

Permalink
Possibly fix theme decorator issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tylersticka committed May 20, 2021
1 parent 776b981 commit 52bf4ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export const globalTypes = {
icon: 'paintbrush',
items: [
{
// 'null' value supports a "no value selected" state, if 'undefined'
// there are sometimes missing 'key' errors in console
value: null,
title: 'No theme',
},
{
Expand Down
16 changes: 11 additions & 5 deletions .storybook/theme-decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { useEffect } from '@storybook/client-api';
* @param {string} [theme] - New theme to apply.
*/
const updateTheme = (element, theme) => {
if (!element || !element.classList) {
return;
}

const themes = [];
element.classList.forEach((className) => {
if (className.startsWith('t-') && className !== theme) {
Expand Down Expand Up @@ -41,11 +45,13 @@ export const withTheme = (story, context) => {
useEffect(() => {
// Remove any existing theme classes from the root element
updateTheme(document.documentElement);
// Query for the most appropriate story preview element
const previewElement = document
.querySelector(`#story--${context.id}`)
.closest('.docs-story');
updateTheme(previewElement, theme);
// Query for the most appropriate story element
const storyElement = document.querySelector(`#story--${context.id}`);
// Only proceed if we actually found the relevant story.
if (storyElement) {
const previewElement = storyElement.closest('.docs-story');
updateTheme(previewElement, theme);
}
});
}

Expand Down

0 comments on commit 52bf4ff

Please sign in to comment.