@adea-ai/ui 0.151.15 → 0.152.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/components/composites/accent-picker/accent-picker.d.ts +44 -10
  2. package/dist/components/composites/accent-picker/accent-picker.d.ts.map +1 -1
  3. package/dist/components/composites/accent-picker/accent-picker.js +155 -58
  4. package/dist/components/composites/accent-picker/accent-picker.js.map +1 -1
  5. package/dist/components/composites/accent-picker/color-math.d.ts +13 -0
  6. package/dist/components/composites/accent-picker/color-math.d.ts.map +1 -0
  7. package/dist/components/composites/accent-picker/color-math.js +61 -0
  8. package/dist/components/composites/accent-picker/color-math.js.map +1 -0
  9. package/dist/components/composites/accent-picker/custom-color-popover.d.ts +18 -0
  10. package/dist/components/composites/accent-picker/custom-color-popover.d.ts.map +1 -0
  11. package/dist/components/composites/accent-picker/custom-color-popover.js +248 -0
  12. package/dist/components/composites/accent-picker/custom-color-popover.js.map +1 -0
  13. package/dist/components/composites/accent-picker/index.d.ts +1 -1
  14. package/dist/components/composites/accent-picker/index.d.ts.map +1 -1
  15. package/dist/components/composites/accent-picker/index.js +1 -1
  16. package/dist/components/composites/appearance-editor/appearance-choices.d.ts.map +1 -1
  17. package/dist/components/composites/appearance-editor/appearance-choices.js +37 -74
  18. package/dist/components/composites/appearance-editor/appearance-choices.js.map +1 -1
  19. package/dist/components/composites/icon-picker/icon-picker.js +1 -1
  20. package/dist/components/composites/icon-picker/icon-picker.js.map +1 -1
  21. package/dist/index.js +2 -2
  22. package/dist/r/accent-picker.json +13 -0
  23. package/dist/r/appearance-editor.json +1 -0
  24. package/dist/r/registry.json +14 -0
  25. package/dist/r/src/components/composites/accent-picker/accent-picker.tsx +154 -38
  26. package/dist/r/src/components/composites/accent-picker/color-math.ts +54 -0
  27. package/dist/r/src/components/composites/accent-picker/custom-color-popover.tsx +220 -0
  28. package/dist/r/src/components/composites/accent-picker/index.ts +6 -1
  29. package/dist/r/src/components/composites/appearance-editor/appearance-choices.tsx +34 -58
  30. package/dist/r/src/components/composites/icon-picker/icon-picker.tsx +1 -1
  31. package/package.json +1 -1
  32. package/registry.json +14 -0
  33. package/src/components/composites/accent-picker/accent-picker.tsx +154 -38
  34. package/src/components/composites/accent-picker/color-math.ts +54 -0
  35. package/src/components/composites/accent-picker/custom-color-popover.tsx +220 -0
  36. package/src/components/composites/accent-picker/index.ts +6 -1
  37. package/src/components/composites/appearance-editor/appearance-choices.tsx +34 -58
  38. package/src/components/composites/icon-picker/icon-picker.tsx +1 -1
@@ -0,0 +1,54 @@
1
+ /** HSV with hue in degrees [0, 360) and saturation/value in [0, 1]. */
2
+ export type Hsv = Readonly<{ h: number; s: number; v: number }>
3
+
4
+ const clamp = (value: number, min = 0, max = 1) => Math.min(max, Math.max(min, value))
5
+
6
+ /** `#rrggbb` (any case) to HSV; anything else is `undefined`. */
7
+ export function hexToHsv(hex: string): Hsv | undefined {
8
+ const match = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(hex.trim())
9
+ if (!match) return undefined
10
+ const [r, g, b] = match.slice(1).map((part) => Number.parseInt(part, 16) / 255) as [
11
+ number,
12
+ number,
13
+ number,
14
+ ]
15
+ const max = Math.max(r, g, b)
16
+ const delta = max - Math.min(r, g, b)
17
+ let h = 0
18
+ if (delta !== 0) {
19
+ if (max === r) h = ((g - b) / delta) % 6
20
+ else if (max === g) h = (b - r) / delta + 2
21
+ else h = (r - g) / delta + 4
22
+ }
23
+ h = (h * 60 + 360) % 360
24
+ return { h, s: max === 0 ? 0 : delta / max, v: max }
25
+ }
26
+
27
+ /** HSV to lowercase `#rrggbb`. */
28
+ export function hsvToHex(hsv: Hsv): `#${string}` {
29
+ const h = ((hsv.h % 360) + 360) % 360
30
+ const s = clamp(hsv.s)
31
+ const v = clamp(hsv.v)
32
+ const c = v * s
33
+ const x = c * (1 - Math.abs(((h / 60) % 2) - 1))
34
+ const m = v - c
35
+ const [r, g, b] =
36
+ h < 60
37
+ ? [c, x, 0]
38
+ : h < 120
39
+ ? [x, c, 0]
40
+ : h < 180
41
+ ? [0, c, x]
42
+ : h < 240
43
+ ? [0, x, c]
44
+ : h < 300
45
+ ? [x, 0, c]
46
+ : [c, 0, x]
47
+ const part = (channel: number) =>
48
+ Math.round((channel + m) * 255)
49
+ .toString(16)
50
+ .padStart(2, '0')
51
+ return `#${part(r)}${part(g)}${part(b)}`
52
+ }
53
+
54
+ export { clamp }
@@ -0,0 +1,220 @@
1
+ import type { JSX } from 'solid-js'
2
+ import { Show, createSignal } from 'solid-js'
3
+ import { cn } from '#lib/utils'
4
+ import { Button } from '../../ui/button'
5
+ import { Input } from '../../ui/input'
6
+ import { Popover, PopoverContent, PopoverTrigger } from '../../ui/popover'
7
+ import { clamp, hexToHsv, hsvToHex, type Hsv } from './color-math'
8
+
9
+ /**
10
+ * The Custom entry of the accent picker: a button that opens a colour picker
11
+ * anchored right under it, right edges aligned since Custom ends the row (a
12
+ * saturation/brightness field, a hue strip and a hex field). Nothing is committed while the colour is being explored; Apply (or
13
+ * Enter in the hex field) commits it and closes, Escape or a click outside
14
+ * leaves the stored colour alone. The operating system's colour panel is not
15
+ * used because on macOS it floats wherever it was last left, not by the button.
16
+ */
17
+ export function CustomColorPopover(props: {
18
+ /** The stored custom colour, or `undefined` while a preset or inherit is chosen. */
19
+ value: string | undefined
20
+ /** Where the picker starts while no custom colour is stored. */
21
+ seed: string
22
+ disabled?: boolean
23
+ onCommit(value: `#${string}`): void
24
+ }) {
25
+ const [open, setOpen] = createSignal(false)
26
+ const [draft, setDraft] = createSignal<Hsv>({ h: 0, s: 0, v: 0 })
27
+ const [hexText, setHexText] = createSignal('')
28
+ const draftHex = () => hsvToHex(draft())
29
+ // The hex field is what Apply commits, so text that is not a colour yet blocks it.
30
+ const typedHex = () => {
31
+ const text = hexText().trim()
32
+ return hexToHsv(text.startsWith('#') ? text : `#${text}`)
33
+ }
34
+
35
+ const start = () => {
36
+ const initial = hexToHsv(props.value ?? props.seed) ?? { h: 262, s: 0.8, v: 0.9 }
37
+ setDraft(initial)
38
+ setHexText(hsvToHex(initial))
39
+ }
40
+ const update = (next: Hsv) => {
41
+ setDraft(next)
42
+ setHexText(hsvToHex(next))
43
+ }
44
+ const commit = () => {
45
+ if (!typedHex()) return
46
+ props.onCommit(draftHex())
47
+ setOpen(false)
48
+ }
49
+
50
+ return (
51
+ <Popover
52
+ open={open()}
53
+ onOpenChange={(next) => {
54
+ if (next) start()
55
+ setOpen(next)
56
+ }}
57
+ placement="bottom-end"
58
+ >
59
+ <PopoverTrigger
60
+ as={Button}
61
+ type="button"
62
+ variant={props.value ? 'secondary' : 'outline'}
63
+ size="sm"
64
+ data-slot="swatch-custom"
65
+ disabled={props.disabled}
66
+ aria-pressed={props.value !== undefined}
67
+ class="shrink-0 gap-1.5"
68
+ >
69
+ <Show when={props.value}>
70
+ {(color) => (
71
+ <span
72
+ aria-hidden="true"
73
+ data-slot="swatch-custom-chip"
74
+ class="size-3 rounded-full border border-border bg-(--swatch-custom)"
75
+ style={{ '--swatch-custom': color() }}
76
+ />
77
+ )}
78
+ </Show>
79
+ Custom
80
+ </PopoverTrigger>
81
+ <PopoverContent class="w-60" aria-label="Custom colour">
82
+ <div class="flex flex-col gap-3" data-slot="custom-color">
83
+ <DragArea
84
+ label="Saturation and brightness"
85
+ value={Math.round(draft().s * 100)}
86
+ max={100}
87
+ valueText={`Saturation ${Math.round(draft().s * 100)}%, brightness ${Math.round(draft().v * 100)}%`}
88
+ class="h-36 rounded-md"
89
+ style={{
90
+ 'background-image': `linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, hsl(${draft().h} 100% 50%))`,
91
+ }}
92
+ thumb={{ x: draft().s, y: 1 - draft().v }}
93
+ onPoint={(x, y) => update({ ...draft(), s: x, v: 1 - y })}
94
+ onStep={(dx, dy) =>
95
+ update({ ...draft(), s: clamp(draft().s + dx), v: clamp(draft().v - dy) })
96
+ }
97
+ />
98
+ <DragArea
99
+ label="Hue"
100
+ value={Math.round(draft().h)}
101
+ max={360}
102
+ valueText={`${Math.round(draft().h)} degrees`}
103
+ class="h-3 rounded-full"
104
+ style={{
105
+ 'background-image':
106
+ 'linear-gradient(to right, #f00, #ff0, #0f0, #0ff, #00f, #f0f, #f00)',
107
+ }}
108
+ thumb={{ x: draft().h / 360, y: 0.5 }}
109
+ onPoint={(x) => update({ ...draft(), h: Math.min(359.99, x * 360) })}
110
+ onStep={(dx) =>
111
+ update({ ...draft(), h: Math.min(359.99, Math.max(0, draft().h + dx * 360)) })
112
+ }
113
+ />
114
+ <div class="flex items-center gap-2">
115
+ <span
116
+ aria-hidden="true"
117
+ class="size-8 shrink-0 rounded-md border border-border bg-(--swatch-draft)"
118
+ style={{ '--swatch-draft': draftHex() }}
119
+ />
120
+ <Input
121
+ aria-label="Hex colour"
122
+ class="min-w-0 flex-1 font-mono"
123
+ value={hexText()}
124
+ maxLength={7}
125
+ spellcheck={false}
126
+ aria-invalid={typedHex() ? undefined : true}
127
+ onInput={(event) => {
128
+ const text = event.currentTarget.value.trim()
129
+ setHexText(text)
130
+ const parsed = typedHex()
131
+ if (parsed) setDraft(parsed)
132
+ }}
133
+ onKeyDown={(event) => {
134
+ if (event.key === 'Enter') {
135
+ event.preventDefault()
136
+ commit()
137
+ }
138
+ }}
139
+ />
140
+ <Button type="button" size="sm" disabled={!typedHex()} onClick={commit}>
141
+ Apply
142
+ </Button>
143
+ </div>
144
+ </div>
145
+ </PopoverContent>
146
+ </Popover>
147
+ )
148
+ }
149
+
150
+ /**
151
+ * A pointer- and keyboard-driven area reporting a position in [0, 1] on each
152
+ * axis: the saturation/brightness field and the hue strip. Its listeners are
153
+ * native (`on:`) rather than delegated, so this module registers nothing on
154
+ * `window.document` when it loads.
155
+ */
156
+ function DragArea(props: {
157
+ label: string
158
+ /** The value assistive technology reads, from 0 to `max`; the 2D field reports saturation and names both axes in `valueText`. */
159
+ value: number
160
+ max: number
161
+ valueText: string
162
+ class: string
163
+ style: JSX.CSSProperties
164
+ thumb: { x: number; y: number }
165
+ onPoint(x: number, y: number): void
166
+ onStep(dx: number, dy: number): void
167
+ }) {
168
+ const point = (event: PointerEvent) => {
169
+ const rect = (event.currentTarget as HTMLElement).getBoundingClientRect()
170
+ if (rect.width === 0 || rect.height === 0) return
171
+ props.onPoint(
172
+ clamp((event.clientX - rect.left) / rect.width),
173
+ clamp((event.clientY - rect.top) / rect.height)
174
+ )
175
+ }
176
+ return (
177
+ <div
178
+ role="slider"
179
+ tabIndex={0}
180
+ aria-label={props.label}
181
+ aria-valuemin={0}
182
+ aria-valuemax={props.max}
183
+ aria-valuenow={props.value}
184
+ aria-valuetext={props.valueText}
185
+ data-slot="custom-color-area"
186
+ class={cn(
187
+ 'relative w-full cursor-crosshair touch-none border border-border outline-none',
188
+ 'focus-visible:ring-3 focus-visible:ring-primary-subtle',
189
+ props.class
190
+ )}
191
+ style={props.style}
192
+ on:pointerdown={(event) => {
193
+ event.currentTarget.setPointerCapture?.(event.pointerId)
194
+ point(event)
195
+ }}
196
+ on:pointermove={(event) => {
197
+ if (event.buttons & 1) point(event)
198
+ }}
199
+ on:keydown={(event) => {
200
+ const step = event.shiftKey ? 0.1 : 0.02
201
+ const moves: Record<string, [number, number]> = {
202
+ ArrowLeft: [-step, 0],
203
+ ArrowRight: [step, 0],
204
+ ArrowUp: [0, -step],
205
+ ArrowDown: [0, step],
206
+ }
207
+ const move = moves[event.key]
208
+ if (!move) return
209
+ event.preventDefault()
210
+ props.onStep(move[0], move[1])
211
+ }}
212
+ >
213
+ <span
214
+ aria-hidden="true"
215
+ class="pointer-events-none absolute size-3.5 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-background shadow-sm ring-1 ring-border"
216
+ style={{ left: `${props.thumb.x * 100}%`, top: `${props.thumb.y * 100}%` }}
217
+ />
218
+ </div>
219
+ )
220
+ }
@@ -1 +1,6 @@
1
- export { AccentPicker, type AccentPickerProps } from './accent-picker'
1
+ export {
2
+ AccentPicker,
3
+ isCustomAccentValue,
4
+ type AccentPickerOption,
5
+ type AccentPickerProps,
6
+ } from './accent-picker'
@@ -28,8 +28,7 @@ import { Monitor, Moon, Sun } from 'lucide-solid'
28
28
  import { For, Show } from 'solid-js'
29
29
  import { isThemeAccentId } from '#lib/theme-accents'
30
30
  import { THEME_DEFAULT_ID } from '#lib/theme-defaults'
31
- import { Button } from '../../ui/button'
32
- import { Input } from '../../ui/input'
31
+ import { AccentPicker, isCustomAccentValue } from '../accent-picker'
33
32
  import { AccentSwatchGroups } from './accent-swatch-groups'
34
33
  import type { AppearanceEditorProps } from './appearance-types'
35
34
  import { ThemeMiniature, ThemeMiniatureSplit } from './theme-preview'
@@ -135,15 +134,24 @@ export function AccentChoices(props: AppearanceEditorProps) {
135
134
  ? [{ id: THEME_DEFAULT_ID, label: 'Theme default' }, ...presets]
136
135
  : presets
137
136
  }
138
- // The `row` style's Custom entry is a button that opens the native colour
139
- // selector, so the draft only changes once a colour is chosen there. The
140
- // input stays in the tree for the selector to anchor to and for the value a
141
- // saved hex restores.
142
- let colorPicker: HTMLInputElement | undefined
143
- const openCustomColor = () => {
144
- if (!colorPicker || props.saving) return
145
- if (typeof colorPicker.showPicker === 'function') colorPicker.showPicker()
146
- else colorPicker.click()
137
+ // The `row` style is the shared AccentPicker every accent setting uses: the
138
+ // swatches, then a Custom button that opens the native colour selector, so the
139
+ // draft only changes once a colour is chosen there. The app has nothing to
140
+ // inherit from, so there is no inherit swatch; "Theme default" leads when the
141
+ // host keeps it.
142
+ const rowOptions = () => {
143
+ const presets = props.accentOptions.filter((option) => option.id !== THEME_DEFAULT_ID)
144
+ return showThemeDefault()
145
+ ? [
146
+ {
147
+ id: THEME_DEFAULT_ID,
148
+ label: 'Theme default',
149
+ light: themeDefaultColor(),
150
+ dark: themeDefaultColor(),
151
+ },
152
+ ...presets,
153
+ ]
154
+ : presets
147
155
  }
148
156
  return (
149
157
  <Show
@@ -209,53 +217,21 @@ export function AccentChoices(props: AppearanceEditorProps) {
209
217
  </Radio>
210
218
  }
211
219
  >
212
- <div class="flex w-full min-w-0 items-center gap-2" data-accent-choices>
213
- <Radio
214
- value={accentSelection()}
215
- disabled={props.saving}
216
- orientation="horizontal"
217
- aria-label="Accent"
218
- class="min-w-0 flex-1"
219
- onChange={(accent) => props.onChange({ accent })}
220
- >
221
- <AccentSwatchGroups
222
- accentOptions={props.accentOptions}
223
- resolvedAppearance={resolvedAppearance()}
224
- showThemeDefault={showThemeDefault()}
225
- themeDefaultColor={themeDefaultColor()}
226
- layout="row"
227
- />
228
- </Radio>
229
- <Button
230
- type="button"
231
- variant="outline"
232
- size="sm"
233
- tabIndex={0}
234
- disabled={props.saving}
235
- aria-pressed={isCustomAccent(props)}
236
- onClick={openCustomColor}
237
- >
238
- Custom
239
- </Button>
240
- <Input
241
- ref={(element) => {
242
- colorPicker = element
243
- }}
244
- type="color"
245
- // `w-px` beats the Input's own `w-full`: an absolutely positioned
246
- // full-width input sized the sheet's scroll width.
247
- class="sr-only w-px"
248
- tabIndex={-1}
249
- aria-label="Custom accent color"
250
- value={
251
- /^#[0-9a-f]{6}$/i.test(props.draft.accent)
252
- ? props.draft.accent
253
- : (props.customAccentValue ?? '#000000')
254
- }
255
- disabled={props.saving}
256
- onChange={(event) => props.onChange({ accent: event.currentTarget.value })}
257
- />
258
- </div>
220
+ <AccentPicker
221
+ label="Accent"
222
+ inherit={false}
223
+ options={rowOptions()}
224
+ layout="row"
225
+ appearance={resolvedAppearance()}
226
+ value={props.draft.accent}
227
+ customSeed={
228
+ isCustomAccentValue(props.customAccentValue) ? props.customAccentValue : '#000000'
229
+ }
230
+ disabled={props.saving}
231
+ onValueChange={(accent) => {
232
+ if (accent !== null) props.onChange({ accent })
233
+ }}
234
+ />
259
235
  </Show>
260
236
  )
261
237
  }
@@ -231,7 +231,7 @@ export function IconPickerDropdown(props: IconPickerDropdownProps) {
231
231
  </span>
232
232
  <ChevronDown class="size-4 text-muted-foreground" aria-hidden="true" />
233
233
  </PopoverTrigger>
234
- <PopoverContent class="w-[min(22rem,var(--kb-popper-available-width))]">
234
+ <PopoverContent class="w-88 max-w-(--kb-popper-content-available-width)">
235
235
  <IconPicker
236
236
  {...picker}
237
237
  onValueChange={(key) => {