Skip to content

: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.

801 B
Raw Blame
import { useEffect, useState } from 'react'

/** Whether the page shows the dark theme, read from `body[theme]`, the attribute the surrounding CSS
 *  follows. `themeLogic.isDarkModeOn` can lag behind it, which left the editor light on a dark page. */
export function useBodyIsDark(): boolean {
    const [isDark, setIsDark] = useState(() => document.body.getAttribute('theme') === 'dark')
    useEffect(() => {
        const sync = (): void => setIsDark(document.body.getAttribute('theme') === 'dark')
        // The attribute may already have changed between the first render and here.
        sync()
        const observer = new MutationObserver(sync)
        observer.observe(document.body, { attributeFilter: ['theme'] })
        return () => observer.disconnect()
    }, [])
    return isDark
}