fix: fix light/dark/system theme switcher

- Add darkMode: class to tailwind config for class-based dark mode
- Load saved theme from localStorage on mount and apply immediately
- Add media query listener for system theme changes in system mode
This commit is contained in:
2026-02-06 17:08:42 -05:00
parent c7a82c4de8
commit 114fa556eb
7 changed files with 62 additions and 30 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
frontend/node_modules
eval
data

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -4,8 +4,8 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Eval Markdown Editor</title>
<script type="module" crossorigin src="/assets/index-PGya2Dnr.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-IEmHo4ub.css">
<script type="module" crossorigin src="/assets/index-DBLy_rVI.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-CEN2dKUd.css">
</head>
<body>
<div id="root"></div>

View File

@@ -24,11 +24,41 @@ function App() {
const [theme, setTheme] = useState<'light' | 'dark' | 'system'>('system')
const [content, setContent] = useState('')
// Load theme from localStorage on mount and apply it
useEffect(() => {
const savedTheme = localStorage.getItem('theme')
if (savedTheme && ['light', 'dark', 'system'].includes(savedTheme)) {
setTheme(savedTheme as 'light' | 'dark' | 'system')
// Apply immediately so we don't show the default state first
applyTheme(savedTheme as 'light' | 'dark' | 'system')
}
}, [])
// Load files on mount
useEffect(() => {
loadFiles()
}, [])
// Apply theme when it changes
useEffect(() => {
applyTheme(theme)
}, [theme])
// Listen for system theme changes when in system mode
useEffect(() => {
if (theme !== 'system') return
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
const handleSystemThemeChange = () => {
applyTheme('system')
}
mediaQuery.addEventListener('change', handleSystemThemeChange)
return () => {
mediaQuery.removeEventListener('change', handleSystemThemeChange)
}
}, [theme])
useEffect(() => {
if (currentFile) {
setContent(currentFile.content)

View File

@@ -3,6 +3,7 @@ export default {
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
],
darkMode: 'class',
theme: {
extend: {},
},