mirror of
https://github.com/hanzoai/shadcn.git
synced 2026-10-30 18:22:59 +00:00
Squash merge of shadcn-ui/ui main branch into hanzo/ui, bringing in: - [email protected] through [email protected] releases - New apps/v4 directory structure - Registry system improvements and MCP server - Framework support (Vue, Svelte, React Native) - New templates (vite-app, monorepo-next, laravel) - Zod 4 compatibility (scoped override for zod@>4) - Test fixture corrections for alias resolution All 867 merge conflicts resolved preserving Hanzo customizations. 1020/1020 shadcn tests pass.
57 lines
1.2 KiB
TypeScript
57 lines
1.2 KiB
TypeScript
"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
|
|
}
|