Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dark mode class not applied in "open canvas in new tab" mode #173

Open
quantizor opened this issue Jan 14, 2022 · 2 comments
Open

Dark mode class not applied in "open canvas in new tab" mode #173

quantizor opened this issue Jan 14, 2022 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@quantizor
Copy link

Storybook 6.x, storybook-dark-mode 1.0.8

When using the config storyPreview: true the dark mode class is not applied to the canvas body tag when using the Storybook functionality to open the story in a new tab.

Screen Shot 2022-01-14 at 5 02 38 PM

Result:

Screen Shot 2022-01-14 at 5 02 44 PM

@farmerpaul
Copy link

Unfortunately, this bug seems to make the stylePreview: true setting incompatible with Chromatic's component views, which are also rendered in isolation. Chromatic relies heavily on isolated component renderings for its features.

@farmerpaul
Copy link

I came up with a workaround for anyone interested! Create .storybook/preview-head.html and populate it with this code:

<script>
  /* =================================================
  Manually apply dark/light mode class to target element if not already applied.
  Workaround for bug of storybook-dark-mode (stylePreview: true in isolated view).
  @see https://github.com/hipstersmoothie/storybook-dark-mode/issues/173
  =================================================== */
  window.addEventListener('DOMContentLoaded', () => {
    const getStore = () => {
      const storeJson = localStorage.getItem('sb-addon-themes-3');
      return storeJson ? JSON.parse(storeJson) : undefined;
    };

    const store = getStore();
    const target = document.querySelector(store.classTarget);

    // Check if required dark/light class has NOT been applied to the target.
    if (
      target &&
      !target.classList.contains(store.current === 'light' ? store.lightClass : store.darkClass)
    ) {
      // If absent, bug is occurring, so update classList and respond to local storage changes.
      const updateTheme = () => {
        const newStore = getStore();
        if (newStore) {
          target.classList.toggle(newStore.lightClass, newStore.current === 'light');
          target.classList.toggle(newStore.darkClass, newStore.current === 'dark');
        }
      };

      window.addEventListener('storage', updateTheme);
      updateTheme();
    }
  });
</script>

@hipstersmoothie hipstersmoothie added the help wanted Extra attention is needed label Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants