Skip to content

Commit

Permalink
fix theme color dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo committed Nov 18, 2024
1 parent 1ee54b0 commit eb5de5f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
7 changes: 0 additions & 7 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ export const metadata: Metadata = {
],
};

export const viewport: Viewport = {
themeColor: [
{ color: "#EEF2F8", media: "(prefers-color-scheme: light)" },
{ color: "#171717", media: "(prefers-color-scheme: dark)" },
]
}

export default async function RootLayout({
children,
}: Readonly<{
Expand Down
4 changes: 3 additions & 1 deletion src/components/layout/app-layout.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { SidebarProvider } from "@/components/ui/sidebar";
import { AppSidebar } from "./app-sidebar";
import { AppThemeColor } from "./app-theme-color";

export function AppLayout({ children }: { children: React.ReactNode }) {
return (
<SidebarProvider>
<AppThemeColor />
<AppSidebar />
<main className="relative grow">
<main className="grow relative">
<div className="absolute inset-0">
{children}
</div>
Expand Down
28 changes: 28 additions & 0 deletions src/components/layout/app-theme-color.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use client'

import { useTheme } from "next-themes"
import { useEffect } from "react"

const THEME_COLOR_DARK = '#171717'
const THEME_COLOR_LIGHT = '#EEF2F8'

// Set the theme color meta tag based on the current theme
export function AppThemeColor() {
const { resolvedTheme } = useTheme()

useEffect(() => {
let themeColorMeta = document.querySelector(
'meta[name="theme-color"]',
) as HTMLMetaElement

if (!themeColorMeta) {
themeColorMeta = document.createElement('meta')
themeColorMeta.name = 'theme-color'
document.head.appendChild(themeColorMeta)
}

themeColorMeta.content = resolvedTheme === 'dark' ? THEME_COLOR_DARK : THEME_COLOR_LIGHT
}, [resolvedTheme])

return null
}

0 comments on commit eb5de5f

Please sign in to comment.