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.

2.3 KB
Raw Blame
import { useValues } from 'kea'
import posthog from 'posthog-js'
import { useEffect } from 'react'

import { themeLogic } from 'lib/logic/themeLogic'
import { sceneLogic } from 'scenes/sceneLogic'

export function useThemedHtml(overflowHidden = true, forcedTheme: 'light' | 'dark' | null = null): void {
    const { isDarkModeOn, customCss } = useValues(themeLogic)
    const { sceneConfig } = useValues(sceneLogic)
    const isDarkTheme = forcedTheme ? forcedTheme === 'dark' : isDarkModeOn

    const CUSTOM_THEME_STYLES_ID = 'ph-custom-theme-styles'

    useEffect(() => {
        const oldStyle = document.getElementById(CUSTOM_THEME_STYLES_ID)
        if (oldStyle) {
            document.head.removeChild(oldStyle)
        }

        document.body.setAttribute('theme', isDarkTheme ? 'dark' : 'light')

        if (customCss) {
            const newStyle = document.createElement('style')
            newStyle.id = CUSTOM_THEME_STYLES_ID
            newStyle.appendChild(document.createTextNode(customCss))
            document.head.appendChild(newStyle)
        }
    }, [customCss, isDarkTheme])

    useEffect(() => {
        // overflow-hidden since each area handles scrolling individually (e.g. navbar, scene, side panel)
        if (overflowHidden) {
            document.body.classList.add('overflow-hidden')
        }
    }, [overflowHidden])

    useEffect(() => {
        // Add a theme-color meta tag to the head to change the address bar color on browsers that support it
        try {
            const root = document.documentElement
            const style = getComputedStyle(root)
            const backgroundColor = sceneConfig?.projectBased
                ? style.getPropertyValue('--surface-secondary')
                : style.getPropertyValue('--color-bg-bridge')

            document.head.querySelector('meta[name="theme-color"]')?.remove()
            const meta = document.createElement('meta')
            meta.name = 'theme-color'
            meta.content = backgroundColor
            document.head.appendChild(meta)
        } catch (e) {
            console.warn('Failed to set theme-color meta tag. This could indicate the variables no longer exist', e)
            posthog.captureException(new Error('Failed to set theme-color meta tag'), { extra: { error: e } })
        }
    }, [isDarkTheme, sceneConfig?.projectBased])
}