-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
848cf32
commit a387773
Showing
3 changed files
with
45 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use client'; | ||
|
||
import { useEffect } from 'react'; | ||
import { useTheme } from 'next-themes'; | ||
|
||
export function ThemeColorManager() { | ||
const { theme, systemTheme } = useTheme(); | ||
|
||
useEffect(() => { | ||
const updateThemeColor = () => { | ||
const currentTheme = theme === 'system' ? systemTheme : theme; | ||
const meta = document.querySelector('meta[name="theme-color"]'); | ||
|
||
if (!meta) { | ||
const newMeta = document.createElement('meta'); | ||
newMeta.name = 'theme-color'; | ||
document.head.appendChild(newMeta); | ||
} | ||
|
||
const themeColor = currentTheme === 'dark' | ||
? 'hsl(30 15% 8%)' // 深色模式背景色 | ||
: 'hsl(0 0% 98%)'; // 浅色模式背景色 | ||
|
||
document.querySelector('meta[name="theme-color"]')?.setAttribute('content', themeColor); | ||
}; | ||
|
||
// Update on mount and theme change | ||
updateThemeColor(); | ||
|
||
// Listen for system theme changes | ||
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); | ||
mediaQuery.addEventListener('change', updateThemeColor); | ||
|
||
return () => mediaQuery.removeEventListener('change', updateThemeColor); | ||
}, [theme, systemTheme]); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters