From 88f09e2b7d7bace6ca090a5511cef2b82a9a9c76 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Wed, 9 Aug 2023 12:13:40 -0700 Subject: [PATCH] fix: test failing, theme toggle was put in wrong file --- src/lib/components/ui/Navbar.svelte | 2 +- src/lib/ui/colors.ts | 41 +++++++++++++++++++++++++++++ src/lib/ui/image.ts | 41 ----------------------------- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/lib/components/ui/Navbar.svelte b/src/lib/components/ui/Navbar.svelte index 8c020d89..976c1966 100644 --- a/src/lib/components/ui/Navbar.svelte +++ b/src/lib/components/ui/Navbar.svelte @@ -14,7 +14,7 @@ import MenuButton from '$lib/components/ui/menu/MenuButton.svelte' import { LINKED_INSTANCE_URL, instance } from '$lib/instance.js' import { site } from '$lib/lemmy.js' - import { theme } from '$lib/ui/image.js' + import { theme } from '$lib/ui/colors.js' import { ArrowLeftOnRectangle, ArrowRightOnRectangle, diff --git a/src/lib/ui/colors.ts b/src/lib/ui/colors.ts index e0ad379d..25baf4c0 100644 --- a/src/lib/ui/colors.ts +++ b/src/lib/ui/colors.ts @@ -12,3 +12,44 @@ export enum Color { 'border' = 'border border-slate-200 dark:border-zinc-700 bg-slate-100 dark:bg-zinc-800 hover:bg-slate-200 hover:dark:bg-zinc-700', 'borderDark' = 'border border-slate-200 dark:border-zinc-800 bg-slate-100 dark:bg-zinc-900 hover:bg-slate-200 hover:dark:bg-zinc-800', } + +import { writable } from 'svelte/store' + +export const theme = writable<'system' | 'light' | 'dark'>('system') + +export const toggleTheme = () => { + theme.update((theme) => { + if (theme == 'light') { + return 'dark' + } else if (theme == 'dark') { + return 'system' + } else { + return 'light' + } + }) +} + +if (typeof localStorage != 'undefined') { + const localTheme: 'system' | 'light' | 'dark' = + (localStorage.getItem('theme') as 'system' | 'light' | 'dark') || 'system' + + theme.update((theme) => localTheme) + + theme.subscribe((theme) => { + if (typeof document != 'undefined') { + const prefersDark = window.matchMedia( + '(prefers-color-scheme: dark)' + ).matches + + const html = document.querySelector('html') + + if (theme == 'system') { + html?.classList.toggle('dark', prefersDark) + } else { + html?.classList.toggle('dark', theme === 'dark') + } + + localStorage.setItem('theme', theme) + } + }) +} diff --git a/src/lib/ui/image.ts b/src/lib/ui/image.ts index 7731749b..12d10407 100644 --- a/src/lib/ui/image.ts +++ b/src/lib/ui/image.ts @@ -11,44 +11,3 @@ export const isVideo = (inputUrl: string | undefined) => { return url.endsWith('mp4') || url.endsWith('webm') || url.endsWith('mov') } - -import { writable } from 'svelte/store' - -export const theme = writable<'system' | 'light' | 'dark'>('system') - -export const toggleTheme = () => { - theme.update((theme) => { - if (theme == 'light') { - return 'dark' - } else if (theme == 'dark') { - return 'system' - } else { - return 'light' - } - }) -} - -if (typeof localStorage != 'undefined') { - const localTheme: 'system' | 'light' | 'dark' = - (localStorage.getItem('theme') as 'system' | 'light' | 'dark') || 'system' - - theme.update((theme) => localTheme) - - theme.subscribe((theme) => { - if (typeof document != 'undefined') { - const prefersDark = window.matchMedia( - '(prefers-color-scheme: dark)' - ).matches - - const html = document.querySelector('html') - - if (theme == 'system') { - html?.classList.toggle('dark', prefersDark) - } else { - html?.classList.toggle('dark', theme === 'dark') - } - - localStorage.setItem('theme', theme) - } - }) -}