Skip to content

Commit 594eb7f

Browse files
committed
feat(favicon): add favicon component to handle theme-based icons
1 parent af7a2e8 commit 594eb7f

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

public/icon-dark.svg

Lines changed: 12 additions & 0 deletions
Loading
File renamed without changes.

src/app/favicon.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use client";
2+
3+
import { useEffect } from "react";
4+
import { useTheme } from "next-themes";
5+
6+
export default function Favicon() {
7+
const { resolvedTheme } = useTheme();
8+
9+
useEffect(() => {
10+
const favicon =
11+
document.querySelector("link[rel*='icon']") ||
12+
document.createElement("link");
13+
favicon.setAttribute("rel", "icon");
14+
15+
if (resolvedTheme === "dark") {
16+
favicon.setAttribute("href", "/icon-dark.svg");
17+
} else {
18+
favicon.setAttribute("href", "/icon-light.svg");
19+
}
20+
21+
document.head.appendChild(favicon);
22+
}, [resolvedTheme]);
23+
24+
return null;
25+
}

src/app/providers.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { HeroUIProvider, ToastProvider } from "@heroui/react";
44
import { ThemeProvider as NextThemesProvider } from "next-themes";
5+
import Favicon from "./favicon";
56

67
type Props = {
78
children: React.ReactNode;
@@ -22,6 +23,7 @@ export default function Providers({ children }: Props) {
2223
}}
2324
/>
2425
<NextThemesProvider attribute="class" defaultTheme="system" enableSystem>
26+
<Favicon />
2527
{children}
2628
</NextThemesProvider>
2729
</HeroUIProvider>

0 commit comments

Comments
 (0)