feat: add basic theming support

This commit is contained in:
Xylight committed 2024-03-04 17:48:09 -08:00
1 parent 2d991fc4a2
commit 81ac6accf4
7 files changed
+299 -72

No files matched your search

+5 -4
View File
@@ -1,5 +1,6 @@
<script lang="ts">
import { userSettings } from '$lib/settings.js'
import { colors, colorsToVars } from '$lib/ui/colors'
import { routes } from '$lib/util.js'
export let route: { id: string | null } | undefined = undefined
@@ -11,7 +12,7 @@
<svelte:window bind:scrollY={scroll} />
<div class="shell {$$props.class}">
<div class="shell {$$props.class}" style={colorsToVars($colors)}>
<slot />
<slot
name="navbar"
@@ -29,17 +30,17 @@
>
<slot
name="sidebar"
class="hidden md:flex sticky top-16 left-0 w-full max-w-full"
class="hidden md:flex sticky top-16 left-0 w-full max-w-full bg-slate-50 dark:bg-zinc-950"
style="grid-area: sidebar; width: 100% !important;"
/>
<slot
name="main"
class="w-full bg-slate-25 dark:bg-zinc-925 dark:bg-transparent justify-self-center shadow-sm"
class="w-full bg-slate-25 dark:bg-zinc-925 justify-self-center shadow-sm"
style="grid-area: main"
/>
<slot
name="suffix"
class="max-xl:hidden w-full sticky top-16 left-0 h-full max-h-[calc(100svh-4rem)]"
class="max-xl:hidden w-full sticky top-16 left-0 h-full max-h-[calc(100svh-4rem)] bg-slate-50 dark:bg-zinc-950"
style="grid-area: suffix;"
/>
</div>
+1 -27
View File
@@ -10,41 +10,15 @@
import Logo from '$lib/components/ui/Logo.svelte'
import { LINKED_INSTANCE_URL, instance } from '$lib/instance.js'
import { site } from '$lib/lemmy.js'
import { userSettings } from '$lib/settings.js'
import { theme } from '$lib/ui/colors.js'
import { Menu, MenuButton, MenuDivider, Spinner } from 'mono-svelte'
import {
Badge,
Button,
Menu,
MenuButton,
MenuDivider,
Select,
Spinner,
toast,
} from 'mono-svelte'
import {
Bars3,
Bookmark,
BugAnt,
CodeBracket,
CodeBracketSquare,
Cog6Tooth,
CommandLine,
ComputerDesktop,
GlobeAlt,
Heart,
Icon,
Inbox,
InformationCircle,
MagnifyingGlass,
Moon,
Newspaper,
PencilSquare,
Plus,
ServerStack,
Sun,
UserCircle,
UserGroup,
} from 'svelte-hero-icons'
import { fly } from 'svelte/transition'
import Profile from './Profile.svelte'
+14 -11
View File
@@ -27,8 +27,9 @@
UserCircle,
UserGroup,
} from 'svelte-hero-icons'
import { theme } from '$lib/ui/colors'
import { legacyTheme } from '$lib/ui/colors'
import { userSettings } from '$lib/settings'
import { goto } from '$app/navigation'
</script>
<Menu {...$$restProps}>
@@ -97,26 +98,28 @@
<Icon src={Cog6Tooth} mini width={16} />
Settings
</MenuButton>
<MenuButton class="!py-0">
<MenuButton
class="!py-0"
on:click={() => {
goto('/theme')
}}
>
<Icon
src={$theme == 'system'
src={$legacyTheme == 'system'
? ComputerDesktop
: $theme == 'light'
: $legacyTheme == 'light'
? Sun
: $theme == 'dark'
: $legacyTheme == 'dark'
? Moon
: Moon}
mini
size="16"
/>
<span>Color Scheme</span>
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class="flex flex-row flex-wrap justify-between w-full items-center"
on:click|stopPropagation
>
<span>Theme</span>
<Select bind:value={$theme} class="ml-auto my-auto w-24" size="sm">
<div class="contents" on:click|stopPropagation={() => {}}>
<Select bind:value={$legacyTheme} class="ml-auto my-auto w-24" size="sm">
<option value="system">System</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
+155 -18
View File
@@ -15,17 +15,154 @@ export enum Color {
import { get, writable } from 'svelte/store'
import { env } from '$env/dynamic/public'
import { browser } from '$app/environment'
const configuredTheme = env.PUBLIC_THEME ?? 'system'
export const theme = writable<'system' | 'light' | 'dark'>(
configuredTheme as 'system' | 'light' | 'dark'
interface ColorData {
theme: number
themes: Colors[]
}
interface Theme {
colors: Colors
name: string
id: number
}
export interface Colors {
slate: {
25?: string,
50?: string
100?: string
200?: string
400?: string
500?: string
600?: string
700?: string
800?: string
900?: string
925?: string
950?: string
},
zinc: {
100?: string
200?: string
300?: string
400?: string
500?: string
600?: string
700?: string
800?: string
900?: string
925?: string
950?: string
},
primary: {
100?: string,
900?: string,
},
other: {
black?: string,
white?: string
}
}
export const defaultColors = {
slate: {
25: 'var(--c-s-25,rgb(252,253,254))',
50: `var(--c-s-50,#f8fafc)`,
100: `var(--c-s-100,#f1f5f9)`,
200: `var(--c-s-200,#e2e8f0)`,
300: `var(--c-s-300,#cbd5e1)`,
400: `var(--c-s-400,#94a3b8)`,
500: `var(--c-s-500,#64748b)`,
600: `var(--c-s-600,#475569)`,
700: `var(--c-s-700,#334155)`,
800: `var(--c-s-800,#1e293b)`,
900: `var(--c-s-900,#0f172a`,
950: `var(--c-s-950,#020617)`,
},
zinc: {
50: `var(--c-z-50,#fafafa)`,
100: `var(--c-z-100,#f4f4f5)`,
200: `var(--c-z-200,#e4e4e7)`,
300: `var(--c-z-300,#d4d4d8)`,
400: `var(--c-z-400,#a1a1aa)`,
500: `var(--c-z-500,#71717a)`,
600: `var(--c-z-600,#52525b)`,
700: 'var(--c-z-700,#34343b)',
800: 'var(--c-z-800,#1f1f24)',
900: 'var(--c-z-900,#121215)',
925: 'var(--c-z-925,#0c0c0e)',
950: `var(--c-z-950,#09090b)`,
},
primary: {
100: 'var(--c-p-100,#f1f5f9)',
900: 'var(--c-p-900,#0f172a)',
},
other: {
black: `#000`,
white: `var(--c-o-white,#fff)`
}
}
export let colors = writable<Colors>({
slate: {},
zinc: {},
primary: {},
other: {}
})
export function colorsToVars(colors: Colors): string {
let vars: string[] = []
for (let category in colors) {
for (let shade in colors[category as keyof Colors]) {
console.log(shade)
if (category)
// @ts-ignore
vars.push(`--c-${category.slice(0, 1)}-${shade}:${colors[category][shade]}`)
}
}
return vars.join(';')
}
export const colorToVar = (category: keyof Colors, shade: keyof keyof Colors): string =>
`--c-${category.slice(0,2)}-${shade.toString()}`
colors.subscribe((v) => {
})
function loadColors() {
const loaded = localStorage.getItem('colors')
if (!loaded) return
colors.set(JSON.parse(loaded))
}
if (browser) {
loadColors()
}
const configuredLegacyTheme = env.PUBLIC_LEGACYTHEME ?? 'system'
export const legacyTheme = writable<'system' | 'light' | 'dark'>(
configuredLegacyTheme as 'system' | 'light' | 'dark'
)
export const toggleTheme = () => {
theme.update((theme) => {
if (theme == 'light') {
export const toggleLegacyTheme = () => {
legacyTheme.update((legacyTheme) => {
if (legacyTheme == 'light') {
return 'dark'
} else if (theme == 'dark') {
} else if (legacyTheme == 'dark') {
return 'system'
} else {
return 'light'
@@ -33,23 +170,23 @@ export const toggleTheme = () => {
})
}
export const inDarkTheme = (): boolean => {
export const inDarkLegacyTheme = (): boolean => {
if (typeof window != 'undefined') {
return get(theme) == 'system'
return get(legacyTheme) == 'system'
? window.matchMedia('(prefers-color-scheme: dark)').matches
: get(theme) == 'dark'
: get(legacyTheme) == 'dark'
}
return false
}
if (typeof localStorage != 'undefined') {
const localTheme: 'system' | 'light' | 'dark' =
(localStorage.getItem('theme') as 'system' | 'light' | 'dark') ||
configuredTheme
const localLegacyTheme: 'system' | 'light' | 'dark' =
(localStorage.getItem('legacyTheme') as 'system' | 'light' | 'dark') ||
configuredLegacyTheme
theme.update((theme) => localTheme)
legacyTheme.update((legacyTheme) => localLegacyTheme)
theme.subscribe((theme) => {
legacyTheme.subscribe((legacyTheme) => {
if (typeof document != 'undefined') {
const prefersDark = window.matchMedia(
'(prefers-color-scheme: dark)'
@@ -57,13 +194,13 @@ if (typeof localStorage != 'undefined') {
const html = document.querySelector('html')
if (theme == 'system') {
if (legacyTheme == 'system') {
html?.classList.toggle('dark', prefersDark)
} else {
html?.classList.toggle('dark', theme === 'dark')
html?.classList.toggle('dark', legacyTheme === 'dark')
}
localStorage.setItem('theme', theme)
localStorage.setItem('legacyTheme', legacyTheme)
}
})
}
+4 -4
View File
@@ -8,7 +8,7 @@
import Sidebar from '$lib/components/ui/sidebar/Sidebar.svelte'
// @ts-ignore
import { pwaInfo } from 'virtual:pwa-info'
import { inDarkTheme, theme } from '$lib/ui/colors.js'
import { inDarkLegacyTheme, legacyTheme } from '$lib/ui/colors.js'
import { userSettings } from '$lib/settings.js'
import { Button, Spinner, ToastContainer } from 'mono-svelte'
import { onMount } from 'svelte'
@@ -41,7 +41,7 @@
}
$: webManifest = pwaInfo ? pwaInfo.webManifest.linkTag : ''
$: darkTheme = $theme && inDarkTheme()
$: darklegacyTheme = $legacyTheme && inDarkLegacyTheme()
onMount(() => {
if (browser) {
@@ -55,7 +55,7 @@
window
.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', (event) => {
darkTheme = inDarkTheme()
darklegacyTheme = inDarkLegacyTheme()
})
document.body.querySelector('.loader')?.classList.add('hidden')
}
@@ -64,7 +64,7 @@
</script>
<svelte:head>
<meta name="theme-color" content={darkTheme ? '#09090b' : '#f8fafc'} />
<meta name="theme-color" content={darklegacyTheme ? '#09090b' : '#f8fafc'} />
{#if $site}
<title>{$site.site_view.site.name}</title>
{#if LINKED_INSTANCE_URL}
+90
View File
@@ -0,0 +1,90 @@
<script lang="ts">
import { Button, Material, Modal, TextArea, toast } from 'mono-svelte'
import { defaultColors, type Colors, colors } from '$lib/ui/colors'
import { ArrowDownTray, ArrowUpTray, Bookmark, Icon } from 'svelte-hero-icons'
const c = defaultColors
const svelteIntellisenseSucks = (): (keyof Colors)[] =>
Object.keys(c) as (keyof Colors)[]
let importing = false
let importText = ''
</script>
{#if importing}
<Modal
bind:open={importing}
on:action={() => {
try {
if (importText == '') {
throw new Error('Import is empty')
}
$colors = JSON.parse(importText)
toast({ content: 'Import successful', type: 'success' })
importing = false
} catch (err) {
// @ts-ignore
toast({ content: err, type: 'error' })
}
}}
title="Import"
action="Import"
>
<TextArea bind:value={importText} style="font-family: monospace;" />
</Modal>
{/if}
<div class="flex flex-col gap-4">
<h1 class="font-bold text-3xl">Theme</h1>
<div class="flex items-center gap-4">
<Button
on:click={() => {
importing = !importing
}}
size="lg"
>
<Icon src={ArrowUpTray} size="16" mini />
Import
</Button>
<Button
on:click={() => {
navigator.clipboard.writeText(JSON.stringify($colors))
toast({ content: 'Copied theme to clipboard.' })
}}
size="lg"
>
<Icon src={ArrowDownTray} size="16" mini />
Export
</Button>
<Button on:click={() => {}} size="lg">
<Icon src={Bookmark} size="16" mini />
Save
</Button>
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-2">
{#each svelteIntellisenseSucks() as category}
<Material class="flex flex-col gap-2">
<h1 class="capitalize font-semibold text-lg">{category}</h1>
<div class="flex flex-row gap-1 flex-wrap items-center justify-between">
{#each Object.keys(c[category]) as shade}
<div class="flex flex-col gap-0.5 w-10 group">
<div
class="w-full h-8 relative rounded-md border dark:border-zinc-600 hover:dark:border-zinc-400
transition-colors"
style="background-color: {defaultColors[category][shade]}"
>
<input
class="rounded-md border cursor-pointer absolute top-0 left-0 w-full h-full opacity-0"
type="color"
bind:value={$colors[category][shade]}
/>
</div>
<span class="font-medium capitalize">{shade}</span>
</div>
{/each}
</div>
</Material>
{/each}
</div>
</div>