52 lines
1.0 KiB
TypeScript
52 lines
1.0 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import { ThemeProvider as NextThemesProvider, useTheme } from "next-themes"
|
|
|
|
function ThemeProvider({
|
|
children,
|
|
...props
|
|
}: React.ComponentProps<typeof NextThemesProvider>) {
|
|
return (
|
|
<NextThemesProvider
|
|
attribute="class"
|
|
defaultTheme="system"
|
|
enableSystem
|
|
disableTransitionOnChange
|
|
{...props}
|
|
>
|
|
<ThemeFavicon />
|
|
{children}
|
|
</NextThemesProvider>
|
|
)
|
|
}
|
|
|
|
function ThemeFavicon() {
|
|
const { resolvedTheme } = useTheme()
|
|
|
|
React.useEffect(() => {
|
|
if (!resolvedTheme) {
|
|
return
|
|
}
|
|
|
|
const href =
|
|
resolvedTheme === "dark" ? "/favicon-dark.ico" : "/favicon.ico"
|
|
|
|
for (const rel of ["icon", "shortcut icon"] as const) {
|
|
let link = document.querySelector<HTMLLinkElement>(`link[rel="${rel}"]`)
|
|
|
|
if (!link) {
|
|
link = document.createElement("link")
|
|
link.rel = rel
|
|
document.head.appendChild(link)
|
|
}
|
|
|
|
link.href = href
|
|
}
|
|
}, [resolvedTheme])
|
|
|
|
return null
|
|
}
|
|
|
|
export { ThemeProvider }
|