: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
import { MakeLogicType, actions, connect, events, kea, listeners, path, reducers, selectors } from 'kea'
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { sceneLogic } from 'scenes/sceneLogic'
import { userLogic } from 'scenes/userLogic'
import type { UserTheme, UserType } from '../../types'
import type { FeatureFlagsSet } from './featureFlagLogic'
import { Theme, themes } from './themes'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface themeLogicValues {
featureFlags: FeatureFlagsSet // featureFlagLogic
themeMode: UserTheme // userLogic
customCss: string | null
customCssEnabled: boolean
darkModeSystemPreference: boolean
isDarkModeOn: boolean
persistedCustomCss: string | null
previewingCustomCss: string | null
selectedTheme: string | null
theme: Theme | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface themeLogicActions {
updateUser: (
user: Partial<UserType>,
successCallback?: (() => void) | undefined,
failureCallback?: (() => void) | undefined
) => {
failureCallback: (() => void) | undefined
successCallback: (() => void) | undefined
user: Partial<UserType>
} // userLogic
saveCustomCss: () => {
value: true
}
setPersistedCustomCss: (css: string | null) => {
css: string | null
}
setPreviewingCustomCss: (css: string | null) => {
css: string | null
}
setTheme: (theme: string | null) => {
theme: string | null
}
syncDarkModePreference: (darkModePreference: boolean) => {
darkModePreference: boolean
}
toggleTheme: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface themeLogicMeta {
__keaTypeGenInternalSelectorTypes: {
theme: (selectedTheme: string | null, featureFlags: FeatureFlagsSet) => Theme | null
customCssEnabled: (featureFlags: FeatureFlagsSet) => boolean
customCss: (persistedCustomCss: string | null, previewingCustomCss: string | null) => string | null
isDarkModeOn: (
themeMode: UserTheme,
darkModeSystemPreference: boolean,
sceneConfig: null | import('../../scenes/sceneTypes').SceneConfig,
theme: Theme | null
) => boolean
}
}
export type themeLogicType = MakeLogicType<themeLogicValues, themeLogicActions, Record<string, any>, themeLogicMeta>
export const themeLogic = kea<themeLogicType>([
path(['layout', 'navigation-3000', 'themeLogic']),
connect(() => ({
logic: [sceneLogic],
values: [userLogic, ['themeMode'], featureFlagLogic, ['featureFlags']],
actions: [userLogic, ['updateUser']],
})),
actions({
syncDarkModePreference: (darkModePreference: boolean) => ({ darkModePreference }),
setTheme: (theme: string | null) => ({ theme }),
saveCustomCss: true,
setPersistedCustomCss: (css: string | null) => ({ css }),
setPreviewingCustomCss: (css: string | null) => ({ css }),
toggleTheme: true,
}),
reducers({
darkModeSystemPreference: [
window.matchMedia('(prefers-color-scheme: dark)').matches,
{
syncDarkModePreference: (_, { darkModePreference }) => darkModePreference,
},
],
selectedTheme: [
null as string | null,
{ persist: true },
{
setTheme: (_, { theme }) => theme,
},
],
persistedCustomCss: [
null as string | null,
{ persist: true },
{
setPersistedCustomCss: (_, { css }) => css,
},
],
previewingCustomCss: [
null as string | null,
{ persist: true },
{
setPreviewingCustomCss: (_, { css }) => css,
},
],
}),
selectors({
theme: [
(s) => [s.selectedTheme, s.featureFlags],
(
selectedTheme: string | null,
featureFlags: import('lib/logic/featureFlagLogic').FeatureFlagsSet
): Theme | null => {
const flagVariant = featureFlags[FEATURE_FLAGS.THEME_OVERRIDE]
return (
(selectedTheme ? themes.find((theme) => theme.id === selectedTheme) : null) ??
(typeof flagVariant === 'string' ? themes.find((theme) => theme.id === flagVariant) : null) ??
null
)
},
],
customCssEnabled: [
(s) => [s.featureFlags],
(featureFlags: import('lib/logic/featureFlagLogic').FeatureFlagsSet): boolean =>
!!featureFlags[FEATURE_FLAGS.CUSTOM_CSS_THEMES],
],
customCss: [
(s) => [s.persistedCustomCss, s.previewingCustomCss],
(persistedCustomCss: string | null, previewingCustomCss: string | null): string | null =>
previewingCustomCss || persistedCustomCss,
],
isDarkModeOn: [
(s) => [s.themeMode, s.darkModeSystemPreference, sceneLogic.selectors.sceneConfig, s.theme],
(
themeMode: import('../../types').UserTheme,
darkModeSystemPreference: boolean,
sceneConfig: null | import('../../scenes/sceneTypes').SceneConfig,
theme: Theme | null
) => {
if (
typeof window !== 'undefined' &&
window.document &&
document.body.classList.contains('storybook-test-runner') &&
document.body.getAttribute('theme') == 'dark'
) {
return true
}
if (theme) {
return !!theme?.dark
}
// NOTE: Unauthenticated users always get the light mode until we have full support for dark mode there
if (sceneConfig?.allowUnauthenticated || sceneConfig?.onlyUnauthenticated) {
return false
}
return themeMode === 'system' ? darkModeSystemPreference : themeMode === 'dark'
},
],
}),
listeners(({ values, actions }) => ({
saveCustomCss() {
actions.setPersistedCustomCss(values.previewingCustomCss)
actions.setPreviewingCustomCss(null)
},
toggleTheme() {
actions.updateUser({ theme_mode: values.isDarkModeOn ? 'light' : 'dark' })
},
})),
events(({ cache, actions }) => ({
afterMount() {
cache.disposables.add(() => {
const prefersColorSchemeMedia = window.matchMedia('(prefers-color-scheme: dark)')
// This setup re-runs whenever the tab becomes visible again (disposables pause on hidden
// tabs, detaching the listener), so re-read the preference to catch a system theme change
// that happened while the tab was hidden and fired no observable `change` event
actions.syncDarkModePreference(prefersColorSchemeMedia.matches)
const onPrefersColorSchemeChange = (e: MediaQueryListEvent): void =>
actions.syncDarkModePreference(e.matches)
prefersColorSchemeMedia.addEventListener('change', onPrefersColorSchemeChange)
return () => prefersColorSchemeMedia.removeEventListener('change', onPrefersColorSchemeChange)
}, 'prefersColorSchemeListener')
},
})),
])