Merge pull request #373 from Xyphyn/new-theme-handler

feat: remake theme handling
This commit is contained in:
Xylight authored and GitHub committed 2024-07-25 12:32:25 -07:00
commit e40d3c5b4a
13 files changed
+684 -281

No files matched your search

+18 -27
View File
@@ -9,12 +9,11 @@
// @ts-ignore
import { pwaInfo } from 'virtual:pwa-info'
import {
colors,
colorsToVars,
defaultColors,
inDarkLegacyTheme,
legacyTheme,
type Colors,
colorScheme,
inDarkColorScheme,
rgbToHex,
theme,
themeVars,
} from '$lib/ui/colors.js'
import { userSettings } from '$lib/settings.js'
import { Button, ModalContainer, Spinner, ToastContainer } from 'mono-svelte'
@@ -28,6 +27,7 @@
import ExpandableImage from '$lib/components/ui/ExpandableImage.svelte'
import { LINKED_INSTANCE_URL } from '$lib/instance'
import { locale } from '$lib/translations'
import { getDefaultColors } from '$lib/ui/presets'
nProgress.configure({
minimum: 0.4,
@@ -51,7 +51,6 @@
}
$: webManifest = pwaInfo ? pwaInfo.webManifest.linkTag : ''
$: darklegacyTheme = $legacyTheme && inDarkLegacyTheme()
onMount(() => {
if (browser) {
@@ -59,37 +58,29 @@
history.replaceState(
null,
'',
window.location.toString().replace('#main', ''),
window.location.toString().replace('#main', '')
)
}
window
.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', (event) => {
darklegacyTheme = inDarkLegacyTheme()
themeColor = getMetaColor($colors)
})
document.body.querySelector('.loader')?.classList.add('hidden')
themeVars.subscribe((vars) => {
document.body.setAttribute('style', vars)
})
}
})
$: title = routes[($page.route.id as keyof typeof routes) ?? '']
$: browser ? document.body.setAttribute('style', colorsToVars($colors)) : ''
// svelte reactivity goof
const getMetaColor = (colors: Colors): string | undefined => {
const regex = /var\([^,]+,\s*(#[0-9A-Fa-f]{6})\)/m
return darklegacyTheme
? colors.zinc?.[925] ?? defaultColors.zinc[925].match(regex)?.[1]
: colors.slate?.[25] ?? defaultColors.slate[25].match(regex)?.[1]
}
$: themeColor = getMetaColor($colors)
</script>
<svelte:head>
<meta name="theme-color" content={themeColor} />
{#if $site?.site_view}
<title>{$site?.site_view.site.name}</title>
<meta
name="theme-color"
content={rgbToHex(
$colorScheme && inDarkColorScheme()
? $theme.colors.zinc?.[925] ?? getDefaultColors().zinc[925]
: $theme.colors.slate?.[25] ?? getDefaultColors().slate[25]
)}
/>
{#if LINKED_INSTANCE_URL}
<link rel="icon" href={$site?.site_view?.site.icon} />
<meta name="description" content={$site?.site_view?.site.description} />
+128 -46
View File
@@ -3,30 +3,32 @@
Button,
Material,
Modal,
Note,
TextArea,
action,
modal,
toast,
} from 'mono-svelte'
import { defaultColors, type Colors, colors } from '$lib/ui/colors'
import {
ArrowDownTray,
ArrowPath,
ArrowUpTray,
Bookmark,
CheckCircle,
Icon,
Plus,
} from 'svelte-hero-icons'
import ColorSwatch from './ColorSwatch.svelte'
import { t } from '$lib/translations'
import Header from '$lib/components/ui/layout/pages/Header.svelte'
const c = defaultColors
const svelteIntellisenseSucks = (): (keyof Colors)[] =>
['slate', 'zinc', 'other'] as (keyof Colors)[]
import { calculateVars, hexToRgb, theme, themeData } from '$lib/ui/colors'
import { getDefaultColors } from '$lib/ui/presets'
import ThemePreset from './ThemePreset.svelte'
let importing = false
let importText = ''
$: defaultColors = getDefaultColors()
</script>
{#if importing}
@@ -37,7 +39,7 @@
if (importText == '') {
throw new Error('Import is empty')
}
$colors = JSON.parse(importText)
$theme.colors = JSON.parse(importText)
toast({ content: $t('message.success'), type: 'success' })
importing = false
} catch (err) {
@@ -54,19 +56,52 @@
<div class="flex flex-col gap-4 h-full">
<Header>Theme</Header>
<div
class="grid grid-cols-2 lg:grid-cols-3 xl:grid-cols-4
gap-4 overflow-auto max-h-72"
>
{#each $themeData.themes as theme}
<ThemePreset bind:theme />
{/each}
<button
on:click={() => {
const newTheme = {
id: Math.max(...$themeData.themes.map((t) => t.id)) + 1,
colors: getDefaultColors(),
name: $t('routes.theme.preset.new'),
}
$themeData.themes = [...$themeData.themes, newTheme]
$themeData.currentTheme = newTheme.id
}}
>
<Material
color="none"
padding="sm"
class="border-dashed border-2 border-slate-400 dark:border-zinc-600
text-slate-400 dark:text-zinc-600 hover:border-slate-600 dark:hover:border-zinc-400
grid place-items-center w-14 h-14"
rounding="xl"
>
<Icon src={Plus} size="28" />
</Material>
</button>
</div>
<div class="flex items-center gap-4">
<Button
on:click={() => {
importing = !importing
}}
size="lg"
disabled={$theme.id <= 0}
>
<Icon src={ArrowUpTray} size="16" mini />
{$t('routes.theme.import')}
</Button>
<Button
on:click={() => {
navigator.clipboard.writeText(JSON.stringify($colors))
navigator.clipboard.writeText(JSON.stringify($theme.colors))
toast({ content: 'Copied theme to clipboard.' })
}}
size="lg"
@@ -75,6 +110,7 @@
{$t('routes.theme.export')}
</Button>
<Button
disabled={$theme.id <= 0}
on:click={() => {
modal({
actions: [
@@ -84,7 +120,7 @@
}),
action({
action: () => {
$colors = { other: {}, primary: {}, zinc: {}, slate: {} }
$theme.colors = { other: {}, primary: {}, zinc: {}, slate: {} }
},
content: $t('routes.theme.reset'),
close: true,
@@ -101,43 +137,89 @@
{$t('routes.theme.reset')}
</Button>
</div>
<Material color="transparent" class="items-center gap-x-4 color-grid gap-y-2">
<h1 class="text-2xl font-bold col-span-2">{$t('routes.theme.accent')}</h1>
<ColorSwatch
backgroundColor={defaultColors.primary[900]}
bind:value={$colors.primary[900]}
class="!w-12 !h-12 col-span-1"
/>
<ColorSwatch
backgroundColor={defaultColors.primary[100]}
bind:value={$colors.primary[100]}
class="!w-12 !h-12 col-span-1"
/>
<span class="font-semibold text-base">
{$t('nav.menu.colorscheme.light')}
</span>
<span class="font-semibold text-base">
{$t('nav.menu.colorscheme.dark')}
</span>
</Material>
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-2">
{#each svelteIntellisenseSucks() as category}
<Material color="transparent" 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 space-evenly">
{#each Object.keys(c[category]) as shade}
<div class="flex flex-col gap-0.5 w-10 group">
<!--@ts-ignore-->
<ColorSwatch
bind:value={$colors[category][shade]}
backgroundColor={defaultColors[category][shade]}
/>
<span class="font-medium capitalize">{shade}</span>
</div>
{/each}
</div>
</Material>
{/each}
{#if $theme.id <= 0}
<Note>
{$t('routes.theme.preset.description')}
</Note>
{/if}
<div
class="relative"
class:opacity-50={$theme.id <= 0}
class:pointer-events-none={$theme.id <= 0}
>
<Material
color="transparent"
class="items-center gap-x-4 color-grid gap-y-2"
>
<h1 class="text-2xl font-bold col-span-2">{$t('routes.theme.accent')}</h1>
<ColorSwatch
value={$theme.colors.primary?.[900]}
on:change={(e) => {
$theme.colors.primary[900] = e.detail
}}
backgroundColor={defaultColors.primary[900]}
on:contextmenu={(e) => {
e.preventDefault()
$theme.colors.primary[900] =
// @ts-ignore
defaultColors.primary[900]
return true
}}
class="!w-12 !h-12 col-span-1"
/>
<ColorSwatch
value={$theme.colors.primary?.[100]}
on:change={(e) => {
$theme.colors.primary[100] = e.detail
}}
backgroundColor={defaultColors.primary[100]}
on:contextmenu={(e) => {
e.preventDefault()
$theme.colors.primary[100] =
// @ts-ignore
defaultColors.primary[100]
return true
}}
class="!w-12 !h-12 col-span-1"
/>
<span class="font-semibold text-base">
{$t('nav.menu.colorscheme.light')}
</span>
<span class="font-semibold text-base">
{$t('nav.menu.colorscheme.dark')}
</span>
</Material>
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-2">
{#each Object.entries(defaultColors) as [category, value]}
<Material color="transparent" 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 space-evenly">
{#each Object.entries(value) as [shade, color]}
<div class="flex flex-col gap-0.5 w-10 group">
<!--@ts-ignore-->
<ColorSwatch
bind:value={$theme.colors[category][shade]}
on:change={(e) => {
$theme.colors[category][shade] = e.detail
}}
on:contextmenu={(e) => {
e.preventDefault()
$theme.colors[category][shade] =
// @ts-ignore
defaultColors[category][shade]
return true
}}
/>
<span class="font-medium capitalize">{shade}</span>
</div>
{/each}
</div>
</Material>
{/each}
</div>
</div>
<div
class="flex items-center gap-2 sm:gap-8 flex-col sm:flex-row mt-auto mx-auto text-sm text-slate-600 dark:text-zinc-400"
+8 -2
View File
@@ -1,16 +1,22 @@
<script lang="ts">
export let backgroundColor: string = ''
import { hexToRgb } from '$lib/ui/colors'
import { createEventDispatcher } from 'svelte'
export let value: string
const dispatch = createEventDispatcher<{ change: string }>()
</script>
<div
class="w-full h-8 relative rounded-md border dark:border-zinc-600 hover:dark:border-zinc-400
transition-colors {$$props.class}"
style="background-color: {backgroundColor}"
style="background-color: rgb({value});"
>
<input
class="rounded-md border cursor-pointer absolute top-0 left-0 w-full h-full opacity-0"
type="color"
bind:value
on:change={() => dispatch('change', hexToRgb(value))}
on:contextmenu
/>
</div>
+97
View File
@@ -0,0 +1,97 @@
<script lang="ts">
import { t } from '$lib/translations'
import { calculateVars, themeData, type Theme } from '$lib/ui/colors'
import { action, Button, Material, modal, TextInput } from 'mono-svelte'
import { CheckCircle, Icon, Trash } from 'svelte-hero-icons'
export let theme: Theme
let editingName = false
</script>
<button class="h-full" on:click={() => ($themeData.currentTheme = theme.id)}>
<Material
padding="none"
class="{theme.id == $themeData.currentTheme
? 'ring-2 ring-inset ring-primary-900 dark:ring-primary-100'
: ''} flex relative cursor-pointer h-full flex-col text-left p-0.5"
>
<div
style="{calculateVars(theme)};"
class="h-20 w-full rounded-lg rounded-b-none flex flex-row flex-wrap p-3 gap-2
bg-slate-25 dark:bg-zinc-925"
>
<div class="text-slate-900 dark:text-zinc-100 text-xs">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</div>
<div
class="bg-white dark:bg-zinc-900 w-8 h-4 rounded-sm border border-slate-200 dark:border-zinc-800"
/>
<div
class="w-3 h-3 bg-primary-900 dark:bg-primary-100 rounded-full ml-auto"
/>
</div>
{#if theme.id == $themeData.currentTheme}
<Icon
src={CheckCircle}
size="20"
solid
class="absolute top-0 right-0 m-2 text-primary-900 dark:text-primary-100"
/>
{/if}
<div class="px-4 py-2 flex items-center gap-1 justify-between">
{#if editingName}
<form on:submit|preventDefault={() => (editingName = false)}>
<TextInput bind:value={theme.name}></TextInput>
</form>
{:else}
<button
on:click={() => {
if (theme.id > 0) editingName = true
}}
class="text-left font-medium text-lg font-display"
>
{theme.name}
</button>
{/if}
{#if theme.id > 0}
<Button
color="ghost"
size="square-md"
class="flex-shrink-0"
on:click={() => {
modal({
actions: [
action({
close: true,
type: 'secondary',
content: $t('common.cancel'),
}),
{
close: true,
content: $t('routes.theme.preset.delete.confirm'),
type: 'danger',
action: () => {
const index = $themeData.themes
.map((t) => t.id)
.indexOf(theme.id)
$themeData.themes = $themeData.themes.toSpliced(index, 1)
if (theme.id == $themeData.currentTheme) {
$themeData.currentTheme = 0
}
},
},
],
type: 'error',
body: '',
title: $t('routes.theme.preset.delete.title'),
})
}}
>
<Icon src={Trash} size="16" mini slot="prefix" />
</Button>
{/if}
</div>
</Material>
</button>