preview.ts 529 B

12345678910111213
  1. import { PARAM_KEY } from "./constants";
  2. // Watch localStorage for changes to the theme
  3. // and update the theme in the Storybook preview
  4. window.addEventListener("storage", (event) => {
  5. if (event.key === PARAM_KEY) {
  6. const newTheme = event.newValue ? JSON.parse(event.newValue) : "auto";
  7. const evaluatedTheme =
  8. newTheme === "auto" ? (window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light") : newTheme;
  9. document.documentElement.setAttribute("data-color-scheme", evaluatedTheme);
  10. }
  11. });