Skip to content

Commit ab910a4

Browse files
vinc3m1claude
andcommitted
Change default theme from light to system
Update theme default to respect device preference instead of always defaulting to light mode. This affects both the React components and the static CSS initialization script to prevent FOUC. Changes: - ThemeToggle: Default to 'system' theme - ThemeSelector: Default to 'system' theme - index.astro: Initialize with 'system' theme in inline script First-time visitors will now see the theme that matches their device's dark/light mode preference. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent f336ede commit ab910a4

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/components/ThemeSelector.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ const ThemeModeSchema = z.enum(['light', 'dark', 'system']);
1515
type ThemeMode = z.infer<typeof ThemeModeSchema>;
1616

1717
function getThemeMode(): ThemeMode {
18-
if (typeof window === 'undefined') return 'light';
18+
if (typeof window === 'undefined') return 'system';
1919
const stored = localStorage.getItem('theme');
2020
const result = ThemeModeSchema.safeParse(stored);
21-
return result.success ? result.data : 'light';
21+
return result.success ? result.data : 'system';
2222
}
2323

2424
function getEffectiveMode(mode: ThemeMode): 'light' | 'dark' {
25-
if (typeof window === 'undefined') return 'light';
25+
if (typeof window === 'undefined') {
26+
// During SSR, assume light as a safe default for hydration
27+
return 'light';
28+
}
2629
if (mode === 'system') {
2730
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
2831
}

src/components/ThemeToggle.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ const ThemeSchema = z.enum(['light', 'dark', 'system']);
99
type Theme = z.infer<typeof ThemeSchema>;
1010

1111
function getThemePreference(): Theme {
12-
if (typeof window === 'undefined') return 'light';
12+
if (typeof window === 'undefined') return 'system';
1313
const stored = localStorage.getItem('theme');
1414
const result = ThemeSchema.safeParse(stored);
15-
return result.success ? result.data : 'light';
15+
return result.success ? result.data : 'system';
1616
}
1717

1818
function getEffectiveTheme(theme: Theme): 'light' | 'dark' {
19-
if (typeof window === 'undefined') return 'light';
19+
if (typeof window === 'undefined') {
20+
// During SSR, assume light as a safe default for hydration
21+
return 'light';
22+
}
2023
if (theme === 'system') {
2124
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
2225
}
@@ -54,7 +57,7 @@ function applyTheme(theme: Theme) {
5457
}
5558

5659
export function ThemeToggle() {
57-
const [theme, setTheme] = useState<Theme>('light');
60+
const [theme, setTheme] = useState<Theme>('system');
5861
const [mounted, setMounted] = useState(false);
5962

6063
useEffect(() => {

src/pages/index.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const structuredData = {
8080
<script is:inline>
8181
(function() {
8282
// Apply light/dark mode
83-
const theme = localStorage.getItem('theme') || 'light';
83+
const theme = localStorage.getItem('theme') || 'system';
8484
const effective = theme === 'system'
8585
? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')
8686
: theme;

0 commit comments

Comments
 (0)