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"> <script lang="ts">
import { userSettings } from '$lib/settings.js' import { userSettings } from '$lib/settings.js'
import { colors, colorsToVars } from '$lib/ui/colors'
import { routes } from '$lib/util.js' import { routes } from '$lib/util.js'
export let route: { id: string | null } | undefined = undefined export let route: { id: string | null } | undefined = undefined
@@ -11,7 +12,7 @@
<svelte:window bind:scrollY={scroll} /> <svelte:window bind:scrollY={scroll} />
<div class="shell {$$props.class}"> <div class="shell {$$props.class}" style={colorsToVars($colors)}>
<slot /> <slot />
<slot <slot
name="navbar" name="navbar"
@@ -29,17 +30,17 @@
> >
<slot <slot
name="sidebar" 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;" style="grid-area: sidebar; width: 100% !important;"
/> />
<slot <slot
name="main" 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" style="grid-area: main"
/> />
<slot <slot
name="suffix" 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;" style="grid-area: suffix;"
/> />
</div> </div>
+1 -27
View File
@@ -10,41 +10,15 @@
import Logo from '$lib/components/ui/Logo.svelte' import Logo from '$lib/components/ui/Logo.svelte'
import { LINKED_INSTANCE_URL, instance } from '$lib/instance.js' import { LINKED_INSTANCE_URL, instance } from '$lib/instance.js'
import { site } from '$lib/lemmy.js' import { site } from '$lib/lemmy.js'
import { userSettings } from '$lib/settings.js' import { Menu, MenuButton, MenuDivider, Spinner } from 'mono-svelte'
import { theme } from '$lib/ui/colors.js'
import { import {
Badge,
Button,
Menu,
MenuButton,
MenuDivider,
Select,
Spinner,
toast,
} from 'mono-svelte'
import {
Bars3,
Bookmark,
BugAnt,
CodeBracket,
CodeBracketSquare,
Cog6Tooth,
CommandLine,
ComputerDesktop,
GlobeAlt, GlobeAlt,
Heart,
Icon, Icon,
Inbox,
InformationCircle,
MagnifyingGlass, MagnifyingGlass,
Moon,
Newspaper, Newspaper,
PencilSquare, PencilSquare,
Plus, Plus,
ServerStack, ServerStack,
Sun,
UserCircle,
UserGroup,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import { fly } from 'svelte/transition' import { fly } from 'svelte/transition'
import Profile from './Profile.svelte' import Profile from './Profile.svelte'
+14 -11
View File
@@ -27,8 +27,9 @@
UserCircle, UserCircle,
UserGroup, UserGroup,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import { theme } from '$lib/ui/colors' import { legacyTheme } from '$lib/ui/colors'
import { userSettings } from '$lib/settings' import { userSettings } from '$lib/settings'
import { goto } from '$app/navigation'
</script> </script>
<Menu {...$$restProps}> <Menu {...$$restProps}>
@@ -97,26 +98,28 @@
<Icon src={Cog6Tooth} mini width={16} /> <Icon src={Cog6Tooth} mini width={16} />
Settings Settings
</MenuButton> </MenuButton>
<MenuButton class="!py-0"> <MenuButton
class="!py-0"
on:click={() => {
goto('/theme')
}}
>
<Icon <Icon
src={$theme == 'system' src={$legacyTheme == 'system'
? ComputerDesktop ? ComputerDesktop
: $theme == 'light' : $legacyTheme == 'light'
? Sun ? Sun
: $theme == 'dark' : $legacyTheme == 'dark'
? Moon ? Moon
: Moon} : Moon}
mini mini
size="16" size="16"
/> />
<span>Color Scheme</span>
<!-- svelte-ignore a11y-click-events-have-key-events --> <!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions --> <!-- svelte-ignore a11y-no-static-element-interactions -->
<div <div class="contents" on:click|stopPropagation={() => {}}>
class="flex flex-row flex-wrap justify-between w-full items-center" <Select bind:value={$legacyTheme} class="ml-auto my-auto w-24" size="sm">
on:click|stopPropagation
>
<span>Theme</span>
<Select bind:value={$theme} class="ml-auto my-auto w-24" size="sm">
<option value="system">System</option> <option value="system">System</option>
<option value="light">Light</option> <option value="light">Light</option>
<option value="dark">Dark</option> <option value="dark">Dark</option>
+155 -18
View File
@@ -15,17 +15,154 @@ export enum Color {
import { get, writable } from 'svelte/store' import { get, writable } from 'svelte/store'
import { env } from '$env/dynamic/public' import { env } from '$env/dynamic/public'
import { browser } from '$app/environment'
const configuredTheme = env.PUBLIC_THEME ?? 'system' interface ColorData {
export const theme = writable<'system' | 'light' | 'dark'>( theme: number
configuredTheme as 'system' | 'light' | 'dark' 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 = () => { export const toggleLegacyTheme = () => {
theme.update((theme) => { legacyTheme.update((legacyTheme) => {
if (theme == 'light') { if (legacyTheme == 'light') {
return 'dark' return 'dark'
} else if (theme == 'dark') { } else if (legacyTheme == 'dark') {
return 'system' return 'system'
} else { } else {
return 'light' return 'light'
@@ -33,23 +170,23 @@ export const toggleTheme = () => {
}) })
} }
export const inDarkTheme = (): boolean => { export const inDarkLegacyTheme = (): boolean => {
if (typeof window != 'undefined') { if (typeof window != 'undefined') {
return get(theme) == 'system' return get(legacyTheme) == 'system'
? window.matchMedia('(prefers-color-scheme: dark)').matches ? window.matchMedia('(prefers-color-scheme: dark)').matches
: get(theme) == 'dark' : get(legacyTheme) == 'dark'
} }
return false return false
} }
if (typeof localStorage != 'undefined') { if (typeof localStorage != 'undefined') {
const localTheme: 'system' | 'light' | 'dark' = const localLegacyTheme: 'system' | 'light' | 'dark' =
(localStorage.getItem('theme') as 'system' | 'light' | 'dark') || (localStorage.getItem('legacyTheme') as 'system' | 'light' | 'dark') ||
configuredTheme configuredLegacyTheme
theme.update((theme) => localTheme) legacyTheme.update((legacyTheme) => localLegacyTheme)
theme.subscribe((theme) => { legacyTheme.subscribe((legacyTheme) => {
if (typeof document != 'undefined') { if (typeof document != 'undefined') {
const prefersDark = window.matchMedia( const prefersDark = window.matchMedia(
'(prefers-color-scheme: dark)' '(prefers-color-scheme: dark)'
@@ -57,13 +194,13 @@ if (typeof localStorage != 'undefined') {
const html = document.querySelector('html') const html = document.querySelector('html')
if (theme == 'system') { if (legacyTheme == 'system') {
html?.classList.toggle('dark', prefersDark) html?.classList.toggle('dark', prefersDark)
} else { } 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' import Sidebar from '$lib/components/ui/sidebar/Sidebar.svelte'
// @ts-ignore // @ts-ignore
import { pwaInfo } from 'virtual:pwa-info' 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 { userSettings } from '$lib/settings.js'
import { Button, Spinner, ToastContainer } from 'mono-svelte' import { Button, Spinner, ToastContainer } from 'mono-svelte'
import { onMount } from 'svelte' import { onMount } from 'svelte'
@@ -41,7 +41,7 @@
} }
$: webManifest = pwaInfo ? pwaInfo.webManifest.linkTag : '' $: webManifest = pwaInfo ? pwaInfo.webManifest.linkTag : ''
$: darkTheme = $theme && inDarkTheme() $: darklegacyTheme = $legacyTheme && inDarkLegacyTheme()
onMount(() => { onMount(() => {
if (browser) { if (browser) {
@@ -55,7 +55,7 @@
window window
.matchMedia('(prefers-color-scheme: dark)') .matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', (event) => { .addEventListener('change', (event) => {
darkTheme = inDarkTheme() darklegacyTheme = inDarkLegacyTheme()
}) })
document.body.querySelector('.loader')?.classList.add('hidden') document.body.querySelector('.loader')?.classList.add('hidden')
} }
@@ -64,7 +64,7 @@
</script> </script>
<svelte:head> <svelte:head>
<meta name="theme-color" content={darkTheme ? '#09090b' : '#f8fafc'} /> <meta name="theme-color" content={darklegacyTheme ? '#09090b' : '#f8fafc'} />
{#if $site} {#if $site}
<title>{$site.site_view.site.name}</title> <title>{$site.site_view.site.name}</title>
{#if LINKED_INSTANCE_URL} {#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>
+30 -8
View File
@@ -1,3 +1,5 @@
import colors from 'tailwindcss/colors'
/** @type {import('tailwindcss').Config} */ /** @type {import('tailwindcss').Config} */
export default { export default {
content: [ content: [
@@ -23,19 +25,39 @@ export default {
}, },
colors: { colors: {
slate: { slate: {
200: '#e8edf4', 25: 'var(--c-s-25,rgb(252,253,254))',
25: 'rgb(252,253,254)', 50: `var(--c-s-50,${colors.slate[50]})`,
100: `var(--c-s-100,${colors.slate[100]})`,
200: `var(--c-s-200,#e8edf4)`,
300: `var(--c-s-300,${colors.slate[300]})`,
400: `var(--c-s-400,${colors.slate[400]})`,
500: `var(--c-s-500,${colors.slate[500]})`,
600: `var(--c-s-600,${colors.slate[600]})`,
700: `var(--c-s-700,${colors.slate[700]})`,
800: `var(--c-s-800,${colors.slate[800]})`,
900: `var(--c-s-900,${colors.slate[900]})`,
950: `var(--c-s-950,${colors.slate[950]})`,
}, },
zinc: { zinc: {
700: '#34343b', 50: `var(--c-z-50,${colors.zinc[50]})`,
800: '#1f1f24', 100: `var(--c-z-100,${colors.zinc[100]})`,
900: '#121215', 200: `var(--c-z-200,${colors.zinc[200]})`,
925: '#0c0c0e', 300: `var(--c-z-300,${colors.zinc[300]})`,
400: `var(--c-z-400,${colors.zinc[400]})`,
500: `var(--c-z-500,${colors.zinc[500]})`,
600: `var(--c-z-600,${colors.zinc[600]})`,
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,${colors.zinc[950]})`,
}, },
primary: { primary: {
100: '#f1f5f9', 100: 'var(--c-p-100,#f1f5f9)',
900: '#0f172a', 900: 'var(--c-p-900,#0f172a)',
}, },
black: `#000`,
white: `var(--c-o-white,#fff)`
}, },
}, },
}, },