allow changing theme by system detection

### 1. Updated Theme Provider (`src/components/theme/Provider.tsx`)

- Enabled system theme support by setting `enableSystem={true}`
- Changed default theme from "dark" to "system" so the app follows the user's system preference by default

### 2. Updated Theme Switcher (`src/components/theme/Switcher.tsx`)

- Added "System" option to the theme dropdown menu alongside "Light" and "Dark"
- Removed the custom system theme detection logic that was interfering with next-themes' built-in system detection
- Simplified the component to rely on next-themes' native system theme handling
This commit is contained in:
TamHC 2025-06-24 21:00:31 +08:00 committed by GitHub
parent 88ab31e820
commit ad8ea4acd6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 2 additions and 19 deletions

View file

@ -7,7 +7,7 @@ const ThemeProviderComponent = ({
children: React.ReactNode;
}) => {
return (
<ThemeProvider attribute="class" enableSystem={false} defaultTheme="dark">
<ThemeProvider attribute="class" enableSystem={true} defaultTheme="system">
{children}
</ThemeProvider>
);

View file

@ -20,24 +20,6 @@ const ThemeSwitcher = ({ className }: { className?: string }) => {
setMounted(true);
}, []);
useEffect(() => {
if (isTheme('system')) {
const preferDarkScheme = window.matchMedia(
'(prefers-color-scheme: dark)',
);
const detectThemeChange = (event: MediaQueryListEvent) => {
const theme: Theme = event.matches ? 'dark' : 'light';
setTheme(theme);
};
preferDarkScheme.addEventListener('change', detectThemeChange);
return () => {
preferDarkScheme.removeEventListener('change', detectThemeChange);
};
}
}, [isTheme, setTheme, theme]);
// Avoid Hydration Mismatch
if (!mounted) {
@ -52,6 +34,7 @@ const ThemeSwitcher = ({ className }: { className?: string }) => {
options={[
{ value: 'light', label: 'Light' },
{ value: 'dark', label: 'Dark' },
{ value: 'system', label: 'System' },
]}
/>
);