feat: dark mode

This commit is contained in:
syy11cn 2022-04-02 17:36:26 +08:00 committed by sunyiyang.11
parent 959e2ec335
commit 1d1f97159c
4 changed files with 9 additions and 5 deletions

View File

@ -12,7 +12,7 @@
(function() {
const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
const setting = localStorage.getItem('color-schema') || 'auto'
if (setting === 'dark' || (prefersDark && setting !== 'light'))
if (setting === 'dark' || (prefersDark && setting !== 'auto'))
document.documentElement.classList.toggle('dark', true)
})()
</script>

View File

@ -1,10 +1,12 @@
import { useRoutes } from 'react-router-dom'
import Footer from './components/footer'
import { routes } from './routes'
const App = () => {
return <>
<p>hello world</p>
{useRoutes(routes)}
<Footer></Footer>
</>
}

View File

@ -1,13 +1,14 @@
export function useDark() {
const [value, setValue] = useState<string>(window.localStorage.getItem('colorSchema') || 'auto')
const [value, setValue] = useState<string>(window.localStorage.getItem('color-schema') || 'auto')
const toggleDark = () => {
setValue(value === 'auto' ? 'dark' : 'auto')
window.localStorage.setItem('colorSchema', value)
const newValue = value === 'auto' ? 'dark' : 'auto'
setValue(newValue)
window.localStorage.setItem('color-schema', newValue)
}
const isDark = value === 'dark'
if (document)
useEffect(() => { document.documentElement.classList.toggle('dark', isDark) })
return [isDark, toggleDark]
return [isDark, toggleDark] as [boolean, () => void]
}

View File

@ -5,6 +5,7 @@
"jsx": "react-jsx",
"module": "commonjs",
"rootDir": "./src",
"baseUrl": "./",
"allowJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,