mirror of
https://old.git.ood.ovh/flambyte/RUX.git
synced 2025-04-29 02:25:23 +02:00
21 lines
No EOL
616 B
JavaScript
21 lines
No EOL
616 B
JavaScript
// https://web.dev/building-a-theme-switch-component/
|
|
const storageKey = 'theme-preference'
|
|
|
|
const getColorPreference = () => {
|
|
if (localStorage.getItem(storageKey))
|
|
return localStorage.getItem(storageKey)
|
|
else
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
? 'dark'
|
|
: 'light'
|
|
}
|
|
|
|
const setPreference = () => {
|
|
localStorage.setItem(storageKey, theme.value)
|
|
reflectPreference()
|
|
}
|
|
|
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', ({matches:isDark}) => {
|
|
theme.value = isDark ? 'dark' : 'light'
|
|
setPreference()
|
|
}) |