-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplePersistDemo.tsx
More file actions
195 lines (179 loc) · 6.3 KB
/
SimplePersistDemo.tsx
File metadata and controls
195 lines (179 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import {useStore} from '@codebelt/classy-store/react';
import {useState} from 'react';
import {Panel} from './Panel';
import {preferencesHandle, preferencesStore} from './persistStores';
import {RenderBadge} from './RenderBadge';
import {useRenderCount} from './useRenderCount';
const accentColors = [
{id: 'indigo' as const, label: 'Indigo', tw: 'bg-indigo-500'},
{id: 'emerald' as const, label: 'Emerald', tw: 'bg-emerald-500'},
{id: 'rose' as const, label: 'Rose', tw: 'bg-rose-500'},
{id: 'amber' as const, label: 'Amber', tw: 'bg-amber-500'},
{id: 'cyan' as const, label: 'Cyan', tw: 'bg-cyan-500'},
];
function ThemeToggle() {
const theme = useStore(preferencesStore, (state) => state.theme);
const renders = useRenderCount();
return (
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<span className="text-sm text-zinc-400">Theme</span>
<button
type="button"
onClick={() =>
preferencesStore.setTheme(theme === 'dark' ? 'light' : 'dark')
}
className="relative inline-flex h-7 w-12 items-center rounded-full transition-colors cursor-pointer"
style={{backgroundColor: theme === 'dark' ? '#6366f1' : '#d1d5db'}}
>
<span
className="inline-block h-5 w-5 rounded-full bg-white transition-transform"
style={{
transform:
theme === 'dark' ? 'translateX(24px)' : 'translateX(4px)',
}}
/>
</button>
<span className="text-sm font-mono text-zinc-300">{theme}</span>
</div>
<RenderBadge count={renders} />
</div>
);
}
function FontSizeSlider() {
const fontSize = useStore(preferencesStore, (state) => state.fontSize);
const renders = useRenderCount();
return (
<div className="flex items-center justify-between">
<div className="flex items-center gap-3 flex-1 mr-3">
<span className="text-sm text-zinc-400 shrink-0">Font Size</span>
<input
type="range"
min={12}
max={24}
value={fontSize}
onChange={(event) =>
preferencesStore.setFontSize(Number(event.target.value))
}
className="flex-1 accent-emerald-500 cursor-pointer"
/>
<span className="text-sm font-mono text-zinc-300 w-8 text-right">
{fontSize}
</span>
</div>
<RenderBadge count={renders} />
</div>
);
}
function AccentColorPicker() {
const accentColor = useStore(preferencesStore, (state) => state.accentColor);
const renders = useRenderCount();
return (
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<span className="text-sm text-zinc-400">Accent</span>
<div className="flex gap-1.5">
{accentColors.map((color) => (
<button
key={color.id}
type="button"
onClick={() => preferencesStore.setAccentColor(color.id)}
className={`w-6 h-6 rounded-full ${color.tw} cursor-pointer transition-all ${
accentColor === color.id
? 'ring-2 ring-white ring-offset-2 ring-offset-zinc-900 scale-110'
: 'opacity-60 hover:opacity-100'
}`}
title={color.label}
/>
))}
</div>
</div>
<RenderBadge count={renders} />
</div>
);
}
function LivePreview() {
const snap = useStore(preferencesStore);
const renders = useRenderCount();
return (
<div className="mt-4">
<div className="flex items-center justify-between mb-2">
<span className="text-xs text-zinc-500 uppercase tracking-wide">
Live Preview
</span>
<RenderBadge count={renders} />
</div>
<div
className="rounded-lg p-4 border transition-all"
style={{
backgroundColor: snap.theme === 'dark' ? '#18181b' : '#f4f4f5',
borderColor: snap.cssVars['--accent'],
fontSize: snap.cssVars['--font-size'],
color: snap.theme === 'dark' ? '#e4e4e7' : '#27272a',
}}
>
<p style={{color: snap.cssVars['--accent']}} className="font-semibold">
Hello, Persist!
</p>
<p className="mt-1 opacity-70">
These preferences survive page refresh.
</p>
</div>
</div>
);
}
function StorageViewer() {
const [raw, setRaw] = useState(() => localStorage.getItem('preferences'));
const snap = useStore(preferencesStore);
// Re-read storage on every render triggered by store changes
const current = localStorage.getItem('preferences');
if (current !== raw) {
setRaw(current);
}
// Prevent unused variable lint warning -- we read the full snapshot
// to re-render when any property changes, which triggers the storage read above.
void snap;
return (
<div className="mt-4">
<span className="text-xs text-zinc-500 uppercase tracking-wide block mb-1.5">
localStorage["preferences"]
</span>
<pre className="text-xs bg-zinc-950 border border-zinc-800 rounded-lg p-3 overflow-x-auto text-emerald-400/80 font-mono leading-relaxed">
{raw ? JSON.stringify(JSON.parse(raw), null, 2) : '(empty)'}
</pre>
</div>
);
}
export function SimplePersistDemo() {
return (
<Panel
title="Simple Persist"
description="persist(store, { name: 'preferences' }) — that's it. All properties are saved to localStorage automatically."
>
<div className="flex flex-col gap-4">
<ThemeToggle />
<FontSizeSlider />
<AccentColorPicker />
</div>
<LivePreview />
<StorageViewer />
<div className="mt-4 flex items-center gap-3">
<button
type="button"
onClick={async () => {
preferencesStore.reset();
await preferencesHandle.clear();
// Re-persist from the reset state
await preferencesHandle.save();
}}
className="px-3 py-1.5 rounded-lg bg-zinc-700 text-zinc-300 text-sm font-medium hover:bg-zinc-600 transition-colors cursor-pointer"
>
Reset & Clear Storage
</button>
<span className="text-xs text-zinc-500">
Refresh the page — your preferences survive.
</span>
</div>
</Panel>
);
}