@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
1 lines • 8.16 kB
Source Map (JSON)
{"version":3,"file":"theme-provider.cjs","names":["css","ThemeProvider: FC<ThemeProviderProps>","THEME_SCHEME_STORAGE_KEY","createStorageManager","useEnvironment","changeThemeScheme: ChangeThemeScheme","themeScheme","EmotionThemeProvider","GlobalStyles: FC","useSystem","Global","ThemeContext"],"sources":["../../../../src/core/system/theme-provider.tsx"],"sourcesContent":["\"use client\"\n\nimport type { ThemeProviderProps as EmotionThemeProviderProps } from \"@emotion/react\"\nimport type { FC, PropsWithChildren } from \"react\"\nimport type { Environment } from \"./environment-provider\"\nimport type {\n ChangeThemeScheme,\n StyledTheme,\n Theme,\n ThemeConfig,\n ThemeScheme,\n UsageTheme,\n} from \"./index.types\"\nimport type { Storage } from \"./storage-manager\"\nimport {\n ThemeProvider as EmotionThemeProvider,\n Global,\n ThemeContext,\n} from \"@emotion/react\"\nimport { use, useCallback, useEffect, useMemo, useState } from \"react\"\nimport { isEmptyObject, isObject, isUndefined, merge } from \"../../utils\"\nimport { THEME_SCHEME_STORAGE_KEY } from \"../constant\"\nimport { css } from \"../css\"\nimport { useEnvironment } from \"./environment-provider\"\nimport { createStorageManager } from \"./storage-manager\"\nimport { useSystem } from \"./system-provider\"\n\nexport const getPreventTransition = (environment: Environment) => {\n const { getDocument, getWindow } = environment\n const win = getWindow()\n const doc = getDocument()\n\n if (!win || !doc) return\n\n const css = doc.createElement(\"style\")\n\n const node = doc.createTextNode(\n `*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}`,\n )\n\n css.appendChild(node)\n\n doc.head.appendChild(css)\n\n return () => {\n const forceReflow = () => win.getComputedStyle(doc.body)\n\n forceReflow()\n\n requestAnimationFrame(() => {\n requestAnimationFrame(() => {\n document.head.removeChild(css)\n })\n })\n }\n}\n\nexport interface ThemeProviderProps\n extends PropsWithChildren,\n Omit<EmotionThemeProviderProps, \"children\" | \"theme\"> {\n /**\n * The config of the yamada ui.\n */\n config?: ThemeConfig\n /**\n * If `storage` is `cookie`, the cookie to use.\n * If not provided, the cookie will be set to `document.cookie`.\n */\n cookie?: string\n /**\n * The storage to use.\n * If you are using a server-side rendering library, you should use `cookie`.\n *\n * @default 'localStorage'\n */\n storage?: Storage\n /**\n * Key of value saved in storage.\n * By default, it is saved to `local storage`.\n */\n storageKey?: string\n /**\n * The theme of the yamada ui.\n */\n theme?: UsageTheme\n}\n\nexport const ThemeProvider: FC<ThemeProviderProps> = ({\n children,\n config,\n cookie,\n storage = !!cookie ? \"cookie\" : \"localStorage\",\n storageKey = THEME_SCHEME_STORAGE_KEY,\n theme = {},\n}) => {\n const { defaultThemeScheme = \"base\", disableTransitionOnChange } =\n config ?? {}\n const storageManager = useMemo(\n () => createStorageManager(storage, storageKey, defaultThemeScheme, cookie),\n [cookie, defaultThemeScheme, storage, storageKey],\n )\n const environment = useEnvironment()\n const [themeScheme, setThemeScheme] = useState<ThemeScheme>(\n storageManager.get(),\n )\n\n const changeThemeScheme: ChangeThemeScheme = useCallback(\n (themeScheme) => {\n const { getDocument } = environment\n const doc = getDocument()\n\n const cleanup = disableTransitionOnChange\n ? getPreventTransition(environment)\n : undefined\n\n if (doc) doc.documentElement.dataset.theme = themeScheme\n\n cleanup?.()\n\n setThemeScheme(themeScheme)\n\n storageManager.set(themeScheme)\n },\n [disableTransitionOnChange, environment, storageManager],\n )\n\n useEffect(() => {\n const themeScheme = storageManager.get()\n\n changeThemeScheme(themeScheme)\n }, [changeThemeScheme, storageManager])\n\n return (\n <EmotionThemeProvider theme={{ changeThemeScheme, themeScheme, ...theme }}>\n <GlobalStyles />\n\n {children}\n </EmotionThemeProvider>\n )\n}\n\nexport const GlobalStyles: FC = () => {\n const system = useSystem()\n const { theme } = useTheme<UsageTheme>()\n const { atRule, wrap } = system.layers\n\n const resetStyle = useMemo(() => {\n const style = theme.styles?.resetStyle\n\n if (!style || isEmptyObject(style)) return undefined\n\n return css(system, theme)(style)\n }, [system, theme])\n\n const globalStyle = useMemo(() => {\n const style = theme.styles?.globalStyle\n\n if (!style || isEmptyObject(style)) return undefined\n\n return css(system, theme)(style)\n }, [system, theme])\n\n const cssVars = useMemo(() => {\n return { \":host, :root, [data-mode]\": system.cssVars }\n }, [system])\n\n return (\n <Global\n styles={[\n atRule,\n wrap(\"tokens\", cssVars),\n wrap(\"reset\", resetStyle),\n wrap(\"global\", globalStyle),\n ]}\n />\n )\n}\n\n/**\n * `useTheme` is a custom hook that returns a function for retrieving and switching themes.\n *\n * @see https://yamada-ui.com/docs/hooks/use-theme\n */\nexport const useTheme = <Y extends UsageTheme = Theme>() => {\n const internalTheme = use(ThemeContext) as StyledTheme<Y>\n\n const theme = useMemo(() => {\n const { themeScheme } = internalTheme\n\n if (isUndefined(themeScheme) || themeScheme === \"base\") return internalTheme\n\n const nestedTheme =\n \"themeSchemes\" in internalTheme && isObject(internalTheme.themeSchemes)\n ? internalTheme.themeSchemes[themeScheme as string]\n : undefined\n\n if (!nestedTheme) return internalTheme\n\n return merge<StyledTheme<Y>>(internalTheme, nestedTheme)\n }, [internalTheme])\n\n const value = useMemo(() => {\n const { changeThemeScheme, themeScheme } = internalTheme\n\n return {\n changeThemeScheme,\n internalTheme,\n theme,\n themeScheme,\n }\n }, [theme, internalTheme])\n\n return value\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA2BA,MAAa,wBAAwB,gBAA6B;CAChE,MAAM,EAAE,aAAa,cAAc;CACnC,MAAM,MAAM,WAAW;CACvB,MAAM,MAAM,aAAa;AAEzB,KAAI,CAAC,OAAO,CAAC,IAAK;CAElB,MAAMA,QAAM,IAAI,cAAc,QAAQ;CAEtC,MAAM,OAAO,IAAI,eACf,2JACD;AAED,OAAI,YAAY,KAAK;AAErB,KAAI,KAAK,YAAYA,MAAI;AAEzB,cAAa;EACX,MAAM,oBAAoB,IAAI,iBAAiB,IAAI,KAAK;AAExD,eAAa;AAEb,8BAA4B;AAC1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