:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(quill): stop calling preview hooks outside story context in storybook (#61818)
Adam Leith679fd38fcad0
diff --git a/packages/quill/apps/storybook/.storybook/preview.tsx b/packages/quill/apps/storybook/.storybook/preview.tsxindex 1ef882c0190a..57b7f210106b 100644--- a/packages/quill/apps/storybook/.storybook/preview.tsx+++ b/packages/quill/apps/storybook/.storybook/preview.tsx@@ -1,36 +1,24 @@import './storybook.css'-import React, { useEffect } from 'react'+import React, { useEffect, useState } from 'react'import type { Preview } from '@storybook/react'import { themes } from '@storybook/theming'import { DocsContainer } from '@storybook/addon-docs'import { useDarkMode } from 'storybook-dark-mode'-/**- * Reads the addon's dark-mode signal and syncs it to:- * - `<html>` class/attribute (so quill's `--background` etc. resolve to dark)- * - the inline story canvas wrapper.- *- * Works for both Story and Docs views — DocsContainer below covers the- * surrounding `.sbdocs-wrapper` chrome via Storybook's official theming.- */-function ThemeSync({ children }: { children: React.ReactNode }): React.ReactElement {- const isDark = useDarkMode()+// The Docs container renders outside Storybook's preview-hooks context, so+// `useDarkMode()` (a preview hook) can't be used here. Track the `<html>.dark`+// class the story decorator sets via a MutationObserver instead.+function ThemedDocsContainer(props: React.ComponentProps<typeof DocsContainer>): React.ReactElement {+ const [isDark, setIsDark] = useState<boolean>(() => document.documentElement.classList.contains('dark'))useEffect(() => {- document.documentElement.classList.toggle('dark', isDark)- if (isDark) {- document.documentElement.setAttribute('theme', 'dark')- } else {- document.documentElement.removeAttribute('theme')- }- }, [isDark])-- return <>{children}</>-}+ const root = document.documentElement+ const observer = new MutationObserver(() => setIsDark(root.classList.contains('dark')))+ observer.observe(root, { attributes: true, attributeFilter: ['class'] })+ return () => observer.disconnect()+ }, [])-function ThemedDocsContainer(props: React.ComponentProps<typeof DocsContainer>): React.ReactElement {- const isDark = useDarkMode()return <DocsContainer {...props} theme={isDark ? themes.dark : themes.light} />}@@ -62,13 +50,28 @@ const preview: Preview = {},},decorators: [- (Story) => (- <ThemeSync>+ // Hooks must run at the decorator's top level — Storybook's preview+ // hooks context (which `useDarkMode` relies on) is only active here,+ // not inside nested components a decorator renders. Syncs the signal to+ // `<html>` class/attribute so quill's `--background` etc. resolve to dark.+ (Story) => {+ const isDark = useDarkMode()++ useEffect(() => {+ document.documentElement.classList.toggle('dark', isDark)+ if (isDark) {+ document.documentElement.setAttribute('theme', 'dark')+ } else {+ document.documentElement.removeAttribute('theme')+ }+ }, [isDark])++ return (<div className="bg-background text-foreground" style={{ padding: '1rem' }}><Story /></div>- </ThemeSync>- ),+ )+ },],}