source: Klonkt/src/services/ThemeService.js@ 8485b64

main
Last change on this file since 8485b64 was 8485b64, checked in by Robin Genis <roboburr@…>, 3 months ago

Remove the Gold accent colour (unused; redundant with Amber/Klonkt-gold)

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/**
2 * ThemeService — Palette and theme management
3 *
4 * 8 Built-in Palettes (from v9):
5 * - Sage (default, warm crème)
6 * - Paper (minimalist white)
7 * - Ocean (cool blues)
8 * - Forest (greens)
9 * - Stone (grays)
10 * - Midnight (dark blue)
11 * - Sunset (warm oranges)
12 * - Cream (light beige)
13 *
14 * Dark/Light mode toggle stored per user
15 */
16
17class ThemeService {
18 // Paper/ink values map 1-to-1 from the [data-palette] CSS in style.css (= what
19 // is ACTUALLY applied); the accent dot is a representative color per palette.
20 static PALETTES = {
21 // Brand default — matches klonkt.com (dark blue + gold).
22 klonkt: {
23 name: 'Klonkt',
24 light: { paper: '#f3f1ea', ink: '#11141c', accent: '#c98a2a' },
25 dark: { paper: '#0b0d12', ink: '#f3f1ea', accent: '#e8b04b' }
26 },
27 sage: {
28 name: 'Sage',
29 light: { paper: '#faf8f3', ink: '#1a1a1a', accent: '#c2410c' },
30 dark: { paper: '#1c1a17', ink: '#f4ede0', accent: '#c2410c' }
31 },
32 paper: {
33 name: 'Paper',
34 light: { paper: '#ffffff', ink: '#09090b', accent: '#000000' },
35 dark: { paper: '#0a0a0a', ink: '#fafafa', accent: '#ffffff' }
36 },
37 forest: {
38 name: 'Forest',
39 light: { paper: '#f2f6ed', ink: '#1a2e15', accent: '#4d7c2a' },
40 dark: { paper: '#0d1f12', ink: '#dcf2d0', accent: '#6fae3f' }
41 },
42 stone: {
43 name: 'Stone',
44 light: { paper: '#f5f0e8', ink: '#2b2218', accent: '#8a6a45' },
45 dark: { paper: '#1a130b', ink: '#f5e9d5', accent: '#b89366' }
46 },
47 midnight: {
48 name: 'Midnight',
49 light: { paper: '#f3f1f8', ink: '#1e1a2e', accent: '#7c5cbf' },
50 dark: { paper: '#0f0a1f', ink: '#e9def7', accent: '#9d88e0' }
51 },
52 sunset: {
53 name: 'Sunset',
54 light: { paper: '#fdf4f3', ink: '#2e1618', accent: '#d6477f' },
55 dark: { paper: '#1f0a14', ink: '#fce7f3', accent: '#f06fa3' }
56 },
57 cream: {
58 name: 'Cream',
59 light: { paper: '#fefaf0', ink: '#2a1f0f', accent: '#d97706' },
60 dark: { paper: '#1a1208', ink: '#fef3d6', accent: '#f0a93a' }
61 },
62 rose: {
63 name: 'Rose',
64 light: { paper: '#fdf2f4', ink: '#2e1419', accent: '#e11d6b' },
65 dark: { paper: '#1f0a0f', ink: '#fce4ea', accent: '#f06b9a' }
66 },
67 // key stays 'mint' (DB-safe), but recolored to warm Terracotta — less green.
68 mint: {
69 name: 'Terracotta',
70 light: { paper: '#faf2ee', ink: '#2e1a12', accent: '#c2410c' },
71 dark: { paper: '#1f120c', ink: '#f7e6da', accent: '#e8783f' }
72 },
73 lilac: {
74 name: 'Lilac',
75 light: { paper: '#faf4fb', ink: '#2a1830', accent: '#a855f7' },
76 dark: { paper: '#170a1c', ink: '#f3e2f7', accent: '#c084fc' }
77 }
78 };
79
80 /**
81 * Curated accent palette — admins pick one of these instead of a free-form
82 * hex picker. Keeps the brand consistent and avoids unreadable combinations.
83 * Each color works against both light and dark themes.
84 */
85 // Balanced across the color wheel — fewer greens/blues (4 of 12),
86 // more warm + purple/pink variation. All readable on both light and dark.
87 static ACCENTS = [
88 { key: 'klonkt', name: 'Klonkt-geel', color: '#e8b04b' },
89 { key: 'red', name: 'Rood', color: '#dc2626' },
90 { key: 'orange', name: 'Oranje', color: '#ea580c' },
91 { key: 'amber', name: 'Amber', color: '#d97706' },
92 { key: 'forest', name: 'Groen', color: '#16a34a' },
93 { key: 'teal', name: 'Turquoise', color: '#0d9488' },
94 { key: 'ocean', name: 'Blauw', color: '#2563eb' },
95 { key: 'indigo', name: 'Indigo', color: '#4f46e5' },
96 { key: 'violet', name: 'Violet', color: '#7c3aed' },
97 { key: 'plum', name: 'Magenta', color: '#c026d3' },
98 { key: 'pink', name: 'Roze', color: '#db2777' },
99 { key: 'brown', name: 'Bruin', color: '#9a3412' },
100 ];
101
102 static listAccents() {
103 return this.ACCENTS;
104 }
105
106 /**
107 * Validate an accent color. Returns the canonical hex if it's in the curated
108 * set (case-insensitive match), or null if it's not. Server-side validation
109 * uses this so we never persist arbitrary hex from the form.
110 */
111 static validateAccent(hex) {
112 if (!hex || typeof hex !== 'string') return null;
113 const target = hex.trim().toLowerCase();
114 const found = this.ACCENTS.find(a => a.color.toLowerCase() === target);
115 return found ? found.color : null;
116 }
117
118 /**
119 * Get palette data
120 */
121 static getPalette(paletteKey) {
122 return this.PALETTES[paletteKey] || this.PALETTES.klonkt;
123 }
124
125 /**
126 * Get all available palettes (with full color data so a picker UI can
127 * render true previews of paper/ink/accent for both light and dark).
128 */
129 static listPalettes() {
130 return Object.entries(this.PALETTES).map(([key, data]) => ({
131 key,
132 name: data.name,
133 light: data.light,
134 dark: data.dark,
135 }));
136 }
137
138 /**
139 * Generate CSS variables for palette + theme
140 */
141 static generateCSSVars(paletteKey, theme = 'dark', accentColor = null) {
142 const palette = this.getPalette(paletteKey);
143 const colors = theme === 'dark' ? palette.dark : palette.light;
144 const accent = accentColor || colors.accent;
145
146 return `
147 :root {
148 --palette: ${paletteKey};
149 --theme: ${theme};
150 --paper: ${colors.paper};
151 --ink: ${colors.ink};
152 --accent: ${accent};
153 }
154 `.trim();
155 }
156
157 /**
158 * Update user's theme preference
159 */
160 static updateUserTheme(db, userId, theme, palette) {
161 if (!['dark', 'light'].includes(theme)) {
162 throw new Error('Invalid theme. Must be "dark" or "light"');
163 }
164 if (!this.PALETTES[palette]) {
165 throw new Error('Invalid palette');
166 }
167
168 db.prepare(`
169 UPDATE users SET theme = ?, palette = ? WHERE id = ?
170 `).run(theme, palette, userId);
171 }
172
173 /**
174 * Update site's palette + accent
175 */
176 static updateSitePalette(db, siteId, paletteKey, accentColor) {
177 if (!this.PALETTES[paletteKey]) {
178 throw new Error('Invalid palette');
179 }
180 if (!/^#[0-9a-f]{6}$/i.test(accentColor)) {
181 throw new Error('Invalid accent color. Must be hex #RRGGBB');
182 }
183
184 db.prepare(`
185 UPDATE sites SET palette = ?, accent = ? WHERE id = ?
186 `).run(paletteKey, accentColor, siteId);
187 }
188
189 /**
190 * Generate full HTML theme meta tags
191 */
192 static generateThemeMeta(userTheme, userPalette, siteTheme, siteAccent) {
193 const theme = userTheme || siteTheme || 'dark';
194 const palette = userPalette || 'klonkt';
195 const accent = siteAccent || '#e8b04b';
196 const paletteData = this.getPalette(palette);
197 const colors = theme === 'dark' ? paletteData.dark : paletteData.light;
198
199 return {
200 colorScheme: 'dark light',
201 themeColor: accent,
202 appleMobileWebAppStatusBarStyle: 'black-translucent',
203 cssVars: this.generateCSSVars(palette, theme, accent),
204 inline: `
205 <style>
206 html {
207 color-scheme: ${theme === 'dark' ? 'dark light' : 'light dark'};
208 }
209 :root {
210 --paper: ${colors.paper};
211 --ink: ${colors.ink};
212 --accent: ${accent};
213 }
214 </style>
215 <script>
216 // Apply theme ASAP (before paint) to avoid flash
217 (function() {
218 try {
219 const t = localStorage.getItem('pcms-theme') || '${theme}';
220 const p = localStorage.getItem('pcms-palette') || '${palette}';
221 document.documentElement.setAttribute('data-theme', t);
222 document.documentElement.setAttribute('data-palette', p);
223 } catch(e) {}
224 })();
225 </script>
226 `.trim()
227 };
228 }
229}
230
231export default ThemeService;
Note: See TracBrowser for help on using the repository browser.