{"version":3,"file":"theme-TY3hfhp4.js","sources":["../../../../../../../app/frontend/shared/ui/theme.tsx"],"sourcesContent":["import { ReactNode, createContext, useCallback, useContext, useState } from 'react'\nimport { setCookie } from 'typescript-cookie'\n\nimport { ThemeType } from '~/widgets/Layout/helpers'\n\ntype Theme = 'dark' | 'light' | 'system'\n\ntype ThemeProviderProps = {\n children: ReactNode\n defaultTheme?: Theme\n storageKey?: string\n}\n\ntype ThemeProviderState = {\n theme: Theme\n setTheme: (theme: Theme) => void\n}\n\nconst initialState: ThemeProviderState = {\n theme: 'system',\n setTheme: () => null,\n}\n\nconst ThemeProviderContext = createContext(initialState)\n// TODO: move it to theme slice\nexport function ThemeProvider({\n children,\n defaultTheme,\n storageKey = 'vite-ui-theme',\n ...props\n}: ThemeProviderProps) {\n const [theme, setTheme] = useState(defaultTheme)\n\n const changeTheme = useCallback(\n (themeToChange: ThemeType) => {\n const root = window.document.documentElement\n\n root.classList.remove('light', 'dark')\n\n if (themeToChange === 'system') {\n const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches\n ? 'dark'\n : 'light'\n setTheme(systemTheme)\n root.classList.add(systemTheme)\n setCookie(storageKey, systemTheme)\n return\n }\n setTheme(themeToChange)\n root.classList.add(themeToChange)\n setCookie(storageKey, themeToChange)\n },\n [storageKey]\n )\n\n const value = {\n theme,\n setTheme: (theme: Theme) => {\n changeTheme(theme)\n },\n }\n\n return (\n \n {children}\n \n )\n}\n\nexport const useTheme = () => {\n const context = useContext(ThemeProviderContext)\n\n if (context === undefined) throw new Error('useTheme must be used within a ThemeProvider')\n\n return context\n}\n"],"names":["initialState","ThemeProviderContext","createContext","ThemeProvider","children","defaultTheme","storageKey","props","theme","setTheme","useState","changeTheme","useCallback","themeToChange","root","systemTheme","setCookie","value","useTheme","context","useContext"],"mappings":"kEAkBA,MAAMA,EAAmC,CACvC,MAAO,SACP,SAAU,IAAM,IAClB,EAEMC,EAAuBC,EAAAA,cAAkCF,CAAY,EAEpE,SAASG,EAAc,CAC5B,SAAAC,EACA,aAAAC,EACA,WAAAC,EAAa,gBACb,GAAGC,CACL,EAAuB,CACrB,KAAM,CAACC,EAAOC,CAAQ,EAAIC,WAAgBL,CAAY,EAEhDM,EAAcC,EAAA,YACjBC,GAA6B,CACtB,MAAAC,EAAO,OAAO,SAAS,gBAI7B,GAFKA,EAAA,UAAU,OAAO,QAAS,MAAM,EAEjCD,IAAkB,SAAU,CAC9B,MAAME,EAAc,OAAO,WAAW,8BAA8B,EAAE,QAClE,OACA,QACJN,EAASM,CAAW,EACfD,EAAA,UAAU,IAAIC,CAAW,EAC9BC,EAAUV,EAAYS,CAAW,EACjC,MACF,CACAN,EAASI,CAAa,EACjBC,EAAA,UAAU,IAAID,CAAa,EAChCG,EAAUV,EAAYO,CAAa,CACrC,EACA,CAACP,CAAU,CAAA,EAGPW,EAAQ,CACZ,MAAAT,EACA,SAAWA,GAAiB,CAC1BG,EAAYH,CAAK,CACnB,CAAA,EAGF,aACGP,EAAqB,SAArB,CAA+B,GAAGM,EAAO,MAAAU,EACvC,SAAAb,CACH,CAAA,CAEJ,CAEO,MAAMc,EAAW,IAAM,CACtB,MAAAC,EAAUC,aAAWnB,CAAoB,EAE/C,GAAIkB,IAAY,OAAiB,MAAA,IAAI,MAAM,8CAA8C,EAElF,OAAAA,CACT"}