fix: dynamic theme color

This commit is contained in:
shinya
2025-06-26 22:41:30 +08:00
parent 4061cc9f59
commit cf42e7c056
3 changed files with 21 additions and 10 deletions

View File

@@ -12,10 +12,6 @@ export const metadata: Metadata = {
title: 'MoonTV',
description: '影视聚合',
manifest: '/manifest.json',
themeColor: [
{ media: '(prefers-color-scheme: light)', color: '#f9fbfe' },
{ media: '(prefers-color-scheme: dark)', color: '#0C111C' },
],
};
export default function RootLayout({

View File

@@ -102,7 +102,7 @@ export default function ScrollableRow({
>
<div
ref={containerRef}
className='flex space-x-6 overflow-x-auto scrollbar-hide pb-10 sm:pb-14'
className='flex space-x-6 overflow-x-auto scrollbar-hide pb-12 sm:pb-14'
onScroll={checkScroll}
>
{children}

View File

@@ -1,4 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-explicit-any,react-hooks/exhaustive-deps */
'use client';
@@ -8,10 +8,23 @@ import { useEffect, useState } from 'react';
export function ThemeToggle() {
const [mounted, setMounted] = useState(false);
const { theme, setTheme } = useTheme();
const { setTheme, resolvedTheme } = useTheme();
const setThemeColor = (theme?: string) => {
const meta = document.querySelector('meta[name="theme-color"]');
if (!meta) {
const meta = document.createElement('meta');
meta.name = 'theme-color';
meta.content = theme === 'dark' ? '#0c111c' : '#f9fbfe';
document.head.appendChild(meta);
} else {
meta.setAttribute('content', theme === 'dark' ? '#0c111c' : '#f9fbfe');
}
};
useEffect(() => {
setMounted(true);
setThemeColor(resolvedTheme);
}, []);
if (!mounted) {
@@ -21,13 +34,15 @@ export function ThemeToggle() {
const toggleTheme = () => {
// 检查浏览器是否支持 View Transitions API
const targetTheme = resolvedTheme === 'dark' ? 'light' : 'dark';
setThemeColor(targetTheme);
if (!(document as any).startViewTransition) {
setTheme(theme === 'dark' ? 'light' : 'dark');
setTheme(targetTheme);
return;
}
(document as any).startViewTransition(() => {
setTheme(theme === 'dark' ? 'light' : 'dark');
setTheme(targetTheme);
});
};
@@ -37,7 +52,7 @@ export function ThemeToggle() {
className='w-10 h-10 p-2 rounded-full flex items-center justify-center text-gray-600 hover:bg-gray-200/50 dark:text-gray-300 dark:hover:bg-gray-700/50 transition-colors'
aria-label='Toggle theme'
>
{theme === 'dark' ? (
{resolvedTheme === 'dark' ? (
<Sun className='w-full h-full' />
) : (
<Moon className='w-full h-full' />