@tamagui/core 1.0.1-rc.0 → 1.0.1-rc.0.2

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/hooks/useMedia.tsx"],
4
- "sourcesContent": ["import { useDeferredValue, useEffect, useMemo, useSyncExternalStore } from 'react'\n\nimport { getConfig } from '../config'\nimport { createProxy } from '../helpers/createProxy'\nimport { matchMedia } from '../helpers/matchMedia'\nimport {\n MediaQueries,\n MediaQueryKey,\n MediaQueryObject,\n MediaQueryState,\n TamaguiInternalConfig,\n} from '../types'\nimport { useSafeRef } from './useSafeRef'\n\nexport const mediaState: MediaQueryState =\n // development time safeguard\n process.env.NODE_ENV === 'development'\n ? createProxy(\n {},\n {\n get(target, key) {\n if (\n typeof key === 'string' &&\n key[0] === '$' &&\n // dont error on $$typeof\n key[1] !== '$'\n ) {\n throw new Error(`Access mediaState should not use \"$\": ${key}`)\n }\n return Reflect.get(target, key)\n },\n }\n )\n : ({} as any)\n\ntype MediaListener = (next: boolean) => void\n\nconst listeners: { [key: string]: Set<MediaListener> } = {}\n\nexport function addMediaQueryListener(key: MediaQueryKey, cb: MediaListener) {\n if (process.env.NODE_ENV === 'development' && key[0] === '$') {\n // eslint-disable-next-line no-console\n console.warn(`Warning, listening to media queries shouldn't use the \"$\" prefix`)\n }\n listeners[key] = listeners[key] || new Set()\n listeners[key].add(cb)\n return () => removeMediaQueryListener(key, cb)\n}\n\nexport function removeMediaQueryListener(key: MediaQueryKey, cb: MediaListener) {\n listeners[key]?.delete(cb)\n}\n\nexport const mediaQueryConfig: MediaQueries = {}\n\nexport const getMedia = () => mediaState\n\n// for SSR capture it at time of startup\nlet initState: MediaQueryState\nexport const getInitialMediaState = () => {\n return (getConfig().disableSSR ? mediaState : initState) || {}\n}\n\nlet mediaKeysOrdered: string[]\nexport const getMediaKeyImportance = (key: string) =>\n // + 2 because we set base usedKeys=1 in getSplitStyles and all media go above 1\n mediaKeysOrdered.indexOf(key[0] === '$' ? key.slice(1) : key) + 2\n\nconst dispose = new Set<Function>()\n\nexport const configureMedia = (config: TamaguiInternalConfig) => {\n const { media, mediaQueryDefaultActive } = config\n if (!media) return\n for (const key in media) {\n mediaState[key] = mediaQueryDefaultActive?.[key] || false\n }\n Object.assign(mediaQueryConfig, media)\n initState = { ...mediaState }\n updateCurrentState()\n mediaKeysOrdered = Object.keys(media)\n if (config.disableSSR) {\n setupMediaListeners()\n }\n}\n\nfunction unlisten() {\n dispose.forEach((cb) => cb())\n dispose.clear()\n}\n\n/**\n * Note: This should *not* set the state on the first render!\n * Because to avoid hydration issues SSR must match the server\n * *and then* re-render with the actual media query state.\n */\nlet configuredKey = ''\nfunction setupMediaListeners() {\n // avoid setting up more than once per config\n const nextKey = JSON.stringify(mediaQueryConfig)\n if (nextKey === configuredKey) {\n return\n }\n configuredKey = nextKey\n\n // hmr, undo existing before re-binding\n unlisten()\n\n for (const key in mediaQueryConfig) {\n const str = mediaObjectToString(mediaQueryConfig[key])\n const getMatch = () => matchMedia(str)\n const match = getMatch()\n if (!match) {\n throw new Error('\u26A0\uFE0F No match')\n }\n // react native needs these deprecated apis for now\n match.addListener(update)\n dispose.add(() => match.removeListener(update))\n\n update()\n\n function update() {\n const next = !!getMatch().matches\n if (next === mediaState[key]) return\n mediaState[key] = next\n updateCurrentState()\n const cur = listeners[key]\n if (cur?.size) {\n cur.forEach((cb) => cb(next))\n }\n }\n }\n}\n\nexport function useMediaListeners(config: TamaguiInternalConfig) {\n if (config.disableSSR) return\n useEffect(() => {\n setupMediaListeners()\n return unlisten\n }, [])\n}\n\nconst currentStateListeners = new Set<any>()\nfunction updateCurrentState() {\n currentStateListeners.forEach((cb) => cb(mediaState))\n}\nfunction subscribe(subscriber: any) {\n currentStateListeners.add(subscriber)\n return () => currentStateListeners.delete(subscriber)\n}\n\ntype MediaKeysState = {\n [key: string]: any\n}\n\nexport function useMedia(): {\n [key in MediaQueryKey]: boolean\n} {\n const keys = useSafeRef<MediaKeysState>({ prev: initState })\n const stateSync = useSyncExternalStore<MediaQueryState>(\n subscribe,\n () => {\n const { prev, ...curKeys } = keys.current\n const shouldUpdate = !prev || Object.keys(curKeys).some((k) => mediaState[k] !== prev[k])\n if (shouldUpdate) {\n const next = { ...mediaState }\n keys.current.prev = next\n return next\n }\n return prev\n },\n () => initState\n )\n const state = useDeferredValue(stateSync)\n\n return useMemo(() => {\n return new Proxy(state, {\n get(_, key: string) {\n if (!keys.current[key]) {\n keys.current[key] = true\n }\n return Reflect.get(state, key)\n },\n })\n }, [state])\n}\n\n/**\n * Useful for more complex components that need access to the currently active props,\n * accounting for the currently active media queries.\n *\n * Use sparingly, is will loop props and trigger re-render on all media queries.\n *\n * */\nexport function useMediaPropsActive<A extends Object>(\n props: A\n): {\n // remove all media\n [Key in keyof A extends `$${string}` ? never : keyof A]?: A[Key]\n} {\n const media = useMedia()\n\n return useMemo(() => {\n const next = {} as A\n const importancesUsed = {}\n const propNames = Object.keys(props)\n\n for (let i = propNames.length - 1; i >= 0; i--) {\n const key = propNames[i]\n const val = props[key]\n if (key[0] === '$') {\n if (!media[key.slice(1)]) {\n continue\n }\n if (val && typeof val === 'object') {\n const subKeys = Object.keys(val)\n for (let j = subKeys.length; j--; j >= 0) {\n const subKey = subKeys[j]\n mergeMediaByImportance(next, subKey, val[subKey], importancesUsed)\n }\n }\n } else {\n mergeMediaByImportance(next, key, val, importancesUsed)\n }\n }\n\n return next\n }, [media, props])\n}\n\nexport function mergeMediaByImportance(\n onto: Record<string, any>,\n key: string,\n value: any,\n importancesUsed: Record<string, number>\n) {\n const importance = getMediaKeyImportance(key)\n if (importancesUsed[key] > importance) {\n return false\n }\n importancesUsed[key] = importance\n onto[key] = value\n return true\n}\n\nfunction camelToHyphen(str: string) {\n return str.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`).toLowerCase()\n}\n\nexport function mediaObjectToString(query: string | MediaQueryObject) {\n if (typeof query === 'string') {\n return query\n }\n return Object.entries(query)\n .map(([feature, value]) => {\n feature = camelToHyphen(feature)\n if (typeof value === 'string') {\n return `(${feature}: ${value})`\n }\n if (typeof value === 'number' && /[height|width]$/.test(feature)) {\n value = `${value}px`\n }\n return `(${feature}: ${value})`\n })\n .join(' and ')\n}\n"],
4
+ "sourcesContent": ["import { useDeferredValue, useEffect, useMemo, useSyncExternalStore } from 'react'\n\nimport { getConfig } from '../config'\nimport { createProxy } from '../helpers/createProxy'\nimport { matchMedia } from '../helpers/matchMedia'\nimport {\n MediaQueries,\n MediaQueryKey,\n MediaQueryObject,\n MediaQueryState,\n TamaguiInternalConfig,\n} from '../types'\nimport { useSafeRef } from './useSafeRef'\n\nexport const mediaState: MediaQueryState =\n // development only safeguard\n process.env.NODE_ENV === 'development'\n ? createProxy(\n {},\n {\n get(target, key) {\n if (\n typeof key === 'string' &&\n key[0] === '$' &&\n // dont error on $$typeof\n key[1] !== '$'\n ) {\n throw new Error(`Access mediaState should not use \"$\": ${key}`)\n }\n return Reflect.get(target, key)\n },\n }\n )\n : ({} as any)\n\ntype MediaListener = (next: boolean) => void\n\nconst listeners: { [key: string]: Set<MediaListener> } = {}\n\nexport function addMediaQueryListener(key: MediaQueryKey, cb: MediaListener) {\n if (process.env.NODE_ENV === 'development' && key[0] === '$') {\n // eslint-disable-next-line no-console\n console.warn(`Warning, listening to media queries shouldn't use the \"$\" prefix`)\n }\n listeners[key] = listeners[key] || new Set()\n listeners[key].add(cb)\n return () => removeMediaQueryListener(key, cb)\n}\n\nexport function removeMediaQueryListener(key: MediaQueryKey, cb: MediaListener) {\n listeners[key]?.delete(cb)\n}\n\nexport const mediaQueryConfig: MediaQueries = {}\n\nexport const getMedia = () => mediaState\n\n// for SSR capture it at time of startup\nlet initState: MediaQueryState\nexport const getInitialMediaState = () => {\n return (getConfig().disableSSR ? mediaState : initState) || {}\n}\n\nlet mediaKeysOrdered: string[]\nexport const getMediaKeyImportance = (key: string) =>\n // + 2 because we set base usedKeys=1 in getSplitStyles and all media go above 1\n mediaKeysOrdered.indexOf(key[0] === '$' ? key.slice(1) : key) + 2\n\nconst dispose = new Set<Function>()\n\nexport const configureMedia = (config: TamaguiInternalConfig) => {\n const { media, mediaQueryDefaultActive } = config\n if (!media) return\n for (const key in media) {\n mediaState[key] = mediaQueryDefaultActive?.[key] || false\n }\n Object.assign(mediaQueryConfig, media)\n initState = { ...mediaState }\n updateCurrentState()\n mediaKeysOrdered = Object.keys(media)\n if (config.disableSSR) {\n setupMediaListeners()\n }\n}\n\nfunction unlisten() {\n dispose.forEach((cb) => cb())\n dispose.clear()\n}\n\n/**\n * Note: This should *not* set the state on the first render!\n * Because to avoid hydration issues SSR must match the server\n * *and then* re-render with the actual media query state.\n */\nlet configuredKey = ''\nfunction setupMediaListeners() {\n // avoid setting up more than once per config\n const nextKey = JSON.stringify(mediaQueryConfig)\n if (nextKey === configuredKey) {\n return\n }\n configuredKey = nextKey\n\n // hmr, undo existing before re-binding\n unlisten()\n\n for (const key in mediaQueryConfig) {\n const str = mediaObjectToString(mediaQueryConfig[key])\n const getMatch = () => matchMedia(str)\n const match = getMatch()\n if (!match) {\n throw new Error('\u26A0\uFE0F No match')\n }\n // react native needs these deprecated apis for now\n match.addListener(update)\n dispose.add(() => match.removeListener(update))\n\n update()\n\n function update() {\n const next = !!getMatch().matches\n if (next === mediaState[key]) return\n mediaState[key] = next\n updateCurrentState()\n const cur = listeners[key]\n if (cur?.size) {\n cur.forEach((cb) => cb(next))\n }\n }\n }\n}\n\nexport function useMediaListeners(config: TamaguiInternalConfig) {\n if (config.disableSSR) return\n useEffect(() => {\n setupMediaListeners()\n return unlisten\n }, [])\n}\n\nconst currentStateListeners = new Set<any>()\nfunction updateCurrentState() {\n currentStateListeners.forEach((cb) => cb(mediaState))\n}\nfunction subscribe(subscriber: any) {\n currentStateListeners.add(subscriber)\n return () => currentStateListeners.delete(subscriber)\n}\n\ntype MediaKeysState = {\n [key: string]: any\n}\n\nexport function useMedia(): {\n [key in MediaQueryKey]: boolean\n} {\n const keys = useSafeRef<MediaKeysState>({ prev: initState })\n const stateSync = useSyncExternalStore<MediaQueryState>(\n subscribe,\n () => {\n const { prev, ...curKeys } = keys.current\n const shouldUpdate = !prev || Object.keys(curKeys).some((k) => mediaState[k] !== prev[k])\n if (shouldUpdate) {\n const next = { ...mediaState }\n keys.current.prev = next\n return next\n }\n return prev\n },\n () => initState\n )\n const state = useDeferredValue(stateSync)\n\n return useMemo(() => {\n return new Proxy(state, {\n get(_, key: string) {\n if (!keys.current[key]) {\n keys.current[key] = true\n }\n return Reflect.get(state, key)\n },\n })\n }, [state])\n}\n\n/**\n * Useful for more complex components that need access to the currently active props,\n * accounting for the currently active media queries.\n *\n * Use sparingly, is will loop props and trigger re-render on all media queries.\n *\n * */\nexport function useMediaPropsActive<A extends Object>(\n props: A\n): {\n // remove all media\n [Key in keyof A extends `$${string}` ? never : keyof A]?: A[Key]\n} {\n const media = useMedia()\n\n return useMemo(() => {\n const next = {} as A\n const importancesUsed = {}\n const propNames = Object.keys(props)\n\n for (let i = propNames.length - 1; i >= 0; i--) {\n const key = propNames[i]\n const val = props[key]\n if (key[0] === '$') {\n if (!media[key.slice(1)]) {\n continue\n }\n if (val && typeof val === 'object') {\n const subKeys = Object.keys(val)\n for (let j = subKeys.length; j--; j >= 0) {\n const subKey = subKeys[j]\n mergeMediaByImportance(next, subKey, val[subKey], importancesUsed)\n }\n }\n } else {\n mergeMediaByImportance(next, key, val, importancesUsed)\n }\n }\n\n return next\n }, [media, props])\n}\n\nexport function mergeMediaByImportance(\n onto: Record<string, any>,\n key: string,\n value: any,\n importancesUsed: Record<string, number>\n) {\n const importance = getMediaKeyImportance(key)\n if (importancesUsed[key] > importance) {\n return false\n }\n importancesUsed[key] = importance\n onto[key] = value\n return true\n}\n\nfunction camelToHyphen(str: string) {\n return str.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`).toLowerCase()\n}\n\nexport function mediaObjectToString(query: string | MediaQueryObject) {\n if (typeof query === 'string') {\n return query\n }\n return Object.entries(query)\n .map(([feature, value]) => {\n feature = camelToHyphen(feature)\n if (typeof value === 'string') {\n return `(${feature}: ${value})`\n }\n if (typeof value === 'number' && /[height|width]$/.test(feature)) {\n value = `${value}px`\n }\n return `(${feature}: ${value})`\n })\n .join(' and ')\n}\n"],
5
5
  "mappings": "AAAA,SAAS,kBAAkB,WAAW,SAAS,4BAA4B;AAE3E,SAAS,iBAAiB;AAC1B,SAAS,mBAAmB;AAC5B,SAAS,kBAAkB;AAQ3B,SAAS,kBAAkB;AAEpB,MAAM,aAEX,QAAQ,IAAI,aAAa,gBACrB;AAAA,EACE,CAAC;AAAA,EACD;AAAA,IACE,IAAI,QAAQ,KAAK;AACf,UACE,OAAO,QAAQ,YACf,IAAI,OAAO,OAEX,IAAI,OAAO,KACX;AACA,cAAM,IAAI,MAAM,yCAAyC,KAAK;AAAA,MAChE;AACA,aAAO,QAAQ,IAAI,QAAQ,GAAG;AAAA,IAChC;AAAA,EACF;AACF,IACC,CAAC;AAIR,MAAM,YAAmD,CAAC;AAEnD,SAAS,sBAAsB,KAAoB,IAAmB;AAC3E,MAAI,QAAQ,IAAI,aAAa,iBAAiB,IAAI,OAAO,KAAK;AAE5D,YAAQ,KAAK,kEAAkE;AAAA,EACjF;AACA,YAAU,OAAO,UAAU,QAAQ,oBAAI,IAAI;AAC3C,YAAU,KAAK,IAAI,EAAE;AACrB,SAAO,MAAM,yBAAyB,KAAK,EAAE;AAC/C;AAEO,SAAS,yBAAyB,KAAoB,IAAmB;AAjDhF;AAkDE,kBAAU,SAAV,mBAAgB,OAAO;AACzB;AAEO,MAAM,mBAAiC,CAAC;AAExC,MAAM,WAAW,MAAM;AAG9B,IAAI;AACG,MAAM,uBAAuB,MAAM;AACxC,UAAQ,UAAU,EAAE,aAAa,aAAa,cAAc,CAAC;AAC/D;AAEA,IAAI;AACG,MAAM,wBAAwB,CAAC,QAEpC,iBAAiB,QAAQ,IAAI,OA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
7
  }
@@ -1,5 +1,5 @@
1
1
  import { isClient, isRSC, isServer, isWeb } from "@tamagui/constants";
2
- import { useContext, useLayoutEffect, useMemo, useState } from "react";
2
+ import { useContext, useEffect, useMemo, useState } from "react";
3
3
  import { getConfig } from "../config";
4
4
  import { isDevTools } from "../constants/isDevTools";
5
5
  import { createProxy } from "../helpers/createProxy";
@@ -119,7 +119,7 @@ const useChangeThemeEffect = (props, root = false) => {
119
119
  const [themeState, setThemeState] = useState(createState);
120
120
  const { isNewTheme, state, themeManager, mounted } = themeState;
121
121
  if (!isServer) {
122
- useLayoutEffect(() => {
122
+ useEffect(() => {
123
123
  if (disable)
124
124
  return;
125
125
  if (props.inverse && !mounted) {
@@ -144,15 +144,16 @@ const useChangeThemeEffect = (props, root = false) => {
144
144
  };
145
145
  }, [disable, state, isNewTheme, debug]);
146
146
  }
147
- if (process.env.NODE_ENV === "development" && debug)
148
- console.log("useTheme.render isNewTheme", isNewTheme, props, themeManager.state.name, parentManager == null ? void 0 : parentManager.state.name);
149
- if (hasNoThemeUpdatingProps(props) || !mounted && parentManager) {
147
+ const isInversingOnMount = Boolean(!themeState.mounted && props.inverse);
148
+ const shouldReturnParentState = hasNoThemeUpdatingProps(props) || isInversingOnMount;
149
+ if (shouldReturnParentState) {
150
150
  if (!parentManager)
151
151
  throw `impossible`;
152
152
  if (process.env.NODE_ENV === "development" && props.debug === "verbose")
153
- console.log("useTheme hasNoThemeUpdatingProps", parentManager.state);
153
+ console.log("useTheme hasNoThemeUpdatingProps", parentManager.state.name, "isInversingOnMount", isInversingOnMount);
154
154
  return {
155
155
  ...parentManager.state,
156
+ className: isInversingOnMount ? "" : parentManager.state.className,
156
157
  themeManager: parentManager,
157
158
  isNewTheme: false
158
159
  };
@@ -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, useLayoutEffect, useMemo, useState } from 'react'\n\nimport { getConfig } from '../config'\nimport { isDevTools } from '../constants/isDevTools'\nimport { createProxy } from '../helpers/createProxy'\nimport { ThemeManager, ThemeManagerState, hasNoThemeUpdatingProps } from '../helpers/ThemeManager'\nimport { ThemeManagerContext } from '../helpers/ThemeManagerContext'\nimport { ThemeParsed, ThemeProps } from '../types'\nimport { GetThemeUnwrapped } from './getThemeUnwrapped'\n\nexport type ChangedThemeResponse = {\n themeManager: ThemeManager | null\n name: string\n isNewTheme?: boolean\n theme?: ThemeParsed | null\n className?: string\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 if (isRSC) {\n return getDefaultThemeProxied()\n }\n\n const { name, theme, themeManager, className, isNewTheme } = useChangeThemeEffect(props)\n\n if (process.env.NODE_ENV === 'development') {\n // ensure we aren't creating too many ThemeManagers\n if (isWeb && isNewTheme && className === themeManager?.parentManager?.state.className) {\n console.error(`Should always change, duplicating ThemeMananger bug`, themeManager)\n // eslint-disable-next-line no-debugger\n debugger\n }\n if (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\n if (!theme) {\n if (process.env.NODE_ENV === 'development') {\n console.warn('No theme found', name, props, themeManager)\n }\n return getDefaultThemeProxied()\n }\n\n return useMemo(() => {\n return getThemeProxied({\n isNewTheme,\n theme,\n name,\n className,\n themeManager,\n })\n }, [theme, isNewTheme, name, className, themeManager])\n}\n\nfunction getThemeProxied({\n theme,\n name,\n className,\n themeManager,\n isNewTheme,\n}: {\n theme: any\n name: string\n className?: string\n themeManager?: ThemeManager | null\n isNewTheme?: boolean\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 } else if (key === GetThemeManager) {\n return themeManager\n } else if (key === GetIsNewTheme) {\n return isNewTheme\n } else if (key === 'name') {\n return name\n } else if (key === 'className') {\n return className\n } else if (!name || key === '__proto__' || typeof key === 'symbol' || key === '$typeof') {\n return Reflect.get(_, key)\n }\n // auto convert variables to plain\n if (key[0] === '$') key = key.slice(1)\n if (!themeManager) {\n return theme[key]\n }\n return themeManager.getValue(key)\n },\n })\n}\n\nconst GetThemeManager = Symbol()\nconst GetIsNewTheme = Symbol()\n\nexport const getThemeManager = (theme: any): ThemeManager | undefined => theme?.[GetThemeManager]\nexport const getThemeIsNewTheme = (theme: any): ThemeManager | undefined => theme?.[GetIsNewTheme]\n\nexport const activeThemeManagers = new Set<ThemeManager>()\n\nexport const useChangeThemeEffect = (props: ThemeProps, root = false): ChangedThemeResponse => {\n if (isRSC) {\n // we need context working for this to work well\n return {\n ...createState().state,\n themeManager: null,\n }\n }\n const parentManager = 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 new Error(`Disable topmost`)\n return {\n ...parentManager.state,\n isNewTheme: false,\n themeManager: parentManager,\n }\n }\n\n type State = {\n isNewTheme: boolean\n state: ThemeManagerState\n themeManager: ThemeManager\n mounted?: boolean\n }\n\n const [themeState, setThemeState] = useState<State>(createState)\n const { isNewTheme, state, themeManager, mounted } = themeState\n\n if (!isServer) {\n useLayoutEffect(() => {\n if (disable) return\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 activeThemeManagers.add(themeManager)\n const disposeChangeListener = parentManager?.onChangeTheme(() => {\n if (!isNewTheme && !isWeb) {\n setThemeState({\n ...themeState,\n state: { ...themeManager.state },\n isNewTheme: false,\n })\n } else {\n updateState()\n }\n })\n return () => {\n activeThemeManagers.delete(themeManager)\n disposeChangeListener?.()\n }\n }, [disable, state, isNewTheme, debug])\n }\n\n // prettier-ignore\n if (process.env.NODE_ENV === 'development' && debug) console.log('useTheme.render isNewTheme', isNewTheme, props, themeManager.state.name, parentManager?.state.name)\n\n // this seems unnecessary\n if (hasNoThemeUpdatingProps(props) || (!mounted && parentManager)) {\n if (!parentManager) throw `impossible`\n // prettier-ignore\n if (process.env.NODE_ENV === 'development' && props.debug === 'verbose') console.log('useTheme hasNoThemeUpdatingProps', parentManager.state)\n return {\n ...parentManager.state,\n themeManager: parentManager,\n isNewTheme: false,\n }\n }\n\n // run inline in render\n updateState()\n\n return {\n ...state,\n isNewTheme,\n themeManager,\n }\n\n function createState(prev?: State) {\n const _ = new ThemeManager(props, root ? 'root' : parentManager)\n const isNewTheme = _ !== parentManager\n // prettier-ignore\n if (process.env.NODE_ENV === 'development' && debug) [console.groupCollapsed('useTheme create() isNewTheme', isNewTheme), console.log('parent.state.name', parentManager?.state.name, '\\n', 'props', props, '\\n', isClient ? parentManager : '', 'getState', isNewTheme && isClient ? _.getState(props) : '', '\\n', 'state', isClient ? { ..._.state } : ''), console.groupEnd()]\n // only inverse relies on this for ssr\n const mounted = !props.inverse ? true : root || prev?.mounted || false\n return {\n // ThemeManager returns parentManager if no change\n isNewTheme,\n state: { ..._.state },\n themeManager: _,\n mounted,\n }\n }\n\n function updateState() {\n const next = themeManager.getState(props, parentManager)\n const shouldChange = themeManager.getStateShouldChange(next, isNewTheme ? state : null)\n if (shouldChange) {\n setThemeState(createState)\n } else {\n if (!next && parentManager?.state.name !== state.name) {\n // were changing back to parent state\n setThemeState(createState)\n }\n }\n }\n}\n"],
5
- "mappings": "AACA,SAAS,UAAU,OAAO,UAAU,aAAa;AACjD,SAAS,YAAY,iBAAi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cAAc;AAAA,MACjB,cAAc;AAAA,MACd,YAAY;AAAA,IACd;AAAA,EACF;AAGA,cAAY;AAEZ,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,IACA;AAAA,EACF;AAEA,WAAS,YAAY,MAAc;AACjC,UAAM,IAAI,IAAI,aAAa,OAAO,OAAO,SAAS,aAAa;AAC/D,UAAMA,cAAa,MAAM;AAEzB,QAAI,QAAQ,IAAI,aAAa,iBAAiB;AAAO,OAAC,QAAQ,eAAe,gCAAgCA,WAAU,GAAG,QAAQ,IAAI,qBAAqB,+CAAe,MAAM,MAAM,MAAM,SAAS,OAAO,MAAM,WAAW,gBAAgB,IAAI,YAAYA,eAAc,WAAW,EAAE,SAAS,KAAK,IAAI,IAAI,MAAM,SAAS,WAAW,EAAE,GAAG,EAAE,MAAM,IAAI,EAAE,GAAG,QAAQ,SAAS,CAAC;AAEhX,UAAMC,WAAU,CAAC,MAAM,UAAU,OAAO,SAAQ,6BAAM,YAAW;AACjE,WAAO;AAAA,MAEL,YAAAD;AAAA,MACA,OAAO,EAAE,GAAG,EAAE,MAAM;AAAA,MACpB,cAAc;AAAA,MACd,SAAAC;AAAA,IACF;AAAA,EACF;AAEA,WAAS,cAAc;AACrB,UAAM,OAAO,aAAa,SAAS,OAAO,aAAa;AACvD,UAAM,eAAe,aAAa,qBAAqB,MAAM,aAAa,QAAQ,IAAI;AACtF,QAAI,cAAc;AAChB,oBAAc,WAAW;AAAA,IAC3B,OAAO;AACL,UAAI,CAAC,SAAQ,+CAAe,MAAM,UAAS,MAAM,MAAM;AAErD,sBAAc,WAAW;AAAA,MAC3B;AAAA,IACF;AAAA,EACF;AACF;",
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'\nimport { isDevTools } from '../constants/isDevTools'\nimport { createProxy } from '../helpers/createProxy'\nimport { ThemeManager, ThemeManagerState, hasNoThemeUpdatingProps } from '../helpers/ThemeManager'\nimport { ThemeManagerContext } from '../helpers/ThemeManagerContext'\nimport { ThemeParsed, ThemeProps } from '../types'\nimport { GetThemeUnwrapped } from './getThemeUnwrapped'\n\nexport type ChangedThemeResponse = {\n themeManager: ThemeManager | null\n name: string\n isNewTheme?: boolean\n theme?: ThemeParsed | null\n className?: string\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 if (isRSC) {\n return getDefaultThemeProxied()\n }\n\n const { name, theme, themeManager, className, isNewTheme } = useChangeThemeEffect(props)\n\n if (process.env.NODE_ENV === 'development') {\n // ensure we aren't creating too many ThemeManagers\n if (isWeb && isNewTheme && className === themeManager?.parentManager?.state.className) {\n console.error(`Should always change, duplicating ThemeMananger bug`, themeManager)\n // eslint-disable-next-line no-debugger\n debugger\n }\n if (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\n if (!theme) {\n if (process.env.NODE_ENV === 'development') {\n console.warn('No theme found', name, props, themeManager)\n }\n return getDefaultThemeProxied()\n }\n\n return useMemo(() => {\n return getThemeProxied({\n isNewTheme,\n theme,\n name,\n className,\n themeManager,\n })\n }, [theme, isNewTheme, name, className, themeManager])\n}\n\nfunction getThemeProxied({\n theme,\n name,\n className,\n themeManager,\n isNewTheme,\n}: {\n theme: any\n name: string\n className?: string\n themeManager?: ThemeManager | null\n isNewTheme?: boolean\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 } else if (key === GetThemeManager) {\n return themeManager\n } else if (key === GetIsNewTheme) {\n return isNewTheme\n } else if (key === 'name') {\n return name\n } else if (key === 'className') {\n return className\n } else if (!name || key === '__proto__' || typeof key === 'symbol' || key === '$typeof') {\n return Reflect.get(_, key)\n }\n // auto convert variables to plain\n if (key[0] === '$') key = key.slice(1)\n if (!themeManager) {\n return theme[key]\n }\n return themeManager.getValue(key)\n },\n })\n}\n\nconst GetThemeManager = Symbol()\nconst GetIsNewTheme = Symbol()\n\nexport const getThemeManager = (theme: any): ThemeManager | undefined => theme?.[GetThemeManager]\nexport const getThemeIsNewTheme = (theme: any): ThemeManager | undefined => theme?.[GetIsNewTheme]\n\nexport const activeThemeManagers = new Set<ThemeManager>()\n\nexport const useChangeThemeEffect = (props: ThemeProps, root = false): ChangedThemeResponse => {\n if (isRSC) {\n // we need context working for this to work well\n return {\n ...createState().state,\n themeManager: null,\n }\n }\n const parentManager = 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 new Error(`Disable topmost`)\n return {\n ...parentManager.state,\n isNewTheme: false,\n themeManager: parentManager,\n }\n }\n\n type State = {\n isNewTheme: boolean\n state: ThemeManagerState\n themeManager: ThemeManager\n mounted?: boolean\n }\n\n const [themeState, setThemeState] = useState<State>(createState)\n const { isNewTheme, state, themeManager, mounted } = themeState\n\n if (!isServer) {\n useEffect(() => {\n if (disable) return\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 activeThemeManagers.add(themeManager)\n const disposeChangeListener = parentManager?.onChangeTheme(() => {\n if (!isNewTheme && !isWeb) {\n setThemeState({\n ...themeState,\n state: { ...themeManager.state },\n isNewTheme: false,\n })\n } else {\n updateState()\n }\n })\n return () => {\n activeThemeManagers.delete(themeManager)\n disposeChangeListener?.()\n }\n }, [disable, state, isNewTheme, debug])\n }\n\n const isInversingOnMount = Boolean(!themeState.mounted && props.inverse)\n const shouldReturnParentState = hasNoThemeUpdatingProps(props) || isInversingOnMount\n\n if (shouldReturnParentState) {\n if (!parentManager) throw `impossible`\n // prettier-ignore\n if (process.env.NODE_ENV === 'development' && props.debug === 'verbose') console.log('useTheme hasNoThemeUpdatingProps', parentManager.state.name, 'isInversingOnMount', isInversingOnMount)\n return {\n ...parentManager.state,\n className: isInversingOnMount ? '' : parentManager.state.className,\n themeManager: parentManager,\n isNewTheme: false,\n }\n }\n\n // run inline in render\n updateState()\n\n return {\n ...state,\n isNewTheme,\n themeManager,\n }\n\n function createState(prev?: State) {\n const _ = new ThemeManager(props, root ? 'root' : parentManager)\n const isNewTheme = _ !== parentManager\n // prettier-ignore\n if (process.env.NODE_ENV === 'development' && debug) [console.groupCollapsed('useTheme create() isNewTheme', isNewTheme), console.log('parent.state.name', parentManager?.state.name, '\\n', 'props', props, '\\n', isClient ? parentManager : '', 'getState', isNewTheme && isClient ? _.getState(props) : '', '\\n', 'state', isClient ? { ..._.state } : ''), console.groupEnd()]\n // only inverse relies on this for ssr\n const mounted = !props.inverse ? true : root || prev?.mounted || false\n return {\n // ThemeManager returns parentManager if no change\n isNewTheme,\n state: { ..._.state },\n themeManager: _,\n mounted,\n }\n }\n\n function updateState() {\n const next = themeManager.getState(props, parentManager)\n const shouldChange = themeManager.getStateShouldChange(next, isNewTheme ? state : null)\n if (shouldChange) {\n setThemeState(createState)\n } else {\n if (!next && parentManager?.state.name !== state.name) {\n // were changing back to parent state\n setThemeState(createState)\n }\n }\n }\n}\n"],
5
+ "mappings": "AACA,SAAS,UAAU,OAAO,UAAU,aAAa;AACjD,SAAS,YAAY,WAA4B,SAAS,gBAAgB;AAE1E,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAC3B,SAAS,mBAAmB;AAC5B,SAAS,cAAiC,+BAA+B;AACzE,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G;AAAA,IACH;AAAA,IACA;AAAA,EACF;AAEA,WAAS,YAAY,MAAc;AACjC,UAAM,IAAI,IAAI,aAAa,OAAO,OAAO,SAAS,aAAa;AAC/D,UAAMA,cAAa,MAAM;AAEzB,QAAI,QAAQ,IAAI,aAAa,iBAAiB;AAAO,OAAC,QAAQ,eAAe,gCAAgCA,WAAU,GAAG,QAAQ,IAAI,qBAAqB,+CAAe,MAAM,MAAM,MAAM,SAAS,OAAO,MAAM,WAAW,gBAAgB,IAAI,YAAYA,eAAc,WAAW,EAAE,SAAS,KAAK,IAAI,IAAI,MAAM,SAAS,WAAW,EAAE,GAAG,EAAE,MAAM,IAAI,EAAE,GAAG,QAAQ,SAAS,CAAC;AAEhX,UAAMC,WAAU,CAAC,MAAM,UAAU,OAAO,SAAQ,6BAAM,YAAW;AACjE,WAAO;AAAA,MAEL,YAAAD;AAAA,MACA,OAAO,EAAE,GAAG,EAAE,MAAM;AAAA,MACpB,cAAc;AAAA,MACd,SAAAC;AAAA,IACF;AAAA,EACF;AAEA,WAAS,cAAc;AACrB,UAAM,OAAO,aAAa,SAAS,OAAO,aAAa;AACvD,UAAM,eAAe,aAAa,qBAAqB,MAAM,aAAa,QAAQ,IAAI;AACtF,QAAI,cAAc;AAChB,oBAAc,WAAW;AAAA,IAC3B,OAAO;AACL,UAAI,CAAC,SAAQ,+CAAe,MAAM,UAAS,MAAM,MAAM;AAErD,sBAAc,WAAW;AAAA,MAC3B;AAAA,IACF;AAAA,EACF;AACF;",
6
6
  "names": ["isNewTheme", "mounted"]
7
7
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/views/Theme.tsx"],
4
- "sourcesContent": ["import { isWeb } from '@tamagui/constants'\nimport React, { useMemo } from 'react'\n\nimport { variableToString } from '../createVariable'\nimport { ThemeManager } from '../helpers/ThemeManager'\nimport { ThemeManagerContext } from '../helpers/ThemeManagerContext'\nimport { useChangeThemeEffect } from '../hooks/useTheme'\nimport { ThemeProps } from '../types'\n\nexport function Theme(props: ThemeProps) {\n // @ts-expect-error its a permanent prop meant for internal views\n if (props.disable) {\n return props.children\n }\n\n const isRoot = !!props['_isRoot']\n const { name, theme, themeManager, isNewTheme, className } = useChangeThemeEffect(props, isRoot)\n\n const disableThemeClass = props.disableThemeClass\n const missingTheme = !name || !theme\n\n // memo here, changing theme without re-rendering all children is a critical optimization\n // may require some effort of end user to memoize but without this memo they'd have no option\n let contents = useMemo(() => {\n const next = props['data-themeable']\n ? React.cloneElement(props.children, { ['data-themeable']: true })\n : props.children\n\n return missingTheme ? next : wrapThemeManagerContext(next, themeManager)\n }, [missingTheme, props.children, themeManager])\n\n if (process.env.NODE_ENV === 'development' && missingTheme) {\n // eslint-disable-next-line no-console\n name && !theme && console.warn(`No theme found by name ${name}`)\n return props.children\n }\n\n if (disableThemeClass || missingTheme) {\n return contents\n }\n\n if (isWeb) {\n const cn = [props.className, className, '_dsp_contents'].filter(Boolean).join(' ')\n contents = (\n <span\n className={cn}\n style={{\n // in order to provide currentColor, set color by default\n color: variableToString(theme?.color),\n }}\n >\n {contents}\n </span>\n )\n }\n\n return contents\n}\n\nexport function wrapThemeManagerContext(\n children: any,\n themeManager?: ThemeManager | null,\n shouldReset?: boolean\n) {\n // be sure to memoize themeManager to avoid reparenting\n if (!themeManager) {\n return children\n }\n // be sure to memoize shouldReset to avoid reparenting\n let next = children\n // TODO likely not necessary if we do reset logic now in useTheme?\n // reset to parent theme\n if (shouldReset && themeManager) {\n next = <Theme name={themeManager.state.parentName}>{next}</Theme>\n }\n return <ThemeManagerContext.Provider value={themeManager}>{next}</ThemeManagerContext.Provider>\n}\n"],
4
+ "sourcesContent": ["import { isWeb } from '@tamagui/constants'\nimport React, { useMemo } from 'react'\n\nimport { variableToString } from '../createVariable'\nimport { ThemeManager } from '../helpers/ThemeManager'\nimport { ThemeManagerContext } from '../helpers/ThemeManagerContext'\nimport { useChangeThemeEffect } from '../hooks/useTheme'\nimport { ThemeProps } from '../types'\n\nexport function Theme(props: ThemeProps) {\n // @ts-expect-error only for internal views\n if (props.disable) {\n return props.children\n }\n\n const isRoot = !!props['_isRoot']\n const { name, theme, themeManager, isNewTheme, className } = useChangeThemeEffect(props, isRoot)\n\n const disableThemeClass = props.disableThemeClass\n const missingTheme = !name || !theme\n\n // memo here, changing theme without re-rendering all children is a critical optimization\n // may require some effort of end user to memoize but without this memo they'd have no option\n let contents = useMemo(() => {\n const next = props['data-themeable']\n ? React.cloneElement(props.children, { ['data-themeable']: true })\n : props.children\n\n return missingTheme ? next : wrapThemeManagerContext(next, themeManager)\n }, [missingTheme, props.children, themeManager])\n\n if (process.env.NODE_ENV === 'development' && missingTheme) {\n // eslint-disable-next-line no-console\n name && !theme && console.warn(`No theme found by name ${name}`)\n return props.children\n }\n\n if (disableThemeClass || missingTheme) {\n return contents\n }\n\n if (isWeb) {\n const cn = [props.className, className, '_dsp_contents'].filter(Boolean).join(' ')\n contents = (\n <span\n className={cn}\n style={{\n // in order to provide currentColor, set color by default\n color: variableToString(theme?.color),\n }}\n >\n {contents}\n </span>\n )\n }\n\n return contents\n}\n\nexport function wrapThemeManagerContext(\n children: any,\n themeManager?: ThemeManager | null,\n shouldReset?: boolean\n) {\n // be sure to memoize themeManager to avoid reparenting\n if (!themeManager) {\n return children\n }\n // be sure to memoize shouldReset to avoid reparenting\n let next = children\n // TODO likely not necessary if we do reset logic now in useTheme?\n // reset to parent theme\n if (shouldReset && themeManager) {\n next = <Theme name={themeManager.state.parentName}>{next}</Theme>\n }\n return <ThemeManagerContext.Provider value={themeManager}>{next}</ThemeManagerContext.Provider>\n}\n"],
5
5
  "mappings": "AA4CM;AA5CN,SAAS,aAAa;AACtB,OAAO,SAAS,eAAe;AAE/B,SAAS,wBAAwB;AAEjC,SAAS,2BAA2B;AACpC,SAAS,4BAA4B;AAG9B,SAAS,MAAM,OAAmB;AAEvC,MAAI,MAAM,SAAS;AACjB,WAAO,MAAM;AAAA,EACf;AAEA,QAAM,SAAS,CAAC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1.0.1-rc.0.2",
4
4
  "source": "src/index.ts",
5
5
  "main": "dist/cjs",
6
6
  "module": "dist/esm",
@@ -28,15 +28,15 @@
28
28
  ],
29
29
  "dependencies": {
30
30
  "@expo/match-media": "^0.3.0",
31
- "@tamagui/compose-refs": "^1.0.1-rc.0",
32
- "@tamagui/constants": "^1.0.1-rc.0",
33
- "@tamagui/helpers": "^1.0.1-rc.0",
34
- "@tamagui/react-native-use-pressable": "^1.0.1-rc.0",
35
- "@tamagui/react-native-use-responder-events": "^1.0.1-rc.0",
36
- "@tamagui/types-react-native": "^1.0.1-rc.0",
37
- "@tamagui/use-did-finish-ssr": "^1.0.1-rc.0",
38
- "@tamagui/use-event": "^1.0.1-rc.0",
39
- "@tamagui/use-force-update": "^1.0.1-rc.0",
31
+ "@tamagui/compose-refs": "^1.0.1-rc.0.2",
32
+ "@tamagui/constants": "^1.0.1-rc.0.2",
33
+ "@tamagui/helpers": "^1.0.1-rc.0.2",
34
+ "@tamagui/react-native-use-pressable": "^1.0.1-rc.0.2",
35
+ "@tamagui/react-native-use-responder-events": "^1.0.1-rc.0.2",
36
+ "@tamagui/types-react-native": "^1.0.1-rc.0.2",
37
+ "@tamagui/use-did-finish-ssr": "^1.0.1-rc.0.2",
38
+ "@tamagui/use-event": "^1.0.1-rc.0.2",
39
+ "@tamagui/use-force-update": "^1.0.1-rc.0.2",
40
40
  "normalize-css-color": "^1.0.2"
41
41
  },
42
42
  "peerDependencies": {
@@ -44,7 +44,7 @@
44
44
  "react-dom": ">=18"
45
45
  },
46
46
  "devDependencies": {
47
- "@tamagui/build": "^1.0.1-rc.0",
47
+ "@tamagui/build": "^1.0.1-rc.0.2",
48
48
  "@types/node": "^16.11.9",
49
49
  "@types/react": "^18.0.15",
50
50
  "@types/react-native": "^0.69.2",
@@ -2,13 +2,11 @@ import { useComposedRefs } from '@tamagui/compose-refs'
2
2
  import { isClient, isRSC, isServer, isWeb, useIsomorphicLayoutEffect } from '@tamagui/constants'
3
3
  import {
4
4
  composeEventHandlers,
5
- stylePropsTransform,
6
5
  stylePropsView,
7
6
  validPseudoKeys,
8
7
  validStyles,
9
8
  } from '@tamagui/helpers'
10
9
  import { useResponderEvents } from '@tamagui/react-native-use-responder-events'
11
- import type { ViewStyle } from '@tamagui/types-react-native'
12
10
  import React, {
13
11
  Children,
14
12
  Fragment,
@@ -16,6 +14,7 @@ import React, {
16
14
  createElement,
17
15
  forwardRef,
18
16
  memo,
17
+ startTransition,
19
18
  useCallback,
20
19
  useContext,
21
20
  useEffect,
@@ -28,10 +27,9 @@ import { FontLanguageContext } from './contexts/FontLanguageContext'
28
27
  import { TextAncestorContext } from './contexts/TextAncestorContext'
29
28
  import { extendStaticConfig, parseStaticConfig } from './helpers/extendStaticConfig'
30
29
  import { getRect } from './helpers/getRect'
31
- import { getSubStyle, useSplitStyles } from './helpers/getSplitStyles'
30
+ import { useSplitStyles } from './helpers/getSplitStyles'
32
31
  import { getAllSelectors } from './helpers/insertStyleRule'
33
32
  import { mergeProps } from './helpers/mergeProps'
34
- import { mergeTransform } from './helpers/mergeTransform'
35
33
  import { proxyThemeVariables } from './helpers/proxyThemeVariables'
36
34
  import { useShallowSetState } from './helpers/useShallowSetState'
37
35
  import { measureLayout, useElementLayout } from './hooks/useElementLayout'
@@ -248,15 +246,11 @@ export function createComponent<
248
246
  const shouldSetMounted = needsMount && state.unmounted
249
247
  const setMounted = shouldSetMounted
250
248
  ? () => {
251
- // for some reason without some small delay it doesn't animate css
252
- setTimeout(
253
- () => {
254
- setStateShallow({
255
- unmounted: false,
256
- })
257
- },
258
- isWeb ? 10 : 0
259
- )
249
+ startTransition(() => {
250
+ setStateShallow({
251
+ unmounted: false,
252
+ })
253
+ })
260
254
  }
261
255
  : undefined
262
256
 
@@ -2,6 +2,7 @@ import { THEME_CLASSNAME_PREFIX } from '../constants/constants'
2
2
  import { Variable, variableToString } from '../createVariable'
3
3
  import { CreateTamaguiProps, ThemeParsed } from '../types'
4
4
  import { tokensValueToVariable } from './registerCSSVariable'
5
+ import { simpleHash } from '@tamagui/helpers'
5
6
 
6
7
  export function getThemeCSSRules({
7
8
  config,
@@ -34,7 +35,8 @@ export function getThemeCSSRules({
34
35
  } else {
35
36
  value = tokensValueToVariable.get(variable.val)!.variable
36
37
  }
37
- vars += `--${themeKey}:${value};`
38
+ // Hash themeKey in case it has invalid chars too
39
+ vars += `--${simpleHash(themeKey, 40)}:${value};`
38
40
  }
39
41
 
40
42
  const isDarkOrLightBase = themeName === 'dark' || themeName === 'light'
@@ -80,7 +80,7 @@ export function updateInserted() {
80
80
  continue
81
81
  }
82
82
  const [identifier, cssRule] = response
83
- if (!allSelectors[identifier]) {
83
+ if (!(identifier in allSelectors)) {
84
84
  const isTransform = identifier.startsWith('_transform')
85
85
  const shouldInsert = isTransform ? addTransform(identifier, cssRule.cssText, cssRule) : true
86
86
  if (shouldInsert) {
@@ -95,7 +95,8 @@ export function updateInserted() {
95
95
  function getTamaguiSelector(rule: CSSRule | null): readonly [string, CSSStyleRule] | null {
96
96
  if (rule instanceof CSSStyleRule) {
97
97
  const text = rule.selectorText
98
- if (text.startsWith('._')) {
98
+ // .startsWith('._')
99
+ if (text[0] === '.' && text[1] === '_') {
99
100
  return [text.slice(1), rule]
100
101
  }
101
102
  if (text.startsWith(':root') && text.includes('._')) {
@@ -134,16 +135,14 @@ export type PartialStyleObject = Pick<StyleObject, 'identifier' | 'property' | '
134
135
  export type RulesToInsert = PartialStyleObject[]
135
136
 
136
137
  export function insertStyleRules(rulesToInsert: RulesToInsert) {
138
+ if (!rulesToInsert.length) return
137
139
  if (isClient && !sheet) {
138
- if (process.env.NODE_ENV === 'development') {
139
- // eslint-disable-next-line no-console
140
- console.warn(`Missing sheet`)
141
- }
140
+ if (process.env.NODE_ENV === 'development') throw `impossible`
142
141
  return
143
142
  }
144
143
  for (const { identifier, rules } of rulesToInsert) {
145
- if (allSelectors[identifier]) continue
146
- allSelectors[identifier] = process.env.NODE_ENV === 'development' ? rules.join('\n') : ''
144
+ if (identifier in allSelectors) continue
145
+ allSelectors[identifier] = process.env.NODE_ENV === 'development' ? rules.join('\n') : ' '
147
146
  updateRules(identifier, rules)
148
147
  if (sheet) {
149
148
  for (const rule of rules) {
@@ -159,5 +158,5 @@ export function shouldInsertStyleRules(styleObject: PartialStyleObject) {
159
158
  if (IS_STATIC) {
160
159
  return true
161
160
  }
162
- return !allSelectors[styleObject.identifier]
161
+ return !(styleObject.identifier in allSelectors)
163
162
  }
@@ -13,7 +13,7 @@ import {
13
13
  import { useSafeRef } from './useSafeRef'
14
14
 
15
15
  export const mediaState: MediaQueryState =
16
- // development time safeguard
16
+ // development only safeguard
17
17
  process.env.NODE_ENV === 'development'
18
18
  ? createProxy(
19
19
  {},
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable no-console */
2
2
  import { isClient, isRSC, isServer, isWeb } from '@tamagui/constants'
3
- import { useContext, useLayoutEffect, useMemo, useState } from 'react'
3
+ import { useContext, useEffect, useLayoutEffect, useMemo, useState } from 'react'
4
4
 
5
5
  import { getConfig } from '../config'
6
6
  import { isDevTools } from '../constants/isDevTools'
@@ -160,7 +160,7 @@ export const useChangeThemeEffect = (props: ThemeProps, root = false): ChangedTh
160
160
  const { isNewTheme, state, themeManager, mounted } = themeState
161
161
 
162
162
  if (!isServer) {
163
- useLayoutEffect(() => {
163
+ useEffect(() => {
164
164
  if (disable) return
165
165
  // SSR safe inverse (because server can't know prefers scheme)
166
166
  // could be done through fancy selectors like how we do prefers-media
@@ -188,16 +188,16 @@ export const useChangeThemeEffect = (props: ThemeProps, root = false): ChangedTh
188
188
  }, [disable, state, isNewTheme, debug])
189
189
  }
190
190
 
191
- // prettier-ignore
192
- if (process.env.NODE_ENV === 'development' && debug) console.log('useTheme.render isNewTheme', isNewTheme, props, themeManager.state.name, parentManager?.state.name)
191
+ const isInversingOnMount = Boolean(!themeState.mounted && props.inverse)
192
+ const shouldReturnParentState = hasNoThemeUpdatingProps(props) || isInversingOnMount
193
193
 
194
- // this seems unnecessary
195
- if (hasNoThemeUpdatingProps(props) || (!mounted && parentManager)) {
194
+ if (shouldReturnParentState) {
196
195
  if (!parentManager) throw `impossible`
197
196
  // prettier-ignore
198
- if (process.env.NODE_ENV === 'development' && props.debug === 'verbose') console.log('useTheme hasNoThemeUpdatingProps', parentManager.state)
197
+ if (process.env.NODE_ENV === 'development' && props.debug === 'verbose') console.log('useTheme hasNoThemeUpdatingProps', parentManager.state.name, 'isInversingOnMount', isInversingOnMount)
199
198
  return {
200
199
  ...parentManager.state,
200
+ className: isInversingOnMount ? '' : parentManager.state.className,
201
201
  themeManager: parentManager,
202
202
  isNewTheme: false,
203
203
  }
@@ -8,7 +8,7 @@ import { useChangeThemeEffect } from '../hooks/useTheme'
8
8
  import { ThemeProps } from '../types'
9
9
 
10
10
  export function Theme(props: ThemeProps) {
11
- // @ts-expect-error its a permanent prop meant for internal views
11
+ // @ts-expect-error only for internal views
12
12
  if (props.disable) {
13
13
  return props.children
14
14
  }