/
githubmirror
/
ui
Обзор
Документация
Войти
/
githubmirror
/
ui
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
apps/v4/components/active-theme.tsx
56 строк
1 KB
shadcn
chore: update deps (#9022)
12 дек 2025, 20:01
Не верифицирован
12 дек 2025, 20:01
86d9b00
Код
Авторство
О чём код?
"use client" import { createContext, useContext, useEffect, useState, type ReactNode, } from "react" const DEFAULT_THEME = "default" type ThemeContextType = { activeTheme: string setActiveTheme: (theme: string) => void } const ThemeContext = createContext<ThemeContextType | undefined>(undefined) export function ActiveThemeProvider({ children, initialTheme, }: { children: ReactNode initialTheme?: string }) { const [activeTheme, setActiveTheme] = useState<string>( () => initialTheme || DEFAULT_THEME ) useEffect(() => { Array.from(document.body.classList) .filter((className) => className.startsWith("theme-")) .forEach((className) => { document.body.classList.remove(className) }) document.body.classList.add(`theme-${activeTheme}`) if (activeTheme.endsWith("-scaled")) { document.body.classList.add("theme-scaled") } }, [activeTheme]) return ( <ThemeContext.Provider value={{ activeTheme, setActiveTheme }}> {children} </ThemeContext.Provider> ) } export function useThemeConfig() { const context = useContext(ThemeContext) if (context === undefined) { throw new Error("useThemeConfig must be used within an ActiveThemeProvider") } return context }