cleanup: switch to $state for themes

This commit is contained in:
James Xylight committed 2025-02-10 17:34:40 -08:00
1 parent a5ba6d66f6
commit 80df5c5409
19 files changed
+169 -349

No files matched your search

@@ -2,9 +2,7 @@ import {
derived,
get,
writable,
type Stores,
type Updater,
type Unsubscriber,
type Writable,
type Readable,
} from 'svelte/store'
@@ -79,55 +77,89 @@ export function rgbToHex(rgbString: string): string {
return `#${toHex(r)}${toHex(g)}${toHex(b)}`
}
function themeStore(
data: Writable<ThemeData>,
fn: (
values: ThemeData,
set: (value: Theme) => void,
update: (fn: Updater<Theme>) => void,
) => any,
) {
const derive: Readable<Theme> = derived(data, fn)
const { subscribe } = derive
$effect.root(() => {
$effect(() => {
if (browser) {
const filteredThemes = theme.data.themes.filter((t) => t.id > 0)
localStorage.setItem(
'theme.data',
JSON.stringify({
...theme.data,
themes: filteredThemes,
}),
)
}
})
})
return {
subscribe,
set: (value: Theme) => {
data.update((data) => {
let index = data.themes.findIndex((t) => t.id == value.id)
data.themes[index] = value
return data
})
const configuredColorScheme = env.PUBLIC_COLORSCHEME ?? 'system'
class ThemeState {
constructor() {
const localColorScheme: ColorScheme =
(localStorage.getItem('colorScheme') as ColorScheme) ||
configuredColorScheme
this.#colorScheme = localColorScheme
if (browser) {
const prefersDark = window.matchMedia(
'(prefers-color-scheme: dark)',
).matches
const html = document.querySelector('html')
if (this.#colorScheme == 'system')
html?.classList.toggle('dark', prefersDark)
else html?.classList.toggle('dark', this.#colorScheme === 'dark')
localStorage.setItem('colorScheme', this.#colorScheme)
}
}
#data = $state<ThemeData>(
loadTheme() ?? {
scheme: 'system',
themes: presets,
currentTheme: 0,
},
)
#current = $derived(
this.#data.themes.find((i) => i.id == this.#data.currentTheme) ??
getDefaultTheme(),
)
#colorScheme = $state<ColorScheme>(configuredColorScheme as ColorScheme)
#vars = $derived(calculateVars(this.#current))
get current() {
return this.#current
}
set current(value) {
if (!value) return
const index = this.#data.themes.findIndex((i) => i.id == value?.id)
this.#data.themes[index] = value
}
get colorScheme() {
return this.#colorScheme
}
set colorScheme(value) {
this.#colorScheme = value
}
get vars() {
return this.#vars
}
get data() {
return this.#data
}
set data(value) {
this.#data = value
}
}
export let themeData = writable<ThemeData>(
loadTheme() ?? {
scheme: 'system',
themes: presets,
currentTheme: 0,
},
)
themeData.subscribe((themeData) => {
if (browser) {
const filteredThemes = themeData.themes.filter((t) => t.id > 0)
localStorage.setItem(
'themeData',
JSON.stringify({
...themeData,
themes: filteredThemes,
}),
)
}
})
export let theme = themeStore(
themeData,
(data) =>
data.themes.find((t) => t.id == data.currentTheme) ?? getDefaultTheme(),
)
export let theme = new ThemeState()
export function calculateVars(theme: Theme) {
let cssVariables = ''
@@ -141,59 +173,18 @@ export function calculateVars(theme: Theme) {
return cssVariables.trim()
}
export const themeVars = derived(theme, (t) => calculateVars(t as Theme))
const configuredColorScheme = env.PUBLIC_COLORSCHEME ?? 'system'
export const colorScheme = writable<ColorScheme>(
configuredColorScheme as ColorScheme,
)
export const toggleColorScheme = () => {
colorScheme.update((colorScheme) => {
if (colorScheme == 'light') return 'dark'
else if (colorScheme == 'dark') return 'system'
else return 'light'
})
}
export const inDarkColorScheme = (): boolean => {
if (typeof window != 'undefined') {
return get(colorScheme) == 'system'
if (browser) {
return theme.colorScheme == 'system'
? window.matchMedia('(prefers-color-scheme: dark)').matches
: get(colorScheme) == 'dark'
: theme.colorScheme == 'dark'
}
return false
}
function loadColorScheme() {
const localColorScheme: ColorScheme =
(localStorage.getItem('colorScheme') as ColorScheme) ||
configuredColorScheme
colorScheme.update((colorScheme) => localColorScheme)
colorScheme.subscribe((colorScheme) => {
if (typeof document != 'undefined') {
const prefersDark = window.matchMedia(
'(prefers-color-scheme: dark)',
).matches
const html = document.querySelector('html')
if (colorScheme == 'system') {
html?.classList.toggle('dark', prefersDark)
} else {
html?.classList.toggle('dark', colorScheme === 'dark')
}
localStorage.setItem('colorScheme', colorScheme)
}
})
}
function loadTheme() {
if (!browser) return
const localTheme = localStorage.getItem('themeData')
const localTheme = localStorage.getItem('theme.data')
if (localTheme) {
const data = JSON.parse(localTheme)
@@ -204,27 +195,3 @@ function loadTheme() {
return
}
if (browser) {
try {
loadColorScheme()
} catch (e) {}
try {
const oldColors = localStorage.getItem('colors')
if (oldColors) {
themeData.update((td) => ({
...td,
currentTheme: Math.max(...td.themes.map((t) => t.id)) + 1,
themes: [
...td.themes,
{
id: Math.max(...td.themes.map((t) => t.id)) + 1,
colors: JSON.parse(oldColors),
name: t.get('routes.theme.preset.imported') || 'Your Theme',
},
],
}))
localStorage.removeItem('colors')
}
} catch (e) {}
}
+1 -1
View File
@@ -1,5 +1,5 @@
import { env } from '$env/dynamic/public'
import type { Theme, ThemeColors } from './colors'
import type { Theme, ThemeColors } from './colors.svelte'
export function getDefaultColors(): ThemeColors {
return env.PUBLIC_THEME