@tamagui/web 1.14.8 → 1.14.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/hooks/useTheme.js +8 -12
- package/dist/cjs/hooks/useTheme.js.map +2 -2
- package/dist/esm/hooks/useTheme.js +8 -12
- package/dist/esm/hooks/useTheme.js.map +2 -2
- package/dist/esm/hooks/useTheme.mjs +8 -12
- package/dist/esm/hooks/useTheme.mjs.map +2 -2
- package/package.json +9 -9
- package/src/hooks/useTheme.tsx +8 -11
- package/types/hooks/useTheme.d.ts.map +1 -1
|
@@ -173,7 +173,6 @@ const useChangeThemeEffect = (props, root = false, keys, disableUpdate) => {
|
|
|
173
173
|
};
|
|
174
174
|
}, [isNewTheme, themeManager, state, debug]);
|
|
175
175
|
(0, import_react.useLayoutEffect)(() => {
|
|
176
|
-
var _a;
|
|
177
176
|
const nextState = getShouldUpdateTheme(themeManager);
|
|
178
177
|
if (nextState) {
|
|
179
178
|
if (isNewTheme) {
|
|
@@ -185,18 +184,15 @@ const useChangeThemeEffect = (props, root = false, keys, disableUpdate) => {
|
|
|
185
184
|
setThemeState(createState);
|
|
186
185
|
}
|
|
187
186
|
}
|
|
188
|
-
const disposeChangeListener =
|
|
189
|
-
(
|
|
190
|
-
|
|
191
|
-
if (
|
|
192
|
-
|
|
193
|
-
console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys });
|
|
194
|
-
}
|
|
195
|
-
setThemeState(createState);
|
|
187
|
+
const disposeChangeListener = parentManager == null ? void 0 : parentManager.onChangeTheme((name, manager) => {
|
|
188
|
+
const shouldUpdate = Boolean((keys == null ? void 0 : keys.length) || isNewTheme);
|
|
189
|
+
if (shouldUpdate) {
|
|
190
|
+
if (process.env.NODE_ENV === "development" && props["debug"]) {
|
|
191
|
+
console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys });
|
|
196
192
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
);
|
|
193
|
+
setThemeState(createState);
|
|
194
|
+
}
|
|
195
|
+
}, themeManager.id);
|
|
200
196
|
return () => {
|
|
201
197
|
disposeChangeListener == null ? void 0 : disposeChangeListener();
|
|
202
198
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/hooks/useTheme.tsx"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable no-console */\nimport { isClient, isRSC, isServer, isWeb } from '@tamagui/constants'\nimport { useContext, useEffect, useLayoutEffect, useMemo, useState } from 'react'\n\nimport { getConfig } from '../config.js'\nimport { isDevTools } from '../constants/isDevTools.js'\nimport { isVariable } from '../createVariable.js'\nimport { createProxy } from '../helpers/createProxy.js'\nimport {\n ThemeManager,\n ThemeManagerState,\n getNonComponentParentManager,\n hasNoThemeUpdatingProps,\n} from '../helpers/ThemeManager.js'\nimport { ThemeManagerContext } from '../helpers/ThemeManagerContext.js'\nimport type { ThemeParsed, ThemeProps } from '../types.js'\nimport { GetThemeUnwrapped } from './getThemeUnwrapped.js'\nimport { useServerRef } from './useServerHooks.js'\n\nexport type ChangedThemeResponse = {\n isNewTheme: boolean\n themeManager: ThemeManager | null\n name: string\n theme?: ThemeParsed | null\n className?: string\n}\n\ntype State = {\n state: ThemeManagerState\n themeManager: ThemeManager\n isNewTheme: boolean\n mounted?: boolean\n}\n\nconst emptyProps = { name: null }\n\nfunction getDefaultThemeProxied() {\n const config = getConfig()\n const name = Object.keys(config.themes)[0]\n return getThemeProxied({\n theme: config.themes[name],\n name,\n })\n}\n\nexport const useTheme = (props: ThemeProps = emptyProps): ThemeParsed => {\n return (isRSC ? null : useThemeWithState(props)?.theme) || getDefaultThemeProxied()\n}\n\nexport const useThemeWithState = (props: ThemeProps) => {\n const keys = useServerRef<string[]>([])\n\n const changedTheme = useChangeThemeEffect(\n props,\n false,\n keys.current,\n isClient\n ? () => {\n return props.shouldUpdate?.() ?? keys.current.length === 0\n }\n : undefined\n )\n\n const { themeManager, theme, name, className } = changedTheme\n\n if (process.env.NODE_ENV === 'development' && props.debug === 'verbose') {\n console.groupCollapsed(' \uD83D\uDD39 useTheme =>', name)\n const logs = { ...props, name, className, ...(isDevTools && { theme }) }\n for (const key in logs) {\n console.log(' ', key, logs[key])\n }\n console.groupEnd()\n }\n\n if (!changedTheme.theme) {\n if (process.env.NODE_ENV === 'development') {\n console.warn('No theme found', name, props, themeManager)\n }\n return null\n }\n\n const proxiedTheme = useMemo(() => {\n return getThemeProxied(changedTheme as any, keys.current)\n }, [theme, name, className, themeManager])\n\n return {\n ...changedTheme,\n theme: proxiedTheme,\n }\n}\n\nexport function getThemeProxied(\n {\n theme,\n themeManager,\n }: Partial<ChangedThemeResponse> & {\n theme: ThemeParsed\n },\n keys?: string[]\n) {\n return createProxy(theme, {\n has(_, key) {\n if (typeof key === 'string') {\n if (key[0] === '$') key = key.slice(1)\n return themeManager?.allKeys.has(key)\n }\n return Reflect.has(theme, key)\n },\n get(_, key) {\n if (key === GetThemeUnwrapped) {\n return theme\n }\n if (\n key === '__proto__' ||\n key === '$typeof' ||\n typeof key !== 'string' ||\n !themeManager\n ) {\n return Reflect.get(_, key)\n }\n\n // auto convert variables to plain\n const keyString = key[0] === '$' ? key.slice(1) : key\n const val = themeManager.getValue(keyString)\n\n if (val && keys) {\n return new Proxy(val as any, {\n // when they touch the actual value we only track it\n // if its a variable (web), its ignored!\n get(_, subkey) {\n if (subkey === 'val' && !keys.includes(keyString)) {\n keys.push(keyString)\n }\n return Reflect.get(val as any, subkey)\n },\n })\n }\n\n return val\n },\n })\n}\n\nexport const activeThemeManagers = new Set<ThemeManager>()\n\nexport const useChangeThemeEffect = (\n props: ThemeProps,\n root = false,\n keys?: string[],\n disableUpdate?: () => boolean\n): ChangedThemeResponse => {\n if (isRSC) {\n // we need context working for this to work well\n return {\n isNewTheme: false,\n ...createState().state,\n themeManager: null,\n }\n }\n\n const parentManager = getNonComponentParentManager(useContext(ThemeManagerContext))\n\n const {\n debug,\n // @ts-expect-error internal use only\n disable,\n } = props\n\n if (disable) {\n if (!parentManager) throw `\u274C`\n return {\n isNewTheme: false,\n ...parentManager.state,\n themeManager: parentManager,\n }\n }\n\n const [themeState, setThemeState] = useState<State>(createState)\n const { state, themeManager, mounted, isNewTheme } = themeState\n const isInversingOnMount = Boolean(!themeState.mounted && props.inverse)\n const shouldReturnParentState = isInversingOnMount\n\n function getShouldUpdateTheme(\n manager = themeManager,\n nextState?: ThemeManagerState | null,\n forceShouldChange = false\n ) {\n const next = nextState || manager.getState(props, parentManager)\n if (!next) return\n if (disableUpdate?.() === true) return\n if (!forceShouldChange && !manager.getStateShouldChange(next, state)) return\n return next\n }\n\n if (!isServer) {\n useEffect(() => {\n // SSR safe inverse (because server can't know prefers scheme)\n // could be done through fancy selectors like how we do prefers-media\n // but may be a bit of explosion of selectors\n if (props.inverse && !mounted) {\n setThemeState({ ...themeState, mounted: true })\n return\n }\n\n if (!isNewTheme) return\n if (!themeManager) return\n\n activeThemeManagers.add(themeManager)\n\n return () => {\n activeThemeManagers.delete(themeManager)\n }\n }, [isNewTheme, themeManager, state, debug])\n\n // listen for parent change + notify children change\n useLayoutEffect(() => {\n const nextState = getShouldUpdateTheme(themeManager)\n\n if (nextState) {\n if (isNewTheme) {\n // if it's a new theme we can just update + publish to children\n themeManager.updateState(nextState, true)\n }\n\n // if not we will be creating a whole new themeManager\n setThemeState(createState)\n } else {\n if (isNewTheme) {\n // need to revert to parent\n setThemeState(createState)\n }\n }\n\n const disposeChangeListener = themeManager.parentManager?.onChangeTheme(\n (name, manager) => {\n const shouldUpdate = Boolean(keys?.length || isNewTheme)\n if (shouldUpdate) {\n if (process.env.NODE_ENV === 'development' && props['debug']) {\n console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys })\n }\n setThemeState(createState)\n }\n },\n themeManager.id\n )\n\n return () => {\n disposeChangeListener?.()\n }\n }, [\n parentManager,\n isNewTheme,\n props.componentName,\n props.inverse,\n props.name,\n props.reset,\n ])\n\n if (process.env.NODE_ENV === 'development') {\n useEffect(() => {\n globalThis['TamaguiThemeManagers'] ??= new Set()\n globalThis['TamaguiThemeManagers'].add(themeManager)\n return () => {\n globalThis['TamaguiThemeManagers'].delete(themeManager)\n }\n }, [themeManager])\n }\n }\n\n if (shouldReturnParentState) {\n if (!parentManager) throw 'impossible'\n return {\n isNewTheme: false,\n ...parentManager.state,\n className: isInversingOnMount ? '' : parentManager.state.className,\n themeManager: parentManager,\n }\n }\n\n return {\n ...state,\n isNewTheme,\n themeManager,\n }\n\n function createState(prev?: State): State {\n if (prev && disableUpdate?.()) {\n return prev\n }\n\n // returns previous theme manager if no change\n let themeManager: ThemeManager\n let state: ThemeManagerState | undefined\n\n const getNewThemeManager = () => {\n return new ThemeManager(props, root ? 'root' : parentManager)\n }\n\n if (prev?.themeManager) {\n themeManager = prev.themeManager\n\n // this could be a bit better, problem is on toggling light/dark the state is actually\n // showing light even when the last was dark. but technically allso onChangeTheme should\n // basically always call on a change, so i'm wondering if we even need the shouldUpdate\n // at all anymore. this forces updates onChangeTheme for all dynamic style accessed components\n // which is correct, potentially in the future we can avoid forceChange and just know to\n // update if keys.length is set + onChangeTheme called\n const forceChange = Boolean(keys?.length)\n\n const next = themeManager.getState(props, parentManager)\n const nextState = getShouldUpdateTheme(themeManager, next, forceChange)\n\n if (nextState) {\n state = nextState\n\n if (!prev.isNewTheme || !isWeb) {\n themeManager = getNewThemeManager()\n } else {\n themeManager.updateState(nextState, true)\n }\n } else {\n if (prev.isNewTheme) {\n // reset to parent\n if (parentManager && !next) {\n themeManager = parentManager\n }\n }\n }\n } else {\n themeManager = getNewThemeManager()\n }\n\n const isNewTheme = Boolean(themeManager !== parentManager)\n\n // only inverse relies on this for ssr\n const mounted = !props.inverse ? true : root || prev?.mounted\n\n if (!state) {\n state = isNewTheme ? { ...themeManager.state } : { ...parentManager!.state }\n }\n\n const response = {\n state,\n themeManager,\n isNewTheme,\n mounted,\n }\n\n if (process.env.NODE_ENV === 'development' && props['debug']) {\n console.groupCollapsed(` \uD83D\uDD37 ${themeManager.id} useChangeThemeEffect createState`)\n const parentState = { ...parentManager?.state }\n const parentId = parentManager?.id\n const themeManagerState = { ...themeManager.state }\n console.log({\n props,\n parentState,\n parentId,\n themeManager,\n prev,\n response,\n themeManagerState,\n })\n console.groupEnd()\n }\n\n return response\n }\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,uBAAiD;AACjD,mBAA0E;AAE1E,oBAA0B;AAC1B,wBAA2B;AAE3B,yBAA4B;AAC5B,0BAKO;AACP,iCAAoC;AAEpC,+BAAkC;AAClC,4BAA6B;AAiB7B,MAAM,aAAa,EAAE,MAAM,KAAK;AAEhC,SAAS,yBAAyB;AAChC,QAAM,aAAS,yBAAU;AACzB,QAAM,OAAO,OAAO,KAAK,OAAO,MAAM,EAAE,CAAC;AACzC,SAAO,gBAAgB;AAAA,IACrB,OAAO,OAAO,OAAO,IAAI;AAAA,IACzB;AAAA,EACF,CAAC;AACH;AAEO,MAAM,WAAW,CAAC,QAAoB,eAA4B;AA7CzE;AA8CE,UAAQ,yBAAQ,QAAO,uBAAkB,KAAK,MAAvB,mBAA0B,UAAU,uBAAuB;AACpF;AAEO,MAAM,oBAAoB,CAAC,UAAsB;AACtD,QAAM,WAAO,oCAAuB,CAAC,CAAC;AAEtC,QAAM,eAAe;AAAA,IACnB;AAAA,IACA;AAAA,IACA,KAAK;AAAA,IACL,4BACI,MAAM;AAzDd;AA0DU,eAAO,WAAM,iBAAN,mCAA0B,KAAK,QAAQ,WAAW;AAAA,IAC3D,IACA;AAAA,EACN;AAEA,QAAM,EAAE,cAAc,OAAO,MAAM,UAAU,IAAI;AAEjD,MAAI,QAAQ,IAAI,aAAa,iBAAiB,MAAM,UAAU,WAAW;AACvE,YAAQ,eAAe,2BAAoB,IAAI;AAC/C,UAAM,OAAO,EAAE,GAAG,OAA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AC;AAAc;AAEnB,0BAAoB,IAAI,YAAY;AAEpC,aAAO,MAAM;AACX,4BAAoB,OAAO,YAAY;AAAA,MACzC;AAAA,IACF,GAAG,CAAC,YAAY,cAAc,OAAO,KAAK,CAAC;AAG3C,sCAAgB,MAAM;
|
|
4
|
+
"sourcesContent": ["/* eslint-disable no-console */\nimport { isClient, isRSC, isServer, isWeb } from '@tamagui/constants'\nimport { useContext, useEffect, useLayoutEffect, useMemo, useState } from 'react'\n\nimport { getConfig } from '../config.js'\nimport { isDevTools } from '../constants/isDevTools.js'\nimport { isVariable } from '../createVariable.js'\nimport { createProxy } from '../helpers/createProxy.js'\nimport {\n ThemeManager,\n ThemeManagerState,\n getNonComponentParentManager,\n hasNoThemeUpdatingProps,\n} from '../helpers/ThemeManager.js'\nimport { ThemeManagerContext } from '../helpers/ThemeManagerContext.js'\nimport type { ThemeParsed, ThemeProps } from '../types.js'\nimport { GetThemeUnwrapped } from './getThemeUnwrapped.js'\nimport { useServerRef } from './useServerHooks.js'\n\nexport type ChangedThemeResponse = {\n isNewTheme: boolean\n themeManager: ThemeManager | null\n name: string\n theme?: ThemeParsed | null\n className?: string\n}\n\ntype State = {\n state: ThemeManagerState\n themeManager: ThemeManager\n isNewTheme: boolean\n mounted?: boolean\n}\n\nconst emptyProps = { name: null }\n\nfunction getDefaultThemeProxied() {\n const config = getConfig()\n const name = Object.keys(config.themes)[0]\n return getThemeProxied({\n theme: config.themes[name],\n name,\n })\n}\n\nexport const useTheme = (props: ThemeProps = emptyProps): ThemeParsed => {\n return (isRSC ? null : useThemeWithState(props)?.theme) || getDefaultThemeProxied()\n}\n\nexport const useThemeWithState = (props: ThemeProps) => {\n const keys = useServerRef<string[]>([])\n\n const changedTheme = useChangeThemeEffect(\n props,\n false,\n keys.current,\n isClient\n ? () => {\n return props.shouldUpdate?.() ?? keys.current.length === 0\n }\n : undefined\n )\n\n const { themeManager, theme, name, className } = changedTheme\n\n if (process.env.NODE_ENV === 'development' && props.debug === 'verbose') {\n console.groupCollapsed(' \uD83D\uDD39 useTheme =>', name)\n const logs = { ...props, name, className, ...(isDevTools && { theme }) }\n for (const key in logs) {\n console.log(' ', key, logs[key])\n }\n console.groupEnd()\n }\n\n if (!changedTheme.theme) {\n if (process.env.NODE_ENV === 'development') {\n console.warn('No theme found', name, props, themeManager)\n }\n return null\n }\n\n const proxiedTheme = useMemo(() => {\n return getThemeProxied(changedTheme as any, keys.current)\n }, [theme, name, className, themeManager])\n\n return {\n ...changedTheme,\n theme: proxiedTheme,\n }\n}\n\nexport function getThemeProxied(\n {\n theme,\n themeManager,\n }: Partial<ChangedThemeResponse> & {\n theme: ThemeParsed\n },\n keys?: string[]\n) {\n return createProxy(theme, {\n has(_, key) {\n if (typeof key === 'string') {\n if (key[0] === '$') key = key.slice(1)\n return themeManager?.allKeys.has(key)\n }\n return Reflect.has(theme, key)\n },\n get(_, key) {\n if (key === GetThemeUnwrapped) {\n return theme\n }\n if (\n key === '__proto__' ||\n key === '$typeof' ||\n typeof key !== 'string' ||\n !themeManager\n ) {\n return Reflect.get(_, key)\n }\n\n // auto convert variables to plain\n const keyString = key[0] === '$' ? key.slice(1) : key\n const val = themeManager.getValue(keyString)\n\n if (val && keys) {\n return new Proxy(val as any, {\n // when they touch the actual value we only track it\n // if its a variable (web), its ignored!\n get(_, subkey) {\n if (subkey === 'val' && !keys.includes(keyString)) {\n keys.push(keyString)\n }\n return Reflect.get(val as any, subkey)\n },\n })\n }\n\n return val\n },\n })\n}\n\nexport const activeThemeManagers = new Set<ThemeManager>()\n\nexport const useChangeThemeEffect = (\n props: ThemeProps,\n root = false,\n keys?: string[],\n disableUpdate?: () => boolean\n): ChangedThemeResponse => {\n if (isRSC) {\n // we need context working for this to work well\n return {\n isNewTheme: false,\n ...createState().state,\n themeManager: null,\n }\n }\n\n const parentManager = getNonComponentParentManager(useContext(ThemeManagerContext))\n\n const {\n debug,\n // @ts-expect-error internal use only\n disable,\n } = props\n\n if (disable) {\n if (!parentManager) throw `\u274C`\n return {\n isNewTheme: false,\n ...parentManager.state,\n themeManager: parentManager,\n }\n }\n\n const [themeState, setThemeState] = useState<State>(createState)\n const { state, themeManager, mounted, isNewTheme } = themeState\n const isInversingOnMount = Boolean(!themeState.mounted && props.inverse)\n const shouldReturnParentState = isInversingOnMount\n\n function getShouldUpdateTheme(\n manager = themeManager,\n nextState?: ThemeManagerState | null,\n forceShouldChange = false\n ) {\n const next = nextState || manager.getState(props, parentManager)\n if (!next) return\n if (disableUpdate?.() === true) return\n if (!forceShouldChange && !manager.getStateShouldChange(next, state)) return\n return next\n }\n\n if (!isServer) {\n useEffect(() => {\n // SSR safe inverse (because server can't know prefers scheme)\n // could be done through fancy selectors like how we do prefers-media\n // but may be a bit of explosion of selectors\n if (props.inverse && !mounted) {\n setThemeState({ ...themeState, mounted: true })\n return\n }\n\n if (!isNewTheme) return\n if (!themeManager) return\n\n activeThemeManagers.add(themeManager)\n\n return () => {\n activeThemeManagers.delete(themeManager)\n }\n }, [isNewTheme, themeManager, state, debug])\n\n // listen for parent change + notify children change\n useLayoutEffect(() => {\n const nextState = getShouldUpdateTheme(themeManager)\n\n if (nextState) {\n if (isNewTheme) {\n // if it's a new theme we can just update + publish to children\n themeManager.updateState(nextState, true)\n }\n\n // if not we will be creating a whole new themeManager\n setThemeState(createState)\n } else {\n if (isNewTheme) {\n // need to revert to parent\n setThemeState(createState)\n }\n }\n\n const disposeChangeListener = parentManager?.onChangeTheme((name, manager) => {\n const shouldUpdate = Boolean(keys?.length || isNewTheme)\n if (shouldUpdate) {\n if (process.env.NODE_ENV === 'development' && props['debug']) {\n console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys })\n }\n setThemeState(createState)\n }\n }, themeManager.id)\n\n return () => {\n disposeChangeListener?.()\n }\n }, [\n parentManager,\n isNewTheme,\n props.componentName,\n props.inverse,\n props.name,\n props.reset,\n ])\n\n if (process.env.NODE_ENV === 'development') {\n useEffect(() => {\n globalThis['TamaguiThemeManagers'] ??= new Set()\n globalThis['TamaguiThemeManagers'].add(themeManager)\n return () => {\n globalThis['TamaguiThemeManagers'].delete(themeManager)\n }\n }, [themeManager])\n }\n }\n\n if (shouldReturnParentState) {\n if (!parentManager) throw 'impossible'\n return {\n isNewTheme: false,\n ...parentManager.state,\n className: isInversingOnMount ? '' : parentManager.state.className,\n themeManager: parentManager,\n }\n }\n\n return {\n ...state,\n isNewTheme,\n themeManager,\n }\n\n function createState(prev?: State): State {\n if (prev && disableUpdate?.()) {\n return prev\n }\n\n // returns previous theme manager if no change\n let themeManager: ThemeManager\n let state: ThemeManagerState | undefined\n\n const getNewThemeManager = () => {\n return new ThemeManager(props, root ? 'root' : parentManager)\n }\n\n if (prev?.themeManager) {\n themeManager = prev.themeManager\n\n // this could be a bit better, problem is on toggling light/dark the state is actually\n // showing light even when the last was dark. but technically allso onChangeTheme should\n // basically always call on a change, so i'm wondering if we even need the shouldUpdate\n // at all anymore. this forces updates onChangeTheme for all dynamic style accessed components\n // which is correct, potentially in the future we can avoid forceChange and just know to\n // update if keys.length is set + onChangeTheme called\n const forceChange = Boolean(keys?.length)\n\n const next = themeManager.getState(props, parentManager)\n const nextState = getShouldUpdateTheme(themeManager, next, forceChange)\n\n if (nextState) {\n state = nextState\n\n if (!prev.isNewTheme || !isWeb) {\n themeManager = getNewThemeManager()\n } else {\n themeManager.updateState(nextState, true)\n }\n } else {\n if (prev.isNewTheme) {\n // reset to parent\n if (parentManager && !next) {\n themeManager = parentManager\n }\n }\n }\n } else {\n themeManager = getNewThemeManager()\n }\n\n const isNewTheme = Boolean(themeManager !== parentManager)\n\n // only inverse relies on this for ssr\n const mounted = !props.inverse ? true : root || prev?.mounted\n\n if (!state) {\n state = isNewTheme ? { ...themeManager.state } : { ...parentManager!.state }\n }\n\n const response = {\n state,\n themeManager,\n isNewTheme,\n mounted,\n }\n\n if (process.env.NODE_ENV === 'development' && props['debug']) {\n console.groupCollapsed(` \uD83D\uDD37 ${themeManager.id} useChangeThemeEffect createState`)\n const parentState = { ...parentManager?.state }\n const parentId = parentManager?.id\n const themeManagerState = { ...themeManager.state }\n console.log({\n props,\n parentState,\n parentId,\n themeManager,\n prev,\n response,\n themeManagerState,\n })\n console.groupEnd()\n }\n\n return response\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,uBAAiD;AACjD,mBAA0E;AAE1E,oBAA0B;AAC1B,wBAA2B;AAE3B,yBAA4B;AAC5B,0BAKO;AACP,iCAAoC;AAEpC,+BAAkC;AAClC,4BAA6B;AAiB7B,MAAM,aAAa,EAAE,MAAM,KAAK;AAEhC,SAAS,yBAAyB;AAChC,QAAM,aAAS,yBAAU;AACzB,QAAM,OAAO,OAAO,KAAK,OAAO,MAAM,EAAE,CAAC;AACzC,SAAO,gBAAgB;AAAA,IACrB,OAAO,OAAO,OAAO,IAAI;AAAA,IACzB;AAAA,EACF,CAAC;AACH;AAEO,MAAM,WAAW,CAAC,QAAoB,eAA4B;AA7CzE;AA8CE,UAAQ,yBAAQ,QAAO,uBAAkB,KAAK,MAAvB,mBAA0B,UAAU,uBAAuB;AACpF;AAEO,MAAM,oBAAoB,CAAC,UAAsB;AACtD,QAAM,WAAO,oCAAuB,CAAC,CAAC;AAEtC,QAAM,eAAe;AAAA,IACnB;AAAA,IACA;AAAA,IACA,KAAK;AAAA,IACL,4BACI,MAAM;AAzDd;AA0DU,eAAO,WAAM,iBAAN,mCAA0B,KAAK,QAAQ,WAAW;AAAA,IAC3D,IACA;AAAA,EACN;AAEA,QAAM,EAAE,cAAc,OAAO,MAAM,UAAU,IAAI;AAEjD,MAAI,QAAQ,IAAI,aAAa,iBAAiB,MAAM,UAAU,WAAW;AACvE,YAAQ,eAAe,2BAAoB,IAAI;AAC/C,UAAM,OAAO,EAAE,GAAG,OAAO,MAAM,WAAW,GAAI,gCAAc,EAAE,MAAM,EAAG;AACvE,eAAW,OAAO,MAAM;AACtB,cAAQ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Ae,MAAM;AAC9C,YAAM,WAAW,+CAAe;AAChC,YAAM,oBAAoB,EAAE,GAAGA,cAAa,MAAM;AAClD,cAAQ,IAAI;AAAA,QACV;AAAA,QACA;AAAA,QACA;AAAA,QACA,cAAAA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC;AACD,cAAQ,SAAS;AAAA,IACnB;AAEA,WAAO;AAAA,EACT;AACF;",
|
|
6
6
|
"names": ["_", "themeManager", "state", "isNewTheme", "mounted"]
|
|
7
7
|
}
|
|
@@ -149,7 +149,6 @@ const useChangeThemeEffect = (props, root = false, keys, disableUpdate) => {
|
|
|
149
149
|
};
|
|
150
150
|
}, [isNewTheme, themeManager, state, debug]);
|
|
151
151
|
useLayoutEffect(() => {
|
|
152
|
-
var _a;
|
|
153
152
|
const nextState = getShouldUpdateTheme(themeManager);
|
|
154
153
|
if (nextState) {
|
|
155
154
|
if (isNewTheme) {
|
|
@@ -161,18 +160,15 @@ const useChangeThemeEffect = (props, root = false, keys, disableUpdate) => {
|
|
|
161
160
|
setThemeState(createState);
|
|
162
161
|
}
|
|
163
162
|
}
|
|
164
|
-
const disposeChangeListener =
|
|
165
|
-
(
|
|
166
|
-
|
|
167
|
-
if (
|
|
168
|
-
|
|
169
|
-
console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys });
|
|
170
|
-
}
|
|
171
|
-
setThemeState(createState);
|
|
163
|
+
const disposeChangeListener = parentManager == null ? void 0 : parentManager.onChangeTheme((name, manager) => {
|
|
164
|
+
const shouldUpdate = Boolean((keys == null ? void 0 : keys.length) || isNewTheme);
|
|
165
|
+
if (shouldUpdate) {
|
|
166
|
+
if (process.env.NODE_ENV === "development" && props["debug"]) {
|
|
167
|
+
console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys });
|
|
172
168
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
);
|
|
169
|
+
setThemeState(createState);
|
|
170
|
+
}
|
|
171
|
+
}, themeManager.id);
|
|
176
172
|
return () => {
|
|
177
173
|
disposeChangeListener == null ? void 0 : disposeChangeListener();
|
|
178
174
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/hooks/useTheme.tsx"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable no-console */\nimport { isClient, isRSC, isServer, isWeb } from '@tamagui/constants'\nimport { useContext, useEffect, useLayoutEffect, useMemo, useState } from 'react'\n\nimport { getConfig } from '../config.js'\nimport { isDevTools } from '../constants/isDevTools.js'\nimport { isVariable } from '../createVariable.js'\nimport { createProxy } from '../helpers/createProxy.js'\nimport {\n ThemeManager,\n ThemeManagerState,\n getNonComponentParentManager,\n hasNoThemeUpdatingProps,\n} from '../helpers/ThemeManager.js'\nimport { ThemeManagerContext } from '../helpers/ThemeManagerContext.js'\nimport type { ThemeParsed, ThemeProps } from '../types.js'\nimport { GetThemeUnwrapped } from './getThemeUnwrapped.js'\nimport { useServerRef } from './useServerHooks.js'\n\nexport type ChangedThemeResponse = {\n isNewTheme: boolean\n themeManager: ThemeManager | null\n name: string\n theme?: ThemeParsed | null\n className?: string\n}\n\ntype State = {\n state: ThemeManagerState\n themeManager: ThemeManager\n isNewTheme: boolean\n mounted?: boolean\n}\n\nconst emptyProps = { name: null }\n\nfunction getDefaultThemeProxied() {\n const config = getConfig()\n const name = Object.keys(config.themes)[0]\n return getThemeProxied({\n theme: config.themes[name],\n name,\n })\n}\n\nexport const useTheme = (props: ThemeProps = emptyProps): ThemeParsed => {\n return (isRSC ? null : useThemeWithState(props)?.theme) || getDefaultThemeProxied()\n}\n\nexport const useThemeWithState = (props: ThemeProps) => {\n const keys = useServerRef<string[]>([])\n\n const changedTheme = useChangeThemeEffect(\n props,\n false,\n keys.current,\n isClient\n ? () => {\n return props.shouldUpdate?.() ?? keys.current.length === 0\n }\n : undefined\n )\n\n const { themeManager, theme, name, className } = changedTheme\n\n if (process.env.NODE_ENV === 'development' && props.debug === 'verbose') {\n console.groupCollapsed(' \uD83D\uDD39 useTheme =>', name)\n const logs = { ...props, name, className, ...(isDevTools && { theme }) }\n for (const key in logs) {\n console.log(' ', key, logs[key])\n }\n console.groupEnd()\n }\n\n if (!changedTheme.theme) {\n if (process.env.NODE_ENV === 'development') {\n console.warn('No theme found', name, props, themeManager)\n }\n return null\n }\n\n const proxiedTheme = useMemo(() => {\n return getThemeProxied(changedTheme as any, keys.current)\n }, [theme, name, className, themeManager])\n\n return {\n ...changedTheme,\n theme: proxiedTheme,\n }\n}\n\nexport function getThemeProxied(\n {\n theme,\n themeManager,\n }: Partial<ChangedThemeResponse> & {\n theme: ThemeParsed\n },\n keys?: string[]\n) {\n return createProxy(theme, {\n has(_, key) {\n if (typeof key === 'string') {\n if (key[0] === '$') key = key.slice(1)\n return themeManager?.allKeys.has(key)\n }\n return Reflect.has(theme, key)\n },\n get(_, key) {\n if (key === GetThemeUnwrapped) {\n return theme\n }\n if (\n key === '__proto__' ||\n key === '$typeof' ||\n typeof key !== 'string' ||\n !themeManager\n ) {\n return Reflect.get(_, key)\n }\n\n // auto convert variables to plain\n const keyString = key[0] === '$' ? key.slice(1) : key\n const val = themeManager.getValue(keyString)\n\n if (val && keys) {\n return new Proxy(val as any, {\n // when they touch the actual value we only track it\n // if its a variable (web), its ignored!\n get(_, subkey) {\n if (subkey === 'val' && !keys.includes(keyString)) {\n keys.push(keyString)\n }\n return Reflect.get(val as any, subkey)\n },\n })\n }\n\n return val\n },\n })\n}\n\nexport const activeThemeManagers = new Set<ThemeManager>()\n\nexport const useChangeThemeEffect = (\n props: ThemeProps,\n root = false,\n keys?: string[],\n disableUpdate?: () => boolean\n): ChangedThemeResponse => {\n if (isRSC) {\n // we need context working for this to work well\n return {\n isNewTheme: false,\n ...createState().state,\n themeManager: null,\n }\n }\n\n const parentManager = getNonComponentParentManager(useContext(ThemeManagerContext))\n\n const {\n debug,\n // @ts-expect-error internal use only\n disable,\n } = props\n\n if (disable) {\n if (!parentManager) throw `\u274C`\n return {\n isNewTheme: false,\n ...parentManager.state,\n themeManager: parentManager,\n }\n }\n\n const [themeState, setThemeState] = useState<State>(createState)\n const { state, themeManager, mounted, isNewTheme } = themeState\n const isInversingOnMount = Boolean(!themeState.mounted && props.inverse)\n const shouldReturnParentState = isInversingOnMount\n\n function getShouldUpdateTheme(\n manager = themeManager,\n nextState?: ThemeManagerState | null,\n forceShouldChange = false\n ) {\n const next = nextState || manager.getState(props, parentManager)\n if (!next) return\n if (disableUpdate?.() === true) return\n if (!forceShouldChange && !manager.getStateShouldChange(next, state)) return\n return next\n }\n\n if (!isServer) {\n useEffect(() => {\n // SSR safe inverse (because server can't know prefers scheme)\n // could be done through fancy selectors like how we do prefers-media\n // but may be a bit of explosion of selectors\n if (props.inverse && !mounted) {\n setThemeState({ ...themeState, mounted: true })\n return\n }\n\n if (!isNewTheme) return\n if (!themeManager) return\n\n activeThemeManagers.add(themeManager)\n\n return () => {\n activeThemeManagers.delete(themeManager)\n }\n }, [isNewTheme, themeManager, state, debug])\n\n // listen for parent change + notify children change\n useLayoutEffect(() => {\n const nextState = getShouldUpdateTheme(themeManager)\n\n if (nextState) {\n if (isNewTheme) {\n // if it's a new theme we can just update + publish to children\n themeManager.updateState(nextState, true)\n }\n\n // if not we will be creating a whole new themeManager\n setThemeState(createState)\n } else {\n if (isNewTheme) {\n // need to revert to parent\n setThemeState(createState)\n }\n }\n\n const disposeChangeListener = themeManager.parentManager?.onChangeTheme(\n (name, manager) => {\n const shouldUpdate = Boolean(keys?.length || isNewTheme)\n if (shouldUpdate) {\n if (process.env.NODE_ENV === 'development' && props['debug']) {\n console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys })\n }\n setThemeState(createState)\n }\n },\n themeManager.id\n )\n\n return () => {\n disposeChangeListener?.()\n }\n }, [\n parentManager,\n isNewTheme,\n props.componentName,\n props.inverse,\n props.name,\n props.reset,\n ])\n\n if (process.env.NODE_ENV === 'development') {\n useEffect(() => {\n globalThis['TamaguiThemeManagers'] ??= new Set()\n globalThis['TamaguiThemeManagers'].add(themeManager)\n return () => {\n globalThis['TamaguiThemeManagers'].delete(themeManager)\n }\n }, [themeManager])\n }\n }\n\n if (shouldReturnParentState) {\n if (!parentManager) throw 'impossible'\n return {\n isNewTheme: false,\n ...parentManager.state,\n className: isInversingOnMount ? '' : parentManager.state.className,\n themeManager: parentManager,\n }\n }\n\n return {\n ...state,\n isNewTheme,\n themeManager,\n }\n\n function createState(prev?: State): State {\n if (prev && disableUpdate?.()) {\n return prev\n }\n\n // returns previous theme manager if no change\n let themeManager: ThemeManager\n let state: ThemeManagerState | undefined\n\n const getNewThemeManager = () => {\n return new ThemeManager(props, root ? 'root' : parentManager)\n }\n\n if (prev?.themeManager) {\n themeManager = prev.themeManager\n\n // this could be a bit better, problem is on toggling light/dark the state is actually\n // showing light even when the last was dark. but technically allso onChangeTheme should\n // basically always call on a change, so i'm wondering if we even need the shouldUpdate\n // at all anymore. this forces updates onChangeTheme for all dynamic style accessed components\n // which is correct, potentially in the future we can avoid forceChange and just know to\n // update if keys.length is set + onChangeTheme called\n const forceChange = Boolean(keys?.length)\n\n const next = themeManager.getState(props, parentManager)\n const nextState = getShouldUpdateTheme(themeManager, next, forceChange)\n\n if (nextState) {\n state = nextState\n\n if (!prev.isNewTheme || !isWeb) {\n themeManager = getNewThemeManager()\n } else {\n themeManager.updateState(nextState, true)\n }\n } else {\n if (prev.isNewTheme) {\n // reset to parent\n if (parentManager && !next) {\n themeManager = parentManager\n }\n }\n }\n } else {\n themeManager = getNewThemeManager()\n }\n\n const isNewTheme = Boolean(themeManager !== parentManager)\n\n // only inverse relies on this for ssr\n const mounted = !props.inverse ? true : root || prev?.mounted\n\n if (!state) {\n state = isNewTheme ? { ...themeManager.state } : { ...parentManager!.state }\n }\n\n const response = {\n state,\n themeManager,\n isNewTheme,\n mounted,\n }\n\n if (process.env.NODE_ENV === 'development' && props['debug']) {\n console.groupCollapsed(` \uD83D\uDD37 ${themeManager.id} useChangeThemeEffect createState`)\n const parentState = { ...parentManager?.state }\n const parentId = parentManager?.id\n const themeManagerState = { ...themeManager.state }\n console.log({\n props,\n parentState,\n parentId,\n themeManager,\n prev,\n response,\n themeManagerState,\n })\n console.groupEnd()\n }\n\n return response\n }\n}\n"],
|
|
5
|
-
"mappings": "AACA,SAAS,UAAU,OAAO,UAAU,aAAa;AACjD,SAAS,YAAY,WAAW,iBAAiB,SAAS,gBAAgB;AAE1E,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAE3B,SAAS,mBAAmB;AAC5B;AAAA,EACE;AAAA,EAEA;AAAA,OAEK;AACP,SAAS,2BAA2B;AAEpC,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AAiB7B,MAAM,aAAa,EAAE,MAAM,KAAK;AAEhC,SAAS,yBAAyB;AAChC,QAAM,SAAS,UAAU;AACzB,QAAM,OAAO,OAAO,KAAK,OAAO,MAAM,EAAE,CAAC;AACzC,SAAO,gBAAgB;AAAA,IACrB,OAAO,OAAO,OAAO,IAAI;AAAA,IACzB;AAAA,EACF,CAAC;AACH;AAEO,MAAM,WAAW,CAAC,QAAoB,eAA4B;AA7CzE;AA8CE,UAAQ,QAAQ,QAAO,uBAAkB,KAAK,MAAvB,mBAA0B,UAAU,uBAAuB;AACpF;AAEO,MAAM,oBAAoB,CAAC,UAAsB;AACtD,QAAM,OAAO,aAAuB,CAAC,CAAC;AAEtC,QAAM,eAAe;AAAA,IACnB;AAAA,IACA;AAAA,IACA,KAAK;AAAA,IACL,WACI,MAAM;AAzDd;AA0DU,eAAO,WAAM,iBAAN,mCAA0B,KAAK,QAAQ,WAAW;AAAA,IAC3D,IACA;AAAA,EACN;AAEA,QAAM,EAAE,cAAc,OAAO,MAAM,UAAU,IAAI;AAEjD,MAAI,QAAQ,IAAI,aAAa,iBAAiB,MAAM,UAAU,WAAW;AACvE,YAAQ,eAAe,2BAAoB,IAAI;AAC/C,UAAM,OAAO,EAAE,GAAG,OAAO,MAAM,WAAW,GAAI,cA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AY;AAEpC,aAAO,MAAM;AACX,4BAAoB,OAAO,YAAY;AAAA,MACzC;AAAA,IACF,GAAG,CAAC,YAAY,cAAc,OAAO,KAAK,CAAC;AAG3C,oBAAgB,MAAM;
|
|
4
|
+
"sourcesContent": ["/* eslint-disable no-console */\nimport { isClient, isRSC, isServer, isWeb } from '@tamagui/constants'\nimport { useContext, useEffect, useLayoutEffect, useMemo, useState } from 'react'\n\nimport { getConfig } from '../config.js'\nimport { isDevTools } from '../constants/isDevTools.js'\nimport { isVariable } from '../createVariable.js'\nimport { createProxy } from '../helpers/createProxy.js'\nimport {\n ThemeManager,\n ThemeManagerState,\n getNonComponentParentManager,\n hasNoThemeUpdatingProps,\n} from '../helpers/ThemeManager.js'\nimport { ThemeManagerContext } from '../helpers/ThemeManagerContext.js'\nimport type { ThemeParsed, ThemeProps } from '../types.js'\nimport { GetThemeUnwrapped } from './getThemeUnwrapped.js'\nimport { useServerRef } from './useServerHooks.js'\n\nexport type ChangedThemeResponse = {\n isNewTheme: boolean\n themeManager: ThemeManager | null\n name: string\n theme?: ThemeParsed | null\n className?: string\n}\n\ntype State = {\n state: ThemeManagerState\n themeManager: ThemeManager\n isNewTheme: boolean\n mounted?: boolean\n}\n\nconst emptyProps = { name: null }\n\nfunction getDefaultThemeProxied() {\n const config = getConfig()\n const name = Object.keys(config.themes)[0]\n return getThemeProxied({\n theme: config.themes[name],\n name,\n })\n}\n\nexport const useTheme = (props: ThemeProps = emptyProps): ThemeParsed => {\n return (isRSC ? null : useThemeWithState(props)?.theme) || getDefaultThemeProxied()\n}\n\nexport const useThemeWithState = (props: ThemeProps) => {\n const keys = useServerRef<string[]>([])\n\n const changedTheme = useChangeThemeEffect(\n props,\n false,\n keys.current,\n isClient\n ? () => {\n return props.shouldUpdate?.() ?? keys.current.length === 0\n }\n : undefined\n )\n\n const { themeManager, theme, name, className } = changedTheme\n\n if (process.env.NODE_ENV === 'development' && props.debug === 'verbose') {\n console.groupCollapsed(' \uD83D\uDD39 useTheme =>', name)\n const logs = { ...props, name, className, ...(isDevTools && { theme }) }\n for (const key in logs) {\n console.log(' ', key, logs[key])\n }\n console.groupEnd()\n }\n\n if (!changedTheme.theme) {\n if (process.env.NODE_ENV === 'development') {\n console.warn('No theme found', name, props, themeManager)\n }\n return null\n }\n\n const proxiedTheme = useMemo(() => {\n return getThemeProxied(changedTheme as any, keys.current)\n }, [theme, name, className, themeManager])\n\n return {\n ...changedTheme,\n theme: proxiedTheme,\n }\n}\n\nexport function getThemeProxied(\n {\n theme,\n themeManager,\n }: Partial<ChangedThemeResponse> & {\n theme: ThemeParsed\n },\n keys?: string[]\n) {\n return createProxy(theme, {\n has(_, key) {\n if (typeof key === 'string') {\n if (key[0] === '$') key = key.slice(1)\n return themeManager?.allKeys.has(key)\n }\n return Reflect.has(theme, key)\n },\n get(_, key) {\n if (key === GetThemeUnwrapped) {\n return theme\n }\n if (\n key === '__proto__' ||\n key === '$typeof' ||\n typeof key !== 'string' ||\n !themeManager\n ) {\n return Reflect.get(_, key)\n }\n\n // auto convert variables to plain\n const keyString = key[0] === '$' ? key.slice(1) : key\n const val = themeManager.getValue(keyString)\n\n if (val && keys) {\n return new Proxy(val as any, {\n // when they touch the actual value we only track it\n // if its a variable (web), its ignored!\n get(_, subkey) {\n if (subkey === 'val' && !keys.includes(keyString)) {\n keys.push(keyString)\n }\n return Reflect.get(val as any, subkey)\n },\n })\n }\n\n return val\n },\n })\n}\n\nexport const activeThemeManagers = new Set<ThemeManager>()\n\nexport const useChangeThemeEffect = (\n props: ThemeProps,\n root = false,\n keys?: string[],\n disableUpdate?: () => boolean\n): ChangedThemeResponse => {\n if (isRSC) {\n // we need context working for this to work well\n return {\n isNewTheme: false,\n ...createState().state,\n themeManager: null,\n }\n }\n\n const parentManager = getNonComponentParentManager(useContext(ThemeManagerContext))\n\n const {\n debug,\n // @ts-expect-error internal use only\n disable,\n } = props\n\n if (disable) {\n if (!parentManager) throw `\u274C`\n return {\n isNewTheme: false,\n ...parentManager.state,\n themeManager: parentManager,\n }\n }\n\n const [themeState, setThemeState] = useState<State>(createState)\n const { state, themeManager, mounted, isNewTheme } = themeState\n const isInversingOnMount = Boolean(!themeState.mounted && props.inverse)\n const shouldReturnParentState = isInversingOnMount\n\n function getShouldUpdateTheme(\n manager = themeManager,\n nextState?: ThemeManagerState | null,\n forceShouldChange = false\n ) {\n const next = nextState || manager.getState(props, parentManager)\n if (!next) return\n if (disableUpdate?.() === true) return\n if (!forceShouldChange && !manager.getStateShouldChange(next, state)) return\n return next\n }\n\n if (!isServer) {\n useEffect(() => {\n // SSR safe inverse (because server can't know prefers scheme)\n // could be done through fancy selectors like how we do prefers-media\n // but may be a bit of explosion of selectors\n if (props.inverse && !mounted) {\n setThemeState({ ...themeState, mounted: true })\n return\n }\n\n if (!isNewTheme) return\n if (!themeManager) return\n\n activeThemeManagers.add(themeManager)\n\n return () => {\n activeThemeManagers.delete(themeManager)\n }\n }, [isNewTheme, themeManager, state, debug])\n\n // listen for parent change + notify children change\n useLayoutEffect(() => {\n const nextState = getShouldUpdateTheme(themeManager)\n\n if (nextState) {\n if (isNewTheme) {\n // if it's a new theme we can just update + publish to children\n themeManager.updateState(nextState, true)\n }\n\n // if not we will be creating a whole new themeManager\n setThemeState(createState)\n } else {\n if (isNewTheme) {\n // need to revert to parent\n setThemeState(createState)\n }\n }\n\n const disposeChangeListener = parentManager?.onChangeTheme((name, manager) => {\n const shouldUpdate = Boolean(keys?.length || isNewTheme)\n if (shouldUpdate) {\n if (process.env.NODE_ENV === 'development' && props['debug']) {\n console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys })\n }\n setThemeState(createState)\n }\n }, themeManager.id)\n\n return () => {\n disposeChangeListener?.()\n }\n }, [\n parentManager,\n isNewTheme,\n props.componentName,\n props.inverse,\n props.name,\n props.reset,\n ])\n\n if (process.env.NODE_ENV === 'development') {\n useEffect(() => {\n globalThis['TamaguiThemeManagers'] ??= new Set()\n globalThis['TamaguiThemeManagers'].add(themeManager)\n return () => {\n globalThis['TamaguiThemeManagers'].delete(themeManager)\n }\n }, [themeManager])\n }\n }\n\n if (shouldReturnParentState) {\n if (!parentManager) throw 'impossible'\n return {\n isNewTheme: false,\n ...parentManager.state,\n className: isInversingOnMount ? '' : parentManager.state.className,\n themeManager: parentManager,\n }\n }\n\n return {\n ...state,\n isNewTheme,\n themeManager,\n }\n\n function createState(prev?: State): State {\n if (prev && disableUpdate?.()) {\n return prev\n }\n\n // returns previous theme manager if no change\n let themeManager: ThemeManager\n let state: ThemeManagerState | undefined\n\n const getNewThemeManager = () => {\n return new ThemeManager(props, root ? 'root' : parentManager)\n }\n\n if (prev?.themeManager) {\n themeManager = prev.themeManager\n\n // this could be a bit better, problem is on toggling light/dark the state is actually\n // showing light even when the last was dark. but technically allso onChangeTheme should\n // basically always call on a change, so i'm wondering if we even need the shouldUpdate\n // at all anymore. this forces updates onChangeTheme for all dynamic style accessed components\n // which is correct, potentially in the future we can avoid forceChange and just know to\n // update if keys.length is set + onChangeTheme called\n const forceChange = Boolean(keys?.length)\n\n const next = themeManager.getState(props, parentManager)\n const nextState = getShouldUpdateTheme(themeManager, next, forceChange)\n\n if (nextState) {\n state = nextState\n\n if (!prev.isNewTheme || !isWeb) {\n themeManager = getNewThemeManager()\n } else {\n themeManager.updateState(nextState, true)\n }\n } else {\n if (prev.isNewTheme) {\n // reset to parent\n if (parentManager && !next) {\n themeManager = parentManager\n }\n }\n }\n } else {\n themeManager = getNewThemeManager()\n }\n\n const isNewTheme = Boolean(themeManager !== parentManager)\n\n // only inverse relies on this for ssr\n const mounted = !props.inverse ? true : root || prev?.mounted\n\n if (!state) {\n state = isNewTheme ? { ...themeManager.state } : { ...parentManager!.state }\n }\n\n const response = {\n state,\n themeManager,\n isNewTheme,\n mounted,\n }\n\n if (process.env.NODE_ENV === 'development' && props['debug']) {\n console.groupCollapsed(` \uD83D\uDD37 ${themeManager.id} useChangeThemeEffect createState`)\n const parentState = { ...parentManager?.state }\n const parentId = parentManager?.id\n const themeManagerState = { ...themeManager.state }\n console.log({\n props,\n parentState,\n parentId,\n themeManager,\n prev,\n response,\n themeManagerState,\n })\n console.groupEnd()\n }\n\n return response\n }\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,UAAU,OAAO,UAAU,aAAa;AACjD,SAAS,YAAY,WAAW,iBAAiB,SAAS,gBAAgB;AAE1E,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAE3B,SAAS,mBAAmB;AAC5B;AAAA,EACE;AAAA,EAEA;AAAA,OAEK;AACP,SAAS,2BAA2B;AAEpC,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AAiB7B,MAAM,aAAa,EAAE,MAAM,KAAK;AAEhC,SAAS,yBAAyB;AAChC,QAAM,SAAS,UAAU;AACzB,QAAM,OAAO,OAAO,KAAK,OAAO,MAAM,EAAE,CAAC;AACzC,SAAO,gBAAgB;AAAA,IACrB,OAAO,OAAO,OAAO,IAAI;AAAA,IACzB;AAAA,EACF,CAAC;AACH;AAEO,MAAM,WAAW,CAAC,QAAoB,eAA4B;AA7CzE;AA8CE,UAAQ,QAAQ,QAAO,uBAAkB,KAAK,MAAvB,mBAA0B,UAAU,uBAAuB;AACpF;AAEO,MAAM,oBAAoB,CAAC,UAAsB;AACtD,QAAM,OAAO,aAAuB,CAAC,CAAC;AAEtC,QAAM,eAAe;AAAA,IACnB;AAAA,IACA;AAAA,IACA,KAAK;AAAA,IACL,WACI,MAAM;AAzDd;AA0DU,eAAO,WAAM,iBAAN,mCAA0B,KAAK,QAAQ,WAAW;AAAA,IAC3D,IACA;AAAA,EACN;AAEA,QAAM,EAAE,cAAc,OAAO,MAAM,UAAU,IAAI;AAEjD,MAAI,QAAQ,IAAI,aAAa,iBAAiB,MAAM,UAAU,WAAW;AACvE,YAAQ,eAAe,2BAAoB,IAAI;AAC/C,UAAM,OAAO,EAAE,GAAG,OAAO,MAAM,WAAW,GAAI,cAAc,EAAE,MAAM,EAAG;AACvE,eAAW,OAAO,MAAM;AACtB,cA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+CAAe,MAAM;AAC9C,YAAM,WAAW,+CAAe;AAChC,YAAM,oBAAoB,EAAE,GAAGA,cAAa,MAAM;AAClD,cAAQ,IAAI;AAAA,QACV;AAAA,QACA;AAAA,QACA;AAAA,QACA,cAAAA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC;AACD,cAAQ,SAAS;AAAA,IACnB;AAEA,WAAO;AAAA,EACT;AACF;",
|
|
6
6
|
"names": ["_", "themeManager", "state", "isNewTheme", "mounted"]
|
|
7
7
|
}
|
|
@@ -149,7 +149,6 @@ const useChangeThemeEffect = (props, root = false, keys, disableUpdate) => {
|
|
|
149
149
|
};
|
|
150
150
|
}, [isNewTheme, themeManager, state, debug]);
|
|
151
151
|
useLayoutEffect(() => {
|
|
152
|
-
var _a;
|
|
153
152
|
const nextState = getShouldUpdateTheme(themeManager);
|
|
154
153
|
if (nextState) {
|
|
155
154
|
if (isNewTheme) {
|
|
@@ -161,18 +160,15 @@ const useChangeThemeEffect = (props, root = false, keys, disableUpdate) => {
|
|
|
161
160
|
setThemeState(createState);
|
|
162
161
|
}
|
|
163
162
|
}
|
|
164
|
-
const disposeChangeListener =
|
|
165
|
-
(
|
|
166
|
-
|
|
167
|
-
if (
|
|
168
|
-
|
|
169
|
-
console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys });
|
|
170
|
-
}
|
|
171
|
-
setThemeState(createState);
|
|
163
|
+
const disposeChangeListener = parentManager == null ? void 0 : parentManager.onChangeTheme((name, manager) => {
|
|
164
|
+
const shouldUpdate = Boolean((keys == null ? void 0 : keys.length) || isNewTheme);
|
|
165
|
+
if (shouldUpdate) {
|
|
166
|
+
if (process.env.NODE_ENV === "development" && props["debug"]) {
|
|
167
|
+
console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys });
|
|
172
168
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
);
|
|
169
|
+
setThemeState(createState);
|
|
170
|
+
}
|
|
171
|
+
}, themeManager.id);
|
|
176
172
|
return () => {
|
|
177
173
|
disposeChangeListener == null ? void 0 : disposeChangeListener();
|
|
178
174
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/hooks/useTheme.tsx"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable no-console */\nimport { isClient, isRSC, isServer, isWeb } from '@tamagui/constants'\nimport { useContext, useEffect, useLayoutEffect, useMemo, useState } from 'react'\n\nimport { getConfig } from '../config.js'\nimport { isDevTools } from '../constants/isDevTools.js'\nimport { isVariable } from '../createVariable.js'\nimport { createProxy } from '../helpers/createProxy.js'\nimport {\n ThemeManager,\n ThemeManagerState,\n getNonComponentParentManager,\n hasNoThemeUpdatingProps,\n} from '../helpers/ThemeManager.js'\nimport { ThemeManagerContext } from '../helpers/ThemeManagerContext.js'\nimport type { ThemeParsed, ThemeProps } from '../types.js'\nimport { GetThemeUnwrapped } from './getThemeUnwrapped.js'\nimport { useServerRef } from './useServerHooks.js'\n\nexport type ChangedThemeResponse = {\n isNewTheme: boolean\n themeManager: ThemeManager | null\n name: string\n theme?: ThemeParsed | null\n className?: string\n}\n\ntype State = {\n state: ThemeManagerState\n themeManager: ThemeManager\n isNewTheme: boolean\n mounted?: boolean\n}\n\nconst emptyProps = { name: null }\n\nfunction getDefaultThemeProxied() {\n const config = getConfig()\n const name = Object.keys(config.themes)[0]\n return getThemeProxied({\n theme: config.themes[name],\n name,\n })\n}\n\nexport const useTheme = (props: ThemeProps = emptyProps): ThemeParsed => {\n return (isRSC ? null : useThemeWithState(props)?.theme) || getDefaultThemeProxied()\n}\n\nexport const useThemeWithState = (props: ThemeProps) => {\n const keys = useServerRef<string[]>([])\n\n const changedTheme = useChangeThemeEffect(\n props,\n false,\n keys.current,\n isClient\n ? () => {\n return props.shouldUpdate?.() ?? keys.current.length === 0\n }\n : undefined\n )\n\n const { themeManager, theme, name, className } = changedTheme\n\n if (process.env.NODE_ENV === 'development' && props.debug === 'verbose') {\n console.groupCollapsed(' \uD83D\uDD39 useTheme =>', name)\n const logs = { ...props, name, className, ...(isDevTools && { theme }) }\n for (const key in logs) {\n console.log(' ', key, logs[key])\n }\n console.groupEnd()\n }\n\n if (!changedTheme.theme) {\n if (process.env.NODE_ENV === 'development') {\n console.warn('No theme found', name, props, themeManager)\n }\n return null\n }\n\n const proxiedTheme = useMemo(() => {\n return getThemeProxied(changedTheme as any, keys.current)\n }, [theme, name, className, themeManager])\n\n return {\n ...changedTheme,\n theme: proxiedTheme,\n }\n}\n\nexport function getThemeProxied(\n {\n theme,\n themeManager,\n }: Partial<ChangedThemeResponse> & {\n theme: ThemeParsed\n },\n keys?: string[]\n) {\n return createProxy(theme, {\n has(_, key) {\n if (typeof key === 'string') {\n if (key[0] === '$') key = key.slice(1)\n return themeManager?.allKeys.has(key)\n }\n return Reflect.has(theme, key)\n },\n get(_, key) {\n if (key === GetThemeUnwrapped) {\n return theme\n }\n if (\n key === '__proto__' ||\n key === '$typeof' ||\n typeof key !== 'string' ||\n !themeManager\n ) {\n return Reflect.get(_, key)\n }\n\n // auto convert variables to plain\n const keyString = key[0] === '$' ? key.slice(1) : key\n const val = themeManager.getValue(keyString)\n\n if (val && keys) {\n return new Proxy(val as any, {\n // when they touch the actual value we only track it\n // if its a variable (web), its ignored!\n get(_, subkey) {\n if (subkey === 'val' && !keys.includes(keyString)) {\n keys.push(keyString)\n }\n return Reflect.get(val as any, subkey)\n },\n })\n }\n\n return val\n },\n })\n}\n\nexport const activeThemeManagers = new Set<ThemeManager>()\n\nexport const useChangeThemeEffect = (\n props: ThemeProps,\n root = false,\n keys?: string[],\n disableUpdate?: () => boolean\n): ChangedThemeResponse => {\n if (isRSC) {\n // we need context working for this to work well\n return {\n isNewTheme: false,\n ...createState().state,\n themeManager: null,\n }\n }\n\n const parentManager = getNonComponentParentManager(useContext(ThemeManagerContext))\n\n const {\n debug,\n // @ts-expect-error internal use only\n disable,\n } = props\n\n if (disable) {\n if (!parentManager) throw `\u274C`\n return {\n isNewTheme: false,\n ...parentManager.state,\n themeManager: parentManager,\n }\n }\n\n const [themeState, setThemeState] = useState<State>(createState)\n const { state, themeManager, mounted, isNewTheme } = themeState\n const isInversingOnMount = Boolean(!themeState.mounted && props.inverse)\n const shouldReturnParentState = isInversingOnMount\n\n function getShouldUpdateTheme(\n manager = themeManager,\n nextState?: ThemeManagerState | null,\n forceShouldChange = false\n ) {\n const next = nextState || manager.getState(props, parentManager)\n if (!next) return\n if (disableUpdate?.() === true) return\n if (!forceShouldChange && !manager.getStateShouldChange(next, state)) return\n return next\n }\n\n if (!isServer) {\n useEffect(() => {\n // SSR safe inverse (because server can't know prefers scheme)\n // could be done through fancy selectors like how we do prefers-media\n // but may be a bit of explosion of selectors\n if (props.inverse && !mounted) {\n setThemeState({ ...themeState, mounted: true })\n return\n }\n\n if (!isNewTheme) return\n if (!themeManager) return\n\n activeThemeManagers.add(themeManager)\n\n return () => {\n activeThemeManagers.delete(themeManager)\n }\n }, [isNewTheme, themeManager, state, debug])\n\n // listen for parent change + notify children change\n useLayoutEffect(() => {\n const nextState = getShouldUpdateTheme(themeManager)\n\n if (nextState) {\n if (isNewTheme) {\n // if it's a new theme we can just update + publish to children\n themeManager.updateState(nextState, true)\n }\n\n // if not we will be creating a whole new themeManager\n setThemeState(createState)\n } else {\n if (isNewTheme) {\n // need to revert to parent\n setThemeState(createState)\n }\n }\n\n const disposeChangeListener = themeManager.parentManager?.onChangeTheme(\n (name, manager) => {\n const shouldUpdate = Boolean(keys?.length || isNewTheme)\n if (shouldUpdate) {\n if (process.env.NODE_ENV === 'development' && props['debug']) {\n console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys })\n }\n setThemeState(createState)\n }\n },\n themeManager.id\n )\n\n return () => {\n disposeChangeListener?.()\n }\n }, [\n parentManager,\n isNewTheme,\n props.componentName,\n props.inverse,\n props.name,\n props.reset,\n ])\n\n if (process.env.NODE_ENV === 'development') {\n useEffect(() => {\n globalThis['TamaguiThemeManagers'] ??= new Set()\n globalThis['TamaguiThemeManagers'].add(themeManager)\n return () => {\n globalThis['TamaguiThemeManagers'].delete(themeManager)\n }\n }, [themeManager])\n }\n }\n\n if (shouldReturnParentState) {\n if (!parentManager) throw 'impossible'\n return {\n isNewTheme: false,\n ...parentManager.state,\n className: isInversingOnMount ? '' : parentManager.state.className,\n themeManager: parentManager,\n }\n }\n\n return {\n ...state,\n isNewTheme,\n themeManager,\n }\n\n function createState(prev?: State): State {\n if (prev && disableUpdate?.()) {\n return prev\n }\n\n // returns previous theme manager if no change\n let themeManager: ThemeManager\n let state: ThemeManagerState | undefined\n\n const getNewThemeManager = () => {\n return new ThemeManager(props, root ? 'root' : parentManager)\n }\n\n if (prev?.themeManager) {\n themeManager = prev.themeManager\n\n // this could be a bit better, problem is on toggling light/dark the state is actually\n // showing light even when the last was dark. but technically allso onChangeTheme should\n // basically always call on a change, so i'm wondering if we even need the shouldUpdate\n // at all anymore. this forces updates onChangeTheme for all dynamic style accessed components\n // which is correct, potentially in the future we can avoid forceChange and just know to\n // update if keys.length is set + onChangeTheme called\n const forceChange = Boolean(keys?.length)\n\n const next = themeManager.getState(props, parentManager)\n const nextState = getShouldUpdateTheme(themeManager, next, forceChange)\n\n if (nextState) {\n state = nextState\n\n if (!prev.isNewTheme || !isWeb) {\n themeManager = getNewThemeManager()\n } else {\n themeManager.updateState(nextState, true)\n }\n } else {\n if (prev.isNewTheme) {\n // reset to parent\n if (parentManager && !next) {\n themeManager = parentManager\n }\n }\n }\n } else {\n themeManager = getNewThemeManager()\n }\n\n const isNewTheme = Boolean(themeManager !== parentManager)\n\n // only inverse relies on this for ssr\n const mounted = !props.inverse ? true : root || prev?.mounted\n\n if (!state) {\n state = isNewTheme ? { ...themeManager.state } : { ...parentManager!.state }\n }\n\n const response = {\n state,\n themeManager,\n isNewTheme,\n mounted,\n }\n\n if (process.env.NODE_ENV === 'development' && props['debug']) {\n console.groupCollapsed(` \uD83D\uDD37 ${themeManager.id} useChangeThemeEffect createState`)\n const parentState = { ...parentManager?.state }\n const parentId = parentManager?.id\n const themeManagerState = { ...themeManager.state }\n console.log({\n props,\n parentState,\n parentId,\n themeManager,\n prev,\n response,\n themeManagerState,\n })\n console.groupEnd()\n }\n\n return response\n }\n}\n"],
|
|
5
|
-
"mappings": "AACA,SAAS,UAAU,OAAO,UAAU,aAAa;AACjD,SAAS,YAAY,WAAW,iBAAiB,SAAS,gBAAgB;AAE1E,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAE3B,SAAS,mBAAmB;AAC5B;AAAA,EACE;AAAA,EAEA;AAAA,OAEK;AACP,SAAS,2BAA2B;AAEpC,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AAiB7B,MAAM,aAAa,EAAE,MAAM,KAAK;AAEhC,SAAS,yBAAyB;AAChC,QAAM,SAAS,UAAU;AACzB,QAAM,OAAO,OAAO,KAAK,OAAO,MAAM,EAAE,CAAC;AACzC,SAAO,gBAAgB;AAAA,IACrB,OAAO,OAAO,OAAO,IAAI;AAAA,IACzB;AAAA,EACF,CAAC;AACH;AAEO,MAAM,WAAW,CAAC,QAAoB,eAA4B;AA7CzE;AA8CE,UAAQ,QAAQ,QAAO,uBAAkB,KAAK,MAAvB,mBAA0B,UAAU,uBAAuB;AACpF;AAEO,MAAM,oBAAoB,CAAC,UAAsB;AACtD,QAAM,OAAO,aAAuB,CAAC,CAAC;AAEtC,QAAM,eAAe;AAAA,IACnB;AAAA,IACA;AAAA,IACA,KAAK;AAAA,IACL,WACI,MAAM;AAzDd;AA0DU,eAAO,WAAM,iBAAN,mCAA0B,KAAK,QAAQ,WAAW;AAAA,IAC3D,IACA;AAAA,EACN;AAEA,QAAM,EAAE,cAAc,OAAO,MAAM,UAAU,IAAI;AAEjD,MAAI,QAAQ,IAAI,aAAa,iBAAiB,MAAM,UAAU,WAAW;AACvE,YAAQ,eAAe,2BAAoB,IAAI;AAC/C,UAAM,OAAO,EAAE,GAAG,OAAO,MAAM,WAAW,GAAI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,YAAY;AAEpC,aAAO,MAAM;AACX,4BAAoB,OAAO,YAAY;AAAA,MACzC;AAAA,IACF,GAAG,CAAC,YAAY,cAAc,OAAO,KAAK,CAAC;AAG3C,oBAAgB,MAAM;
|
|
4
|
+
"sourcesContent": ["/* eslint-disable no-console */\nimport { isClient, isRSC, isServer, isWeb } from '@tamagui/constants'\nimport { useContext, useEffect, useLayoutEffect, useMemo, useState } from 'react'\n\nimport { getConfig } from '../config.js'\nimport { isDevTools } from '../constants/isDevTools.js'\nimport { isVariable } from '../createVariable.js'\nimport { createProxy } from '../helpers/createProxy.js'\nimport {\n ThemeManager,\n ThemeManagerState,\n getNonComponentParentManager,\n hasNoThemeUpdatingProps,\n} from '../helpers/ThemeManager.js'\nimport { ThemeManagerContext } from '../helpers/ThemeManagerContext.js'\nimport type { ThemeParsed, ThemeProps } from '../types.js'\nimport { GetThemeUnwrapped } from './getThemeUnwrapped.js'\nimport { useServerRef } from './useServerHooks.js'\n\nexport type ChangedThemeResponse = {\n isNewTheme: boolean\n themeManager: ThemeManager | null\n name: string\n theme?: ThemeParsed | null\n className?: string\n}\n\ntype State = {\n state: ThemeManagerState\n themeManager: ThemeManager\n isNewTheme: boolean\n mounted?: boolean\n}\n\nconst emptyProps = { name: null }\n\nfunction getDefaultThemeProxied() {\n const config = getConfig()\n const name = Object.keys(config.themes)[0]\n return getThemeProxied({\n theme: config.themes[name],\n name,\n })\n}\n\nexport const useTheme = (props: ThemeProps = emptyProps): ThemeParsed => {\n return (isRSC ? null : useThemeWithState(props)?.theme) || getDefaultThemeProxied()\n}\n\nexport const useThemeWithState = (props: ThemeProps) => {\n const keys = useServerRef<string[]>([])\n\n const changedTheme = useChangeThemeEffect(\n props,\n false,\n keys.current,\n isClient\n ? () => {\n return props.shouldUpdate?.() ?? keys.current.length === 0\n }\n : undefined\n )\n\n const { themeManager, theme, name, className } = changedTheme\n\n if (process.env.NODE_ENV === 'development' && props.debug === 'verbose') {\n console.groupCollapsed(' \uD83D\uDD39 useTheme =>', name)\n const logs = { ...props, name, className, ...(isDevTools && { theme }) }\n for (const key in logs) {\n console.log(' ', key, logs[key])\n }\n console.groupEnd()\n }\n\n if (!changedTheme.theme) {\n if (process.env.NODE_ENV === 'development') {\n console.warn('No theme found', name, props, themeManager)\n }\n return null\n }\n\n const proxiedTheme = useMemo(() => {\n return getThemeProxied(changedTheme as any, keys.current)\n }, [theme, name, className, themeManager])\n\n return {\n ...changedTheme,\n theme: proxiedTheme,\n }\n}\n\nexport function getThemeProxied(\n {\n theme,\n themeManager,\n }: Partial<ChangedThemeResponse> & {\n theme: ThemeParsed\n },\n keys?: string[]\n) {\n return createProxy(theme, {\n has(_, key) {\n if (typeof key === 'string') {\n if (key[0] === '$') key = key.slice(1)\n return themeManager?.allKeys.has(key)\n }\n return Reflect.has(theme, key)\n },\n get(_, key) {\n if (key === GetThemeUnwrapped) {\n return theme\n }\n if (\n key === '__proto__' ||\n key === '$typeof' ||\n typeof key !== 'string' ||\n !themeManager\n ) {\n return Reflect.get(_, key)\n }\n\n // auto convert variables to plain\n const keyString = key[0] === '$' ? key.slice(1) : key\n const val = themeManager.getValue(keyString)\n\n if (val && keys) {\n return new Proxy(val as any, {\n // when they touch the actual value we only track it\n // if its a variable (web), its ignored!\n get(_, subkey) {\n if (subkey === 'val' && !keys.includes(keyString)) {\n keys.push(keyString)\n }\n return Reflect.get(val as any, subkey)\n },\n })\n }\n\n return val\n },\n })\n}\n\nexport const activeThemeManagers = new Set<ThemeManager>()\n\nexport const useChangeThemeEffect = (\n props: ThemeProps,\n root = false,\n keys?: string[],\n disableUpdate?: () => boolean\n): ChangedThemeResponse => {\n if (isRSC) {\n // we need context working for this to work well\n return {\n isNewTheme: false,\n ...createState().state,\n themeManager: null,\n }\n }\n\n const parentManager = getNonComponentParentManager(useContext(ThemeManagerContext))\n\n const {\n debug,\n // @ts-expect-error internal use only\n disable,\n } = props\n\n if (disable) {\n if (!parentManager) throw `\u274C`\n return {\n isNewTheme: false,\n ...parentManager.state,\n themeManager: parentManager,\n }\n }\n\n const [themeState, setThemeState] = useState<State>(createState)\n const { state, themeManager, mounted, isNewTheme } = themeState\n const isInversingOnMount = Boolean(!themeState.mounted && props.inverse)\n const shouldReturnParentState = isInversingOnMount\n\n function getShouldUpdateTheme(\n manager = themeManager,\n nextState?: ThemeManagerState | null,\n forceShouldChange = false\n ) {\n const next = nextState || manager.getState(props, parentManager)\n if (!next) return\n if (disableUpdate?.() === true) return\n if (!forceShouldChange && !manager.getStateShouldChange(next, state)) return\n return next\n }\n\n if (!isServer) {\n useEffect(() => {\n // SSR safe inverse (because server can't know prefers scheme)\n // could be done through fancy selectors like how we do prefers-media\n // but may be a bit of explosion of selectors\n if (props.inverse && !mounted) {\n setThemeState({ ...themeState, mounted: true })\n return\n }\n\n if (!isNewTheme) return\n if (!themeManager) return\n\n activeThemeManagers.add(themeManager)\n\n return () => {\n activeThemeManagers.delete(themeManager)\n }\n }, [isNewTheme, themeManager, state, debug])\n\n // listen for parent change + notify children change\n useLayoutEffect(() => {\n const nextState = getShouldUpdateTheme(themeManager)\n\n if (nextState) {\n if (isNewTheme) {\n // if it's a new theme we can just update + publish to children\n themeManager.updateState(nextState, true)\n }\n\n // if not we will be creating a whole new themeManager\n setThemeState(createState)\n } else {\n if (isNewTheme) {\n // need to revert to parent\n setThemeState(createState)\n }\n }\n\n const disposeChangeListener = parentManager?.onChangeTheme((name, manager) => {\n const shouldUpdate = Boolean(keys?.length || isNewTheme)\n if (shouldUpdate) {\n if (process.env.NODE_ENV === 'development' && props['debug']) {\n console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys })\n }\n setThemeState(createState)\n }\n }, themeManager.id)\n\n return () => {\n disposeChangeListener?.()\n }\n }, [\n parentManager,\n isNewTheme,\n props.componentName,\n props.inverse,\n props.name,\n props.reset,\n ])\n\n if (process.env.NODE_ENV === 'development') {\n useEffect(() => {\n globalThis['TamaguiThemeManagers'] ??= new Set()\n globalThis['TamaguiThemeManagers'].add(themeManager)\n return () => {\n globalThis['TamaguiThemeManagers'].delete(themeManager)\n }\n }, [themeManager])\n }\n }\n\n if (shouldReturnParentState) {\n if (!parentManager) throw 'impossible'\n return {\n isNewTheme: false,\n ...parentManager.state,\n className: isInversingOnMount ? '' : parentManager.state.className,\n themeManager: parentManager,\n }\n }\n\n return {\n ...state,\n isNewTheme,\n themeManager,\n }\n\n function createState(prev?: State): State {\n if (prev && disableUpdate?.()) {\n return prev\n }\n\n // returns previous theme manager if no change\n let themeManager: ThemeManager\n let state: ThemeManagerState | undefined\n\n const getNewThemeManager = () => {\n return new ThemeManager(props, root ? 'root' : parentManager)\n }\n\n if (prev?.themeManager) {\n themeManager = prev.themeManager\n\n // this could be a bit better, problem is on toggling light/dark the state is actually\n // showing light even when the last was dark. but technically allso onChangeTheme should\n // basically always call on a change, so i'm wondering if we even need the shouldUpdate\n // at all anymore. this forces updates onChangeTheme for all dynamic style accessed components\n // which is correct, potentially in the future we can avoid forceChange and just know to\n // update if keys.length is set + onChangeTheme called\n const forceChange = Boolean(keys?.length)\n\n const next = themeManager.getState(props, parentManager)\n const nextState = getShouldUpdateTheme(themeManager, next, forceChange)\n\n if (nextState) {\n state = nextState\n\n if (!prev.isNewTheme || !isWeb) {\n themeManager = getNewThemeManager()\n } else {\n themeManager.updateState(nextState, true)\n }\n } else {\n if (prev.isNewTheme) {\n // reset to parent\n if (parentManager && !next) {\n themeManager = parentManager\n }\n }\n }\n } else {\n themeManager = getNewThemeManager()\n }\n\n const isNewTheme = Boolean(themeManager !== parentManager)\n\n // only inverse relies on this for ssr\n const mounted = !props.inverse ? true : root || prev?.mounted\n\n if (!state) {\n state = isNewTheme ? { ...themeManager.state } : { ...parentManager!.state }\n }\n\n const response = {\n state,\n themeManager,\n isNewTheme,\n mounted,\n }\n\n if (process.env.NODE_ENV === 'development' && props['debug']) {\n console.groupCollapsed(` \uD83D\uDD37 ${themeManager.id} useChangeThemeEffect createState`)\n const parentState = { ...parentManager?.state }\n const parentId = parentManager?.id\n const themeManagerState = { ...themeManager.state }\n console.log({\n props,\n parentState,\n parentId,\n themeManager,\n prev,\n response,\n themeManagerState,\n })\n console.groupEnd()\n }\n\n return response\n }\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,UAAU,OAAO,UAAU,aAAa;AACjD,SAAS,YAAY,WAAW,iBAAiB,SAAS,gBAAgB;AAE1E,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAE3B,SAAS,mBAAmB;AAC5B;AAAA,EACE;AAAA,EAEA;AAAA,OAEK;AACP,SAAS,2BAA2B;AAEpC,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AAiB7B,MAAM,aAAa,EAAE,MAAM,KAAK;AAEhC,SAAS,yBAAyB;AAChC,QAAM,SAAS,UAAU;AACzB,QAAM,OAAO,OAAO,KAAK,OAAO,MAAM,EAAE,CAAC;AACzC,SAAO,gBAAgB;AAAA,IACrB,OAAO,OAAO,OAAO,IAAI;AAAA,IACzB;AAAA,EACF,CAAC;AACH;AAEO,MAAM,WAAW,CAAC,QAAoB,eAA4B;AA7CzE;AA8CE,UAAQ,QAAQ,QAAO,uBAAkB,KAAK,MAAvB,mBAA0B,UAAU,uBAAuB;AACpF;AAEO,MAAM,oBAAoB,CAAC,UAAsB;AACtD,QAAM,OAAO,aAAuB,CAAC,CAAC;AAEtC,QAAM,eAAe;AAAA,IACnB;AAAA,IACA;AAAA,IACA,KAAK;AAAA,IACL,WACI,MAAM;AAzDd;AA0DU,eAAO,WAAM,iBAAN,mCAA0B,KAAK,QAAQ,WAAW;AAAA,IAC3D,IACA;AAAA,EACN;AAEA,QAAM,EAAE,cAAc,OAAO,MAAM,UAAU,IAAI;AAEjD,MAAI,QAAQ,IAAI,aAAa,iBAAiB,MAAM,UAAU,WAAW;AACvE,YAAQ,eAAe,2BAAoB,IAAI;AAC/C,UAAM,OAAO,EAAE,GAAG,OAAO,MAAM,WAAW,GAAI,cAAc,EAAE,MAAM,EAAG;AACvE,eAAW,OAAO,MAAM;AACtB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AG,+CAAe,MAAM;AAC9C,YAAM,WAAW,+CAAe;AAChC,YAAM,oBAAoB,EAAE,GAAGA,cAAa,MAAM;AAClD,cAAQ,IAAI;AAAA,QACV;AAAA,QACA;AAAA,QACA;AAAA,QACA,cAAAA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC;AACD,cAAQ,SAAS;AAAA,IACnB;AAEA,WAAO;AAAA,EACT;AACF;",
|
|
6
6
|
"names": ["_", "themeManager", "state", "isNewTheme", "mounted"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/web",
|
|
3
|
-
"version": "1.14.
|
|
3
|
+
"version": "1.14.9",
|
|
4
4
|
"source": "src/index.ts",
|
|
5
5
|
"main": "dist/cjs",
|
|
6
6
|
"module": "dist/esm",
|
|
@@ -27,19 +27,19 @@
|
|
|
27
27
|
"reset.css"
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@tamagui/compose-refs": "1.14.
|
|
31
|
-
"@tamagui/constants": "1.14.
|
|
32
|
-
"@tamagui/helpers": "1.14.
|
|
33
|
-
"@tamagui/normalize-css-color": "1.14.
|
|
34
|
-
"@tamagui/use-did-finish-ssr": "1.14.
|
|
35
|
-
"@tamagui/use-event": "1.14.
|
|
36
|
-
"@tamagui/use-force-update": "1.14.
|
|
30
|
+
"@tamagui/compose-refs": "1.14.9",
|
|
31
|
+
"@tamagui/constants": "1.14.9",
|
|
32
|
+
"@tamagui/helpers": "1.14.9",
|
|
33
|
+
"@tamagui/normalize-css-color": "1.14.9",
|
|
34
|
+
"@tamagui/use-did-finish-ssr": "1.14.9",
|
|
35
|
+
"@tamagui/use-event": "1.14.9",
|
|
36
|
+
"@tamagui/use-force-update": "1.14.9"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
39
|
"react": "*"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@tamagui/build": "1.14.
|
|
42
|
+
"@tamagui/build": "1.14.9",
|
|
43
43
|
"@testing-library/react": "^13.4.0",
|
|
44
44
|
"csstype": "^3.0.10",
|
|
45
45
|
"react": "^18.2.0",
|
package/src/hooks/useTheme.tsx
CHANGED
|
@@ -231,18 +231,15 @@ export const useChangeThemeEffect = (
|
|
|
231
231
|
}
|
|
232
232
|
}
|
|
233
233
|
|
|
234
|
-
const disposeChangeListener =
|
|
235
|
-
(
|
|
236
|
-
|
|
237
|
-
if (
|
|
238
|
-
|
|
239
|
-
console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys })
|
|
240
|
-
}
|
|
241
|
-
setThemeState(createState)
|
|
234
|
+
const disposeChangeListener = parentManager?.onChangeTheme((name, manager) => {
|
|
235
|
+
const shouldUpdate = Boolean(keys?.length || isNewTheme)
|
|
236
|
+
if (shouldUpdate) {
|
|
237
|
+
if (process.env.NODE_ENV === 'development' && props['debug']) {
|
|
238
|
+
console.log(`onChangeTheme`, shouldUpdate, { props, name, manager, keys })
|
|
242
239
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
)
|
|
240
|
+
setThemeState(createState)
|
|
241
|
+
}
|
|
242
|
+
}, themeManager.id)
|
|
246
243
|
|
|
247
244
|
return () => {
|
|
248
245
|
disposeChangeListener?.()
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTheme.d.ts","sourceRoot":"","sources":["../../src/hooks/useTheme.tsx"],"names":[],"mappings":"AAQA,OAAO,EACL,YAAY,EAIb,MAAM,4BAA4B,CAAA;AAEnC,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAI1D,MAAM,MAAM,oBAAoB,GAAG;IACjC,UAAU,EAAE,OAAO,CAAA;IACnB,YAAY,EAAE,YAAY,GAAG,IAAI,CAAA;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,CAAC,EAAE,WAAW,GAAG,IAAI,CAAA;I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