@openartifactsai/ui 0.0.0-stage → 0.23.0-rc.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +82 -4
- package/src/button.d.ts +30 -0
- package/src/button.js +29 -0
- package/src/clicks.d.ts +63 -0
- package/src/clicks.js +75 -0
- package/src/cn.d.ts +3 -0
- package/src/cn.js +5 -0
- package/src/color-picker.d.ts +98 -0
- package/src/color-picker.js +395 -0
- package/src/color.d.ts +150 -0
- package/src/color.js +543 -0
- package/src/crash-boundary.d.ts +29 -0
- package/src/crash-boundary.js +32 -0
- package/src/data/google-fonts.json +1775 -0
- package/src/drag-cover.d.ts +48 -0
- package/src/drag-cover.js +41 -0
- package/src/effect-editors.d.ts +48 -0
- package/src/effect-editors.js +230 -0
- package/src/effects.d.ts +118 -0
- package/src/effects.js +425 -0
- package/src/escape-to-popup.d.ts +53 -0
- package/src/escape-to-popup.js +78 -0
- package/src/field-announce.d.ts +28 -0
- package/src/field-announce.js +43 -0
- package/src/fields.d.ts +176 -0
- package/src/fields.js +377 -0
- package/src/focusables.d.ts +50 -0
- package/src/focusables.js +61 -0
- package/src/font-picker.d.ts +62 -0
- package/src/font-picker.js +242 -0
- package/src/fonts.d.ts +121 -0
- package/src/fonts.js +273 -0
- package/src/history-diff.d.ts +64 -0
- package/src/history-diff.js +151 -0
- package/src/history.d.ts +95 -0
- package/src/history.js +160 -0
- package/src/index.d.ts +38 -0
- package/src/index.js +38 -0
- package/src/key-chord.d.ts +74 -0
- package/src/key-chord.js +228 -0
- package/src/keys.d.ts +142 -0
- package/src/keys.js +172 -0
- package/src/layout.d.ts +74 -0
- package/src/layout.js +173 -0
- package/src/link-card-view.d.ts +97 -0
- package/src/link-card-view.js +175 -0
- package/src/link-card.d.ts +136 -0
- package/src/link-card.js +158 -0
- package/src/menu-shortcuts.d.ts +31 -0
- package/src/menu-shortcuts.js +91 -0
- package/src/menu.d.ts +120 -0
- package/src/menu.js +100 -0
- package/src/number-entry.d.ts +140 -0
- package/src/number-entry.js +202 -0
- package/src/number-limits.d.ts +2 -0
- package/src/number-limits.js +2 -0
- package/src/number-wheel.d.ts +29 -0
- package/src/number-wheel.js +27 -0
- package/src/numbers.d.ts +28 -0
- package/src/numbers.js +124 -0
- package/src/overlays.d.ts +71 -0
- package/src/overlays.js +131 -0
- package/src/pressed.d.ts +5 -0
- package/src/pressed.js +10 -0
- package/src/scroll.d.ts +24 -0
- package/src/scroll.js +111 -0
- package/src/scrub-number-field.d.ts +79 -0
- package/src/scrub-number-field.js +254 -0
- package/src/select-announce.d.ts +50 -0
- package/src/select-announce.js +71 -0
- package/src/slider-session.d.ts +43 -0
- package/src/slider-session.js +39 -0
- package/src/styles.css +96 -0
- package/src/tab-strip-layout.d.ts +76 -0
- package/src/tab-strip-layout.js +111 -0
- package/src/tab-strip.d.ts +73 -0
- package/src/tab-strip.js +498 -0
- package/src/text-area-view.d.ts +35 -0
- package/src/text-area-view.js +142 -0
- package/src/text-area.d.ts +38 -0
- package/src/text-area.js +51 -0
- package/src/theme.css +34 -0
- package/src/toast-fit.d.ts +21 -0
- package/src/toast-fit.js +30 -0
- package/src/toast-tone.d.ts +27 -0
- package/src/toast-tone.js +22 -0
- package/src/tooltip.d.ts +23 -0
- package/src/tooltip.js +18 -0
package/src/fields.d.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { type FocusEvent, type InputHTMLAttributes, type ReactNode } from 'react';
|
|
2
|
+
import { type NumberLimit } from './number-entry.js';
|
|
3
|
+
/**
|
|
4
|
+
* Shared look of single-line inputs. Every field's box also carries `data-ui-field`, so a page can give the fields
|
|
5
|
+
* of one part of it another look (a compact inspector, say).
|
|
6
|
+
*/
|
|
7
|
+
export declare 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";
|
|
8
|
+
/**
|
|
9
|
+
* A plain text input. As `type="number"`, a wheel turned over it while it has the focus steps the value without the
|
|
10
|
+
* number sliding sideways in the box.
|
|
11
|
+
*/
|
|
12
|
+
export declare const TextInput: import("react").ForwardRefExoticComponent<InputHTMLAttributes<HTMLInputElement> & import("react").RefAttributes<HTMLInputElement>>;
|
|
13
|
+
/** Props of {@link CommitInput}. */
|
|
14
|
+
export interface CommitInputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'value' | 'onChange'> {
|
|
15
|
+
value: string;
|
|
16
|
+
/** Called on Enter or blur, only when the text changed. Returning `false` refuses the text: the field shows `value` again. */
|
|
17
|
+
onCommit: (value: string) => void | boolean;
|
|
18
|
+
/** Called with the draft as it is typed, and with `value` again when Escape puts it back. */
|
|
19
|
+
onDraftChange?: (draft: string) => void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A text input that keeps a local draft and commits on Enter or blur; Escape
|
|
23
|
+
* restores the last committed value. Inspector edits use it so every
|
|
24
|
+
* keystroke does not become a document write. ⌘S / Ctrl+S commits the draft
|
|
25
|
+
* and lets the key go on to the page's save, keeping focus in the field.
|
|
26
|
+
*/
|
|
27
|
+
export declare function CommitInput({ value, onCommit, onDraftChange, className, ...rest }: CommitInputProps): import("react/jsx-runtime").JSX.Element;
|
|
28
|
+
/** Props of {@link NumberInput}. */
|
|
29
|
+
export interface NumberInputProps {
|
|
30
|
+
value: number | null;
|
|
31
|
+
/** Live updates while scrubbing or typing. */
|
|
32
|
+
onChange?: (value: number | null) => void;
|
|
33
|
+
/** Final value after typing (blur), scrub release or a step. */
|
|
34
|
+
onCommit: (value: number | null) => void;
|
|
35
|
+
/** Short label drawn in the scrub area, e.g. `W`, `X` or an icon. */
|
|
36
|
+
label?: ReactNode;
|
|
37
|
+
unit?: string;
|
|
38
|
+
min?: number;
|
|
39
|
+
max?: number;
|
|
40
|
+
step?: number;
|
|
41
|
+
placeholder?: string;
|
|
42
|
+
className?: string;
|
|
43
|
+
'aria-label'?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Told when the field changed what the user typed: held it at a bound, or
|
|
46
|
+
* (in a field whose minimum is zero or more) dropped a minus sign. Called
|
|
47
|
+
* once per commit or key with every change, in order, instead of the field
|
|
48
|
+
* saying it ({@link numberLimitMessage}).
|
|
49
|
+
*/
|
|
50
|
+
onLimit?: (limits: readonly NumberLimit[]) => void;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A number field whose label can be dragged horizontally to scrub the value.
|
|
54
|
+
*
|
|
55
|
+
* Typed text past `min` or `max` is held at the bound. When `min` is zero or
|
|
56
|
+
* more the field takes no negative numbers: the minus key does nothing, and
|
|
57
|
+
* typed or pasted text that starts with a minus sign keeps its number without
|
|
58
|
+
* the sign (`-5` is 5, not the minimum). The field says both to screen
|
|
59
|
+
* readers, unless `onLimit` takes them over. ⌘S /
|
|
60
|
+
* Ctrl+S commits what is typed before the page saves.
|
|
61
|
+
*/
|
|
62
|
+
export declare function NumberInput({ value, onChange, onCommit, label, unit, min, max, step, placeholder, className, onLimit, ...aria }: NumberInputProps): import("react/jsx-runtime").JSX.Element;
|
|
63
|
+
/** One option of {@link SelectInput} and {@link Segmented}. */
|
|
64
|
+
export interface Option<T extends string> {
|
|
65
|
+
value: T;
|
|
66
|
+
label: ReactNode;
|
|
67
|
+
/** Icon-only segments need an accessible name. */
|
|
68
|
+
title?: string;
|
|
69
|
+
/** Listed but not to be picked. */
|
|
70
|
+
disabled?: boolean;
|
|
71
|
+
}
|
|
72
|
+
/** Props of {@link SelectInput}. */
|
|
73
|
+
export interface SelectInputProps<T extends string> {
|
|
74
|
+
value: T | null;
|
|
75
|
+
options: Option<T>[];
|
|
76
|
+
onChange: (value: T) => void;
|
|
77
|
+
placeholder?: string;
|
|
78
|
+
className?: string;
|
|
79
|
+
'aria-label'?: string;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Focus handlers for a select's trigger that say its new value (`label`)
|
|
83
|
+
* politely while it has the focus (see `select-announce.ts`).
|
|
84
|
+
*/
|
|
85
|
+
export declare function useSelectAnnouncement(label: string): {
|
|
86
|
+
onFocus(event: FocusEvent<HTMLElement>): void;
|
|
87
|
+
onBlur(event: FocusEvent<HTMLElement>): void;
|
|
88
|
+
};
|
|
89
|
+
/** A dropdown select. Its new value is said while its trigger has the focus. */
|
|
90
|
+
export declare function SelectInput<T extends string>({ value, options, onChange, placeholder, className, ...aria }: SelectInputProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
91
|
+
/** Props of {@link Segmented}. */
|
|
92
|
+
export interface SegmentedProps<T extends string> {
|
|
93
|
+
value: T | null;
|
|
94
|
+
options: Option<T>[];
|
|
95
|
+
onChange: (value: T) => void;
|
|
96
|
+
/**
|
|
97
|
+
* Called with the picked option when it is pressed again (to let the pick go, say). Without it, that press does
|
|
98
|
+
* nothing and the pick stays.
|
|
99
|
+
*/
|
|
100
|
+
onRepick?: (value: T) => void;
|
|
101
|
+
className?: string;
|
|
102
|
+
'aria-label'?: string;
|
|
103
|
+
}
|
|
104
|
+
/** A segmented control (one of a few options, all visible). */
|
|
105
|
+
export declare function Segmented<T extends string>({ value, options, onChange, onRepick, className, ...aria }: SegmentedProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
106
|
+
/** Props of {@link SwitchInput}. */
|
|
107
|
+
export interface SwitchInputProps {
|
|
108
|
+
checked: boolean;
|
|
109
|
+
onChange: (checked: boolean) => void;
|
|
110
|
+
'aria-label'?: string;
|
|
111
|
+
disabled?: boolean;
|
|
112
|
+
}
|
|
113
|
+
/** An on/off switch. A held Enter flips it once. */
|
|
114
|
+
export declare function SwitchInput({ checked, onChange, disabled, ...aria }: SwitchInputProps): import("react/jsx-runtime").JSX.Element;
|
|
115
|
+
/** Class names for the parts of a {@link SliderInput}, for an editor with its own look. */
|
|
116
|
+
export interface SliderLook {
|
|
117
|
+
root?: string;
|
|
118
|
+
control?: string;
|
|
119
|
+
track?: string;
|
|
120
|
+
indicator?: string;
|
|
121
|
+
thumb?: string;
|
|
122
|
+
}
|
|
123
|
+
/** Props of {@link SliderInput}. */
|
|
124
|
+
export interface SliderInputProps {
|
|
125
|
+
value: number;
|
|
126
|
+
min: number;
|
|
127
|
+
max: number;
|
|
128
|
+
step?: number;
|
|
129
|
+
/**
|
|
130
|
+
* What an arrow key moves when it is not `step` (a range with no step of
|
|
131
|
+
* its own drags in fine steps but keys by its field's step); Shift: ten
|
|
132
|
+
* times it.
|
|
133
|
+
*/
|
|
134
|
+
keyStep?: number;
|
|
135
|
+
/** Each move, during a drag or from a key. */
|
|
136
|
+
onChange: (value: number) => void;
|
|
137
|
+
/** Once per drag when it ends, and after each key. */
|
|
138
|
+
onCommit?: (value: number) => void;
|
|
139
|
+
disabled?: boolean;
|
|
140
|
+
/** The selection holds different values: it reads "Mixed" and shows no thumb position. */
|
|
141
|
+
mixed?: boolean;
|
|
142
|
+
/** "%": the value reads as a percentage. */
|
|
143
|
+
unit?: string;
|
|
144
|
+
thumbAlignment?: 'center' | 'edge' | 'edge-client-only';
|
|
145
|
+
look?: SliderLook;
|
|
146
|
+
'aria-label'?: string;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* A horizontal slider. A drag always ends in one `onCommit`: also when the
|
|
150
|
+
* pointer is cancelled, or the slider is disabled or removed mid-drag; only
|
|
151
|
+
* the pointer that started it ends it, and its range holds still meanwhile.
|
|
152
|
+
* Pointer movement after a cancelled drag changes nothing. The arrows move a
|
|
153
|
+
* step, PageUp and PageDown ten, Home and End go to the ends; Space does not
|
|
154
|
+
* scroll the page.
|
|
155
|
+
*/
|
|
156
|
+
export declare function SliderInput({ value, min, max, step, keyStep, onChange, onCommit, disabled, mixed, unit, thumbAlignment, look, ...aria }: SliderInputProps): ReactNode;
|
|
157
|
+
/** Props of {@link ColorInput}. */
|
|
158
|
+
export interface ColorInputProps {
|
|
159
|
+
/** Any CSS color string, or `null` for "not set". */
|
|
160
|
+
value: string | null;
|
|
161
|
+
onCommit: (value: string) => void;
|
|
162
|
+
/** Swatches offered above the text field (e.g. design-system tokens). */
|
|
163
|
+
swatches?: {
|
|
164
|
+
label: string;
|
|
165
|
+
value: string;
|
|
166
|
+
css?: string;
|
|
167
|
+
}[];
|
|
168
|
+
/** Text shown while no color is set (default "none"). */
|
|
169
|
+
placeholder?: string;
|
|
170
|
+
'aria-label'?: string;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* A color field: a swatch opening the browser's picker, a text field for any
|
|
174
|
+
* CSS color (hex, rgb, oklch, `var(--token)`), and optional token swatches.
|
|
175
|
+
*/
|
|
176
|
+
export declare function ColorInput({ value, onCommit, swatches, placeholder, ...aria }: ColorInputProps): import("react/jsx-runtime").JSX.Element;
|
package/src/fields.js
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* Form controls used by inspectors: text, number (with scrubbing), select,
|
|
4
|
+
* switch, slider, segmented control and color.
|
|
5
|
+
*/
|
|
6
|
+
import { NumberField as BaseNumberField } from '@base-ui/react/number-field';
|
|
7
|
+
import { Select as BaseSelect } from '@base-ui/react/select';
|
|
8
|
+
import { Slider as BaseSlider } from '@base-ui/react/slider';
|
|
9
|
+
import { Switch as BaseSwitch } from '@base-ui/react/switch';
|
|
10
|
+
import { ToggleGroup } from '@base-ui/react/toggle-group';
|
|
11
|
+
import { Toggle } from '@base-ui/react/toggle';
|
|
12
|
+
import { Check, ChevronDown } from 'lucide-react';
|
|
13
|
+
import { forwardRef, useEffect, useMemo, useRef, useState } from 'react';
|
|
14
|
+
import { cn } from "./cn.js";
|
|
15
|
+
import { blurredWithWindow } from "./focusables.js";
|
|
16
|
+
import { isComposingKey, preventRepeatedActivation, shortcutLetter } from "./keys.js";
|
|
17
|
+
import { POPUP_CLASS } from "./menu.js";
|
|
18
|
+
import { announceFromField, prepareFieldAnnounce } from "./field-announce.js";
|
|
19
|
+
import { holdNumberTextOnWheel } from "./number-wheel.js";
|
|
20
|
+
import { useEscapeHandoffRef } from "./escape-to-popup.js";
|
|
21
|
+
import { isMinusSign, limitsKept, numberLimitMessage, refusesMinus, settleTypedText, startsWithMinus, stepTypedText } from "./number-entry.js";
|
|
22
|
+
import { createSelectAnnouncer } from "./select-announce.js";
|
|
23
|
+
import { DRAG_ENDS, endsDrag, isContextClick, largeStepOf, sliderValueText } from "./slider-session.js";
|
|
24
|
+
import { onApplePlatform } from "./key-chord.js";
|
|
25
|
+
/**
|
|
26
|
+
* Shared look of single-line inputs. Every field's box also carries `data-ui-field`, so a page can give the fields
|
|
27
|
+
* of one part of it another look (a compact inspector, say).
|
|
28
|
+
*/
|
|
29
|
+
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';
|
|
30
|
+
/**
|
|
31
|
+
* A plain text input. As `type="number"`, a wheel turned over it while it has the focus steps the value without the
|
|
32
|
+
* number sliding sideways in the box.
|
|
33
|
+
*/
|
|
34
|
+
export const TextInput = forwardRef(function TextInput({ className, onWheel, ...rest }, ref) {
|
|
35
|
+
return (_jsx("input", { ref: ref, "data-ui-field": "", className: cn(INPUT_CLASS, '[&[data-ui-wheeling]::-webkit-inner-spin-button]:hidden', className), ...rest, onWheel: (event) => {
|
|
36
|
+
onWheel?.(event);
|
|
37
|
+
holdNumberTextOnWheel(event.currentTarget);
|
|
38
|
+
} }));
|
|
39
|
+
});
|
|
40
|
+
/**
|
|
41
|
+
* A text input that keeps a local draft and commits on Enter or blur; Escape
|
|
42
|
+
* restores the last committed value. Inspector edits use it so every
|
|
43
|
+
* keystroke does not become a document write. ⌘S / Ctrl+S commits the draft
|
|
44
|
+
* and lets the key go on to the page's save, keeping focus in the field.
|
|
45
|
+
*/
|
|
46
|
+
export function CommitInput({ value, onCommit, onDraftChange, className, ...rest }) {
|
|
47
|
+
const [draft, setDraft] = useState(value);
|
|
48
|
+
useEffect(() => setDraft(value), [value]);
|
|
49
|
+
/** Set by Enter and Escape, which already decided: the blur they cause commits nothing more. */
|
|
50
|
+
const settled = useRef(false);
|
|
51
|
+
const commit = () => {
|
|
52
|
+
if (settled.current) {
|
|
53
|
+
settled.current = false;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (draft !== value && onCommit(draft) === false) {
|
|
57
|
+
setDraft(value);
|
|
58
|
+
onDraftChange?.(value);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
return (_jsx("input", { "data-ui-field": "", className: cn(INPUT_CLASS, className), value: draft, onChange: (event) => {
|
|
62
|
+
setDraft(event.target.value);
|
|
63
|
+
onDraftChange?.(event.target.value);
|
|
64
|
+
}, onBlur: commit, onKeyDown: (event) => {
|
|
65
|
+
if (isComposingKey(event.nativeEvent))
|
|
66
|
+
return;
|
|
67
|
+
if ((event.metaKey || event.ctrlKey) && !event.altKey && shortcutLetter(event) === 's') {
|
|
68
|
+
commit();
|
|
69
|
+
}
|
|
70
|
+
else if (event.key === 'Enter') {
|
|
71
|
+
commit();
|
|
72
|
+
settled.current = true;
|
|
73
|
+
event.target.blur();
|
|
74
|
+
settled.current = false;
|
|
75
|
+
}
|
|
76
|
+
else if (event.key === 'Escape') {
|
|
77
|
+
settled.current = true;
|
|
78
|
+
setDraft(value);
|
|
79
|
+
onDraftChange?.(value);
|
|
80
|
+
event.target.blur();
|
|
81
|
+
settled.current = false;
|
|
82
|
+
}
|
|
83
|
+
}, ...rest }));
|
|
84
|
+
}
|
|
85
|
+
/** Base UI's step sizes for Alt and Shift with an arrow key (the field sets neither). */
|
|
86
|
+
const SMALL_STEP = 0.1;
|
|
87
|
+
const LARGE_STEP = 10;
|
|
88
|
+
/** The decimal separator the locale writes numbers with. */
|
|
89
|
+
function localeDecimal() {
|
|
90
|
+
return new Intl.NumberFormat().formatToParts(1.5).find((part) => part.type === 'decimal')?.value ?? '.';
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Commits what is typed in a field before ⌘S / Ctrl+S reaches the page's
|
|
94
|
+
* save, keeping the field focused with its text selected. The key is not
|
|
95
|
+
* stopped: the page saves the committed value.
|
|
96
|
+
*/
|
|
97
|
+
function commitBeforeSave(field) {
|
|
98
|
+
field.blur();
|
|
99
|
+
field.focus({ preventScroll: true });
|
|
100
|
+
field.select();
|
|
101
|
+
queueMicrotask(() => field.select());
|
|
102
|
+
}
|
|
103
|
+
/** True for ⌘S / Ctrl+S (without Alt), on any keyboard layout. */
|
|
104
|
+
function isSaveKey(event) {
|
|
105
|
+
return (event.metaKey || event.ctrlKey) && !event.altKey && shortcutLetter(event) === 's';
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Replaces a field's text as if it were typed, so the field's own input
|
|
109
|
+
* handling takes it in. False when the browser cannot.
|
|
110
|
+
*/
|
|
111
|
+
function retype(field, text) {
|
|
112
|
+
field.select();
|
|
113
|
+
return document.execCommand('insertText', false, text);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* A number field whose label can be dragged horizontally to scrub the value.
|
|
117
|
+
*
|
|
118
|
+
* Typed text past `min` or `max` is held at the bound. When `min` is zero or
|
|
119
|
+
* more the field takes no negative numbers: the minus key does nothing, and
|
|
120
|
+
* typed or pasted text that starts with a minus sign keeps its number without
|
|
121
|
+
* the sign (`-5` is 5, not the minimum). The field says both to screen
|
|
122
|
+
* readers, unless `onLimit` takes them over. ⌘S /
|
|
123
|
+
* Ctrl+S commits what is typed before the page saves.
|
|
124
|
+
*/
|
|
125
|
+
export function NumberInput({ value, onChange, onCommit, label, unit, min, max, step = 1, placeholder, className, onLimit, ...aria }) {
|
|
126
|
+
const input = useRef(null);
|
|
127
|
+
/** The text in the field was typed or pasted and not committed yet. */
|
|
128
|
+
const typed = useRef(false);
|
|
129
|
+
const decimal = useMemo(localeDecimal, []);
|
|
130
|
+
const bounds = { min, max };
|
|
131
|
+
const tell = (limits) => {
|
|
132
|
+
if (limits.length === 0)
|
|
133
|
+
return;
|
|
134
|
+
if (onLimit)
|
|
135
|
+
return onLimit(limits);
|
|
136
|
+
const name = aria['aria-label'] ?? (typeof label === 'string' ? label : undefined);
|
|
137
|
+
if (input.current)
|
|
138
|
+
announceFromField(input.current, numberLimitMessage(limits, { name, unit }));
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* The value to pass on for one the field reports. Typed text that starts with a minus sign the field refuses
|
|
142
|
+
* keeps its number without the sign (left alone, the field would hold it at the minimum). On a commit from typed
|
|
143
|
+
* text, the changes are told.
|
|
144
|
+
*/
|
|
145
|
+
const typedValue = (next, reason, event, committed) => {
|
|
146
|
+
const text = input.current?.value;
|
|
147
|
+
const stepped = reason === 'keyboard' && event instanceof KeyboardEvent && (event.key === 'ArrowUp' || event.key === 'ArrowDown');
|
|
148
|
+
const fromText = typed.current && text !== undefined && (reason === 'input-change' || reason === 'input-blur' || stepped);
|
|
149
|
+
if (committed)
|
|
150
|
+
typed.current = false;
|
|
151
|
+
if (!fromText)
|
|
152
|
+
return next;
|
|
153
|
+
const settled = settleTypedText(text, bounds, decimal);
|
|
154
|
+
if (settled === null)
|
|
155
|
+
return next;
|
|
156
|
+
const minusDropped = settled.limits.some((limit) => limit.reason === 'no-minus');
|
|
157
|
+
const kept = minusDropped && !stepped ? settled.value : next;
|
|
158
|
+
if (committed)
|
|
159
|
+
tell(limitsKept(settled.limits, kept));
|
|
160
|
+
return kept;
|
|
161
|
+
};
|
|
162
|
+
return (_jsxs(BaseNumberField.Root, { value: value, min: min, max: max, step: step, onValueChange: (next, details) => onChange?.(typedValue(next, details.reason, details.event, false)), onValueCommitted: (next, details) => onCommit(typedValue(next, details.reason, details.event, true)), "data-ui-field": "", className: cn('flex h-7 min-w-0 items-center rounded-control border border-line bg-surface-2 focus-within:border-accent', className), children: [label !== undefined ? (_jsxs(BaseNumberField.ScrubArea, { className: "flex h-full w-6 shrink-0 cursor-ew-resize items-center justify-center text-[11px] text-fg-subtle", children: [label, _jsx(BaseNumberField.ScrubAreaCursor, {})] })) : null, _jsx(BaseNumberField.Input, { ref: input, placeholder: placeholder, "aria-label": aria['aria-label'], onFocus: (event) => prepareFieldAnnounce(event.currentTarget), onInput: () => {
|
|
163
|
+
typed.current = true;
|
|
164
|
+
}, onPaste: () => {
|
|
165
|
+
typed.current = true;
|
|
166
|
+
}, onBlur: (event) => {
|
|
167
|
+
// Leaving the window keeps the field as it is: the text goes on being typed on return.
|
|
168
|
+
if (blurredWithWindow(event.currentTarget))
|
|
169
|
+
return;
|
|
170
|
+
// The field commits (or turns the text down) during this blur; what is typed after it is new.
|
|
171
|
+
queueMicrotask(() => {
|
|
172
|
+
typed.current = false;
|
|
173
|
+
});
|
|
174
|
+
}, onKeyDown: (event) => {
|
|
175
|
+
if (isComposingKey(event.nativeEvent))
|
|
176
|
+
return;
|
|
177
|
+
const field = event.currentTarget;
|
|
178
|
+
if (isSaveKey(event)) {
|
|
179
|
+
commitBeforeSave(field);
|
|
180
|
+
}
|
|
181
|
+
else if (event.key === 'Enter') {
|
|
182
|
+
field.blur();
|
|
183
|
+
}
|
|
184
|
+
else if (refusesMinus(bounds) && isMinusSign(event.key) && !event.ctrlKey && !event.metaKey) {
|
|
185
|
+
// The field turns the key down too, except with Alt (which types dashes on a Mac).
|
|
186
|
+
event.preventDefault();
|
|
187
|
+
if (!event.repeat)
|
|
188
|
+
tell([{ reason: 'no-minus', limit: min ?? 0 }]);
|
|
189
|
+
}
|
|
190
|
+
else if ((event.key === 'ArrowUp' || event.key === 'ArrowDown') && !event.ctrlKey && !event.metaKey && typed.current && refusesMinus(bounds) && startsWithMinus(field.value)) {
|
|
191
|
+
// Left to the field, a step from "-5" would start at -5 and stop at the minimum.
|
|
192
|
+
const amount = event.altKey ? SMALL_STEP : event.shiftKey ? LARGE_STEP : step;
|
|
193
|
+
const stepped = stepTypedText(field.value, event.key === 'ArrowUp' ? amount : -amount, bounds, decimal);
|
|
194
|
+
if (stepped === null)
|
|
195
|
+
return;
|
|
196
|
+
event.preventDefault();
|
|
197
|
+
const next = stepped.value;
|
|
198
|
+
if (!retype(field, new Intl.NumberFormat().format(next)))
|
|
199
|
+
onChange?.(next);
|
|
200
|
+
typed.current = false;
|
|
201
|
+
onCommit(next);
|
|
202
|
+
tell(stepped.limits);
|
|
203
|
+
}
|
|
204
|
+
}, className: cn('h-full w-full min-w-0 bg-transparent text-[13px] text-fg outline-none', label === undefined && 'pl-2') }), unit ? _jsx("span", { className: "pr-2 text-[11px] text-fg-subtle", children: unit }) : null] }));
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Focus handlers for a select's trigger that say its new value (`label`)
|
|
208
|
+
* politely while it has the focus (see `select-announce.ts`).
|
|
209
|
+
*/
|
|
210
|
+
export function useSelectAnnouncement(label) {
|
|
211
|
+
const announcer = useRef(createSelectAnnouncer()).current;
|
|
212
|
+
useEffect(() => announcer.change(label), [announcer, label]);
|
|
213
|
+
useEffect(() => () => announcer.dispose(), [announcer]);
|
|
214
|
+
return {
|
|
215
|
+
onFocus: (event) => announcer.focus(event.currentTarget, label),
|
|
216
|
+
onBlur: (event) => announcer.blur(event.currentTarget),
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
/** A dropdown select. Its new value is said while its trigger has the focus. */
|
|
220
|
+
export function SelectInput({ value, options, onChange, placeholder = '—', className, ...aria }) {
|
|
221
|
+
const current = options.find((option) => option.value === value);
|
|
222
|
+
const announce = useSelectAnnouncement(current ? (typeof current.label === 'string' ? current.label : (current.title ?? current.value)) : '');
|
|
223
|
+
const handoffRef = useEscapeHandoffRef();
|
|
224
|
+
return (_jsxs(BaseSelect.Root, { value: value, onValueChange: (next) => next !== null && onChange(next), children: [_jsxs(BaseSelect.Trigger, { ...announce, "aria-label": aria['aria-label'], "data-ui-field": "", className: cn(INPUT_CLASS, 'flex items-center justify-between gap-1 text-left', className), children: [_jsx(BaseSelect.Value, { children: (current) => options.find((option) => option.value === current)?.label ?? _jsx("span", { className: "text-fg-subtle", children: placeholder }) }), _jsx(BaseSelect.Icon, { children: _jsx(ChevronDown, { className: "size-3.5 text-fg-subtle" }) })] }), _jsx(BaseSelect.Portal, { children: _jsx(BaseSelect.Positioner, { sideOffset: 4, className: "z-[1000]", alignItemWithTrigger: false, children: _jsx(BaseSelect.Popup, { ref: handoffRef, className: cn(POPUP_CLASS, 'max-h-80 min-w-[min(8rem,var(--available-width))] max-w-[var(--available-width)] overflow-auto'), children: _jsx(BaseSelect.List, { children: options.map((option) => (_jsxs(BaseSelect.Item, { value: option.value, disabled: option.disabled, className: "flex min-h-7 cursor-default select-none items-center gap-2 rounded-md px-2 py-1 outline-none data-[highlighted]:bg-hover data-[disabled]:text-fg-subtle", children: [_jsx("span", { className: "flex size-4 shrink-0 items-center justify-center", children: _jsx(BaseSelect.ItemIndicator, { children: _jsx(Check, { className: "size-3.5" }) }) }), _jsx(BaseSelect.ItemText, { className: "min-w-0 break-words", children: option.label })] }, option.value))) }) }) }) })] }));
|
|
225
|
+
}
|
|
226
|
+
/** A segmented control (one of a few options, all visible). */
|
|
227
|
+
export function Segmented({ value, options, onChange, onRepick, className, ...aria }) {
|
|
228
|
+
return (_jsx(ToggleGroup, { value: value === null ? [] : [value], onValueChange: (next) => {
|
|
229
|
+
const picked = next[0];
|
|
230
|
+
// Pressing the pressed option again leaves the group empty.
|
|
231
|
+
if (picked !== undefined)
|
|
232
|
+
onChange(picked);
|
|
233
|
+
else if (value !== null)
|
|
234
|
+
onRepick?.(value);
|
|
235
|
+
}, "aria-label": aria['aria-label'], "data-ui-field": "", className: cn('flex h-7 items-center gap-0.5 rounded-control bg-surface-2 p-0.5', className), children: options.map((option) => (_jsx(Toggle, { value: option.value, "aria-label": option.title, title: option.title, disabled: option.disabled, className: "flex h-full flex-1 items-center justify-center whitespace-nowrap rounded-[4px] px-1.5 text-fg-muted outline-none hover:text-fg data-[pressed]:bg-surface data-[pressed]:text-fg data-[pressed]:shadow-sm focus-visible:outline-2 focus-visible:outline-accent data-[disabled]:pointer-events-none data-[disabled]:opacity-40", children: option.label }, option.value))) }));
|
|
236
|
+
}
|
|
237
|
+
/** An on/off switch. A held Enter flips it once. */
|
|
238
|
+
export function SwitchInput({ checked, onChange, disabled, ...aria }) {
|
|
239
|
+
return (_jsx(BaseSwitch.Root, { checked: checked, disabled: disabled, onCheckedChange: (next) => onChange(next), onKeyDown: preventRepeatedActivation, "aria-label": aria['aria-label'], className: "relative inline-flex h-4 w-7 shrink-0 items-center rounded-full bg-line-strong p-0.5 transition-colors data-[checked]:bg-accent disabled:opacity-40", children: _jsx(BaseSwitch.Thumb, { className: "size-3 rounded-full bg-white shadow transition-transform data-[checked]:translate-x-3" }) }));
|
|
240
|
+
}
|
|
241
|
+
const SLIDER_LOOK = {
|
|
242
|
+
root: 'flex h-7 w-full items-center data-[disabled]:opacity-40',
|
|
243
|
+
control: 'flex h-4 w-full items-center',
|
|
244
|
+
track: 'h-1 w-full rounded-full bg-line-strong',
|
|
245
|
+
indicator: 'rounded-full bg-accent',
|
|
246
|
+
thumb: 'size-3.5 rounded-full border border-line-strong bg-white shadow outline-none focus-visible:outline-2 focus-visible:outline-accent',
|
|
247
|
+
};
|
|
248
|
+
/**
|
|
249
|
+
* A horizontal slider. A drag always ends in one `onCommit`: also when the
|
|
250
|
+
* pointer is cancelled, or the slider is disabled or removed mid-drag; only
|
|
251
|
+
* the pointer that started it ends it, and its range holds still meanwhile.
|
|
252
|
+
* Pointer movement after a cancelled drag changes nothing. The arrows move a
|
|
253
|
+
* step, PageUp and PageDown ten, Home and End go to the ends; Space does not
|
|
254
|
+
* scroll the page.
|
|
255
|
+
*/
|
|
256
|
+
export function SliderInput({ value, min, max, step = 1, keyStep, onChange, onCommit, disabled, mixed, unit, thumbAlignment, look, ...aria }) {
|
|
257
|
+
const drag = useRef(null);
|
|
258
|
+
const root = useRef(null);
|
|
259
|
+
/** The kind of pointer of the last press that moved the slider. */
|
|
260
|
+
const pressedWith = useRef(null);
|
|
261
|
+
// A mixed slider has no one value to report.
|
|
262
|
+
useEffect(() => {
|
|
263
|
+
if (mixed)
|
|
264
|
+
root.current?.querySelectorAll('[aria-valuenow]').forEach((node) => node.removeAttribute('aria-valuenow'));
|
|
265
|
+
});
|
|
266
|
+
// A drag across the slider selects no text around it.
|
|
267
|
+
useEffect(() => {
|
|
268
|
+
const el = root.current;
|
|
269
|
+
if (!el)
|
|
270
|
+
return;
|
|
271
|
+
const prevent = (event) => event.preventDefault();
|
|
272
|
+
el.addEventListener('selectstart', prevent);
|
|
273
|
+
return () => el.removeEventListener('selectstart', prevent);
|
|
274
|
+
}, []);
|
|
275
|
+
const latest = useRef(onCommit);
|
|
276
|
+
latest.current = onCommit;
|
|
277
|
+
const settle = (final) => {
|
|
278
|
+
const current = drag.current;
|
|
279
|
+
if (!current)
|
|
280
|
+
return;
|
|
281
|
+
drag.current = null;
|
|
282
|
+
current.stop();
|
|
283
|
+
if (final !== undefined || current.moved)
|
|
284
|
+
latest.current?.(final ?? current.last);
|
|
285
|
+
};
|
|
286
|
+
const begin = (event) => {
|
|
287
|
+
if (drag.current || disabled || event.button !== 0)
|
|
288
|
+
return;
|
|
289
|
+
const pointerId = Number.isFinite(event.pointerId) ? event.pointerId : null;
|
|
290
|
+
const end = (ended) => {
|
|
291
|
+
// Listening in the capture phase hears every element's blur too: only the window's ends a drag.
|
|
292
|
+
if (ended.type === 'blur' && ended.target !== window)
|
|
293
|
+
return;
|
|
294
|
+
if (!endsDrag({ type: ended.type, ...(ended instanceof PointerEvent ? { pointerId: ended.pointerId } : {}) }, pointerId)) {
|
|
295
|
+
// Another pointer let go: the slider ends its own tracking on any release, but this drag goes on.
|
|
296
|
+
if (drag.current)
|
|
297
|
+
drag.current.foreign = true;
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
// The slider commits a release itself; settle whatever it left open.
|
|
301
|
+
setTimeout(() => settle(), 0);
|
|
302
|
+
};
|
|
303
|
+
DRAG_ENDS.forEach((type) => window.addEventListener(type, end, true));
|
|
304
|
+
drag.current = { pointerId, last: value, moved: false, range: { min, max, step }, stop: () => DRAG_ENDS.forEach((type) => window.removeEventListener(type, end, true)) };
|
|
305
|
+
};
|
|
306
|
+
useEffect(() => {
|
|
307
|
+
if (disabled)
|
|
308
|
+
settle();
|
|
309
|
+
}, [disabled]);
|
|
310
|
+
useEffect(() => () => settle(), []);
|
|
311
|
+
// After a cancelled drag the slider keeps tracking the pointer until the next
|
|
312
|
+
// release; whatever it reports then belongs to no drag of this slider.
|
|
313
|
+
const strayPointer = (reason) => !drag.current && (reason === 'drag' || reason === 'track-press');
|
|
314
|
+
const single = (next) => (Array.isArray(next) ? (next[0] ?? min) : next);
|
|
315
|
+
const range = drag.current?.range ?? { min, max, step };
|
|
316
|
+
const parts = { ...SLIDER_LOOK, ...look };
|
|
317
|
+
const valueText = (_formatted, current) => sliderValueText(current, { ...(mixed ? { mixed } : {}), ...(unit ? { unit } : {}) }) ?? _formatted;
|
|
318
|
+
return (_jsx(BaseSlider.Root, { value: value, disabled: disabled, min: range.min, max: range.max, step: range.step, largeStep: largeStepOf(range.step), thumbAlignment: thumbAlignment, ref: root, "data-mixed": mixed ? '' : undefined, onPointerDownCapture: (event) => {
|
|
319
|
+
if (isContextClick(event, onApplePlatform(), navigator.maxTouchPoints)) {
|
|
320
|
+
// Control-click on a Mac opens the context menu; the slider does not hear it.
|
|
321
|
+
pressedWith.current = null;
|
|
322
|
+
event.stopPropagation();
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
pressedWith.current = event.pointerType;
|
|
326
|
+
begin(event);
|
|
327
|
+
}, onDragStart: (event) => event.preventDefault(), onContextMenu: (event) => {
|
|
328
|
+
// A long press with a finger or a pen moves the slider, with no system menu.
|
|
329
|
+
if (pressedWith.current === 'touch' || pressedWith.current === 'pen')
|
|
330
|
+
event.preventDefault();
|
|
331
|
+
}, onKeyDownCapture: (event) => {
|
|
332
|
+
const direction = { ArrowUp: 1, ArrowRight: 1, ArrowDown: -1, ArrowLeft: -1 }[event.key];
|
|
333
|
+
if (keyStep === undefined || direction === undefined || disabled)
|
|
334
|
+
return;
|
|
335
|
+
event.preventDefault();
|
|
336
|
+
event.stopPropagation();
|
|
337
|
+
const from = mixed ? min : value;
|
|
338
|
+
const next = Math.min(max, Math.max(min, Math.round((from + direction * keyStep * (event.shiftKey ? 10 : 1)) * 1e6) / 1e6));
|
|
339
|
+
if (next === value)
|
|
340
|
+
return;
|
|
341
|
+
onChange(next);
|
|
342
|
+
onCommit?.(next);
|
|
343
|
+
}, onKeyDown: (event) => {
|
|
344
|
+
if (event.key === ' ')
|
|
345
|
+
event.preventDefault();
|
|
346
|
+
}, onValueChange: (next, details) => {
|
|
347
|
+
if (strayPointer(details.reason))
|
|
348
|
+
return;
|
|
349
|
+
const v = single(next);
|
|
350
|
+
if (drag.current)
|
|
351
|
+
drag.current = { ...drag.current, last: v, moved: true };
|
|
352
|
+
onChange(v);
|
|
353
|
+
}, onValueCommitted: (next, details) => {
|
|
354
|
+
if (strayPointer(details.reason))
|
|
355
|
+
return;
|
|
356
|
+
const v = single(next);
|
|
357
|
+
const current = drag.current;
|
|
358
|
+
if (current?.foreign) {
|
|
359
|
+
// Released by another pointer: the drag ends when its own pointer lets go.
|
|
360
|
+
current.foreign = false;
|
|
361
|
+
current.last = v;
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (current)
|
|
365
|
+
settle(v);
|
|
366
|
+
else
|
|
367
|
+
onCommit?.(v);
|
|
368
|
+
}, className: parts.root, children: _jsx(BaseSlider.Control, { className: parts.control, children: _jsxs(BaseSlider.Track, { className: parts.track, children: [mixed ? null : _jsx(BaseSlider.Indicator, { className: parts.indicator }), _jsx(BaseSlider.Thumb, { "aria-label": aria['aria-label'], getAriaValueText: valueText, "data-mixed": mixed ? '' : undefined, className: cn(parts.thumb, mixed && 'opacity-0 focus-visible:opacity-100') })] }) }) }));
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* A color field: a swatch opening the browser's picker, a text field for any
|
|
372
|
+
* CSS color (hex, rgb, oklch, `var(--token)`), and optional token swatches.
|
|
373
|
+
*/
|
|
374
|
+
export function ColorInput({ value, onCommit, swatches, placeholder = 'none', ...aria }) {
|
|
375
|
+
const hex = value && /^#[0-9a-f]{6}$/i.test(value) ? value : '#000000';
|
|
376
|
+
return (_jsxs("div", { className: "flex min-w-0 flex-col gap-1.5", children: [_jsxs("div", { className: "flex min-w-0 items-center gap-1.5", children: [_jsx("label", { className: "relative size-7 shrink-0 overflow-hidden rounded-control border border-line", style: { background: value ?? 'transparent' }, title: "Pick a color", children: _jsx("input", { type: "color", value: hex, "aria-label": aria['aria-label'] ? `${aria['aria-label']} picker` : 'Color picker', onChange: (event) => onCommit(event.target.value), className: "absolute inset-0 cursor-pointer opacity-0" }) }), _jsx(CommitInput, { value: value ?? '', onCommit: onCommit, placeholder: placeholder, "aria-label": aria['aria-label'] })] }), swatches && swatches.length ? (_jsx("div", { className: "flex flex-wrap gap-1", children: swatches.map((swatch) => (_jsx("button", { type: "button", title: swatch.label, "aria-label": swatch.label, onClick: () => onCommit(swatch.css ?? swatch.value), className: "size-5 rounded-[4px] border border-line", style: { background: swatch.value } }, swatch.label))) })) : null] }));
|
|
377
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which descendants a keyboard user can Tab to, and whether a box scrolls.
|
|
3
|
+
* {@link ScrollBody} uses both to give a scrolling region with no tab stop
|
|
4
|
+
* of its own one, so it can be scrolled from the keyboard.
|
|
5
|
+
*/
|
|
6
|
+
/** Elements that may be tab stops. */
|
|
7
|
+
export declare const TABBABLE_CANDIDATE_SELECTOR: string;
|
|
8
|
+
/** True when `element` is a tab stop: enabled, shown, not hidden from assistive technology. */
|
|
9
|
+
export declare function isTabbable(element: HTMLElement): boolean;
|
|
10
|
+
/** The tab stops inside `root`, in document order. */
|
|
11
|
+
export declare function tabbableElements(root: ParentNode): HTMLElement[];
|
|
12
|
+
/** The sizes and overflow styles {@link scrollsOverflow} reads. */
|
|
13
|
+
export interface ScrollMetrics {
|
|
14
|
+
scrollWidth: number;
|
|
15
|
+
clientWidth: number;
|
|
16
|
+
scrollHeight: number;
|
|
17
|
+
clientHeight: number;
|
|
18
|
+
overflowX: string;
|
|
19
|
+
overflowY: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* True when the content overflows an axis that scrolls (`auto` or
|
|
23
|
+
* `scroll`; `visible`, `hidden` and `clip` do not). A pixel of rounding is
|
|
24
|
+
* ignored.
|
|
25
|
+
*/
|
|
26
|
+
export declare function scrollsOverflow(metrics: ScrollMetrics): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Where a scrolling box stands: `none` when its content fits, `more` while
|
|
29
|
+
* there is more below, `end` once scrolled to the bottom (a pixel of
|
|
30
|
+
* rounding allowed).
|
|
31
|
+
*/
|
|
32
|
+
export declare function scrollStanding(metrics: ScrollMetrics & {
|
|
33
|
+
scrollTop: number;
|
|
34
|
+
}): 'none' | 'more' | 'end';
|
|
35
|
+
/** The parts of an element {@link blurredWithWindow} reads. */
|
|
36
|
+
export interface BlurredElement {
|
|
37
|
+
ownerDocument: {
|
|
38
|
+
hasFocus(): boolean;
|
|
39
|
+
activeElement: unknown;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** Whether a blur came from the window losing focus: the element is still the page's focus and gets it back on return. */
|
|
43
|
+
export declare function blurredWithWindow(element: BlurredElement): boolean;
|
|
44
|
+
/** The top and bottom edges {@link fullyInView} compares. */
|
|
45
|
+
export interface VerticalEdges {
|
|
46
|
+
top: number;
|
|
47
|
+
bottom: number;
|
|
48
|
+
}
|
|
49
|
+
/** Whether `inner` lies wholly inside `outer` from top to bottom, with half a pixel of rounding allowed. */
|
|
50
|
+
export declare function fullyInView(inner: VerticalEdges, outer: VerticalEdges): boolean;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which descendants a keyboard user can Tab to, and whether a box scrolls.
|
|
3
|
+
* {@link ScrollBody} uses both to give a scrolling region with no tab stop
|
|
4
|
+
* of its own one, so it can be scrolled from the keyboard.
|
|
5
|
+
*/
|
|
6
|
+
/** Elements that may be tab stops. */
|
|
7
|
+
export const TABBABLE_CANDIDATE_SELECTOR = [
|
|
8
|
+
'a[href]',
|
|
9
|
+
'button',
|
|
10
|
+
'iframe',
|
|
11
|
+
'input',
|
|
12
|
+
'select',
|
|
13
|
+
'textarea',
|
|
14
|
+
'summary',
|
|
15
|
+
'audio[controls]',
|
|
16
|
+
'video[controls]',
|
|
17
|
+
"[contenteditable]:not([contenteditable='false'])",
|
|
18
|
+
'[tabindex]',
|
|
19
|
+
].join(', ');
|
|
20
|
+
/** Elements that are tab stops without a `tabindex` attribute even when `tabIndex` reads -1. */
|
|
21
|
+
const IMPLICIT_TAB_STOP_SELECTOR = "audio[controls], video[controls], [contenteditable]:not([contenteditable='false'])";
|
|
22
|
+
/** True when `element` is a tab stop: enabled, shown, not hidden from assistive technology. */
|
|
23
|
+
export function isTabbable(element) {
|
|
24
|
+
const stop = element.tabIndex >= 0 || (!element.hasAttribute('tabindex') && element.matches(IMPLICIT_TAB_STOP_SELECTOR));
|
|
25
|
+
if (!stop || element.getAttribute('aria-hidden') === 'true' || element.matches(':disabled'))
|
|
26
|
+
return false;
|
|
27
|
+
return typeof element.checkVisibility !== 'function' || element.checkVisibility({ visibilityProperty: true });
|
|
28
|
+
}
|
|
29
|
+
/** The tab stops inside `root`, in document order. */
|
|
30
|
+
export function tabbableElements(root) {
|
|
31
|
+
return Array.from(root.querySelectorAll(TABBABLE_CANDIDATE_SELECTOR)).filter(isTabbable);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* True when the content overflows an axis that scrolls (`auto` or
|
|
35
|
+
* `scroll`; `visible`, `hidden` and `clip` do not). A pixel of rounding is
|
|
36
|
+
* ignored.
|
|
37
|
+
*/
|
|
38
|
+
export function scrollsOverflow(metrics) {
|
|
39
|
+
const scrolls = (overflow) => !/visible|hidden|clip/.test(overflow);
|
|
40
|
+
return ((metrics.scrollWidth > metrics.clientWidth + 1 && scrolls(metrics.overflowX)) ||
|
|
41
|
+
(metrics.scrollHeight > metrics.clientHeight + 1 && scrolls(metrics.overflowY)));
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Where a scrolling box stands: `none` when its content fits, `more` while
|
|
45
|
+
* there is more below, `end` once scrolled to the bottom (a pixel of
|
|
46
|
+
* rounding allowed).
|
|
47
|
+
*/
|
|
48
|
+
export function scrollStanding(metrics) {
|
|
49
|
+
if (!scrollsOverflow(metrics))
|
|
50
|
+
return 'none';
|
|
51
|
+
return metrics.scrollTop + metrics.clientHeight >= metrics.scrollHeight - 1 ? 'end' : 'more';
|
|
52
|
+
}
|
|
53
|
+
/** Whether a blur came from the window losing focus: the element is still the page's focus and gets it back on return. */
|
|
54
|
+
export function blurredWithWindow(element) {
|
|
55
|
+
const doc = element.ownerDocument;
|
|
56
|
+
return !doc.hasFocus() && doc.activeElement === element;
|
|
57
|
+
}
|
|
58
|
+
/** Whether `inner` lies wholly inside `outer` from top to bottom, with half a pixel of rounding allowed. */
|
|
59
|
+
export function fullyInView(inner, outer) {
|
|
60
|
+
return inner.top >= outer.top - 0.5 && inner.bottom <= outer.bottom + 0.5;
|
|
61
|
+
}
|