| 1 | "use client"; |
| 2 | |
| 3 | import { Check, Monitor, Moon, Palette, Sun } from "lucide-react"; |
| 4 | import { useTheme } |
| 5 | import { useState, useSyncExternalStore |
| 6 | |
| 7 | import { Button } |
| 8 | import { |
| 9 | Dialog, |
| 10 | DialogContent, |
| 11 | DialogDescription, |
| 12 | DialogFooter, |
| 13 | DialogHeader, |
| 14 | DialogTitle, |
| 15 | } from "@/components/ui/dialog"; |
| 16 | import { |
| 17 | DropdownMenu, |
| 18 | DropdownMenuContent, |
| 19 | DropdownMenuItem, |
| 20 | DropdownMenuLabel, |
| 21 | DropdownMenuSeparator, |
| 22 | DropdownMenuSub, |
| 23 | DropdownMenuSubContent, |
| 24 | DropdownMenuSubTrigger, |
| 25 | DropdownMenuTrigger, |
| 26 | } from "@/components/ui/dropdown-menu"; |
| 27 | import { Input } |
| 28 | import { Label } |
| 29 | import { |
| 30 | CUSTOM_THEME_UPDATED_EVENT, |
| 31 | useCustomTheme, |
| 32 | } from "@/components/theme-effects"; |
| 33 | import { |
| 34 | APP_THEMES, |
| 35 | DEFAULT_CUSTOM_THEME, |
| 36 | loadCustomTheme, |
| 37 | saveCustomTheme, |
| 38 | type CustomThemeVars, |
| 39 | } from "@/lib/themes"; |
| 40 | import { cn } |
| 41 | |
| 42 | function Swatch({ colors |
| 43 | return ( |
| 44 | <span className="flex -space-x-1 |
| 45 | {colors.map(( |
| 46 | <span |
| 47 | key={c} |
| 48 | className="size-4 rounded-full border border-black/20" |
| 49 | style={{ backgroundColor: |
| 50 | /> |
| 51 | ))} |
| 52 | </span> |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | function useMounted() { |
| 57 | return useSyncExternalStore( |
| 58 | () => () => |
| 59 | () => true, |
| 60 | () => false, |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | export function ThemePicker({ |
| 65 | align = "end" |
| 66 | }: { |
| 67 | align?: " |
| 68 | }) { |
| 69 | const mounted = |
| 70 | const { theme |
| 71 | const [customOpen, |
| 72 | |
| 73 | const activeDef = |
| 74 | |
| 75 | return ( |
| 76 | <> |
| 77 | <DropdownMenu> |
| 78 | <DropdownMenuTrigger asChild> |
| 79 | <Button variant=" |
| 80 | {mounted && activeDef |
| 81 | <span |
| 82 | className="size-4 rounded-full border border-black/20" |
| 83 | style={{ backgroundColor: |
| 84 | /> |
| 85 | ) : ( |
| 86 | <Palette /> |
| 87 | )} |
| 88 | </Button> |
| 89 | </DropdownMenuTrigger> |
| 90 | <DropdownMenuContent align={ |
| 91 | <DropdownMenuLabel>Theme</DropdownMenuLabel |
| 92 | <ThemeMenuItems onCustomize={ |
| 93 | </DropdownMenuContent> |
| 94 | </DropdownMenu> |
| 95 | <CustomThemeDialog open={ |
| 96 | </> |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | /** The theme choices as a submenu of another menu. The custom theme dialog is the caller's to render, since this unmounts when the menu closes. */ |
| 101 | export function ThemeSubmenu({ |
| 102 | return ( |
| 103 | <DropdownMenuSub> |
| 104 | <DropdownMenuSubTrigger> |
| 105 | <Palette className=" |
| 106 | Theme |
| 107 | </DropdownMenuSubTrigger> |
| 108 | <DropdownMenuSubContent className=" |
| 109 | <ThemeMenuItems onCustomize={ |
| 110 | </DropdownMenuSubContent> |
| 111 | </DropdownMenuSub> |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | function ThemeMenuItems({ onCustomize |
| 116 | const { theme, |
| 117 | const mounted = |
| 118 | const { custom |
| 119 | |
| 120 | const active = |
| 121 | |
| 122 | return ( |
| 123 | <> |
| 124 | <DropdownMenuItem onClick={ |
| 125 | <Monitor className=" |
| 126 | <span className="flex-1 |
| 127 | {active === "system |
| 128 | </DropdownMenuItem> |
| 129 | <DropdownMenuSeparator /> |
| 130 | {APP_THEMES.map(( |
| 131 | <DropdownMenuItem |
| 132 | key={t.id |
| 133 | onClick={() => |
| 134 | className={cn(t |
| 135 | > |
| 136 | <Swatch |
| 137 | colors={ |
| 138 | t.id === |
| 139 | ? [custom.background |
| 140 | : t.swatches |
| 141 | } |
| 142 | /> |
| 143 | <span className="flex-1 |
| 144 | {t.name} |
| 145 | <span className="block text-xs text-muted-foreground |
| 146 | {t.id === |
| 147 | </span> |
| 148 | </span> |
| 149 | {active === t |
| 150 | </DropdownMenuItem> |
| 151 | ))} |
| 152 | <DropdownMenuSeparator /> |
| 153 | <DropdownMenuItem onSelect={ |
| 154 | <Palette className=" |
| 155 | <span className="flex-1 |
| 156 | </DropdownMenuItem> |
| 157 | </> |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | const COLOR_FIELDS = [ |
| 162 | { key: "background", |
| 163 | { key: "foreground", |
| 164 | { key: "primary", |
| 165 | { key: "accent", |
| 166 | ] as const; |
| 167 | |
| 168 | export function CustomThemeDialog({ |
| 169 | open, |
| 170 | onOpenChange, |
| 171 | }: { |
| 172 | open: boolean |
| 173 | onOpenChange: (open |
| 174 | }) { |
| 175 | const { setTheme |
| 176 | const [draft, |
| 177 | |
| 178 | const handleOpenChange = |
| 179 | if (next) setDraft |
| 180 | onOpenChange(next); |
| 181 | }; |
| 182 | |
| 183 | const set = |
| 184 | key: K |
| 185 | value: CustomThemeVars |
| 186 | ) => setDraft( |
| 187 | |
| 188 | const save = |
| 189 | saveCustomTheme(draft); |
| 190 | window.dispatchEvent(new |
| 191 | setTheme("custom"); |
| 192 | onOpenChange(false); |
| 193 | }; |
| 194 | |
| 195 | const reset = |
| 196 | |
| 197 | return ( |
| 198 | <Dialog open={ |
| 199 | <DialogContent className=" |
| 200 | <DialogHeader> |
| 201 | <DialogTitle>Custom theme</DialogTitle |
| 202 | <DialogDescription> |
| 203 | Pick your own colors. They are stored in this browser and applied |
| 204 | whenever the Custom theme is active. |
| 205 | </DialogDescription> |
| 206 | </DialogHeader> |
| 207 | |
| 208 | <div className="flex flex-col gap-4 |
| 209 | <div className="flex flex-col gap-2 |
| 210 | <Label>Base</Label |
| 211 | <div className="grid grid-cols-2 gap-2 |
| 212 | <Button |
| 213 | type="button" |
| 214 | variant={draft.base |
| 215 | size="sm" |
| 216 | onClick={() => |
| 217 | > |
| 218 | <Sun data-icon=" |
| 219 | Light |
| 220 | </Button> |
| 221 | <Button |
| 222 | type="button" |
| 223 | variant={draft.base |
| 224 | size="sm" |
| 225 | onClick={() => |
| 226 | > |
| 227 | <Moon data-icon=" |
| 228 | Dark |
| 229 | </Button> |
| 230 | </div> |
| 231 | <p className="text-xs text-muted-foreground |
| 232 | Base decides code highlighting, toasts and dark-mode utilities. |
| 233 | </p> |
| 234 | </div> |
| 235 | |
| 236 | <div className="grid grid-cols-2 gap-3 |
| 237 | {COLOR_FIELDS.map(( |
| 238 | <div key={key |
| 239 | <Label htmlFor={ |
| 240 | <div className="flex items-center gap-2 |
| 241 | <input |
| 242 | id={`custom-${key |
| 243 | type="color" |
| 244 | aria-label={`${label |
| 245 | value={toColorInputValue(draft |
| 246 | onChange={(e) |
| 247 | className="h-9 w-10 shrink-0 cursor-pointer rounded-md border bg-transparent p-1" |
| 248 | /> |
| 249 | <Input |
| 250 | id={`custom-${key |
| 251 | value={draft[key |
| 252 | onChange={(e) |
| 253 | spellCheck={false} |
| 254 | className="font-mono text-xs" |
| 255 | /> |
| 256 | </div> |
| 257 | </div> |
| 258 | ))} |
| 259 | </div> |
| 260 | |
| 261 | {/* live preview */} |
| 262 | <div |
| 263 | className="overflow-hidden rounded-lg border" |
| 264 | style={{ |
| 265 | backgroundColor: draft.background |
| 266 | color: draft.foreground |
| 267 | }} |
| 268 | > |
| 269 | <div className="flex items-center gap-2 px-3 py-2 text-sm font-medium |
| 270 | <span |
| 271 | className="size-2.5 rounded-full" |
| 272 | style={{ backgroundColor: |
| 273 | /> |
| 274 | Preview |
| 275 | </div> |
| 276 | <div className="flex items-center gap-2 px-3 pb-3 |
| 277 | <span |
| 278 | className="rounded-md px-2.5 py-1 text-xs font-medium" |
| 279 | style={{ |
| 280 | backgroundColor: draft.primary |
| 281 | color: draft.background |
| 282 | }} |
| 283 | > |
| 284 | Primary button |
| 285 | </span> |
| 286 | <span |
| 287 | className="rounded-md px-2.5 py-1 text-xs" |
| 288 | style={{ |
| 289 | backgroundColor: draft.accent |
| 290 | color: draft.foreground |
| 291 | }} |
| 292 | > |
| 293 | Accent chip |
| 294 | </span> |
| 295 | </div> |
| 296 | </div> |
| 297 | </div> |
| 298 | |
| 299 | <DialogFooter> |
| 300 | <Button type=" |
| 301 | Reset |
| 302 | </Button> |
| 303 | <Button type=" |
| 304 | Save & apply |
| 305 | </Button> |
| 306 | </DialogFooter> |
| 307 | </DialogContent> |
| 308 | </Dialog> |
| 309 | ); |
| 310 | } |
| 311 | |
| 312 | /** <input type="color"> needs #rrggbb; fall back to black for named colors. */ |
| 313 | function toColorInputValue(value: |
| 314 | if (/^#[0-9a-fA-F] |
| 315 | const m = |
| 316 | if (m) { |
| 317 | const [r, |
| 318 | return `#${r |
| 319 | } |
| 320 | return "#000000"; |
| 321 | } |