Skip to content

Commit 15fb317

Browse files
committed
Dynamically update theme color meta tag based on theme
Added logic in ThemeProvider to update the <meta name="theme-color"> tag according to the user's selected or system theme. Also set a default themeColor in layout metadata as a fallback. Additionally, introduced an 'active' method to the left-action-menu command in the snippet config.
1 parent 404bfce commit 15fb317

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

snippet/src/config/commands.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ export const commands: Record<string, Command<State>> = {
337337
documentId,
338338
);
339339
},
340+
active: ({ state, documentId }) => {
341+
const uiState = state.plugins['ui']?.documents[documentId];
342+
return uiState?.openMenus['left-action-menu'] !== undefined;
343+
},
340344
},
341345

342346
// ─────────────────────────────────────────────────────────

website/src/app/layout.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export const metadata = {
1212
apple: '/apple-touch-icon.png',
1313
},
1414
manifest: `/site.webmanifest`,
15+
// Initial theme color - will be dynamically updated by ThemeProvider
16+
// based on user's theme preference (system or manual)
17+
themeColor: '#ffffff',
1518
}
1619

1720
export default async function RootLayout({
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
'use client'
22

33
import * as React from 'react'
4-
import { ThemeProvider as NextThemesProvider } from 'next-themes'
4+
import { ThemeProvider as NextThemesProvider, useTheme } from 'next-themes'
5+
6+
const THEME_COLORS = {
7+
light: '#ffffff',
8+
dark: '#030712', // gray-950
9+
}
10+
11+
function ThemeColorMeta() {
12+
const { resolvedTheme } = useTheme()
13+
const [mounted, setMounted] = React.useState(false)
14+
15+
React.useEffect(() => {
16+
setMounted(true)
17+
}, [])
18+
19+
// Render nothing on server, fallback handled by layout.tsx metadata
20+
if (!mounted) return null
21+
22+
const color =
23+
resolvedTheme === 'dark' ? THEME_COLORS.dark : THEME_COLORS.light
24+
25+
// Next.js App Router automatically hoists <meta> tags to <head>
26+
return <meta name="theme-color" content={color} />
27+
}
528

629
export function ThemeProvider({
730
children,
831
...props
932
}: React.ComponentProps<typeof NextThemesProvider>) {
10-
return <NextThemesProvider {...props}>{children}</NextThemesProvider>
33+
return (
34+
<NextThemesProvider {...props}>
35+
<ThemeColorMeta />
36+
{children}
37+
</NextThemesProvider>
38+
)
1139
}

0 commit comments

Comments
 (0)