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