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:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
frontend/node_modules
|
||||
eval
|
||||
data
|
||||
|
||||
1
frontend/dist/assets/index-CEN2dKUd.css
vendored
Normal file
1
frontend/dist/assets/index-CEN2dKUd.css
vendored
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
1
frontend/dist/assets/index-IEmHo4ub.css
vendored
1
frontend/dist/assets/index-IEmHo4ub.css
vendored
File diff suppressed because one or more lines are too long
4
frontend/dist/index.html
vendored
4
frontend/dist/index.html
vendored
@@ -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>
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -3,6 +3,7 @@ export default {
|
||||
"./index.html",
|
||||
"./src/**/*.{js,ts,jsx,tsx}",
|
||||
],
|
||||
darkMode: 'class',
|
||||
theme: {
|
||||
extend: {},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user