Skip to content

Commit

Permalink
set the initial preference for dark/light mode
Browse files Browse the repository at this point in the history
  • Loading branch information
SabAlej committed Nov 2, 2024
1 parent fda474e commit e276b2b
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions playgrounds/app/src/components/theme-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@ import { createSignal, createEffect } from 'solid-js'
import { makePersisted } from '@solid-primitives/storage'

export function createThemeSwitcher() {
const [isDarkMode, setIsDarkMode] = makePersisted(createSignal(false), {
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)')
const [isDarkMode, setIsDarkMode] = makePersisted(createSignal(prefersDarkScheme.matches), {
name: 'isDarkMode',
})

createEffect(() => {
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)')

if (isDarkMode()) {
document.body.classList.add('dark')
} else {
document.body.classList.remove('dark')
}

if (isDarkMode() === null) {
setIsDarkMode(prefersDarkScheme.matches)
}
})

const toggleDarkMode = () => {
Expand Down

0 comments on commit e276b2b

Please sign in to comment.