Skip to content

Commit 2a3ffd8

Browse files
committed
fix: theme toggle now detects system theme and works on first click
Fixes #42 - Add initialization effect to detect existing theme from index.html or fall back to system theme - Change classList.toggle to explicit remove/add to prevent class conflicts - Remove both 'dark' and 'light' classes before adding current theme - Ensures public notes pages respect system theme on load - Fixes double-click requirement for theme switching
1 parent 2a64ba5 commit 2a3ffd8

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/pages/PublicNotePage.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,27 @@ export default function PublicNotePage() {
614614
// Get slug from URL
615615
const slug = window.location.pathname.split('/p/')[1];
616616

617+
// Initialize theme on mount
618+
useEffect(() => {
619+
// Check if theme class is already set by index.html script
620+
const hasLight = document.documentElement.classList.contains('light');
621+
const hasDark = document.documentElement.classList.contains('dark');
622+
623+
if (hasLight || hasDark) {
624+
// Use existing theme from index.html
625+
setTheme(hasDark ? 'dark' : 'light');
626+
} else {
627+
// No theme set, use system theme
628+
setTheme(getSystemTheme());
629+
}
630+
}, []);
631+
617632
// Apply theme to document
618633
useEffect(() => {
619-
document.documentElement.classList.toggle('dark', theme === 'dark');
634+
// Remove both classes first to avoid conflicts
635+
document.documentElement.classList.remove('dark', 'light');
636+
// Add the current theme class
637+
document.documentElement.classList.add(theme);
620638
}, [theme]);
621639

622640
// Show/hide scroll to top button based on scroll position

0 commit comments

Comments
 (0)