Emit dark mode update event when system setting changes (#881)

This commit is contained in:
Evan Simkowitz 2024-09-26 19:02:23 -07:00 committed by GitHub
parent 061949354c
commit 11d09f1740
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,15 +1,18 @@
import { FORCE_RE_RENDER } from "@storybook/core-events";
import { addons } from "@storybook/manager-api";
import { UPDATE_DARK_MODE_EVENT_NAME } from "storybook-dark-mode";
import { dark, light } from "../../theme";
addons.register("theme-switcher", (api) => {
const query = window.matchMedia("(prefers-color-scheme: dark)");
const channel = addons.getChannel();
const update = () => {
const theme = query.matches ? dark : light;
api.setOptions({ theme });
addons.getChannel().emit(FORCE_RE_RENDER);
channel.emit(FORCE_RE_RENDER);
channel.emit(UPDATE_DARK_MODE_EVENT_NAME);
};
addons.getChannel().on("storiesConfigured", update);
channel.on("storiesConfigured", update);
query.addEventListener("change", update);
});