MCPcopy
hub / github.com/system-ui/theme-ui / NestedColorModeProvider

Function NestedColorModeProvider

packages/color-modes/src/index.tsx:312–363  ·  view source on GitHub ↗
({
  outerCtx,
  children,
}: {
  outerCtx: ThemeUIContextValue
  children: React.ReactNode
})

Source from the content-addressed store, hash-verified

310}
311
312function NestedColorModeProvider({
313 outerCtx,
314 children,
315}: {
316 outerCtx: ThemeUIContextValue
317 children: React.ReactNode
318}) {
319 const newTheme = useThemeWithAppliedColorMode({
320 outerTheme: outerCtx.theme,
321 colorMode: outerCtx.colorMode,
322 })
323
324 // Nested theme providers need to be rerendered after hydration for the correct
325 // color mode to apply.
326 const [needsRerender, setNeedsRerender] = useState(
327 // Note: we could also check some "ssr-enabled" flag as an optimization for
328 // SPAs, as deeply nested theme providers will also pay a performance penalty
329 // for this SSR bug fix
330 () => newTheme.config?.useLocalStorage !== false
331 )
332
333 useClientsideEffect(() => void setNeedsRerender(false), [])
334
335 const themeColors = newTheme.rawColors || newTheme.colors
336 const useCustomProperties = newTheme.config?.useCustomProperties
337
338 const colorVars = useMemo(() => {
339 if (useCustomProperties === false) {
340 return {}
341 }
342 const colors = themeColors || {}
343
344 return css(__createColorProperties(colors, colors.modes || {}))(newTheme)
345 }, [newTheme, themeColors, useCustomProperties])
346
347 return (
348 <ThemeUIInternalBaseThemeProvider
349 context={{ ...outerCtx, theme: newTheme }}
350 >
351 {/* Changed CSS Variables will cascade from the wrapping div */}
352 {jsx('div', {
353 'data-themeui-nested-provider': true,
354 // the key here ensures that children will be rerendered after color
355 // mode is read from localStorage
356 key: Number(needsRerender),
357 suppressHydrationWarning: true,
358 css: colorVars,
359 children,
360 })}
361 </ThemeUIInternalBaseThemeProvider>
362 )
363}
364
365export const ColorModeProvider = ({
366 children,

Callers

nothing calls this directly

Calls 4

cssFunction · 0.90
__createColorPropertiesFunction · 0.90
jsxFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…