From 629482e5d3b07a0d1c96d7f8bf4420235015e100 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Wed, 9 Aug 2023 12:05:08 -0700 Subject: [PATCH] feat: add manual dark toggle --- package.json | 2 +- src/app.html | 23 +++++++++++----- src/lib/components/ui/Navbar.svelte | 29 ++++++++++++++++++++ src/lib/ui/image.ts | 41 +++++++++++++++++++++++++++++ src/style/app.css | 10 +++---- tailwind.config.js | 1 + 6 files changed, 92 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 4b3a94d1..7257f351 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "photon-lemmy", - "version": "0.5.6", + "version": "0.5.7", "private": true, "scripts": { "dev": "vite dev", diff --git a/src/app.html b/src/app.html index 9ca7cd73..5f22f237 100644 --- a/src/app.html +++ b/src/app.html @@ -10,16 +10,25 @@ content="Photon: An alternative lemmy client with a sleek design." /> + %sveltekit.head%
%sveltekit.body%
- diff --git a/src/lib/components/ui/Navbar.svelte b/src/lib/components/ui/Navbar.svelte index 0ff2de98..3b5e3b78 100644 --- a/src/lib/components/ui/Navbar.svelte +++ b/src/lib/components/ui/Navbar.svelte @@ -14,6 +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 { ArrowLeftOnRectangle, ArrowRightOnRectangle, @@ -21,13 +22,16 @@ Bookmark, Cog6Tooth, CommandLine, + ComputerDesktop, GlobeAlt, Icon, Inbox, InformationCircle, MagnifyingGlass, + Moon, PencilSquare, Plus, + Sun, UserCircle, } from 'svelte-hero-icons' @@ -227,6 +231,31 @@ About + + +
+ Theme + +
+

  • diff --git a/src/lib/ui/image.ts b/src/lib/ui/image.ts index 12d10407..7731749b 100644 --- a/src/lib/ui/image.ts +++ b/src/lib/ui/image.ts @@ -11,3 +11,44 @@ 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) + } + }) +} diff --git a/src/style/app.css b/src/style/app.css index 67680ca6..9be5fd9a 100644 --- a/src/style/app.css +++ b/src/style/app.css @@ -2,17 +2,15 @@ @tailwind components; @tailwind utilities; -:root { +body { @apply bg-slate-50 dark:bg-zinc-950 text-slate-900 dark:text-zinc-100 !important; accent-color: #000; } -@media (prefers-color-scheme: dark) { - :root { - color-scheme: dark; - accent-color: #fff; - } +.dark { + accent-color: #fff; + color-scheme: dark; } html, diff --git a/tailwind.config.js b/tailwind.config.js index 07177ec9..e5fe9085 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -1,6 +1,7 @@ /** @type {import('tailwindcss').Config} */ export default { content: ['src/**/*.{js,ts,svelte,html}'], + darkMode: 'class', theme: { fontFamily: { 'sans': ['Cantarell', 'system-ui'],