Skip to content

Commit

Permalink
feat: light/dark modes switch immediately with the system theme (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
xingoxu authored Jun 5, 2024
1 parent 3f4e7e9 commit c3ac8d9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
54 changes: 42 additions & 12 deletions src/components/LightDarkSwitch.svelte
Original file line number Diff line number Diff line change
@@ -1,22 +1,52 @@
<script lang="ts">
import Icon from "@iconify/svelte"
import {i18n} from '@i18n/translation'
import type { LIGHT_DARK_MODE } from '@/types/config.ts'
import {
AUTO_MODE,
DARK_MODE,
LIGHT_MODE,
} from '@constants/constants.ts'
import I18nKey from '@i18n/i18nKey'
import {setTheme, getStoredTheme} from '../utils/setting-utils.ts'
import {onMount} from "svelte";
import {AUTO_MODE, DARK_MODE, LIGHT_MODE} from "@constants/constants.ts";
import { i18n } from '@i18n/translation'
import Icon from '@iconify/svelte'
import {
applyThemeToDocument,
getStoredTheme,
setTheme,
} from '@utils/setting-utils.ts'
import { onMount } from 'svelte'
const seq = [LIGHT_MODE, DARK_MODE, AUTO_MODE]
let mode = AUTO_MODE
const seq: LIGHT_DARK_MODE[] = [
LIGHT_MODE,
DARK_MODE,
AUTO_MODE,
]
let mode: LIGHT_DARK_MODE = AUTO_MODE
onMount(() => {
mode = getStoredTheme()
mode = getStoredTheme()
const darkModePreference = window.matchMedia(
'(prefers-color-scheme: dark)',
)
const changeThemeWhenSchemeChanged: Parameters<
typeof darkModePreference.addEventListener<'change'>
>[1] = e => {
applyThemeToDocument(mode)
}
darkModePreference.addEventListener(
'change',
changeThemeWhenSchemeChanged,
)
return () => {
darkModePreference.removeEventListener(
'change',
changeThemeWhenSchemeChanged,
)
}
})
function switchScheme(newMode: string) {
mode = newMode
setTheme(newMode)
function switchScheme(newMode: LIGHT_DARK_MODE) {
mode = newMode
setTheme(newMode)
}
function toggleScheme() {
Expand Down
6 changes: 5 additions & 1 deletion src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { LIGHT_MODE, DARK_MODE, AUTO_MODE } from "@constants/constants"

export type SiteConfig = {
title: string
subtitle: string
Expand All @@ -17,7 +19,7 @@ export type SiteConfig = {
}

export type Favicon = {
src: string,
src: string
theme?: 'light' | 'dark'
sizes?: string
}
Expand Down Expand Up @@ -54,3 +56,5 @@ export type LicenseConfig = {
name: string
url: string
}

export type LIGHT_DARK_MODE = typeof LIGHT_MODE | typeof DARK_MODE | typeof AUTO_MODE
24 changes: 15 additions & 9 deletions src/utils/setting-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {AUTO_MODE, DARK_MODE, DEFAULT_THEME, LIGHT_MODE} from "@constants/constants.ts";
import type { LIGHT_DARK_MODE } from '@/types/config'

export function getDefaultHue(): number {
const fallback = '250'
Expand All @@ -20,25 +21,30 @@ export function setHue(hue: number): void {
r.style.setProperty('--hue', hue)
}

export function setTheme(theme: string): void {
localStorage.setItem('theme', theme)

export function applyThemeToDocument(theme: LIGHT_DARK_MODE) {
switch (theme) {
case LIGHT_MODE:
document.documentElement.classList.remove('dark');
document.documentElement.classList.remove('dark')
break
case DARK_MODE:
document.documentElement.classList.add('dark');
document.documentElement.classList.add('dark')
break
case AUTO_MODE:
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.classList.add('dark');
document.documentElement.classList.add('dark')
} else {
document.documentElement.classList.remove('dark');
document.documentElement.classList.remove('dark')
}
break
}
}

export function getStoredTheme(): string {
return localStorage.getItem('theme') || DEFAULT_THEME
}
export function setTheme(theme: LIGHT_DARK_MODE): void {
localStorage.setItem('theme', theme)
applyThemeToDocument(theme)
}

export function getStoredTheme(): LIGHT_DARK_MODE {
return localStorage.getItem('theme') as LIGHT_DARK_MODE || DEFAULT_THEME
}

0 comments on commit c3ac8d9

Please sign in to comment.