From 11d09f17403925dbdc9ef6edc657f164cc785791 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Thu, 26 Sep 2024 19:02:23 -0700 Subject: [PATCH] Emit dark mode update event when system setting changes (#881) --- .storybook/custom-addons/theme/register.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.storybook/custom-addons/theme/register.ts b/.storybook/custom-addons/theme/register.ts index fd43c2b9f..c13a69b65 100644 --- a/.storybook/custom-addons/theme/register.ts +++ b/.storybook/custom-addons/theme/register.ts @@ -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); });