@tangible/ui 0.2.4 → 0.2.6
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/components/ColorPicker/ColorField.d.ts +31 -2
- package/components/ColorPicker/ColorField.js +100 -8
- package/components/Combobox/Combobox.d.ts +1 -1
- package/components/Combobox/Combobox.js +2 -2
- package/components/Combobox/types.d.ts +5 -0
- package/components/Field/Field.d.ts +13 -1
- package/components/Field/Field.js +5 -3
- package/components/GradientPicker/AngleDial.d.ts +14 -0
- package/components/GradientPicker/AngleDial.js +24 -0
- package/components/GradientPicker/GradientBar.d.ts +38 -0
- package/components/GradientPicker/GradientBar.js +130 -0
- package/components/GradientPicker/GradientPicker.d.ts +32 -0
- package/components/GradientPicker/GradientPicker.js +191 -0
- package/components/GradientPicker/index.d.ts +8 -0
- package/components/GradientPicker/index.js +8 -0
- package/components/GradientPicker/labels.d.ts +42 -0
- package/components/GradientPicker/labels.js +31 -0
- package/components/MultiCombobox/MultiCombobox.d.ts +1 -1
- package/components/MultiCombobox/MultiCombobox.js +2 -2
- package/components/MultiCombobox/types.d.ts +5 -0
- package/components/index.d.ts +2 -0
- package/components/index.js +1 -0
- package/package.json +5 -1
- package/styles/all.css +1 -1
- package/styles/all.expanded.css +253 -24
- package/styles/all.expanded.unlayered.css +253 -24
- package/styles/all.unlayered.css +1 -1
- package/styles/components/_bundle.scss +9 -1
- package/styles/components/button.scss +4 -3
- package/styles/components/color-picker.scss +11 -1
- package/styles/components/gradient-picker.scss +296 -0
- package/styles/components/icon-button.scss +15 -8
- package/styles/components/notice.scss +12 -7
- package/styles/core.css +1 -1
- package/styles/core.unlayered.css +1 -1
- package/styles/system/_checkerboard.scss +24 -0
- package/styles/system/_tokens.scss +16 -6
- package/styles/system/index.scss +2 -1
- package/tui-manifest.json +96 -5
- package/utils/color/gradient.d.ts +42 -0
- package/utils/color/gradient.js +408 -0
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import type { GradientPickerLabels } from '../GradientPicker';
|
|
1
2
|
import { type ColorPickerLabels } from './labels';
|
|
2
3
|
import { type ColorFormat } from '../../utils/color/color';
|
|
4
|
+
import { type GradientType } from '../../utils/color/gradient';
|
|
3
5
|
import type { SizeStandard } from '../../types';
|
|
4
6
|
import type { IconName } from '../../icons/registry';
|
|
5
7
|
import type { Side, Align } from '../Popover';
|
|
@@ -8,9 +10,26 @@ export type ColorFieldLabels = ColorPickerLabels & {
|
|
|
8
10
|
trigger?: string;
|
|
9
11
|
/** Suffix appended to the accessible name for `dynamic` values. @default 'dynamic' */
|
|
10
12
|
dynamic?: string;
|
|
13
|
+
/** Accessible name for the Solid|Gradient tab list. @default 'Color type' */
|
|
14
|
+
modes?: string;
|
|
15
|
+
/** Solid tab text. @default 'Solid' */
|
|
16
|
+
solidTab?: string;
|
|
17
|
+
/** Gradient tab text. @default 'Gradient' */
|
|
18
|
+
gradientTab?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Trigger/name text for a gradient value — the raw CSS string would be
|
|
21
|
+
* spelled out hex-by-hex by screen readers.
|
|
22
|
+
* @default (type, stops) => `Gradient (${type}, ${stops} stops)`
|
|
23
|
+
*/
|
|
24
|
+
gradientText?: (type: GradientType, stopCount: number) => string;
|
|
25
|
+
/** Strings for the embedded GradientPicker (gradient mode only). */
|
|
26
|
+
gradientPicker?: GradientPickerLabels;
|
|
11
27
|
};
|
|
12
28
|
export type ColorFieldProps = {
|
|
13
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* Controlled value — a CSS colour, a `var(--x[, fallback])` reference, or
|
|
31
|
+
* (with `gradient`) a CSS gradient string.
|
|
32
|
+
*/
|
|
14
33
|
value?: string;
|
|
15
34
|
/** Uncontrolled initial value. @default '#000000' */
|
|
16
35
|
defaultValue?: string;
|
|
@@ -21,6 +40,16 @@ export type ColorFieldProps = {
|
|
|
21
40
|
/** Formats offered in the picker's format selector, in order. @default all */
|
|
22
41
|
formats?: ColorFormat[];
|
|
23
42
|
alpha?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Offer a Gradient tab beside the solid picker. The value may then also be
|
|
45
|
+
* a CSS gradient string; the active tab is derived from the value, and
|
|
46
|
+
* switching tabs converts it. Solid → gradient is benign (a two-stop
|
|
47
|
+
* gradient of the colour, or the session's last gradient restored);
|
|
48
|
+
* gradient → solid is DESTRUCTIVE — the gradient collapses to its first
|
|
49
|
+
* stop's colour (recoverable only while the popover stays open).
|
|
50
|
+
* @default false
|
|
51
|
+
*/
|
|
52
|
+
gradient?: boolean;
|
|
24
53
|
disabled?: boolean;
|
|
25
54
|
/**
|
|
26
55
|
* Human-readable name shown on the trigger (e.g. "Primary"). Falls back to
|
|
@@ -56,4 +85,4 @@ export type ColorFieldProps = {
|
|
|
56
85
|
* and a label that opens a {@link ColorPicker} in a dialog popover. This is the
|
|
57
86
|
* reusable field-level control for colour inputs; the picker itself is the panel.
|
|
58
87
|
*/
|
|
59
|
-
export declare function ColorField({ value: controlledValue, defaultValue, onValueChange, format: controlledFormat, defaultFormat, onFormatChange, formats, alpha, disabled, label, showLabel, dynamic, icon, size, side, align, open, defaultOpen, onOpenChange, labels, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby, className, id, ...rest }: ColorFieldProps): import("react/jsx-runtime").JSX.Element;
|
|
88
|
+
export declare function ColorField({ value: controlledValue, defaultValue, onValueChange, format: controlledFormat, defaultFormat, onFormatChange, formats, alpha, gradient, disabled, label, showLabel, dynamic, icon, size, side, align, open, defaultOpen, onOpenChange, labels, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby, className, id, ...rest }: ColorFieldProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -4,10 +4,31 @@ import { cx } from '../../utils/cx.js';
|
|
|
4
4
|
import { isDev } from '../../utils/is-dev.js';
|
|
5
5
|
import { Popover } from '../Popover/index.js';
|
|
6
6
|
import { Icon } from '../Icon/index.js';
|
|
7
|
+
import { Tabs } from '../Tabs/index.js';
|
|
7
8
|
import { ColorPicker } from './ColorPicker.js';
|
|
9
|
+
import { GradientPicker } from '../GradientPicker/index.js';
|
|
8
10
|
import { resolveLabels } from './labels.js';
|
|
9
11
|
import { parseColor, resolveVarColor, formatColor, } from '../../utils/color/color.js';
|
|
12
|
+
import { parseGradient, formatGradient, colorAtPosition, } from '../../utils/color/gradient.js';
|
|
10
13
|
import { getContrastForeground } from '../../utils/color/contrast.js';
|
|
14
|
+
/**
|
|
15
|
+
* A hex the contrast picker can use, from any colour string — resolves
|
|
16
|
+
* `var()` opportunistically; anything unresolvable falls back to black.
|
|
17
|
+
*/
|
|
18
|
+
function hexForContrast(color, el) {
|
|
19
|
+
const p = parseColor(color);
|
|
20
|
+
if (p.kind === 'color')
|
|
21
|
+
return formatColor(p.hsva, 'hex');
|
|
22
|
+
if (p.kind === 'var') {
|
|
23
|
+
const resolved = resolveVarColor(p, el);
|
|
24
|
+
if (resolved) {
|
|
25
|
+
const rp = parseColor(resolved);
|
|
26
|
+
if (rp.kind === 'color')
|
|
27
|
+
return formatColor(rp.hsva, 'hex');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return '#000000';
|
|
31
|
+
}
|
|
11
32
|
/** Best-effort text extraction from a ReactNode, for the accessible name. */
|
|
12
33
|
function nodeToText(node) {
|
|
13
34
|
if (node == null || typeof node === 'boolean')
|
|
@@ -28,7 +49,7 @@ function nodeToText(node) {
|
|
|
28
49
|
* and a label that opens a {@link ColorPicker} in a dialog popover. This is the
|
|
29
50
|
* reusable field-level control for colour inputs; the picker itself is the panel.
|
|
30
51
|
*/
|
|
31
|
-
export function ColorField({ value: controlledValue, defaultValue = '#000000', onValueChange, format: controlledFormat, defaultFormat = 'hex', onFormatChange, formats, alpha = true, disabled, label, showLabel = true, dynamic = false, icon, size = 'md', side = 'bottom', align = 'start', open, defaultOpen, onOpenChange, labels, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby, className, id, ...rest }) {
|
|
52
|
+
export function ColorField({ value: controlledValue, defaultValue = '#000000', onValueChange, format: controlledFormat, defaultFormat = 'hex', onFormatChange, formats, alpha = true, gradient = false, disabled, label, showLabel = true, dynamic = false, icon, size = 'md', side = 'bottom', align = 'start', open, defaultOpen, onOpenChange, labels, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby, className, id, ...rest }) {
|
|
32
53
|
const triggerRef = useRef(null);
|
|
33
54
|
const isValueControlled = useRef(controlledValue !== undefined).current;
|
|
34
55
|
const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue);
|
|
@@ -39,9 +60,19 @@ export function ColorField({ value: controlledValue, defaultValue = '#000000', o
|
|
|
39
60
|
// Re-render once after mount so the trigger swatch can resolve a var().
|
|
40
61
|
const [, setMounted] = useState(false);
|
|
41
62
|
useLayoutEffect(() => setMounted(true), []);
|
|
63
|
+
// The last gradient seen this popover session — switching gradient → solid
|
|
64
|
+
// is destructive (collapses to one colour), so switching back restores this
|
|
65
|
+
// instead of re-seeding a flat two-stop. Cleared when the popover closes:
|
|
66
|
+
// the recall is an editing-session courtesy, not hidden persistent state.
|
|
67
|
+
const lastGradientRef = useRef(null);
|
|
42
68
|
const L = resolveLabels(labels);
|
|
43
69
|
const triggerBase = labels?.trigger ?? 'Select color';
|
|
44
70
|
const dynamicSuffix = labels?.dynamic ?? 'dynamic';
|
|
71
|
+
const modesLabel = labels?.modes ?? 'Color type';
|
|
72
|
+
const solidTabLabel = labels?.solidTab ?? 'Solid';
|
|
73
|
+
const gradientTabLabel = labels?.gradientTab ?? 'Gradient';
|
|
74
|
+
const gradientText = labels?.gradientText ??
|
|
75
|
+
((type, stopCount) => `Gradient (${type}, ${stopCount} stops)`);
|
|
45
76
|
const commitValue = (next) => {
|
|
46
77
|
if (!isValueControlled)
|
|
47
78
|
setUncontrolledValue(next);
|
|
@@ -54,18 +85,30 @@ export function ColorField({ value: controlledValue, defaultValue = '#000000', o
|
|
|
54
85
|
};
|
|
55
86
|
const parsed = parseColor(value);
|
|
56
87
|
const isVar = parsed.kind === 'var';
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
88
|
+
// Gradient mode: the value may be a whole CSS gradient; the tab (and the
|
|
89
|
+
// swatch treatment) derive from whether it parses as one.
|
|
90
|
+
const parsedGradient = gradient ? parseGradient(value) : null;
|
|
91
|
+
const isGradient = parsedGradient?.kind === 'gradient';
|
|
92
|
+
const firstStopColor = isGradient ? parsedGradient.gradient.stops[0].color : null;
|
|
93
|
+
// Contrast for the swatch icon: a gradient has no single colour, so sample
|
|
94
|
+
// the midpoint — where the icon actually sits (best effort; an unresolvable
|
|
95
|
+
// var() falls back to black).
|
|
96
|
+
const resolvedHex = hexForContrast(isGradient ? colorAtPosition(parsedGradient.gradient.stops, 50) : value, triggerRef.current);
|
|
62
97
|
// Dynamic values get a neutral swatch + bolt; concrete colours get the colour
|
|
63
98
|
// + an edit affordance. Icon colour is contrast-picked so it stays legible.
|
|
64
99
|
const swatchBg = dynamic ? 'var(--tui-color-fg, #111)' : resolvedHex;
|
|
65
100
|
const swatchIcon = icon ?? (dynamic ? 'system/bolt-fill' : 'system/edit-small');
|
|
66
101
|
const iconColor = dynamic ? '#fff' : getContrastForeground(resolvedHex);
|
|
67
102
|
// Trigger text: explicit label wins, else the variable name or the value.
|
|
68
|
-
|
|
103
|
+
// Gradients get a compact summary — the raw CSS string would be spelled out
|
|
104
|
+
// hex-by-hex in the accessible name (and overflow the pill).
|
|
105
|
+
const fallbackText = isGradient
|
|
106
|
+
? gradientText(parsedGradient.gradient.type, parsedGradient.gradient.stops.length)
|
|
107
|
+
: isVar
|
|
108
|
+
? parsed.name
|
|
109
|
+
: parsed.kind === 'color'
|
|
110
|
+
? formatColor(parsed.hsva, format)
|
|
111
|
+
: value;
|
|
69
112
|
const labelNode = label ?? fallbackText;
|
|
70
113
|
const labelText = nodeToText(labelNode);
|
|
71
114
|
if (isDev() &&
|
|
@@ -84,5 +127,54 @@ export function ColorField({ value: controlledValue, defaultValue = '#000000', o
|
|
|
84
127
|
const computedAriaLabel = ariaLabel ?? (ariaLabelledby ? undefined : nameContext);
|
|
85
128
|
// Popover dialog: always named with context (a labelledby id can't name it).
|
|
86
129
|
const popoverLabel = ariaLabel ?? nameContext;
|
|
87
|
-
|
|
130
|
+
const solidPicker = (_jsx(ColorPicker, { value: value, onValueChange: commitValue, format: format, onFormatChange: commitFormat, formats: formats, alpha: alpha, labels: labels, "aria-label": L.group }));
|
|
131
|
+
return (_jsxs(Popover, { open: open, defaultOpen: defaultOpen, onOpenChange: (next) => {
|
|
132
|
+
if (!next)
|
|
133
|
+
lastGradientRef.current = null;
|
|
134
|
+
onOpenChange?.(next);
|
|
135
|
+
}, children: [_jsx(Popover.Trigger, { asChild: true, children: _jsxs("button", { ref: triggerRef, type: "button", id: id, className: cx('tui-color-field', size !== 'md' && `is-size-${size}`, !showLabel && 'is-swatch-only', disabled && 'is-disabled', className), disabled: disabled, "aria-label": computedAriaLabel, "aria-labelledby": ariaLabelledby, ...rest, children: [_jsx("span", { className: "tui-color-field__swatch", "aria-hidden": "true", style: {
|
|
136
|
+
'--_swatch': swatchBg,
|
|
137
|
+
'--_swatch-icon': iconColor,
|
|
138
|
+
// A gradient paints the whole swatch; solid values keep the
|
|
139
|
+
// single-colour layer (the CSS fallback).
|
|
140
|
+
// Canonical form, not the raw prop — defence-in-depth against
|
|
141
|
+
// consumer-supplied strings landing verbatim in a CSS var.
|
|
142
|
+
...(isGradient && !dynamic
|
|
143
|
+
? { '--_swatch-image': formatGradient(parsedGradient.gradient) }
|
|
144
|
+
: null),
|
|
145
|
+
}, children: _jsx(Icon, { name: swatchIcon, className: "tui-color-field__swatch-icon" }) }), showLabel && _jsx("span", { className: "tui-color-field__label", children: labelNode })] }) }), _jsx(Popover.Content, { side: side, align: align, modal: true, "aria-label": popoverLabel, className: "tui-color-field__popover", children: gradient ? (_jsxs(Tabs
|
|
146
|
+
// The tab derives from the value; switching CONVERTS the value
|
|
147
|
+
// (matching the reference design): solid → a two-stop gradient of
|
|
148
|
+
// itself, gradient → its first stop's colour. Because switching is
|
|
149
|
+
// destructive (a gradient collapses to one colour), activation is
|
|
150
|
+
// MANUAL — arrowing through the tablist must not convert; only
|
|
151
|
+
// Enter/Space/click commits the switch.
|
|
152
|
+
, {
|
|
153
|
+
// The tab derives from the value; switching CONVERTS the value
|
|
154
|
+
// (matching the reference design): solid → a two-stop gradient of
|
|
155
|
+
// itself, gradient → its first stop's colour. Because switching is
|
|
156
|
+
// destructive (a gradient collapses to one colour), activation is
|
|
157
|
+
// MANUAL — arrowing through the tablist must not convert; only
|
|
158
|
+
// Enter/Space/click commits the switch.
|
|
159
|
+
activationMode: "manual", value: isGradient ? 'gradient' : 'solid', onValueChange: (mode) => {
|
|
160
|
+
if (mode === 'gradient' && !isGradient) {
|
|
161
|
+
// Restore the session's last gradient if there is one.
|
|
162
|
+
const recalled = lastGradientRef.current;
|
|
163
|
+
if (recalled && parseGradient(recalled).kind === 'gradient') {
|
|
164
|
+
commitValue(recalled);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
// Seed from the solid colour — VALIDATED: an unparseable value
|
|
168
|
+
// (named colour, junk) wrapped verbatim would produce a string
|
|
169
|
+
// neither picker can edit, and compound on every switch.
|
|
170
|
+
const seed = parseColor(value).kind === 'invalid' ? '#000000' : value;
|
|
171
|
+
const candidate = `linear-gradient(90deg, ${seed} 0%, ${seed} 100%)`;
|
|
172
|
+
if (parseGradient(candidate).kind === 'gradient')
|
|
173
|
+
commitValue(candidate);
|
|
174
|
+
}
|
|
175
|
+
else if (mode === 'solid' && isGradient && firstStopColor) {
|
|
176
|
+
lastGradientRef.current = value;
|
|
177
|
+
commitValue(firstStopColor);
|
|
178
|
+
}
|
|
179
|
+
}, className: "tui-color-field__tabs", children: [_jsxs(Tabs.List, { "aria-label": modesLabel, children: [_jsx(Tabs.Tab, { value: "solid", children: solidTabLabel }), _jsx(Tabs.Tab, { value: "gradient", children: gradientTabLabel })] }), _jsx(Tabs.Panel, { value: "solid", children: solidPicker }), _jsx(Tabs.Panel, { value: "gradient", children: _jsx(GradientPicker, { value: value, onValueChange: commitValue, formats: formats, alpha: alpha, labels: labels?.gradientPicker }) })] })) : (solidPicker) })] }));
|
|
88
180
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ComboboxProps, ComboboxContentProps, ComboboxOptionProps, ComboboxGroupProps, ComboboxLabelProps } from './types';
|
|
2
|
-
declare function ComboboxRoot({ value: controlledValue, defaultValue, onValueChange, inputValue: controlledInputValue, onInputChange, open: controlledOpen, defaultOpen, onOpenChange, disabled, placeholder, size, openOnFocus, filterMode, onQueryChange, clearable, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, inputClassName, labels: labelsProp, children, }: ComboboxProps): import("react/jsx-runtime").JSX.Element;
|
|
2
|
+
declare function ComboboxRoot({ value: controlledValue, defaultValue, onValueChange, inputValue: controlledInputValue, onInputChange, open: controlledOpen, defaultOpen, onOpenChange, disabled, placeholder, size, openOnFocus, filterMode, onQueryChange, clearable, id: inputIdProp, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, inputClassName, labels: labelsProp, children, }: ComboboxProps): import("react/jsx-runtime").JSX.Element;
|
|
3
3
|
declare namespace ComboboxRoot {
|
|
4
4
|
var displayName: string;
|
|
5
5
|
}
|
|
@@ -13,7 +13,7 @@ import { defaultComboboxLabels } from './types.js';
|
|
|
13
13
|
// =============================================================================
|
|
14
14
|
// Combobox Root
|
|
15
15
|
// =============================================================================
|
|
16
|
-
function ComboboxRoot({ value: controlledValue, defaultValue, onValueChange, inputValue: controlledInputValue, onInputChange, open: controlledOpen, defaultOpen, onOpenChange, disabled = false, placeholder = '', size = 'md', openOnFocus = true, filterMode = 'always', onQueryChange, clearable = true, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, inputClassName, labels: labelsProp, children, }) {
|
|
16
|
+
function ComboboxRoot({ value: controlledValue, defaultValue, onValueChange, inputValue: controlledInputValue, onInputChange, open: controlledOpen, defaultOpen, onOpenChange, disabled = false, placeholder = '', size = 'md', openOnFocus = true, filterMode = 'always', onQueryChange, clearable = true, id: inputIdProp, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, inputClassName, labels: labelsProp, children, }) {
|
|
17
17
|
const labels = { ...defaultComboboxLabels, ...labelsProp };
|
|
18
18
|
// Controlled/uncontrolled value (initialize from defaultValue)
|
|
19
19
|
const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue);
|
|
@@ -48,7 +48,7 @@ function ComboboxRoot({ value: controlledValue, defaultValue, onValueChange, inp
|
|
|
48
48
|
openRef.current = open;
|
|
49
49
|
// IDs
|
|
50
50
|
const baseId = useId();
|
|
51
|
-
const inputId = `${baseId}-input`;
|
|
51
|
+
const inputId = inputIdProp ?? `${baseId}-input`;
|
|
52
52
|
const listboxId = `${baseId}-listbox`;
|
|
53
53
|
// Refs
|
|
54
54
|
const inputRef = useRef(null);
|
|
@@ -83,6 +83,11 @@ export type ComboboxProps = {
|
|
|
83
83
|
* @default true
|
|
84
84
|
*/
|
|
85
85
|
openOnFocus?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Optional input ID override, applied to the inner `<input>`.
|
|
88
|
+
* Useful for Field integration via htmlFor — Field.Control injects this.
|
|
89
|
+
*/
|
|
90
|
+
id?: string;
|
|
86
91
|
/**
|
|
87
92
|
* Accessible name for the combobox.
|
|
88
93
|
*/
|
|
@@ -16,7 +16,19 @@ export type FieldProps = {
|
|
|
16
16
|
className?: string;
|
|
17
17
|
children?: React.ReactNode;
|
|
18
18
|
};
|
|
19
|
-
type LabelProps = React.LabelHTMLAttributes<HTMLLabelElement> & {
|
|
19
|
+
type LabelProps = ((React.LabelHTMLAttributes<HTMLLabelElement> & {
|
|
20
|
+
/**
|
|
21
|
+
* Element to render. Use 'span' for group controls (RadioGroup,
|
|
22
|
+
* SegmentedControl — anything rendering role="radiogroup"/"group" on a
|
|
23
|
+
* <div>) where <label for> would reference a non-labelable element.
|
|
24
|
+
* The aria-labelledby wiring via Field.Control is unaffected.
|
|
25
|
+
* @default 'label'
|
|
26
|
+
*/
|
|
27
|
+
as?: 'label';
|
|
28
|
+
}) | (React.HTMLAttributes<HTMLSpanElement> & {
|
|
29
|
+
as: 'span';
|
|
30
|
+
htmlFor?: never;
|
|
31
|
+
})) & {
|
|
20
32
|
/** Visually hide the label while keeping it accessible */
|
|
21
33
|
hidden?: boolean;
|
|
22
34
|
className?: string;
|
|
@@ -30,14 +30,16 @@ export const Field = forwardRef(function Field({ error = false, required = false
|
|
|
30
30
|
// =============================================================================
|
|
31
31
|
// Field.Label
|
|
32
32
|
// =============================================================================
|
|
33
|
-
function FieldLabel({ hidden = false, className, children, ...rest }) {
|
|
33
|
+
function FieldLabel({ as: Tag = 'label', hidden = false, className, children, ...rest }) {
|
|
34
34
|
const { controlId, labelId, required, labelRendered, labels } = useFieldContext();
|
|
35
35
|
// Signal to Field.Control that a label element exists in the tree.
|
|
36
36
|
// This is read synchronously during the same render pass (Label renders
|
|
37
37
|
// before Control in JSX order) to decide whether aria-labelledby is safe.
|
|
38
38
|
labelRendered.current = true;
|
|
39
39
|
const classes = cx('tui-field__label', hidden && 'tui-visually-hidden', className);
|
|
40
|
-
|
|
40
|
+
// htmlFor only on a real <label> — the `for` attribute must reference a
|
|
41
|
+
// labelable element, which group controls (div[role="radiogroup"]) are not.
|
|
42
|
+
return (_jsxs(Tag, { id: labelId, ...(Tag === 'label' ? { htmlFor: controlId } : undefined), className: classes, ...rest, children: [children, required && (_jsxs(_Fragment, { children: [_jsx("span", { className: "tui-field__required", "aria-hidden": "true", children: "*" }), _jsx("span", { className: "tui-visually-hidden", children: labels.required })] }))] }));
|
|
41
43
|
}
|
|
42
44
|
FieldLabel.displayName = 'Field.Label';
|
|
43
45
|
// =============================================================================
|
|
@@ -71,12 +73,12 @@ function FieldControl({ children }) {
|
|
|
71
73
|
const labelledBy = childLabelledBy ?? (injectedLabelledBy ? labelId : undefined);
|
|
72
74
|
// DEV: warn if Field.Label appears after Field.Control in JSX order —
|
|
73
75
|
// labelRendered will be false at render time but true after mount.
|
|
74
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
75
76
|
useEffect(() => {
|
|
76
77
|
if (isDev() && !injectedLabelledBy && !childLabelledBy && labelRendered.current) {
|
|
77
78
|
console.warn('Field: Field.Label appears after Field.Control in the JSX tree. ' +
|
|
78
79
|
'Move Field.Label before Field.Control so aria-labelledby is wired correctly.');
|
|
79
80
|
}
|
|
81
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
80
82
|
}, []);
|
|
81
83
|
// Clone child with a11y props
|
|
82
84
|
// Note: aria-invalid and aria-required must be string "true", not boolean
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type AngleDialProps = {
|
|
2
|
+
/** Degrees, 0–360; 0 points up (the CSS gradient convention). */
|
|
3
|
+
angle: number;
|
|
4
|
+
onAngleChange: (angle: number) => void;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
className?: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Pointer-only companion dial for the angle input — drag to point the
|
|
10
|
+
* gradient. Deliberately NOT focusable and hidden from assistive tech: the
|
|
11
|
+
* number input beside it is the accessible editor for the same value, and a
|
|
12
|
+
* second tab stop for one value would be noise, not access.
|
|
13
|
+
*/
|
|
14
|
+
export declare function AngleDial({ angle, onAngleChange, disabled, className }: AngleDialProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useRef } from 'react';
|
|
3
|
+
import { cx } from '../../utils/cx.js';
|
|
4
|
+
import { usePointerDrag } from '../ColorPicker/usePointerDrag.js';
|
|
5
|
+
/**
|
|
6
|
+
* Pointer-only companion dial for the angle input — drag to point the
|
|
7
|
+
* gradient. Deliberately NOT focusable and hidden from assistive tech: the
|
|
8
|
+
* number input beside it is the accessible editor for the same value, and a
|
|
9
|
+
* second tab stop for one value would be noise, not access.
|
|
10
|
+
*/
|
|
11
|
+
export function AngleDial({ angle, onAngleChange, disabled, className }) {
|
|
12
|
+
const ref = useRef(null);
|
|
13
|
+
const drag = usePointerDrag((clientX, clientY) => {
|
|
14
|
+
const rect = ref.current?.getBoundingClientRect();
|
|
15
|
+
if (!rect)
|
|
16
|
+
return;
|
|
17
|
+
const dx = clientX - (rect.left + rect.width / 2);
|
|
18
|
+
const dy = clientY - (rect.top + rect.height / 2);
|
|
19
|
+
// atan2(dx, -dy): 0deg up, clockwise — matches linear-gradient().
|
|
20
|
+
const deg = (Math.atan2(dx, -dy) * 180) / Math.PI;
|
|
21
|
+
onAngleChange(Math.round(((deg % 360) + 360) % 360));
|
|
22
|
+
}, disabled);
|
|
23
|
+
return (_jsx("div", { ref: ref, className: cx('tui-angle-dial', disabled && 'is-disabled', className), "aria-hidden": "true", style: { '--_angle': `${angle}deg` }, ...drag, children: _jsx("span", { className: "tui-angle-dial__pointer" }) }));
|
|
24
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type GradientStop } from '../../utils/color/gradient';
|
|
2
|
+
export type GradientBarLabels = {
|
|
3
|
+
/** Accessible name for a stop thumb. */
|
|
4
|
+
stop?: (index: number, total: number, color: string) => string;
|
|
5
|
+
/** Value text for a stop thumb's position. */
|
|
6
|
+
stopValueText?: (percent: number) => string;
|
|
7
|
+
};
|
|
8
|
+
export type GradientBarProps = {
|
|
9
|
+
/** Expected sorted by position — thumbs render (and tab) in array order. */
|
|
10
|
+
stops: GradientStop[];
|
|
11
|
+
/** Highlighted stop (the one being edited). */
|
|
12
|
+
activeIndex?: number | null;
|
|
13
|
+
onActiveIndexChange?: (index: number) => void;
|
|
14
|
+
/** Raw position from drag / keyboard — the owner clamps (e.g. to neighbours). */
|
|
15
|
+
onStopPositionChange?: (index: number, position: number) => void;
|
|
16
|
+
/** Track click between thumbs. The owner inserts and seeds the colour. */
|
|
17
|
+
onStopAdd?: (position: number) => void;
|
|
18
|
+
/** Delete/Backspace on a thumb. The owner enforces the two-stop minimum. */
|
|
19
|
+
onStopRemove?: (index: number) => void;
|
|
20
|
+
/** Enter/Space or click on a thumb — open this stop's colour editor. */
|
|
21
|
+
onStopActivate?: (index: number) => void;
|
|
22
|
+
labels?: GradientBarLabels;
|
|
23
|
+
disabled?: boolean;
|
|
24
|
+
'aria-label'?: string;
|
|
25
|
+
className?: string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The stop strip — stops rendered left-to-right as a linear gradient with a
|
|
29
|
+
* draggable thumb per stop, regardless of the gradient's actual type (the
|
|
30
|
+
* strip edits the stop list, not the geometry).
|
|
31
|
+
*
|
|
32
|
+
* Each thumb is a `role="slider"` for its position: arrows nudge (Shift = ×10),
|
|
33
|
+
* Home/End jump, Delete removes, Enter/Space opens the stop's colour editor.
|
|
34
|
+
* Clicking the track between thumbs adds a stop there. The stop LIST rendered
|
|
35
|
+
* by GradientPicker remains the fully accessible editing surface; the bar is
|
|
36
|
+
* the direct-manipulation view of the same data.
|
|
37
|
+
*/
|
|
38
|
+
export declare function GradientBar({ stops, activeIndex, onActiveIndexChange, onStopPositionChange, onStopAdd, onStopRemove, onStopActivate, labels, disabled, 'aria-label': ariaLabel, className, }: GradientBarProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef } from 'react';
|
|
3
|
+
import { cx } from '../../utils/cx.js';
|
|
4
|
+
import { formatStops } from '../../utils/color/gradient.js';
|
|
5
|
+
import { clamp01 } from '../ColorPicker/usePointerDrag.js';
|
|
6
|
+
import { defaultGradientPickerLabels } from './labels.js';
|
|
7
|
+
/**
|
|
8
|
+
* The stop strip — stops rendered left-to-right as a linear gradient with a
|
|
9
|
+
* draggable thumb per stop, regardless of the gradient's actual type (the
|
|
10
|
+
* strip edits the stop list, not the geometry).
|
|
11
|
+
*
|
|
12
|
+
* Each thumb is a `role="slider"` for its position: arrows nudge (Shift = ×10),
|
|
13
|
+
* Home/End jump, Delete removes, Enter/Space opens the stop's colour editor.
|
|
14
|
+
* Clicking the track between thumbs adds a stop there. The stop LIST rendered
|
|
15
|
+
* by GradientPicker remains the fully accessible editing surface; the bar is
|
|
16
|
+
* the direct-manipulation view of the same data.
|
|
17
|
+
*/
|
|
18
|
+
export function GradientBar({ stops, activeIndex, onActiveIndexChange, onStopPositionChange, onStopAdd, onStopRemove, onStopActivate, labels, disabled, 'aria-label': ariaLabel, className, }) {
|
|
19
|
+
const trackRef = useRef(null);
|
|
20
|
+
const dragIndex = useRef(null);
|
|
21
|
+
// True once a captured drag actually moved — the browser still fires a
|
|
22
|
+
// click on release, which must not open the stop editor.
|
|
23
|
+
const dragMoved = useRef(false);
|
|
24
|
+
// After a keyboard removal the focused thumb unmounts; focus the neighbour
|
|
25
|
+
// (index captured before removal) instead of dropping focus to <body>.
|
|
26
|
+
const pendingFocus = useRef(null);
|
|
27
|
+
const prevLength = useRef(stops.length);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
// Only consume the pending focus when the stop count actually DECREASED.
|
|
30
|
+
// The owner may reject a removal (two-stop minimum), leaving the ref
|
|
31
|
+
// stale — without this guard the next length change of any kind (adding
|
|
32
|
+
// a stop) would steal focus onto a thumb.
|
|
33
|
+
const decreased = stops.length < prevLength.current;
|
|
34
|
+
prevLength.current = stops.length;
|
|
35
|
+
const target = pendingFocus.current;
|
|
36
|
+
pendingFocus.current = null;
|
|
37
|
+
if (!decreased || target == null)
|
|
38
|
+
return;
|
|
39
|
+
const index = Math.min(target, stops.length - 1);
|
|
40
|
+
const thumbs = trackRef.current?.querySelectorAll('[role="slider"]');
|
|
41
|
+
thumbs?.[index]?.focus();
|
|
42
|
+
}, [stops.length]);
|
|
43
|
+
const positionFromClientX = (clientX) => {
|
|
44
|
+
const rect = trackRef.current?.getBoundingClientRect();
|
|
45
|
+
if (!rect || rect.width === 0)
|
|
46
|
+
return 0;
|
|
47
|
+
return clamp01((clientX - rect.left) / rect.width) * 100;
|
|
48
|
+
};
|
|
49
|
+
const handleTrackPointerDown = (e) => {
|
|
50
|
+
if (disabled || e.button !== 0)
|
|
51
|
+
return;
|
|
52
|
+
// Thumbs handle their own pointerdown (and stop propagation).
|
|
53
|
+
onStopAdd?.(positionFromClientX(e.clientX));
|
|
54
|
+
};
|
|
55
|
+
const handleThumbPointerDown = (index) => (e) => {
|
|
56
|
+
if (disabled || e.button !== 0)
|
|
57
|
+
return;
|
|
58
|
+
e.preventDefault();
|
|
59
|
+
e.stopPropagation();
|
|
60
|
+
e.currentTarget.setPointerCapture?.(e.pointerId);
|
|
61
|
+
e.currentTarget.focus();
|
|
62
|
+
dragIndex.current = index;
|
|
63
|
+
dragMoved.current = false;
|
|
64
|
+
onActiveIndexChange?.(index);
|
|
65
|
+
};
|
|
66
|
+
const handleThumbPointerMove = (e) => {
|
|
67
|
+
if (dragIndex.current == null)
|
|
68
|
+
return;
|
|
69
|
+
dragMoved.current = true;
|
|
70
|
+
onStopPositionChange?.(dragIndex.current, positionFromClientX(e.clientX));
|
|
71
|
+
};
|
|
72
|
+
const handleThumbPointerUp = (e) => {
|
|
73
|
+
dragIndex.current = null;
|
|
74
|
+
try {
|
|
75
|
+
e.currentTarget.releasePointerCapture?.(e.pointerId);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
/* pointer already released */
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
const handleThumbKeyDown = (index) => (e) => {
|
|
82
|
+
if (disabled)
|
|
83
|
+
return;
|
|
84
|
+
const position = stops[index].position;
|
|
85
|
+
const step = e.shiftKey ? 10 : 1;
|
|
86
|
+
let next = null;
|
|
87
|
+
if (e.key === 'ArrowLeft' || e.key === 'ArrowDown')
|
|
88
|
+
next = position - step;
|
|
89
|
+
else if (e.key === 'ArrowRight' || e.key === 'ArrowUp')
|
|
90
|
+
next = position + step;
|
|
91
|
+
else if (e.key === 'Home')
|
|
92
|
+
next = 0;
|
|
93
|
+
else if (e.key === 'End')
|
|
94
|
+
next = 100;
|
|
95
|
+
else if (e.key === 'Delete' || e.key === 'Backspace') {
|
|
96
|
+
e.preventDefault();
|
|
97
|
+
pendingFocus.current = index > 0 ? index - 1 : 0;
|
|
98
|
+
onStopRemove?.(index);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
else if (e.key === 'Enter' || e.key === ' ') {
|
|
102
|
+
e.preventDefault();
|
|
103
|
+
onStopActivate?.(index);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
else
|
|
107
|
+
return;
|
|
108
|
+
e.preventDefault();
|
|
109
|
+
onStopPositionChange?.(index, Math.min(100, Math.max(0, next)));
|
|
110
|
+
};
|
|
111
|
+
const stopLabel = labels?.stop ?? defaultGradientPickerLabels.stop;
|
|
112
|
+
const valueText = labels?.stopValueText ?? defaultGradientPickerLabels.stopValueText;
|
|
113
|
+
const sorted = [...stops].sort((a, b) => a.position - b.position);
|
|
114
|
+
return (_jsxs("div", { ref: trackRef, className: cx('tui-gradient-bar', disabled && 'is-disabled', className), role: "group", "aria-label": ariaLabel, onPointerDown: handleTrackPointerDown, children: [_jsx("div", { className: "tui-gradient-bar__track", style: { background: `linear-gradient(90deg, ${formatStops(sorted)})` } }), stops.map((stop, i) => (_jsx("span", { className: cx('tui-gradient-bar__thumb', i === activeIndex && 'is-active'), role: "slider", tabIndex: disabled ? -1 : 0, "aria-label": stopLabel(i, stops.length, stop.color), "aria-valuemin": 0, "aria-valuemax": 100, "aria-valuenow": Math.round(stop.position), "aria-valuetext": valueText(Math.round(stop.position)), "aria-disabled": disabled || undefined,
|
|
115
|
+
// Thumb centres run from thumb/2 to 100% − thumb/2 so the end stops
|
|
116
|
+
// sit fully inside the strip instead of hanging off its corners.
|
|
117
|
+
style: {
|
|
118
|
+
left: `calc(var(--_thumb, 16px) / 2 + ${stop.position / 100} * (100% - var(--_thumb, 16px)))`,
|
|
119
|
+
'--_stop-color': stop.color,
|
|
120
|
+
}, onPointerDown: handleThumbPointerDown(i), onPointerMove: handleThumbPointerMove, onPointerUp: handleThumbPointerUp, onKeyDown: handleThumbKeyDown(i), onClick: (e) => {
|
|
121
|
+
e.stopPropagation();
|
|
122
|
+
// A completed drag ends in a click too — only a clean press
|
|
123
|
+
// (no movement) opens the stop editor.
|
|
124
|
+
if (dragMoved.current) {
|
|
125
|
+
dragMoved.current = false;
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
onStopActivate?.(i);
|
|
129
|
+
} }, i)))] }));
|
|
130
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ColorFormat } from '../../utils/color/color';
|
|
2
|
+
import { type GradientPickerLabels } from './labels';
|
|
3
|
+
export type GradientPickerProps = {
|
|
4
|
+
/** Controlled value — a CSS gradient string. */
|
|
5
|
+
value?: string;
|
|
6
|
+
/** Uncontrolled initial value. @default a two-stop linear gradient */
|
|
7
|
+
defaultValue?: string;
|
|
8
|
+
onValueChange?: (value: string) => void;
|
|
9
|
+
/** Show alpha in the per-stop colour pickers. @default true */
|
|
10
|
+
alpha?: boolean;
|
|
11
|
+
/** Formats offered in the per-stop colour pickers. @default all */
|
|
12
|
+
formats?: ColorFormat[];
|
|
13
|
+
disabled?: boolean;
|
|
14
|
+
/** Override any user-facing string for i18n. */
|
|
15
|
+
labels?: GradientPickerLabels;
|
|
16
|
+
/** Accessible name for the picker group. Takes precedence over `labels.group`. */
|
|
17
|
+
'aria-label'?: string;
|
|
18
|
+
className?: string;
|
|
19
|
+
} & Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'color' | 'role'>;
|
|
20
|
+
/**
|
|
21
|
+
* Gradient picker: a stop strip with draggable thumbs, an editable CSS value
|
|
22
|
+
* field, a fully accessible colour-stop list (each stop opens a {@link
|
|
23
|
+
* ColorPicker} in a popover), and per-type geometry controls (linear angle +
|
|
24
|
+
* dial, radial size/position, conic position).
|
|
25
|
+
*
|
|
26
|
+
* The value contract is a CSS gradient string — `linear-gradient(…)`,
|
|
27
|
+
* `radial-gradient(…)` or `conic-gradient(…)` — accepted (pasted or typed)
|
|
28
|
+
* and emitted. Stops keep whatever colour strings the ColorPicker accepts,
|
|
29
|
+
* including `var(--x)` references. An unparseable value falls back to a
|
|
30
|
+
* default gradient in the controls; committing any edit replaces it.
|
|
31
|
+
*/
|
|
32
|
+
export declare function GradientPicker({ value: controlledValue, defaultValue, onValueChange, alpha, formats, disabled, labels, 'aria-label': ariaLabel, className, ...rest }: GradientPickerProps): import("react/jsx-runtime").JSX.Element;
|