@widgetic/design-system 0.5.28 → 0.5.30
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.
- package/dist/components/chat/ChatHeader.svelte +29 -3
- package/dist/components/chat/ChatHeader.svelte.d.ts +5 -0
- package/dist/components/chat/Composer.svelte +58 -1
- package/dist/components/chat/Composer.svelte.d.ts +8 -0
- package/dist/components/chat/MessageCheckpointRow.svelte +87 -0
- package/dist/components/chat/MessageCheckpointRow.svelte.d.ts +21 -0
- package/dist/components/chat/MessageRatingActions.svelte +157 -0
- package/dist/components/chat/MessageRatingActions.svelte.d.ts +26 -0
- package/dist/components/chat/SuggestionCard.svelte +13 -1
- package/dist/components/chat/SuggestionCard.svelte.d.ts +5 -0
- package/dist/components/chat/chatHeaderLogic.d.ts +8 -0
- package/dist/components/chat/chatHeaderLogic.js +14 -0
- package/dist/components/chat/composerModelMenuLogic.d.ts +9 -0
- package/dist/components/chat/composerModelMenuLogic.js +12 -0
- package/dist/components/chat/index.d.ts +10 -0
- package/dist/components/chat/index.js +8 -0
- package/dist/components/chat/messageRatingActionsLogic.d.ts +11 -0
- package/dist/components/chat/messageRatingActionsLogic.js +17 -0
- package/dist/components/chat/messageRatingTypes.d.ts +2 -0
- package/dist/components/chat/messageRatingTypes.js +1 -0
- package/dist/components/color-picker/ColorPicker.svelte +324 -0
- package/dist/components/color-picker/ColorPicker.svelte.d.ts +34 -0
- package/dist/components/color-picker/colorPickerLogic.d.ts +77 -0
- package/dist/components/color-picker/colorPickerLogic.js +184 -0
- package/dist/components/color-picker/index.d.ts +2 -0
- package/dist/components/color-picker/index.js +3 -0
- package/dist/components/font-picker/FontPicker.svelte +169 -0
- package/dist/components/font-picker/FontPicker.svelte.d.ts +41 -0
- package/dist/components/font-picker/fontPickerLogic.d.ts +21 -0
- package/dist/components/font-picker/fontPickerLogic.js +29 -0
- package/dist/components/font-picker/index.d.ts +2 -0
- package/dist/components/font-picker/index.js +3 -0
- package/dist/components/index.d.ts +3 -1
- package/dist/components/index.js +3 -1
- package/dist/components/shell/ErrorState.svelte +5 -2
- package/dist/components/shell/ErrorState.svelte.d.ts +2 -1
- package/dist/components/shell/NotificationDetail.svelte +96 -0
- package/dist/components/shell/NotificationDetail.svelte.d.ts +28 -0
- package/dist/components/shell/NotificationRow.svelte +6 -4
- package/dist/components/shell/SettingsDialog.svelte +106 -9
- package/dist/components/shell/SettingsDialog.svelte.d.ts +5 -0
- package/dist/components/shell/SettingsNav.svelte +72 -23
- package/dist/components/shell/SettingsNav.svelte.d.ts +7 -0
- package/dist/components/shell/WidgetRow.svelte +6 -6
- package/dist/components/shell/WidgetRow.svelte.d.ts +2 -2
- package/dist/components/shell/index.d.ts +1 -0
- package/dist/components/shell/index.js +1 -0
- package/dist/components/toast/ToasterModal.svelte +4 -2
- package/dist/components/ui/button/button.svelte +11 -9
- package/dist/components/ui/button/button.svelte.d.ts +2 -2
- package/dist/components/ui/icon-button/icon-button.svelte +7 -6
- package/dist/components/ui/icon-button/icon-button.svelte.d.ts +2 -2
- package/dist/components/ui/scroll-area/scroll-area-scrollbar.svelte +1 -1
- package/dist/components/ui/select/select-item.svelte +18 -7
- package/dist/components/ui/select/select-item.svelte.d.ts +8 -2
- package/dist/components/ui/slider/slider.svelte +16 -1
- package/package.json +1 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { VersionRatingKind } from './messageRatingTypes.js';
|
|
2
|
+
export type VersionThumb = 'up' | 'down';
|
|
3
|
+
export declare function shouldShowCopyAction(copyText: string): boolean;
|
|
4
|
+
export declare function shouldShowRegenerateAction(canRegenerate: boolean): boolean;
|
|
5
|
+
export declare function shouldShowRatingActions(ratedMessageId: string | null | undefined): boolean;
|
|
6
|
+
export declare function shouldRenderMessageRatingBar(input: {
|
|
7
|
+
copyText: string;
|
|
8
|
+
canRegenerate: boolean;
|
|
9
|
+
ratedMessageId: string | null | undefined;
|
|
10
|
+
}): boolean;
|
|
11
|
+
export declare function thumbIsPressed(rating: VersionRatingKind | undefined, thumb: VersionThumb): boolean;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function shouldShowCopyAction(copyText) {
|
|
2
|
+
return copyText.trim().length > 0;
|
|
3
|
+
}
|
|
4
|
+
export function shouldShowRegenerateAction(canRegenerate) {
|
|
5
|
+
return canRegenerate;
|
|
6
|
+
}
|
|
7
|
+
export function shouldShowRatingActions(ratedMessageId) {
|
|
8
|
+
return typeof ratedMessageId === 'string' && ratedMessageId.length > 0;
|
|
9
|
+
}
|
|
10
|
+
export function shouldRenderMessageRatingBar(input) {
|
|
11
|
+
return (shouldShowCopyAction(input.copyText) ||
|
|
12
|
+
shouldShowRegenerateAction(input.canRegenerate) ||
|
|
13
|
+
shouldShowRatingActions(input.ratedMessageId));
|
|
14
|
+
}
|
|
15
|
+
export function thumbIsPressed(rating, thumb) {
|
|
16
|
+
return rating === thumb;
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Figma: Color Picker (component set 211:3877, page Color Picker), with the hex field and the "Add" button of the popover
|
|
3
|
+
(frame "Color picker popover" in Settings / Brand kit / color picker open).
|
|
4
|
+
|
|
5
|
+
The panel to put inside a popover (`<Popover.Content class="w-64 p-0">`): a row of swatches to pick in one click, a saturation
|
|
6
|
+
and brightness area with a hue bar next to it, and a hex field with the button that takes the colour. Every colour is
|
|
7
|
+
`#RRGGBB`; there is no opacity (a brand colour is opaque).
|
|
8
|
+
|
|
9
|
+
Both controls are sliders for a screen reader and for a keyboard (arrow keys, Shift for bigger steps, Home and End); the
|
|
10
|
+
area and the bar are a left to right island: a colour field is drawn the same way in every language, so they do not mirror.
|
|
11
|
+
The colour maths is in `colorPickerLogic.ts` (tested by tests/colorPickerLogic.test.ts).
|
|
12
|
+
-->
|
|
13
|
+
<script lang="ts" module>
|
|
14
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
15
|
+
|
|
16
|
+
export type ColorPickerMessages = {
|
|
17
|
+
/** Name of the saturation and brightness area, for a screen reader. */
|
|
18
|
+
area: string;
|
|
19
|
+
/** Spoken value of that area: `{s}` and `{v}` become whole percentages. */
|
|
20
|
+
areaValue: string;
|
|
21
|
+
/** Name of the hue bar. */
|
|
22
|
+
hue: string;
|
|
23
|
+
/** Name of the hex field. */
|
|
24
|
+
hex: string;
|
|
25
|
+
/** Label of the button that takes the colour. */
|
|
26
|
+
add: string;
|
|
27
|
+
/** Name of a swatch: `{hex}` becomes its colour. */
|
|
28
|
+
swatch: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type ColorPickerProps = Omit<HTMLAttributes<HTMLDivElement>, 'children'> & {
|
|
32
|
+
class?: string;
|
|
33
|
+
/** The colour as `#RRGGBB`. Bindable; a value that is not a colour starts the picker on a green. */
|
|
34
|
+
value?: string;
|
|
35
|
+
/** Colours to pick in one click, such as the colours already in a kit. */
|
|
36
|
+
swatches?: string[];
|
|
37
|
+
/** How many swatches fit in the row (Figma draws five). */
|
|
38
|
+
maxSwatches?: number;
|
|
39
|
+
/** Shows the hex field and the button; off for a picker that only changes `value`. */
|
|
40
|
+
showConfirm?: boolean;
|
|
41
|
+
/** Called with `#RRGGBB` when Add is pressed, or Enter in the hex field. */
|
|
42
|
+
onConfirm?: (hex: string) => void;
|
|
43
|
+
/** Called with `#RRGGBB` every time the colour changes, while the person drags or types. */
|
|
44
|
+
onChange?: (hex: string) => void;
|
|
45
|
+
messages?: Partial<ColorPickerMessages>;
|
|
46
|
+
};
|
|
47
|
+
</script>
|
|
48
|
+
|
|
49
|
+
<script lang="ts">
|
|
50
|
+
import { untrack } from 'svelte';
|
|
51
|
+
import { cn } from '../../utils.js';
|
|
52
|
+
import { Button } from '../ui/button/index.js';
|
|
53
|
+
import { Input } from '../ui/input/index.js';
|
|
54
|
+
import {
|
|
55
|
+
HUE_GRADIENT,
|
|
56
|
+
HUE_MAX,
|
|
57
|
+
describeSaturationValue,
|
|
58
|
+
hexToHsv,
|
|
59
|
+
hsvToHex,
|
|
60
|
+
hueColor,
|
|
61
|
+
hueToKeep,
|
|
62
|
+
moveHue,
|
|
63
|
+
moveSaturationValue,
|
|
64
|
+
normalizeHex,
|
|
65
|
+
pointToHue,
|
|
66
|
+
pointToSaturationValue,
|
|
67
|
+
type Hsv
|
|
68
|
+
} from './colorPickerLogic.js';
|
|
69
|
+
|
|
70
|
+
const defaultMessages: ColorPickerMessages = {
|
|
71
|
+
area: 'Saturation and brightness',
|
|
72
|
+
areaValue: 'Saturation {s}%, brightness {v}%',
|
|
73
|
+
hue: 'Hue',
|
|
74
|
+
hex: 'Hex color',
|
|
75
|
+
add: 'Add',
|
|
76
|
+
swatch: 'Use {hex}'
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** Where the picker starts when it is not given a colour: a mid green, so the area is never grey. */
|
|
80
|
+
const STARTING_COLOR: Hsv = { h: 120, s: 0.45, v: 0.5 };
|
|
81
|
+
|
|
82
|
+
let {
|
|
83
|
+
class: className,
|
|
84
|
+
value = $bindable(''),
|
|
85
|
+
swatches = [],
|
|
86
|
+
maxSwatches = 5,
|
|
87
|
+
showConfirm = true,
|
|
88
|
+
onConfirm,
|
|
89
|
+
onChange,
|
|
90
|
+
messages: messagesProp,
|
|
91
|
+
...restProps
|
|
92
|
+
}: ColorPickerProps = $props();
|
|
93
|
+
|
|
94
|
+
const messages = $derived({ ...defaultMessages, ...messagesProp });
|
|
95
|
+
|
|
96
|
+
// The colour the picker opens on: the one it is given, or the starting green. Read once, so there is no frame on the wrong colour.
|
|
97
|
+
const openingColor = untrack(() => hexToHsv(value)) ?? { ...STARTING_COLOR };
|
|
98
|
+
// The picker keeps the colour as hue, saturation and brightness, because that is what its two controls move.
|
|
99
|
+
let hsv = $state<Hsv>(openingColor);
|
|
100
|
+
// What is in the hex field: kept apart from the colour so that half a hex typed by a person is not rewritten under them.
|
|
101
|
+
let hexText = $state(hsvToHex(openingColor));
|
|
102
|
+
let areaElement = $state<HTMLDivElement | null>(null);
|
|
103
|
+
let hueElement = $state<HTMLDivElement | null>(null);
|
|
104
|
+
let draggingArea = false;
|
|
105
|
+
let draggingHue = false;
|
|
106
|
+
|
|
107
|
+
const currentHex = $derived(hsvToHex(hsv));
|
|
108
|
+
const typedHex = $derived(normalizeHex(hexText));
|
|
109
|
+
const hexIsInvalid = $derived(hexText.trim() !== '' && typedHex === null);
|
|
110
|
+
const shownSwatches = $derived(
|
|
111
|
+
swatches
|
|
112
|
+
.map((swatch) => normalizeHex(swatch))
|
|
113
|
+
.filter((swatch): swatch is string => swatch !== null)
|
|
114
|
+
.slice(0, Math.max(0, maxSwatches))
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
// The colour comes in from the parent (a bound `value`): take it, unless it is the colour the picker already shows.
|
|
118
|
+
// Everything but `value` is read untracked, so dragging does not run this again. This is a copy from a prop into the
|
|
119
|
+
// state of a control and not a `$derived`, because a grey or a black has no hue: the state keeps the hue a person had
|
|
120
|
+
// (`hueToKeep`), which a value computed from `value` alone would lose.
|
|
121
|
+
$effect(() => {
|
|
122
|
+
const incoming = normalizeHex(value);
|
|
123
|
+
untrack(() => {
|
|
124
|
+
const next = incoming ? hexToHsv(incoming) : null;
|
|
125
|
+
if (!incoming || !next || incoming === hsvToHex(hsv)) return;
|
|
126
|
+
hsv = { h: hueToKeep(next, hsv.h), s: next.s, v: next.v };
|
|
127
|
+
hexText = incoming;
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
/** Takes a new colour from a control: the picker, the field (keepTyped) or a swatch. */
|
|
132
|
+
function commit(next: Hsv, keepTyped = false): void {
|
|
133
|
+
hsv = next;
|
|
134
|
+
const hex = hsvToHex(next);
|
|
135
|
+
if (!keepTyped) hexText = hex;
|
|
136
|
+
value = hex;
|
|
137
|
+
onChange?.(hex);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// ---- saturation and brightness area
|
|
141
|
+
function moveAreaTo(event: PointerEvent): void {
|
|
142
|
+
if (!areaElement) return;
|
|
143
|
+
const point = pointToSaturationValue(event.clientX, event.clientY, areaElement.getBoundingClientRect());
|
|
144
|
+
commit({ h: hsv.h, s: point.s, v: point.v });
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function handleAreaPointerDown(event: PointerEvent): void {
|
|
148
|
+
if (event.pointerType === 'mouse' && event.button !== 0) return;
|
|
149
|
+
draggingArea = true;
|
|
150
|
+
areaElement?.setPointerCapture(event.pointerId);
|
|
151
|
+
// Cancelling the press keeps the page from selecting text while dragging, and with it goes the focus: give it back.
|
|
152
|
+
areaElement?.focus();
|
|
153
|
+
moveAreaTo(event);
|
|
154
|
+
event.preventDefault();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function handleAreaPointerMove(event: PointerEvent): void {
|
|
158
|
+
if (draggingArea) moveAreaTo(event);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function handlePointerEnd(event: PointerEvent): void {
|
|
162
|
+
draggingArea = false;
|
|
163
|
+
draggingHue = false;
|
|
164
|
+
if (areaElement?.hasPointerCapture(event.pointerId)) areaElement.releasePointerCapture(event.pointerId);
|
|
165
|
+
if (hueElement?.hasPointerCapture(event.pointerId)) hueElement.releasePointerCapture(event.pointerId);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function handleAreaKeydown(event: KeyboardEvent): void {
|
|
169
|
+
const next = moveSaturationValue(hsv, event.key, event.shiftKey);
|
|
170
|
+
if (!next) return;
|
|
171
|
+
event.preventDefault();
|
|
172
|
+
commit({ h: hsv.h, s: next.s, v: next.v });
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ---- hue bar
|
|
176
|
+
function moveHueTo(event: PointerEvent): void {
|
|
177
|
+
if (!hueElement) return;
|
|
178
|
+
commit({ h: pointToHue(event.clientY, hueElement.getBoundingClientRect()), s: hsv.s, v: hsv.v });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function handleHuePointerDown(event: PointerEvent): void {
|
|
182
|
+
if (event.pointerType === 'mouse' && event.button !== 0) return;
|
|
183
|
+
draggingHue = true;
|
|
184
|
+
hueElement?.setPointerCapture(event.pointerId);
|
|
185
|
+
hueElement?.focus();
|
|
186
|
+
moveHueTo(event);
|
|
187
|
+
event.preventDefault();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function handleHuePointerMove(event: PointerEvent): void {
|
|
191
|
+
if (draggingHue) moveHueTo(event);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function handleHueKeydown(event: KeyboardEvent): void {
|
|
195
|
+
const next = moveHue(hsv.h, event.key, event.shiftKey);
|
|
196
|
+
if (next === null) return;
|
|
197
|
+
event.preventDefault();
|
|
198
|
+
commit({ h: next, s: hsv.s, v: hsv.v });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// ---- swatches and the hex field
|
|
202
|
+
function pickSwatch(swatch: string): void {
|
|
203
|
+
const next = hexToHsv(swatch);
|
|
204
|
+
if (!next) return;
|
|
205
|
+
commit({ h: hueToKeep(next, hsv.h), s: next.s, v: next.v });
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Reads the text from the event, so it does not matter whether a binding or this handler runs first.
|
|
209
|
+
function handleHexInput(event: Event): void {
|
|
210
|
+
hexText = (event.currentTarget as HTMLInputElement).value;
|
|
211
|
+
const next = typedHex ? hexToHsv(typedHex) : null;
|
|
212
|
+
if (!next) return;
|
|
213
|
+
commit({ h: hueToKeep(next, hsv.h), s: next.s, v: next.v }, true);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Leaving the field writes what was typed the way the picker writes every colour (`#RRGGBB`), or puts the colour back.
|
|
217
|
+
function handleHexBlur(): void {
|
|
218
|
+
hexText = typedHex ?? currentHex;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function confirm(): void {
|
|
222
|
+
if (!typedHex) return;
|
|
223
|
+
onConfirm?.(typedHex);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function handleHexKeydown(event: KeyboardEvent): void {
|
|
227
|
+
if (event.key !== 'Enter') return;
|
|
228
|
+
event.preventDefault();
|
|
229
|
+
confirm();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// The two thumbs are placed from the left and the top of the area, in a left to right island (see the note at the top).
|
|
233
|
+
// rtl-ok: positions of a colour field, measured from its left edge, the same in every language
|
|
234
|
+
const areaThumbStyle = $derived(`left: ${hsv.s * 100}%; top: ${(1 - hsv.v) * 100}%`);
|
|
235
|
+
const hueThumbStyle = $derived(`top: ${(hsv.h / HUE_MAX) * 100}%`);
|
|
236
|
+
// White to nothing across and nothing to black upwards, over the pure colour of the hue: the saturation area.
|
|
237
|
+
const areaOverlay = 'linear-gradient(to right, white, transparent), linear-gradient(to top, black, transparent)';
|
|
238
|
+
</script>
|
|
239
|
+
|
|
240
|
+
<div class={cn('flex w-full flex-col', className)} {...restProps}>
|
|
241
|
+
<div class="flex flex-col gap-3 p-3">
|
|
242
|
+
{#if shownSwatches.length > 0}
|
|
243
|
+
<div class="flex gap-2">
|
|
244
|
+
{#each shownSwatches as swatch (swatch)}
|
|
245
|
+
<button
|
|
246
|
+
type="button"
|
|
247
|
+
class="border-border duration-fast focus-visible:ring-ring focus-visible:ring-offset-background size-10 shrink-0 rounded-lg border transition-transform hover:scale-105 focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-hidden motion-reduce:transition-none"
|
|
248
|
+
style:background-color={swatch}
|
|
249
|
+
aria-label={messages.swatch.replace('{hex}', swatch)}
|
|
250
|
+
title={swatch}
|
|
251
|
+
onclick={() => pickSwatch(swatch)}
|
|
252
|
+
></button>
|
|
253
|
+
{/each}
|
|
254
|
+
</div>
|
|
255
|
+
{/if}
|
|
256
|
+
<div class="flex items-stretch gap-2" dir="ltr">
|
|
257
|
+
<div
|
|
258
|
+
bind:this={areaElement}
|
|
259
|
+
role="slider"
|
|
260
|
+
tabindex="0"
|
|
261
|
+
aria-label={messages.area}
|
|
262
|
+
aria-valuemin={0}
|
|
263
|
+
aria-valuemax={100}
|
|
264
|
+
aria-valuenow={Math.round(hsv.s * 100)}
|
|
265
|
+
aria-valuetext={describeSaturationValue(messages.areaValue, hsv.s, hsv.v)}
|
|
266
|
+
class="focus-visible:ring-ring focus-visible:ring-offset-background relative aspect-square min-w-0 flex-1 touch-none overflow-hidden rounded-lg focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-hidden"
|
|
267
|
+
style:background-color={hueColor(hsv.h)}
|
|
268
|
+
onpointerdown={handleAreaPointerDown}
|
|
269
|
+
onpointermove={handleAreaPointerMove}
|
|
270
|
+
onpointerup={handlePointerEnd}
|
|
271
|
+
onpointercancel={handlePointerEnd}
|
|
272
|
+
onkeydown={handleAreaKeydown}
|
|
273
|
+
>
|
|
274
|
+
<span class="pointer-events-none absolute inset-0" style:background-image={areaOverlay}></span>
|
|
275
|
+
<span
|
|
276
|
+
class="border-background ring-foreground/20 pointer-events-none absolute size-4 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 ring-1"
|
|
277
|
+
style={areaThumbStyle}
|
|
278
|
+
></span>
|
|
279
|
+
</div>
|
|
280
|
+
<div
|
|
281
|
+
bind:this={hueElement}
|
|
282
|
+
role="slider"
|
|
283
|
+
tabindex="0"
|
|
284
|
+
aria-label={messages.hue}
|
|
285
|
+
aria-orientation="vertical"
|
|
286
|
+
aria-valuemin={0}
|
|
287
|
+
aria-valuemax={HUE_MAX}
|
|
288
|
+
aria-valuenow={Math.round(hsv.h)}
|
|
289
|
+
class="focus-visible:ring-ring focus-visible:ring-offset-background relative w-6 shrink-0 touch-none rounded-lg focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-hidden"
|
|
290
|
+
style:background-image={HUE_GRADIENT}
|
|
291
|
+
onpointerdown={handleHuePointerDown}
|
|
292
|
+
onpointermove={handleHuePointerMove}
|
|
293
|
+
onpointerup={handlePointerEnd}
|
|
294
|
+
onpointercancel={handlePointerEnd}
|
|
295
|
+
onkeydown={handleHueKeydown}
|
|
296
|
+
>
|
|
297
|
+
<span
|
|
298
|
+
class="border-background ring-foreground/20 pointer-events-none absolute left-1/2 h-2 w-8 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 ring-1"
|
|
299
|
+
style={hueThumbStyle}
|
|
300
|
+
></span>
|
|
301
|
+
</div>
|
|
302
|
+
</div>
|
|
303
|
+
</div>
|
|
304
|
+
{#if showConfirm}
|
|
305
|
+
<div class="flex items-center gap-2 px-3 pb-3">
|
|
306
|
+
<Input
|
|
307
|
+
value={hexText}
|
|
308
|
+
type="text"
|
|
309
|
+
inputmode="text"
|
|
310
|
+
autocomplete="off"
|
|
311
|
+
spellcheck="false"
|
|
312
|
+
maxlength={7}
|
|
313
|
+
placeholder="#RRGGBB"
|
|
314
|
+
aria-label={messages.hex}
|
|
315
|
+
aria-invalid={hexIsInvalid || undefined}
|
|
316
|
+
class="min-w-0 flex-1 uppercase"
|
|
317
|
+
oninput={handleHexInput}
|
|
318
|
+
onblur={handleHexBlur}
|
|
319
|
+
onkeydown={handleHexKeydown}
|
|
320
|
+
/>
|
|
321
|
+
<Button type="button" size="sm" disabled={typedHex === null} onclick={confirm}>{messages.add}</Button>
|
|
322
|
+
</div>
|
|
323
|
+
{/if}
|
|
324
|
+
</div>
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
2
|
+
export type ColorPickerMessages = {
|
|
3
|
+
/** Name of the saturation and brightness area, for a screen reader. */
|
|
4
|
+
area: string;
|
|
5
|
+
/** Spoken value of that area: `{s}` and `{v}` become whole percentages. */
|
|
6
|
+
areaValue: string;
|
|
7
|
+
/** Name of the hue bar. */
|
|
8
|
+
hue: string;
|
|
9
|
+
/** Name of the hex field. */
|
|
10
|
+
hex: string;
|
|
11
|
+
/** Label of the button that takes the colour. */
|
|
12
|
+
add: string;
|
|
13
|
+
/** Name of a swatch: `{hex}` becomes its colour. */
|
|
14
|
+
swatch: string;
|
|
15
|
+
};
|
|
16
|
+
export type ColorPickerProps = Omit<HTMLAttributes<HTMLDivElement>, 'children'> & {
|
|
17
|
+
class?: string;
|
|
18
|
+
/** The colour as `#RRGGBB`. Bindable; a value that is not a colour starts the picker on a green. */
|
|
19
|
+
value?: string;
|
|
20
|
+
/** Colours to pick in one click, such as the colours already in a kit. */
|
|
21
|
+
swatches?: string[];
|
|
22
|
+
/** How many swatches fit in the row (Figma draws five). */
|
|
23
|
+
maxSwatches?: number;
|
|
24
|
+
/** Shows the hex field and the button; off for a picker that only changes `value`. */
|
|
25
|
+
showConfirm?: boolean;
|
|
26
|
+
/** Called with `#RRGGBB` when Add is pressed, or Enter in the hex field. */
|
|
27
|
+
onConfirm?: (hex: string) => void;
|
|
28
|
+
/** Called with `#RRGGBB` every time the colour changes, while the person drags or types. */
|
|
29
|
+
onChange?: (hex: string) => void;
|
|
30
|
+
messages?: Partial<ColorPickerMessages>;
|
|
31
|
+
};
|
|
32
|
+
declare const ColorPicker: import("svelte").Component<ColorPickerProps, {}, "value">;
|
|
33
|
+
type ColorPicker = ReturnType<typeof ColorPicker>;
|
|
34
|
+
export default ColorPicker;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colour math of the ColorPicker (Figma: Color Picker, component set 211:3877).
|
|
3
|
+
*
|
|
4
|
+
* Pure functions and no DOM, so that `tests/colorPickerLogic.test.ts` can check every one of them with Node's own test
|
|
5
|
+
* runner. A colour is written `#RRGGBB` in upper case everywhere outside this file; inside it a colour is a hue (0 to 360)
|
|
6
|
+
* with a saturation and a brightness (both 0 to 1), because that is what the two controls of the picker move.
|
|
7
|
+
*
|
|
8
|
+
* No colour is written out as a literal here: the design rules check (`npm run check:design`) reads raw colours in
|
|
9
|
+
* source as a violation, and every colour this file hands out is computed.
|
|
10
|
+
*/
|
|
11
|
+
export type Hsv = {
|
|
12
|
+
h: number;
|
|
13
|
+
s: number;
|
|
14
|
+
v: number;
|
|
15
|
+
};
|
|
16
|
+
/** A box on the screen, as `getBoundingClientRect()` reports it (only the four numbers the picker needs). */
|
|
17
|
+
export type PickerBox = {
|
|
18
|
+
left: number;
|
|
19
|
+
top: number;
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
};
|
|
23
|
+
export declare const HUE_MAX = 360;
|
|
24
|
+
/** One press of an arrow key moves a control by 1%; with Shift (or Page Up and Page Down) by 10%. */
|
|
25
|
+
export declare const SMALL_STEP = 0.01;
|
|
26
|
+
export declare const LARGE_STEP = 0.1;
|
|
27
|
+
/** The same two steps for the hue bar, which counts in degrees. */
|
|
28
|
+
export declare const SMALL_HUE_STEP = 1;
|
|
29
|
+
export declare const LARGE_HUE_STEP = 10;
|
|
30
|
+
/** Keeps a number between two bounds; anything that is not a number (NaN) becomes the lower bound. */
|
|
31
|
+
export declare function clamp(value: number, min: number, max: number): number;
|
|
32
|
+
/**
|
|
33
|
+
* Reads what a person typed as a colour and writes it back as `#RRGGBB`.
|
|
34
|
+
* Six hex digits are accepted with or without the leading hash, in either case and with spaces around them; the short form
|
|
35
|
+
* of three digits is read by doubling every digit. Anything else gives null.
|
|
36
|
+
*/
|
|
37
|
+
export declare function normalizeHex(input: string | null | undefined): string | null;
|
|
38
|
+
/** The hue, saturation and brightness of a hex colour, or null when the text is not a colour. */
|
|
39
|
+
export declare function hexToHsv(input: string | null | undefined): Hsv | null;
|
|
40
|
+
/** The hex colour (`#RRGGBB`, upper case) of a hue, a saturation and a brightness. A hue of 360 is the same red as 0. */
|
|
41
|
+
export declare function hsvToHex({ h, s, v }: Hsv): string;
|
|
42
|
+
/** The fully saturated colour of a hue, as a CSS colour: the base the saturation area is painted over. */
|
|
43
|
+
export declare function hueColor(hue: number): string;
|
|
44
|
+
/** The hue bar: the six corners of the colour wheel from the top (red) to the bottom (red again). */
|
|
45
|
+
export declare const HUE_GRADIENT: string;
|
|
46
|
+
/** Saturation grows to the right and brightness grows upwards; a point outside the box counts as its nearest edge. */
|
|
47
|
+
export declare function pointToSaturationValue(clientX: number, clientY: number, box: PickerBox): {
|
|
48
|
+
s: number;
|
|
49
|
+
v: number;
|
|
50
|
+
};
|
|
51
|
+
/** The hue bar runs from 0 at the top to 360 at the bottom. */
|
|
52
|
+
export declare function pointToHue(clientY: number, box: PickerBox): number;
|
|
53
|
+
/**
|
|
54
|
+
* What an arrow key does on the saturation area: Left and Right change the saturation, Up and Down the brightness
|
|
55
|
+
* (1%, or 10% with Shift); Page Up and Page Down move the brightness by 10%; Home and End set the saturation to 0 and 100%.
|
|
56
|
+
* Returns null for a key the area does not use, so the caller leaves that key alone.
|
|
57
|
+
*/
|
|
58
|
+
export declare function moveSaturationValue(current: {
|
|
59
|
+
s: number;
|
|
60
|
+
v: number;
|
|
61
|
+
}, key: string, large: boolean): {
|
|
62
|
+
s: number;
|
|
63
|
+
v: number;
|
|
64
|
+
} | null;
|
|
65
|
+
/**
|
|
66
|
+
* What a key does on the hue bar. The bar is drawn from 0 at the top, so the keys follow the thumb on the screen: Down and
|
|
67
|
+
* Right move it down (the hue grows), Up and Left move it up. 1 degree, or 10 with Shift; Page Up and Page Down move 10;
|
|
68
|
+
* Home and End go to the first and the last hue. Returns null for a key the bar does not use.
|
|
69
|
+
*/
|
|
70
|
+
export declare function moveHue(current: number, key: string, large: boolean): number | null;
|
|
71
|
+
/**
|
|
72
|
+
* The hue to keep when a new colour comes in: a grey or a black has no hue (it reads as 0), and taking that 0 would throw
|
|
73
|
+
* the hue bar back to the top every time the area is dragged to an edge. In that case the hue the person had is kept.
|
|
74
|
+
*/
|
|
75
|
+
export declare function hueToKeep(incoming: Hsv, currentHue: number): number;
|
|
76
|
+
/** `Saturation 45%, brightness 60%`-style text for the area: `{s}` and `{v}` of the template become whole percentages. */
|
|
77
|
+
export declare function describeSaturationValue(template: string, s: number, v: number): string;
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colour math of the ColorPicker (Figma: Color Picker, component set 211:3877).
|
|
3
|
+
*
|
|
4
|
+
* Pure functions and no DOM, so that `tests/colorPickerLogic.test.ts` can check every one of them with Node's own test
|
|
5
|
+
* runner. A colour is written `#RRGGBB` in upper case everywhere outside this file; inside it a colour is a hue (0 to 360)
|
|
6
|
+
* with a saturation and a brightness (both 0 to 1), because that is what the two controls of the picker move.
|
|
7
|
+
*
|
|
8
|
+
* No colour is written out as a literal here: the design rules check (`npm run check:design`) reads raw colours in
|
|
9
|
+
* source as a violation, and every colour this file hands out is computed.
|
|
10
|
+
*/
|
|
11
|
+
export const HUE_MAX = 360;
|
|
12
|
+
/** One press of an arrow key moves a control by 1%; with Shift (or Page Up and Page Down) by 10%. */
|
|
13
|
+
export const SMALL_STEP = 0.01;
|
|
14
|
+
export const LARGE_STEP = 0.1;
|
|
15
|
+
/** The same two steps for the hue bar, which counts in degrees. */
|
|
16
|
+
export const SMALL_HUE_STEP = 1;
|
|
17
|
+
export const LARGE_HUE_STEP = 10;
|
|
18
|
+
/** Keeps a number between two bounds; anything that is not a number (NaN) becomes the lower bound. */
|
|
19
|
+
export function clamp(value, min, max) {
|
|
20
|
+
if (Number.isNaN(value))
|
|
21
|
+
return min;
|
|
22
|
+
return Math.min(max, Math.max(min, value));
|
|
23
|
+
}
|
|
24
|
+
const HEX_BODY = /^(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
25
|
+
/**
|
|
26
|
+
* Reads what a person typed as a colour and writes it back as `#RRGGBB`.
|
|
27
|
+
* Six hex digits are accepted with or without the leading hash, in either case and with spaces around them; the short form
|
|
28
|
+
* of three digits is read by doubling every digit. Anything else gives null.
|
|
29
|
+
*/
|
|
30
|
+
export function normalizeHex(input) {
|
|
31
|
+
if (typeof input !== 'string')
|
|
32
|
+
return null;
|
|
33
|
+
let body = input.trim();
|
|
34
|
+
if (body.startsWith('#'))
|
|
35
|
+
body = body.slice(1);
|
|
36
|
+
if (!HEX_BODY.test(body))
|
|
37
|
+
return null;
|
|
38
|
+
if (body.length === 3) {
|
|
39
|
+
body = body
|
|
40
|
+
.split('')
|
|
41
|
+
.map((digit) => digit + digit)
|
|
42
|
+
.join('');
|
|
43
|
+
}
|
|
44
|
+
return `#${body.toUpperCase()}`;
|
|
45
|
+
}
|
|
46
|
+
/** The hue, saturation and brightness of a hex colour, or null when the text is not a colour. */
|
|
47
|
+
export function hexToHsv(input) {
|
|
48
|
+
const hex = normalizeHex(input);
|
|
49
|
+
if (!hex)
|
|
50
|
+
return null;
|
|
51
|
+
const red = parseInt(hex.slice(1, 3), 16) / 255;
|
|
52
|
+
const green = parseInt(hex.slice(3, 5), 16) / 255;
|
|
53
|
+
const blue = parseInt(hex.slice(5, 7), 16) / 255;
|
|
54
|
+
const max = Math.max(red, green, blue);
|
|
55
|
+
const min = Math.min(red, green, blue);
|
|
56
|
+
const delta = max - min;
|
|
57
|
+
let hue = 0;
|
|
58
|
+
if (delta !== 0) {
|
|
59
|
+
if (max === red)
|
|
60
|
+
hue = ((green - blue) / delta) % 6;
|
|
61
|
+
else if (max === green)
|
|
62
|
+
hue = (blue - red) / delta + 2;
|
|
63
|
+
else
|
|
64
|
+
hue = (red - green) / delta + 4;
|
|
65
|
+
hue *= 60;
|
|
66
|
+
if (hue < 0)
|
|
67
|
+
hue += HUE_MAX;
|
|
68
|
+
}
|
|
69
|
+
return { h: hue, s: max === 0 ? 0 : delta / max, v: max };
|
|
70
|
+
}
|
|
71
|
+
/** The hex colour (`#RRGGBB`, upper case) of a hue, a saturation and a brightness. A hue of 360 is the same red as 0. */
|
|
72
|
+
export function hsvToHex({ h, s, v }) {
|
|
73
|
+
const hue = ((h % HUE_MAX) + HUE_MAX) % HUE_MAX;
|
|
74
|
+
const saturation = clamp(s, 0, 1);
|
|
75
|
+
const brightness = clamp(v, 0, 1);
|
|
76
|
+
const chroma = brightness * saturation;
|
|
77
|
+
const second = chroma * (1 - Math.abs(((hue / 60) % 2) - 1));
|
|
78
|
+
const match = brightness - chroma;
|
|
79
|
+
let red = 0;
|
|
80
|
+
let green = 0;
|
|
81
|
+
let blue = 0;
|
|
82
|
+
if (hue < 60)
|
|
83
|
+
[red, green, blue] = [chroma, second, 0];
|
|
84
|
+
else if (hue < 120)
|
|
85
|
+
[red, green, blue] = [second, chroma, 0];
|
|
86
|
+
else if (hue < 180)
|
|
87
|
+
[red, green, blue] = [0, chroma, second];
|
|
88
|
+
else if (hue < 240)
|
|
89
|
+
[red, green, blue] = [0, second, chroma];
|
|
90
|
+
else if (hue < 300)
|
|
91
|
+
[red, green, blue] = [second, 0, chroma];
|
|
92
|
+
else
|
|
93
|
+
[red, green, blue] = [chroma, 0, second];
|
|
94
|
+
const channel = (part) => Math.round((part + match) * 255)
|
|
95
|
+
.toString(16)
|
|
96
|
+
.padStart(2, '0');
|
|
97
|
+
return `#${channel(red)}${channel(green)}${channel(blue)}`.toUpperCase();
|
|
98
|
+
}
|
|
99
|
+
/** The fully saturated colour of a hue, as a CSS colour: the base the saturation area is painted over. */
|
|
100
|
+
export function hueColor(hue) {
|
|
101
|
+
return `hwb(${Math.round(clamp(hue, 0, HUE_MAX))} 0% 0%)`;
|
|
102
|
+
}
|
|
103
|
+
/** The hue bar: the six corners of the colour wheel from the top (red) to the bottom (red again). */
|
|
104
|
+
export const HUE_GRADIENT = `linear-gradient(to bottom, ${[0, 60, 120, 180, 240, 300, 360].map(hueColor).join(', ')})`;
|
|
105
|
+
/** Saturation grows to the right and brightness grows upwards; a point outside the box counts as its nearest edge. */
|
|
106
|
+
export function pointToSaturationValue(clientX, clientY, box) {
|
|
107
|
+
if (box.width <= 0 || box.height <= 0)
|
|
108
|
+
return { s: 0, v: 0 };
|
|
109
|
+
return {
|
|
110
|
+
s: clamp((clientX - box.left) / box.width, 0, 1),
|
|
111
|
+
v: 1 - clamp((clientY - box.top) / box.height, 0, 1)
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/** The hue bar runs from 0 at the top to 360 at the bottom. */
|
|
115
|
+
export function pointToHue(clientY, box) {
|
|
116
|
+
if (box.height <= 0)
|
|
117
|
+
return 0;
|
|
118
|
+
return clamp((clientY - box.top) / box.height, 0, 1) * HUE_MAX;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* What an arrow key does on the saturation area: Left and Right change the saturation, Up and Down the brightness
|
|
122
|
+
* (1%, or 10% with Shift); Page Up and Page Down move the brightness by 10%; Home and End set the saturation to 0 and 100%.
|
|
123
|
+
* Returns null for a key the area does not use, so the caller leaves that key alone.
|
|
124
|
+
*/
|
|
125
|
+
export function moveSaturationValue(current, key, large) {
|
|
126
|
+
const step = large ? LARGE_STEP : SMALL_STEP;
|
|
127
|
+
switch (key) {
|
|
128
|
+
case 'ArrowLeft':
|
|
129
|
+
return { s: clamp(current.s - step, 0, 1), v: current.v };
|
|
130
|
+
case 'ArrowRight':
|
|
131
|
+
return { s: clamp(current.s + step, 0, 1), v: current.v };
|
|
132
|
+
case 'ArrowUp':
|
|
133
|
+
return { s: current.s, v: clamp(current.v + step, 0, 1) };
|
|
134
|
+
case 'ArrowDown':
|
|
135
|
+
return { s: current.s, v: clamp(current.v - step, 0, 1) };
|
|
136
|
+
case 'PageUp':
|
|
137
|
+
return { s: current.s, v: clamp(current.v + LARGE_STEP, 0, 1) };
|
|
138
|
+
case 'PageDown':
|
|
139
|
+
return { s: current.s, v: clamp(current.v - LARGE_STEP, 0, 1) };
|
|
140
|
+
case 'Home':
|
|
141
|
+
return { s: 0, v: current.v };
|
|
142
|
+
case 'End':
|
|
143
|
+
return { s: 1, v: current.v };
|
|
144
|
+
default:
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* What a key does on the hue bar. The bar is drawn from 0 at the top, so the keys follow the thumb on the screen: Down and
|
|
150
|
+
* Right move it down (the hue grows), Up and Left move it up. 1 degree, or 10 with Shift; Page Up and Page Down move 10;
|
|
151
|
+
* Home and End go to the first and the last hue. Returns null for a key the bar does not use.
|
|
152
|
+
*/
|
|
153
|
+
export function moveHue(current, key, large) {
|
|
154
|
+
const step = large ? LARGE_HUE_STEP : SMALL_HUE_STEP;
|
|
155
|
+
switch (key) {
|
|
156
|
+
case 'ArrowDown':
|
|
157
|
+
case 'ArrowRight':
|
|
158
|
+
return clamp(current + step, 0, HUE_MAX);
|
|
159
|
+
case 'ArrowUp':
|
|
160
|
+
case 'ArrowLeft':
|
|
161
|
+
return clamp(current - step, 0, HUE_MAX);
|
|
162
|
+
case 'PageDown':
|
|
163
|
+
return clamp(current + LARGE_HUE_STEP, 0, HUE_MAX);
|
|
164
|
+
case 'PageUp':
|
|
165
|
+
return clamp(current - LARGE_HUE_STEP, 0, HUE_MAX);
|
|
166
|
+
case 'Home':
|
|
167
|
+
return 0;
|
|
168
|
+
case 'End':
|
|
169
|
+
return HUE_MAX;
|
|
170
|
+
default:
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* The hue to keep when a new colour comes in: a grey or a black has no hue (it reads as 0), and taking that 0 would throw
|
|
176
|
+
* the hue bar back to the top every time the area is dragged to an edge. In that case the hue the person had is kept.
|
|
177
|
+
*/
|
|
178
|
+
export function hueToKeep(incoming, currentHue) {
|
|
179
|
+
return incoming.s === 0 || incoming.v === 0 ? currentHue : incoming.h;
|
|
180
|
+
}
|
|
181
|
+
/** `Saturation 45%, brightness 60%`-style text for the area: `{s}` and `{v}` of the template become whole percentages. */
|
|
182
|
+
export function describeSaturationValue(template, s, v) {
|
|
183
|
+
return template.replace('{s}', String(Math.round(s * 100))).replace('{v}', String(Math.round(v * 100)));
|
|
184
|
+
}
|