Skip to content

Commit 80292da

Browse files
committed
feature: restoring colors
1 parent 65b8145 commit 80292da

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

src/lib/state/colors.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { formatHex, okhsl, rgb, type Okhsl } from "culori"
33
import * as R from "remeda"
44
import { fromStore } from "$lib/utils"
55
import { undo } from "$lib/undo"
6-
import { combineLatest, map, shareReplay, type Observable } from "rxjs"
6+
import { combineLatest, debounceTime, map, shareReplay, skip, type Observable } from "rxjs"
77
import type { ColorName } from "@catppuccin/palette"
88
import { mocha, presetToOkhsl } from "$lib/presets"
99

@@ -26,7 +26,14 @@ const colorVars$ = colors$.pipe(
2626
)
2727
)
2828

29+
export const restoreAvailable = !!globalThis.localStorage && !!localStorage.getItem("colors")
30+
31+
// Runs in the browser
2932
if (globalThis.document) {
33+
colors$.pipe(skip(2), debounceTime(400)).subscribe((colors) => {
34+
localStorage.setItem("colors", JSON.stringify(colors))
35+
})
36+
3037
colorVars$.subscribe((newVariables) => {
3138
newVariables.forEach(([key, value]) => {
3239
globalThis.document.documentElement.style.setProperty(key, value)

src/routes/+layout.svelte

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import "@unocss/reset/tailwind.css"
66
import "virtual:uno.css"
77
import "../app.css"
8-
import { activeColorHsl$, activeColor, colors$ } from "$lib/state/colors"
8+
import { activeColorHsl$, activeColor, colors$, setColors } from "$lib/state/colors"
99
import { cssVar } from "$lib/utils"
1010
// import { Toaster } from "svelte-sonner"
1111
import { type Okhsl } from "culori"
1212
import Button from "$lib/components/ui/button/button.svelte"
13+
import Json from "$lib/icons/json.svelte"
1314
1415
function getActiveColorForeground(active: Okhsl) {
1516
const lightness = active.l
@@ -22,11 +23,16 @@
2223
}
2324
2425
let smallScreenDialog: HTMLDialogElement
26+
let restoreColorsDialog: HTMLDialogElement
2527
2628
$effect(() => {
2729
if (window.innerWidth < 640) {
2830
smallScreenDialog.showModal()
2931
}
32+
33+
if (localStorage.getItem("colors")) {
34+
restoreColorsDialog.showModal()
35+
}
3036
})
3137
</script>
3238

@@ -35,15 +41,6 @@
3541
</noscript>
3642

3743
<!-- <Toaster /> -->
38-
<dialog bind:this={smallScreenDialog} class="bg-transparent">
39-
<div
40-
class="border-5 flex flex-col gap-5 rounded-xl text-xl border-red px-6 py-3 bg-crust text-rosewater"
41-
>
42-
This app does not work properly on small screens.
43-
44-
<form method="dialog"><Button onclick={() => smallScreenDialog.close()}>Okay</Button></form>
45-
</div>
46-
</dialog>
4744

4845
<div
4946
class="min-h-screen flex flex-col max-h-screen h-screen font-sans"
@@ -55,6 +52,39 @@
5552
</main>
5653
</div>
5754

55+
<dialog bind:this={restoreColorsDialog} class="bg-transparent border-none">
56+
<div class="bg-base rounded-2xl border-2 border-subtext0 p-6 flex flex-col gap-6">
57+
<h1 class="text-lg text-rosewater">Restore colors of last session?</h1>
58+
<div class="flex gap-6">
59+
<form method="dialog">
60+
<Button variant="link" onclick={() => restoreColorsDialog.close()}>Nah</Button>
61+
</form>
62+
<Button
63+
autofocus
64+
onclick={() => {
65+
const saved = localStorage.getItem("colors")
66+
if (saved) {
67+
const colors = JSON.parse(saved)
68+
setColors(colors)
69+
restoreColorsDialog.close()
70+
}
71+
restoreColorsDialog.close()
72+
}}>Restore colors</Button
73+
>
74+
</div>
75+
</div>
76+
</dialog>
77+
78+
<dialog bind:this={smallScreenDialog} class="bg-transparent">
79+
<div
80+
class="border-5 flex flex-col gap-5 rounded-xl text-xl border-red px-6 py-3 bg-crust text-rosewater"
81+
>
82+
This app does not work properly on small screens.
83+
84+
<form method="dialog"><Button onclick={() => smallScreenDialog.close()}>Okay</Button></form>
85+
</div>
86+
</dialog>
87+
5888
<style>
5989
::backdrop {
6090
background-color: theme("colors.crust");

0 commit comments

Comments
 (0)