| 1 | export type ThemeKind = "light" | "dark"; |
| 2 | |
| 3 | export type AppTheme |
| 4 | id: string |
| 5 | name: string |
| 6 | kind: ThemeKind |
| 7 | blurb: string |
| 8 | /** [background, foreground, primary] swatch for the picker */ |
| 9 | swatches: [ |
| 10 | shiki: string |
| 11 | }; |
| 12 | |
| 13 | export const APP_THEMES: |
| 14 | { |
| 15 | id: "light", |
| 16 | name: "Light", |
| 17 | kind: "light", |
| 18 | blurb: "Default light", |
| 19 | swatches: ["#fcfcfd", |
| 20 | shiki: "pierre-light", |
| 21 | }, |
| 22 | { |
| 23 | id: "dark", |
| 24 | name: "Dark", |
| 25 | kind: "dark", |
| 26 | blurb: "Default dark", |
| 27 | swatches: ["#0f0e11", |
| 28 | shiki: "pierre-dark", |
| 29 | }, |
| 30 | { |
| 31 | id: "vesper", |
| 32 | name: "Vesper", |
| 33 | kind: "dark", |
| 34 | blurb: "Warm near-black", |
| 35 | swatches: ["#101010", |
| 36 | shiki: "vesper", |
| 37 | }, |
| 38 | { |
| 39 | id: "dracula", |
| 40 | name: "Dracula", |
| 41 | kind: "dark", |
| 42 | blurb: "Classic purple", |
| 43 | swatches: ["#282a36", |
| 44 | shiki: "dracula", |
| 45 | }, |
| 46 | { |
| 47 | id: "tokyo-night", |
| 48 | name: "Tokyo Night", |
| 49 | kind: "dark", |
| 50 | blurb: "Cool blue night", |
| 51 | swatches: ["#1a1b26", |
| 52 | shiki: "tokyo-night", |
| 53 | }, |
| 54 | { |
| 55 | id: "catppuccin", |
| 56 | name: "Catppuccin Mocha", |
| 57 | kind: "dark", |
| 58 | blurb: "Pastel dark", |
| 59 | swatches: ["#1e1e2e", |
| 60 | shiki: "catppuccin-mocha", |
| 61 | }, |
| 62 | { |
| 63 | id: "nord", |
| 64 | name: "Nord", |
| 65 | kind: "dark", |
| 66 | blurb: "Arctic calm", |
| 67 | swatches: ["#2e3440", |
| 68 | shiki: "nord", |
| 69 | }, |
| 70 | { |
| 71 | id: "one-dark-pro", |
| 72 | name: "One Dark Pro", |
| 73 | kind: "dark", |
| 74 | blurb: "Atom classic", |
| 75 | swatches: ["#282c34", |
| 76 | shiki: "one-dark-pro", |
| 77 | }, |
| 78 | { |
| 79 | id: "monokai", |
| 80 | name: "Monokai", |
| 81 | kind: "dark", |
| 82 | blurb: "High-contrast pop", |
| 83 | swatches: ["#272822", |
| 84 | shiki: "monokai", |
| 85 | }, |
| 86 | { |
| 87 | id: "gruvbox", |
| 88 | name: "Gruvbox", |
| 89 | kind: "dark", |
| 90 | blurb: "Retro groove", |
| 91 | swatches: ["#282828", |
| 92 | shiki: "gruvbox-dark-medium", |
| 93 | }, |
| 94 | { |
| 95 | id: "github-dark", |
| 96 | name: "GitHub Dark", |
| 97 | kind: "dark", |
| 98 | blurb: "Dimmed GitHub", |
| 99 | swatches: ["#0d1117", |
| 100 | shiki: "github-dark", |
| 101 | }, |
| 102 | { |
| 103 | id: "rose-pine", |
| 104 | name: "Rosé Pine", |
| 105 | kind: "dark", |
| 106 | blurb: "Muted rose", |
| 107 | swatches: ["#232136", |
| 108 | shiki: "rose-pine-moon", |
| 109 | }, |
| 110 | { |
| 111 | id: "custom", |
| 112 | name: "Custom", |
| 113 | kind: "dark", |
| 114 | blurb: "Your colors", |
| 115 | swatches: ["#18181b", |
| 116 | shiki: "github-dark", |
| 117 | }, |
| 118 | ]; |
| 119 | |
| 120 | export const APP_THEME_IDS = |
| 121 | |
| 122 | const THEME_BY_ID = new |
| 123 | |
| 124 | export function getAppTheme(id |
| 125 | if (id && |
| 126 | return THEME_BY_ID.get |
| 127 | } |
| 128 | |
| 129 | export function themeKind( |
| 130 | id: string |
| 131 | customBase?: ThemeKind |
| 132 | ): ThemeKind { |
| 133 | if (id === |
| 134 | return getAppTheme(id |
| 135 | } |
| 136 | |
| 137 | export type CustomThemeVars |
| 138 | base: ThemeKind |
| 139 | background: string |
| 140 | foreground: string |
| 141 | primary: string |
| 142 | accent: string |
| 143 | radius: string |
| 144 | }; |
| 145 | |
| 146 | export const CUSTOM_THEME_KEY = |
| 147 | |
| 148 | export const DEFAULT_CUSTOM_THEME: |
| 149 | base: "dark", |
| 150 | background: "#18181b", |
| 151 | foreground: "#fafafa", |
| 152 | primary: "#8b5cf6", |
| 153 | accent: "#27272a", |
| 154 | radius: "0.625rem", |
| 155 | }; |
| 156 | |
| 157 | export function loadCustomTheme(): |
| 158 | if (typeof window |
| 159 | try { |
| 160 | const raw = |
| 161 | if (!raw) |
| 162 | const parsed = |
| 163 | return { |
| 164 | base: parsed.base |
| 165 | background: |
| 166 | validCssColor(parsed.background |
| 167 | foreground: |
| 168 | validCssColor(parsed.foreground |
| 169 | primary: validCssColor(parsed |
| 170 | accent: validCssColor(parsed |
| 171 | radius: |
| 172 | typeof parsed.radius |
| 173 | ? parsed.radius |
| 174 | : DEFAULT_CUSTOM_THEME.radius, |
| 175 | }; |
| 176 | } catch { |
| 177 | return DEFAULT_CUSTOM_THEME; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | export function saveCustomTheme(vars |
| 182 | try { |
| 183 | window.localStorage.setItem |
| 184 | } catch { |
| 185 | /* storage full / blocked — theme just won't persist */ |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | function validCssColor(value: |
| 190 | if (typeof value |
| 191 | if (typeof window |
| 192 | return value; |
| 193 | const el = |
| 194 | el.color = |
| 195 | return el.color |
| 196 | } |
| 197 | |
| 198 | /** Push custom-theme vars onto <html> as CSS variables. No-op on server. */ |
| 199 | export function applyCustomThemeVars(vars |
| 200 | if (typeof document |
| 201 | const root = |
| 202 | root.style.setProperty |
| 203 | root.style.setProperty |
| 204 | root.style.setProperty |
| 205 | root.style.setProperty |
| 206 | root.style.setProperty |
| 207 | root.style.setProperty |
| 208 | } |
| 209 | |
| 210 | export function clearCustomThemeVars() |
| 211 | if (typeof document |
| 212 | const root = |
| 213 | for (const prop |
| 214 | "--background", |
| 215 | "--foreground", |
| 216 | "--primary", |
| 217 | "--accent", |
| 218 | "--ring", |
| 219 | "--radius", |
| 220 | ]) { |
| 221 | root.style.removeProperty |
| 222 | } |
| 223 | } |