@open-artifact/ui 0.9.2 → 0.10.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.
- package/package.json +1 -1
- package/src/color-picker.js +16 -7
- package/src/fields.d.ts +3 -1
- package/src/fields.js +20 -4
- package/src/keys.d.ts +12 -0
- package/src/keys.js +9 -0
- package/src/scrub-number-field.js +4 -0
package/package.json
CHANGED
package/src/color-picker.js
CHANGED
|
@@ -161,11 +161,19 @@ export function ColorPickerPanel({ value, onChange, onCommit, fallback, format:
|
|
|
161
161
|
setHsva(fixed);
|
|
162
162
|
commit(text(fixed));
|
|
163
163
|
};
|
|
164
|
+
/** A color typed into the text field and not committed yet: shown in the picker only. */
|
|
165
|
+
const [typed, setTyped] = useState(null);
|
|
166
|
+
const previewText = (raw) => {
|
|
167
|
+
const trimmed = raw.trim();
|
|
168
|
+
const next = raw === shownText ? null : parseColor(/^[0-9a-f]{3,8}$/i.test(trimmed) ? `#${trimmed}` : trimmed);
|
|
169
|
+
setTyped(next ? rgbaToHsva(alpha ? next.rgba : { ...next.rgba, a: 1 }, hsva.h) : null);
|
|
170
|
+
};
|
|
164
171
|
const pickText = (raw) => {
|
|
165
|
-
|
|
166
|
-
|
|
172
|
+
setTyped(null);
|
|
173
|
+
const entered = raw.trim();
|
|
174
|
+
if (!entered)
|
|
167
175
|
return;
|
|
168
|
-
const hexless = /^[0-9a-f]{3,8}$/i.test(
|
|
176
|
+
const hexless = /^[0-9a-f]{3,8}$/i.test(entered) ? `#${entered}` : entered;
|
|
169
177
|
const next = parseColor(hexless);
|
|
170
178
|
if (next) {
|
|
171
179
|
setHsva(rgbaToHsva(alpha ? next.rgba : { ...next.rgba, a: 1 }, hsva.h));
|
|
@@ -174,8 +182,8 @@ export function ColorPickerPanel({ value, onChange, onCommit, fallback, format:
|
|
|
174
182
|
return;
|
|
175
183
|
}
|
|
176
184
|
// Other CSS colors (`var(--x)`, `color-mix()`) pass through when the browser reads them.
|
|
177
|
-
if (typeof CSS !== 'undefined' && CSS.supports('color',
|
|
178
|
-
commit(
|
|
185
|
+
if (typeof CSS !== 'undefined' && CSS.supports('color', entered))
|
|
186
|
+
commit(entered);
|
|
179
187
|
};
|
|
180
188
|
const pickSwatch = (swatch) => {
|
|
181
189
|
const next = parseColor(swatch.value);
|
|
@@ -189,7 +197,8 @@ export function ColorPickerPanel({ value, onChange, onCommit, fallback, format:
|
|
|
189
197
|
commit(formatColor(hsvaToRgba(alpha ? hsva : { ...hsva, a: 1 }), next, {}, next === 'oklch' && parsed?.notation.format === 'oklch' ? parsed.oklch : undefined));
|
|
190
198
|
};
|
|
191
199
|
const dropper = typeof window !== 'undefined' && 'EyeDropper' in window ? window.EyeDropper : null;
|
|
192
|
-
const
|
|
200
|
+
const seen = typed ?? hsva;
|
|
201
|
+
const rgb = hsvaToRgba({ ...seen, a: 1 });
|
|
193
202
|
const solid = formatColor(rgb, 'hex');
|
|
194
203
|
const shownText = value !== null && !parsed ? value : text(hsva);
|
|
195
204
|
const current = value ?? null;
|
|
@@ -201,7 +210,7 @@ export function ColorPickerPanel({ value, onChange, onCommit, fallback, format:
|
|
|
201
210
|
}
|
|
202
211
|
return [...out.entries()];
|
|
203
212
|
}, [swatches]);
|
|
204
|
-
return (_jsxs("div", { className: cn('flex w-60 flex-col gap-3', className), children: [_jsx(ColorArea, { hsva:
|
|
213
|
+
return (_jsxs("div", { className: cn('flex w-60 flex-col gap-3', className), children: [_jsx(ColorArea, { hsva: seen, onDrag: drag, onSettle: settle }), _jsx(ColorSlider, { label: "Hue", value: seen.h / 360, background: "linear-gradient(to right, #f00 0%, #ff0 16.67%, #0f0 33.33%, #0ff 50%, #00f 66.67%, #f0f 83.33%, #f00 100%)", thumb: `hsl(${seen.h} 100% 50%)`, valueText: `${Math.round(hsva.h)} degrees`, onDrag: (t) => drag({ ...hsva, h: Math.min(359.9, t * 360) }), onSettle: (t) => settle({ ...hsva, h: Math.min(359.9, t * 360) }) }), alpha ? (_jsx(ColorSlider, { label: "Opacity", value: seen.a, background: `linear-gradient(to right, transparent, ${solid}), ${CHECKERBOARD}`, thumb: formatColor({ ...rgb, a: seen.a }, 'rgb'), valueText: `${Math.round(hsva.a * 100)}%`, onDrag: (t) => drag({ ...hsva, a: Math.round(t * 100) / 100 }), onSettle: (t) => settle({ ...hsva, a: Math.round(t * 100) / 100 }) })) : null, _jsx(Segmented, { "aria-label": "Color format", value: parsed || value === null ? format : null, options: FORMAT_OPTIONS, onChange: switchFormat, className: "text-[11px]" }), _jsxs("div", { className: "flex items-center gap-1.5", children: [_jsx("span", { "aria-hidden": "true", className: "size-7 shrink-0 rounded-control shadow-[inset_0_0_0_1px_rgb(0_0_0/0.12)]", style: { background: `linear-gradient(${formatColor(hsvaToRgba(seen), 'rgb')}, ${formatColor(hsvaToRgba(seen), 'rgb')}), ${CHECKERBOARD}` } }), _jsx(CommitInput, { "aria-label": "Color value", value: shownText, onCommit: pickText, onDraftChange: previewText, spellCheck: false, className: "font-mono text-[12px]" }), dropper ? (_jsx("button", { type: "button", "aria-label": "Pick a color from the screen", title: "Pick a color from the screen", onKeyDown: preventRepeatedActivation, onClick: () => {
|
|
205
214
|
void new dropper()
|
|
206
215
|
.open()
|
|
207
216
|
.then((result) => {
|
package/src/fields.d.ts
CHANGED
|
@@ -8,13 +8,15 @@ export interface CommitInputProps extends Omit<InputHTMLAttributes<HTMLInputElem
|
|
|
8
8
|
value: string;
|
|
9
9
|
/** Called on Enter or blur, only when the text changed. */
|
|
10
10
|
onCommit: (value: string) => void;
|
|
11
|
+
/** Called with the draft as it is typed, and with `value` again when Escape puts it back. */
|
|
12
|
+
onDraftChange?: (draft: string) => void;
|
|
11
13
|
}
|
|
12
14
|
/**
|
|
13
15
|
* A text input that keeps a local draft and commits on Enter or blur; Escape
|
|
14
16
|
* restores the last committed value. Inspector edits use it so every
|
|
15
17
|
* keystroke does not become a document write.
|
|
16
18
|
*/
|
|
17
|
-
export declare function CommitInput({ value, onCommit, className, ...rest }: CommitInputProps): import("react/jsx-runtime").JSX.Element;
|
|
19
|
+
export declare function CommitInput({ value, onCommit, onDraftChange, className, ...rest }: CommitInputProps): import("react/jsx-runtime").JSX.Element;
|
|
18
20
|
/** Props of {@link NumberInput}. */
|
|
19
21
|
export interface NumberInputProps {
|
|
20
22
|
value: number | null;
|
package/src/fields.js
CHANGED
|
@@ -10,9 +10,9 @@ import { Switch as BaseSwitch } from '@base-ui/react/switch';
|
|
|
10
10
|
import { ToggleGroup } from '@base-ui/react/toggle-group';
|
|
11
11
|
import { Toggle } from '@base-ui/react/toggle';
|
|
12
12
|
import { Check, ChevronDown } from 'lucide-react';
|
|
13
|
-
import { forwardRef, useEffect, useState } from 'react';
|
|
13
|
+
import { forwardRef, useEffect, useRef, useState } from 'react';
|
|
14
14
|
import { cn } from "./cn.js";
|
|
15
|
-
import { preventRepeatedActivation } from "./keys.js";
|
|
15
|
+
import { isComposingKey, preventRepeatedActivation } from "./keys.js";
|
|
16
16
|
import { POPUP_CLASS } from "./menu.js";
|
|
17
17
|
/** Shared look of single-line inputs. */
|
|
18
18
|
export const INPUT_CLASS = 'h-7 w-full min-w-0 rounded-control border border-line bg-surface-2 px-2 text-[13px] text-fg outline-none placeholder:text-fg-subtle focus:border-accent focus:bg-surface';
|
|
@@ -25,21 +25,37 @@ export const TextInput = forwardRef(function TextInput({ className, ...rest }, r
|
|
|
25
25
|
* restores the last committed value. Inspector edits use it so every
|
|
26
26
|
* keystroke does not become a document write.
|
|
27
27
|
*/
|
|
28
|
-
export function CommitInput({ value, onCommit, className, ...rest }) {
|
|
28
|
+
export function CommitInput({ value, onCommit, onDraftChange, className, ...rest }) {
|
|
29
29
|
const [draft, setDraft] = useState(value);
|
|
30
30
|
useEffect(() => setDraft(value), [value]);
|
|
31
|
+
/** Set by Enter and Escape, which already decided: the blur they cause commits nothing more. */
|
|
32
|
+
const settled = useRef(false);
|
|
31
33
|
const commit = () => {
|
|
34
|
+
if (settled.current) {
|
|
35
|
+
settled.current = false;
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
32
38
|
if (draft !== value)
|
|
33
39
|
onCommit(draft);
|
|
34
40
|
};
|
|
35
|
-
return (_jsx("input", { className: cn(INPUT_CLASS, className), value: draft, onChange: (event) =>
|
|
41
|
+
return (_jsx("input", { className: cn(INPUT_CLASS, className), value: draft, onChange: (event) => {
|
|
42
|
+
setDraft(event.target.value);
|
|
43
|
+
onDraftChange?.(event.target.value);
|
|
44
|
+
}, onBlur: commit, onKeyDown: (event) => {
|
|
45
|
+
if (isComposingKey(event.nativeEvent))
|
|
46
|
+
return;
|
|
36
47
|
if (event.key === 'Enter') {
|
|
37
48
|
commit();
|
|
49
|
+
settled.current = true;
|
|
38
50
|
event.target.blur();
|
|
51
|
+
settled.current = false;
|
|
39
52
|
}
|
|
40
53
|
else if (event.key === 'Escape') {
|
|
54
|
+
settled.current = true;
|
|
41
55
|
setDraft(value);
|
|
56
|
+
onDraftChange?.(value);
|
|
42
57
|
event.target.blur();
|
|
58
|
+
settled.current = false;
|
|
43
59
|
}
|
|
44
60
|
}, ...rest }));
|
|
45
61
|
}
|
package/src/keys.d.ts
CHANGED
|
@@ -11,6 +11,18 @@ export interface KeyLike {
|
|
|
11
11
|
key: string;
|
|
12
12
|
repeat: boolean;
|
|
13
13
|
}
|
|
14
|
+
/** The fields of a keyboard event that tell an input method is composing. */
|
|
15
|
+
export interface ComposingLike {
|
|
16
|
+
isComposing?: boolean;
|
|
17
|
+
keyCode?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* True while an input method composes (Chinese, Japanese, Korean…): the
|
|
21
|
+
* Enter or Escape it sees picks or drops the candidate, so a field must not
|
|
22
|
+
* also commit or cancel on it. Safari reports the end of a composition as
|
|
23
|
+
* key code 229 with `isComposing` already false.
|
|
24
|
+
*/
|
|
25
|
+
export declare function isComposingKey(event: ComposingLike): boolean;
|
|
14
26
|
/** True for an auto-repeated Enter or Space. */
|
|
15
27
|
export declare function isRepeatedActivation(event: KeyLike): boolean;
|
|
16
28
|
/** A keydown event as the guards see it. */
|
package/src/keys.js
CHANGED
|
@@ -6,6 +6,15 @@
|
|
|
6
6
|
* and a press that moves focus (closing a toast, opening a dialog) lands the
|
|
7
7
|
* repeats on whatever gets focus next.
|
|
8
8
|
*/
|
|
9
|
+
/**
|
|
10
|
+
* True while an input method composes (Chinese, Japanese, Korean…): the
|
|
11
|
+
* Enter or Escape it sees picks or drops the candidate, so a field must not
|
|
12
|
+
* also commit or cancel on it. Safari reports the end of a composition as
|
|
13
|
+
* key code 229 with `isComposing` already false.
|
|
14
|
+
*/
|
|
15
|
+
export function isComposingKey(event) {
|
|
16
|
+
return Boolean(event.isComposing) || event.keyCode === 229;
|
|
17
|
+
}
|
|
9
18
|
/** True for an auto-repeated Enter or Space. */
|
|
10
19
|
export function isRepeatedActivation(event) {
|
|
11
20
|
return event.repeat && (event.key === 'Enter' || event.key === ' ');
|
|
@@ -140,6 +140,10 @@ export function ScrubNumberField({ value, mixed = false, onChange, onCommit, lab
|
|
|
140
140
|
if (disabled || event.button !== 0)
|
|
141
141
|
return;
|
|
142
142
|
event.preventDefault();
|
|
143
|
+
// A field typed into elsewhere commits before the scrub writes its own value.
|
|
144
|
+
const typing = document.activeElement;
|
|
145
|
+
if (typing instanceof HTMLElement && typing !== inputRef.current && (typing.matches('input, textarea') || typing.isContentEditable))
|
|
146
|
+
typing.blur();
|
|
143
147
|
event.currentTarget.setPointerCapture(event.pointerId);
|
|
144
148
|
const start = mixed || value === null ? (min ?? 0) : value;
|
|
145
149
|
scrub.current = { start, current: start, travel: 0, carry: 0, pointerId: event.pointerId };
|