@finstack-com-public/design 0.10.2 → 0.11.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/CHANGELOG.md +89 -0
- package/dist/css/checkbox.css +3 -1
- package/dist/css/combobox.css +58 -0
- package/dist/css/combobox.md +75 -0
- package/dist/css/data-type.md +1 -1
- package/dist/css/diag.md +1 -1
- package/dist/css/drag.css +7 -0
- package/dist/css/drag.md +2 -0
- package/dist/css/grid.css +4 -0
- package/dist/css/input.md +6 -1
- package/dist/css/pill.css +7 -0
- package/dist/css/select.css +103 -0
- package/dist/css/select.md +69 -0
- package/dist/css/slider.css +169 -0
- package/dist/css/slider.md +87 -0
- package/dist/css/stacked-bar.css +16 -0
- package/dist/css/switch.css +137 -0
- package/dist/css/switch.md +28 -0
- package/dist/css/tokens.css +3 -3
- package/dist/css-text/checkbox.js +1 -1
- package/dist/css-text/combobox.d.ts +2 -0
- package/dist/css-text/combobox.js +1 -0
- package/dist/css-text/drag.js +1 -1
- package/dist/css-text/grid.js +1 -1
- package/dist/css-text/kit.js +511 -4
- package/dist/css-text/pill.js +1 -1
- package/dist/css-text/select.d.ts +2 -0
- package/dist/css-text/select.js +1 -0
- package/dist/css-text/slider.d.ts +2 -0
- package/dist/css-text/slider.js +1 -0
- package/dist/css-text/stacked-bar.js +1 -1
- package/dist/css-text/switch.d.ts +2 -0
- package/dist/css-text/switch.js +1 -0
- package/dist/css-text/tokens.js +1 -1
- package/dist/dom/combobox.d.ts +47 -0
- package/dist/dom/combobox.js +178 -0
- package/dist/dom/index.d.ts +3 -0
- package/dist/dom/index.js +3 -0
- package/dist/dom/listbox.d.ts +11 -0
- package/dist/dom/listbox.js +9 -3
- package/dist/dom/select.d.ts +39 -0
- package/dist/dom/select.js +221 -0
- package/dist/dom/slider.d.ts +51 -0
- package/dist/dom/slider.js +256 -0
- package/dist/grid.json +1 -1
- package/dist/kit.css +511 -4
- package/dist/react/actions.js +12 -6
- package/dist/react/inputs.d.ts +181 -0
- package/dist/react/inputs.js +291 -15
- package/dist/react/navigation.js +33 -15
- package/dist/tokens/contrast-pairs.js +24 -0
- package/dist/tokens/grid.js +1 -1
- package/dist/tokens.json +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// The custom select (F2 draft §2, T132): a select-only combobox on createListbox. Focus stays on
|
|
2
|
+
// the trigger (`role="combobox"`); the list is the completion list's `.fs-menu--listbox`. This
|
|
3
|
+
// helper adds the closed-state keys, Space, Alt+Up, PageUp/PageDown, typeahead and the value.
|
|
4
|
+
import { createListbox } from "./listbox.js";
|
|
5
|
+
const OPTIONS = '[role="option"]';
|
|
6
|
+
const VALUE = 'fs-select__value';
|
|
7
|
+
const PLACEHOLDER = 'fs-select__placeholder';
|
|
8
|
+
const PAGE = 10;
|
|
9
|
+
const isDisabled = (el) => el.getAttribute('aria-disabled') === 'true';
|
|
10
|
+
/** The text an option is known by: its `__label`, else all of it. */
|
|
11
|
+
const textOf = (option) => (option.querySelector('.fs-menu__label') ?? option).textContent?.replace(/\s+/g, ' ').trim() ?? '';
|
|
12
|
+
export function createSelect(root, options) {
|
|
13
|
+
let opts = { ...options };
|
|
14
|
+
const { trigger, list } = opts;
|
|
15
|
+
const all = () => [...list.querySelectorAll(OPTIONS)];
|
|
16
|
+
const enabled = () => all().filter((o) => !isDisabled(o));
|
|
17
|
+
const byId = (id) => (id === null ? null : (all().find((o) => o.id === id) ?? null));
|
|
18
|
+
const marked = all().find((o) => o.classList.contains('fs-is-value'));
|
|
19
|
+
let value = opts.value !== undefined ? opts.value : (marked?.id ?? null);
|
|
20
|
+
const shown = trigger.querySelector(`.${VALUE}, .${PLACEHOLDER}`);
|
|
21
|
+
const givenPlaceholder = shown?.classList.contains(PLACEHOLDER) ? (shown.textContent ?? '') : '';
|
|
22
|
+
// Disabled and read-only triggers stay focusable but never open.
|
|
23
|
+
const blocked = () => trigger.getAttribute('aria-disabled') === 'true' || trigger.getAttribute('aria-readonly') === 'true';
|
|
24
|
+
let typed = '';
|
|
25
|
+
let typedAt = -Infinity;
|
|
26
|
+
// The select's keys listen on the trigger before createListbox's (registered first): it takes
|
|
27
|
+
// Tab, Space and the letters itself, and lets the rest through to the listbox while open.
|
|
28
|
+
trigger.addEventListener('keydown', onKeydown, true);
|
|
29
|
+
const lb = createListbox(list, { input: trigger, wrap: false, placement: opts.placement });
|
|
30
|
+
list.addEventListener('fs-select', onAccepted);
|
|
31
|
+
trigger.addEventListener('click', onClick);
|
|
32
|
+
render();
|
|
33
|
+
function render() {
|
|
34
|
+
if (opts.writeValue === false)
|
|
35
|
+
return;
|
|
36
|
+
const option = byId(value);
|
|
37
|
+
for (const o of all())
|
|
38
|
+
o.classList.toggle('fs-is-value', o === option);
|
|
39
|
+
let out = trigger.querySelector(`.${VALUE}, .${PLACEHOLDER}`);
|
|
40
|
+
if (!out) {
|
|
41
|
+
out = trigger.ownerDocument.createElement('span');
|
|
42
|
+
trigger.append(out);
|
|
43
|
+
}
|
|
44
|
+
out.classList.toggle(VALUE, !!option);
|
|
45
|
+
out.classList.toggle(PLACEHOLDER, !option);
|
|
46
|
+
out.textContent = option ? textOf(option) : (opts.placeholder ?? givenPlaceholder);
|
|
47
|
+
const input = root.querySelector(':scope > input[type="hidden"]');
|
|
48
|
+
if (input)
|
|
49
|
+
input.value = option ? (option.dataset.value ?? option.id) : '';
|
|
50
|
+
}
|
|
51
|
+
/** Opens with `target` current: the value (the list's own pick, F2's note), or an option. */
|
|
52
|
+
function openOn(target) {
|
|
53
|
+
if (lb.isOpen) {
|
|
54
|
+
if (target instanceof HTMLElement)
|
|
55
|
+
lb.setActive(target);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
// Mark the value so createListbox opens on it; clear any other mark.
|
|
59
|
+
const option = byId(value);
|
|
60
|
+
for (const o of all()) {
|
|
61
|
+
if (o === option && !isDisabled(o))
|
|
62
|
+
o.setAttribute('aria-selected', 'true');
|
|
63
|
+
else
|
|
64
|
+
o.removeAttribute('aria-selected');
|
|
65
|
+
}
|
|
66
|
+
list.style.minWidth = `${trigger.getBoundingClientRect().width}px`;
|
|
67
|
+
lb.open();
|
|
68
|
+
const items = enabled();
|
|
69
|
+
if (target === 'first')
|
|
70
|
+
lb.setActive(items[0] ?? null);
|
|
71
|
+
else if (target === 'last')
|
|
72
|
+
lb.setActive(items[items.length - 1] ?? null);
|
|
73
|
+
else if (target instanceof HTMLElement)
|
|
74
|
+
lb.setActive(target);
|
|
75
|
+
}
|
|
76
|
+
function commit(option) {
|
|
77
|
+
if (isDisabled(option))
|
|
78
|
+
return;
|
|
79
|
+
const changed = option.id !== value;
|
|
80
|
+
value = option.id;
|
|
81
|
+
render();
|
|
82
|
+
if (changed)
|
|
83
|
+
root.dispatchEvent(new CustomEvent('fs-change', { bubbles: true, detail: { value } }));
|
|
84
|
+
}
|
|
85
|
+
function acceptActive() {
|
|
86
|
+
const option = lb.active;
|
|
87
|
+
if (option)
|
|
88
|
+
commit(option);
|
|
89
|
+
lb.close();
|
|
90
|
+
}
|
|
91
|
+
// fs-select is internal: the select reports a change with fs-change.
|
|
92
|
+
function onAccepted(e) {
|
|
93
|
+
if (e.target !== list)
|
|
94
|
+
return;
|
|
95
|
+
e.stopPropagation();
|
|
96
|
+
commit(e.detail.option);
|
|
97
|
+
}
|
|
98
|
+
function onClick() {
|
|
99
|
+
if (lb.isOpen)
|
|
100
|
+
lb.close();
|
|
101
|
+
else if (!blocked())
|
|
102
|
+
openOn('value');
|
|
103
|
+
}
|
|
104
|
+
/** The option typing `key` moves to, from `from` (the current option or the value). */
|
|
105
|
+
function typeahead(key, from) {
|
|
106
|
+
const now = Date.now();
|
|
107
|
+
typed = now - typedAt > (opts.typeaheadTimeout ?? 500) ? key : typed + key;
|
|
108
|
+
typedAt = now;
|
|
109
|
+
const items = enabled();
|
|
110
|
+
const at = from ? items.indexOf(from) : -1;
|
|
111
|
+
const query = typed.toLocaleLowerCase();
|
|
112
|
+
// One letter typed again cycles through the options starting with it; a longer string stays
|
|
113
|
+
// on the current option while it still matches.
|
|
114
|
+
const cycling = [...query].every((c) => c === query[0]);
|
|
115
|
+
const needle = cycling ? query[0] : query;
|
|
116
|
+
const start = cycling ? at + 1 : Math.max(at, 0);
|
|
117
|
+
for (let n = 0; n < items.length; n++) {
|
|
118
|
+
const option = items[(start + n) % items.length];
|
|
119
|
+
if (textOf(option).toLocaleLowerCase().startsWith(needle))
|
|
120
|
+
return option;
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
function onKeydown(e) {
|
|
125
|
+
if (e.isComposing || e.ctrlKey || e.metaKey)
|
|
126
|
+
return;
|
|
127
|
+
const consume = () => {
|
|
128
|
+
e.preventDefault();
|
|
129
|
+
e.stopImmediatePropagation();
|
|
130
|
+
};
|
|
131
|
+
const printable = e.key.length === 1 && !e.altKey;
|
|
132
|
+
const typing = printable && (e.key !== ' ' || (typed !== '' && Date.now() - typedAt <= (opts.typeaheadTimeout ?? 500)));
|
|
133
|
+
if (!lb.isOpen) {
|
|
134
|
+
if (blocked())
|
|
135
|
+
return;
|
|
136
|
+
if (typing) {
|
|
137
|
+
consume();
|
|
138
|
+
return openOn(typeahead(e.key, byId(value)) ?? 'value');
|
|
139
|
+
}
|
|
140
|
+
switch (e.key) {
|
|
141
|
+
case 'ArrowDown':
|
|
142
|
+
case 'Enter':
|
|
143
|
+
case ' ':
|
|
144
|
+
consume();
|
|
145
|
+
return openOn('value');
|
|
146
|
+
case 'ArrowUp':
|
|
147
|
+
if (e.altKey)
|
|
148
|
+
return;
|
|
149
|
+
consume();
|
|
150
|
+
return openOn('first');
|
|
151
|
+
case 'Home':
|
|
152
|
+
consume();
|
|
153
|
+
return openOn('first');
|
|
154
|
+
case 'End':
|
|
155
|
+
consume();
|
|
156
|
+
return openOn('last');
|
|
157
|
+
}
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (typing) {
|
|
161
|
+
consume();
|
|
162
|
+
const option = typeahead(e.key, lb.active);
|
|
163
|
+
if (option)
|
|
164
|
+
lb.setActive(option);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
switch (e.key) {
|
|
168
|
+
case ' ':
|
|
169
|
+
consume();
|
|
170
|
+
return acceptActive();
|
|
171
|
+
case 'ArrowUp':
|
|
172
|
+
if (!e.altKey)
|
|
173
|
+
return;
|
|
174
|
+
consume();
|
|
175
|
+
return acceptActive();
|
|
176
|
+
case 'Tab':
|
|
177
|
+
// Accepts and closes; the key still moves focus on (APG).
|
|
178
|
+
e.stopImmediatePropagation();
|
|
179
|
+
return acceptActive();
|
|
180
|
+
case 'PageUp':
|
|
181
|
+
case 'PageDown': {
|
|
182
|
+
consume();
|
|
183
|
+
const items = enabled();
|
|
184
|
+
const i = lb.active ? items.indexOf(lb.active) : -1;
|
|
185
|
+
const next = e.key === 'PageDown' ? Math.min(i + PAGE, items.length - 1) : Math.max(i - PAGE, 0);
|
|
186
|
+
return lb.setActive(items[next] ?? null);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
get isOpen() {
|
|
192
|
+
return lb.isOpen;
|
|
193
|
+
},
|
|
194
|
+
get value() {
|
|
195
|
+
return value;
|
|
196
|
+
},
|
|
197
|
+
setValue(id) {
|
|
198
|
+
value = id;
|
|
199
|
+
render();
|
|
200
|
+
},
|
|
201
|
+
open() {
|
|
202
|
+
openOn('value');
|
|
203
|
+
},
|
|
204
|
+
close() {
|
|
205
|
+
lb.close();
|
|
206
|
+
},
|
|
207
|
+
update(next = {}) {
|
|
208
|
+
opts = { ...opts, ...next };
|
|
209
|
+
if (next.value !== undefined)
|
|
210
|
+
value = next.value;
|
|
211
|
+
lb.update({ placement: opts.placement });
|
|
212
|
+
render();
|
|
213
|
+
},
|
|
214
|
+
destroy() {
|
|
215
|
+
lb.destroy();
|
|
216
|
+
trigger.removeEventListener('keydown', onKeydown, true);
|
|
217
|
+
trigger.removeEventListener('click', onClick);
|
|
218
|
+
list.removeEventListener('fs-select', onAccepted);
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** The words a range needs: each thumb's accessible name. */
|
|
2
|
+
export interface SliderLabels {
|
|
3
|
+
/** The lower thumb's name ("From"). */
|
|
4
|
+
start: string;
|
|
5
|
+
/** The upper thumb's name ("To"). */
|
|
6
|
+
end: string;
|
|
7
|
+
}
|
|
8
|
+
interface SliderBaseOptions {
|
|
9
|
+
min: number;
|
|
10
|
+
max: number;
|
|
11
|
+
/** Values snap to `step` from `min`. Default 1. */
|
|
12
|
+
step?: number;
|
|
13
|
+
/** PageUp/PageDown's step. Default ten steps. */
|
|
14
|
+
pageStep?: number;
|
|
15
|
+
/** The value in words, for `aria-valuetext` and the `__value` output ("40 %"). */
|
|
16
|
+
valueText: (value: number) => string;
|
|
17
|
+
}
|
|
18
|
+
export interface SliderOptions extends SliderBaseOptions {
|
|
19
|
+
value: number;
|
|
20
|
+
}
|
|
21
|
+
export interface RangeSliderOptions extends SliderBaseOptions {
|
|
22
|
+
/** The two ends, lower first (fixed as a range at creation, as the calendar's `mode`). */
|
|
23
|
+
value: readonly [number, number];
|
|
24
|
+
labels: SliderLabels;
|
|
25
|
+
}
|
|
26
|
+
export interface SliderChangeDetail {
|
|
27
|
+
value: number;
|
|
28
|
+
}
|
|
29
|
+
export interface RangeSliderChangeDetail {
|
|
30
|
+
value: [number, number];
|
|
31
|
+
}
|
|
32
|
+
export interface Slider {
|
|
33
|
+
readonly value: number;
|
|
34
|
+
/** Sets the value (snapped and clamped) without an event. */
|
|
35
|
+
setValue(value: number): void;
|
|
36
|
+
update(options: Partial<Omit<SliderOptions, 'value'>>): void;
|
|
37
|
+
/** Removes every listener and ends a drag, silently (T88). */
|
|
38
|
+
destroy(): void;
|
|
39
|
+
}
|
|
40
|
+
export interface RangeSlider {
|
|
41
|
+
readonly value: [number, number];
|
|
42
|
+
/** Sets both ends (snapped, clamped and ordered) without an event. */
|
|
43
|
+
setValue(value: readonly [number, number]): void;
|
|
44
|
+
update(options: Partial<Omit<RangeSliderOptions, 'value'>>): void;
|
|
45
|
+
destroy(): void;
|
|
46
|
+
}
|
|
47
|
+
/** A range's `__value` text: both ends' words. */
|
|
48
|
+
export declare const rangeValueText: (low: string, high: string) => string;
|
|
49
|
+
export declare function createSlider(root: HTMLElement, options: RangeSliderOptions): RangeSlider;
|
|
50
|
+
export declare function createSlider(root: HTMLElement, options: SliderOptions): Slider;
|
|
51
|
+
export {};
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// The slider and the range slider (F2 draft §4, T134): one or two `role="slider"` thumbs on a track,
|
|
2
|
+
// moved by the keys (APG Slider and Multi-Thumb Slider) and by the pointer (pointer capture). A
|
|
3
|
+
// range's thumbs depend on each other: neither crosses the other. Values are numbers; every word
|
|
4
|
+
// (the value text, a range thumb's name) is passed in.
|
|
5
|
+
const THUMB = '.fs-slider__thumb';
|
|
6
|
+
const DRAGGING = 'fs-is-dragging';
|
|
7
|
+
/** The digits after the point, so a snapped 0.1 + 0.2 reads 0.3. */
|
|
8
|
+
const decimals = (n) => {
|
|
9
|
+
const s = String(n);
|
|
10
|
+
const e = s.indexOf('e-');
|
|
11
|
+
if (e >= 0)
|
|
12
|
+
return Number(s.slice(e + 2));
|
|
13
|
+
const dot = s.indexOf('.');
|
|
14
|
+
return dot < 0 ? 0 : s.length - dot - 1;
|
|
15
|
+
};
|
|
16
|
+
/** A range's `__value` text: both ends' words. */
|
|
17
|
+
export const rangeValueText = (low, high) => `${low} – ${high}`;
|
|
18
|
+
export function createSlider(root, options) {
|
|
19
|
+
let opts = { ...options };
|
|
20
|
+
const isRange = Array.isArray(options.value);
|
|
21
|
+
const thumbs = [...root.querySelectorAll(THUMB)];
|
|
22
|
+
if (thumbs.length !== (isRange ? 2 : 1)) {
|
|
23
|
+
throw new Error(`createSlider: a ${isRange ? 'range needs two' : 'slider needs one'} ${THUMB}`);
|
|
24
|
+
}
|
|
25
|
+
const track = root.querySelector('.fs-slider__track') ?? root;
|
|
26
|
+
const step = () => (opts.step && opts.step > 0 ? opts.step : 1);
|
|
27
|
+
const pageStep = () => (opts.pageStep && opts.pageStep > 0 ? opts.pageStep : step() * 10);
|
|
28
|
+
const clamp = (v, lo, hi) => Math.min(Math.max(v, lo), hi);
|
|
29
|
+
const snap = (v) => {
|
|
30
|
+
const s = step();
|
|
31
|
+
const snapped = opts.min + Math.round((v - opts.min) / s) * s;
|
|
32
|
+
return Number(snapped.toFixed(Math.max(decimals(s), decimals(opts.min))));
|
|
33
|
+
};
|
|
34
|
+
/** Snapped and clamped into the whole range, lower first. */
|
|
35
|
+
const normalise = (v) => {
|
|
36
|
+
const raw = isRange ? [...v] : [v];
|
|
37
|
+
const out = raw.map((x) => clamp(snap(Number.isFinite(x) ? x : opts.min), opts.min, opts.max));
|
|
38
|
+
return out.length === 2 && out[0] > out[1] ? [out[1], out[0]] : out;
|
|
39
|
+
};
|
|
40
|
+
let values = normalise(opts.value);
|
|
41
|
+
/** Where thumb `i` may go: the whole range, or up to the other thumb. */
|
|
42
|
+
const bounds = (i) => !isRange ? [opts.min, opts.max] : i === 0 ? [opts.min, values[1]] : [values[0], opts.max];
|
|
43
|
+
const percent = (v) => (opts.max === opts.min ? 0 : ((v - opts.min) / (opts.max - opts.min)) * 100);
|
|
44
|
+
const isRtl = () => getComputedStyle(root).direction === 'rtl';
|
|
45
|
+
const isDisabled = (thumb) => thumb.getAttribute('aria-disabled') === 'true';
|
|
46
|
+
const detail = () => ({ value: isRange ? [values[0], values[1]] : values[0] });
|
|
47
|
+
const emit = (type) => root.dispatchEvent(new CustomEvent(type, { bubbles: true, detail: detail() }));
|
|
48
|
+
function render() {
|
|
49
|
+
const text = (v) => opts.valueText(v);
|
|
50
|
+
thumbs.forEach((thumb, i) => {
|
|
51
|
+
const v = values[i];
|
|
52
|
+
const [lo, hi] = bounds(i);
|
|
53
|
+
if (!thumb.hasAttribute('role'))
|
|
54
|
+
thumb.setAttribute('role', 'slider');
|
|
55
|
+
if (!thumb.hasAttribute('tabindex'))
|
|
56
|
+
thumb.tabIndex = 0;
|
|
57
|
+
thumb.setAttribute('aria-valuemin', String(lo));
|
|
58
|
+
thumb.setAttribute('aria-valuemax', String(hi));
|
|
59
|
+
thumb.setAttribute('aria-valuenow', String(v));
|
|
60
|
+
thumb.setAttribute('aria-valuetext', text(v));
|
|
61
|
+
if (isRange && opts.labels)
|
|
62
|
+
thumb.setAttribute('aria-label', i === 0 ? opts.labels.start : opts.labels.end);
|
|
63
|
+
thumb.style.setProperty('--fs-slider-at', `${percent(v)}%`);
|
|
64
|
+
});
|
|
65
|
+
root.style.setProperty('--fs-slider-low', `${isRange ? percent(values[0]) : 0}%`);
|
|
66
|
+
root.style.setProperty('--fs-slider-high', `${percent(values[values.length - 1])}%`);
|
|
67
|
+
const out = root.querySelector('.fs-slider__value');
|
|
68
|
+
if (out)
|
|
69
|
+
out.textContent = isRange ? rangeValueText(text(values[0]), text(values[1])) : text(values[0]);
|
|
70
|
+
const inputs = root.querySelectorAll(':scope > input[type="hidden"]');
|
|
71
|
+
inputs.forEach((input, i) => {
|
|
72
|
+
if (i < values.length)
|
|
73
|
+
input.value = String(values[i]);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** Moves thumb `i` to `to` (snapped unless `exact`), inside its bounds. Whether it moved. */
|
|
77
|
+
function moveTo(i, to, exact = false) {
|
|
78
|
+
const [lo, hi] = bounds(i);
|
|
79
|
+
const next = clamp(exact ? to : snap(to), lo, hi);
|
|
80
|
+
if (next === values[i])
|
|
81
|
+
return false;
|
|
82
|
+
values[i] = next;
|
|
83
|
+
render();
|
|
84
|
+
emit('fs-input');
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
function onKeydown(e) {
|
|
88
|
+
if (e.altKey || e.ctrlKey || e.metaKey)
|
|
89
|
+
return;
|
|
90
|
+
const i = thumbs.indexOf(e.target);
|
|
91
|
+
if (i < 0 || isDisabled(thumbs[i]))
|
|
92
|
+
return;
|
|
93
|
+
const v = values[i];
|
|
94
|
+
const [lo, hi] = bounds(i);
|
|
95
|
+
const flip = isRtl() ? -1 : 1;
|
|
96
|
+
let to;
|
|
97
|
+
let exact = false;
|
|
98
|
+
switch (e.key) {
|
|
99
|
+
case 'ArrowRight':
|
|
100
|
+
to = v + step() * flip;
|
|
101
|
+
break;
|
|
102
|
+
case 'ArrowLeft':
|
|
103
|
+
to = v - step() * flip;
|
|
104
|
+
break;
|
|
105
|
+
case 'ArrowUp':
|
|
106
|
+
to = v + step();
|
|
107
|
+
break;
|
|
108
|
+
case 'ArrowDown':
|
|
109
|
+
to = v - step();
|
|
110
|
+
break;
|
|
111
|
+
case 'PageUp':
|
|
112
|
+
to = v + pageStep();
|
|
113
|
+
break;
|
|
114
|
+
case 'PageDown':
|
|
115
|
+
to = v - pageStep();
|
|
116
|
+
break;
|
|
117
|
+
case 'Home':
|
|
118
|
+
to = lo;
|
|
119
|
+
exact = true;
|
|
120
|
+
break;
|
|
121
|
+
case 'End':
|
|
122
|
+
to = hi;
|
|
123
|
+
exact = true;
|
|
124
|
+
break;
|
|
125
|
+
default:
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
e.preventDefault();
|
|
129
|
+
if (moveTo(i, to, exact))
|
|
130
|
+
emit('fs-change');
|
|
131
|
+
}
|
|
132
|
+
// Pointer: a press on the track moves the nearer thumb there and starts a drag; a press on a
|
|
133
|
+
// thumb grabs it where it was pressed (no jump). Pointer capture keeps the moves coming.
|
|
134
|
+
let drag = null;
|
|
135
|
+
const valueAt = (clientX) => {
|
|
136
|
+
const rect = track.getBoundingClientRect();
|
|
137
|
+
if (rect.width <= 0)
|
|
138
|
+
return null;
|
|
139
|
+
let frac = clamp((clientX - rect.left) / rect.width, 0, 1);
|
|
140
|
+
if (isRtl())
|
|
141
|
+
frac = 1 - frac;
|
|
142
|
+
return opts.min + frac * (opts.max - opts.min);
|
|
143
|
+
};
|
|
144
|
+
/** The thumb a press at `at` moves, or `null` while two stacked thumbs wait for a direction. */
|
|
145
|
+
function nearest(at) {
|
|
146
|
+
if (!isRange)
|
|
147
|
+
return 0;
|
|
148
|
+
const [a, b] = values;
|
|
149
|
+
if (a === b)
|
|
150
|
+
return at > a ? 1 : at < a ? 0 : null;
|
|
151
|
+
const da = Math.abs(at - a);
|
|
152
|
+
const db = Math.abs(at - b);
|
|
153
|
+
return db < da ? 1 : 0;
|
|
154
|
+
}
|
|
155
|
+
function onPointerDown(e) {
|
|
156
|
+
if (drag || e.button !== 0 || e.target.closest?.('.fs-slider__value'))
|
|
157
|
+
return;
|
|
158
|
+
const at = valueAt(e.clientX);
|
|
159
|
+
if (at === null)
|
|
160
|
+
return;
|
|
161
|
+
const hit = thumbs.findIndex((t) => t === e.target || t.contains(e.target));
|
|
162
|
+
const stacked = isRange && values[0] === values[1];
|
|
163
|
+
let i = hit >= 0 && !stacked ? hit : nearest(at);
|
|
164
|
+
const pending = i === null || (hit >= 0 && stacked);
|
|
165
|
+
if (i === null)
|
|
166
|
+
i = 0;
|
|
167
|
+
if (isDisabled(thumbs[i]))
|
|
168
|
+
return;
|
|
169
|
+
e.preventDefault();
|
|
170
|
+
let offset = 0;
|
|
171
|
+
if (hit >= 0) {
|
|
172
|
+
const r = thumbs[hit].getBoundingClientRect();
|
|
173
|
+
offset = e.clientX - (r.left + r.width / 2);
|
|
174
|
+
}
|
|
175
|
+
drag = { i, pointerId: e.pointerId, offset, start: [...values], pending };
|
|
176
|
+
thumbs[i].focus({ preventScroll: true });
|
|
177
|
+
try {
|
|
178
|
+
root.setPointerCapture?.(e.pointerId);
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// A synthetic or ended pointer can't be captured: the drag still follows root's events.
|
|
182
|
+
}
|
|
183
|
+
thumbs[i].classList.add(DRAGGING);
|
|
184
|
+
if (hit < 0 && !pending)
|
|
185
|
+
moveTo(i, at);
|
|
186
|
+
}
|
|
187
|
+
function onPointerMove(e) {
|
|
188
|
+
if (!drag || e.pointerId !== drag.pointerId)
|
|
189
|
+
return;
|
|
190
|
+
const at = valueAt(e.clientX - drag.offset);
|
|
191
|
+
if (at === null)
|
|
192
|
+
return;
|
|
193
|
+
if (drag.pending) {
|
|
194
|
+
const v = values[0];
|
|
195
|
+
if (snap(at) === v)
|
|
196
|
+
return;
|
|
197
|
+
const i = at > v ? 1 : 0;
|
|
198
|
+
thumbs[drag.i].classList.remove(DRAGGING);
|
|
199
|
+
drag.i = i;
|
|
200
|
+
drag.pending = false;
|
|
201
|
+
thumbs[i].classList.add(DRAGGING);
|
|
202
|
+
thumbs[i].focus({ preventScroll: true });
|
|
203
|
+
}
|
|
204
|
+
moveTo(drag.i, at);
|
|
205
|
+
}
|
|
206
|
+
function endDrag(silent = false) {
|
|
207
|
+
if (!drag)
|
|
208
|
+
return;
|
|
209
|
+
const { i, pointerId, start } = drag;
|
|
210
|
+
drag = null;
|
|
211
|
+
thumbs[i].classList.remove(DRAGGING);
|
|
212
|
+
try {
|
|
213
|
+
if (root.hasPointerCapture?.(pointerId))
|
|
214
|
+
root.releasePointerCapture(pointerId);
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
// Already released.
|
|
218
|
+
}
|
|
219
|
+
if (!silent && values.some((v, n) => v !== start[n]))
|
|
220
|
+
emit('fs-change');
|
|
221
|
+
}
|
|
222
|
+
function onPointerEnd(e) {
|
|
223
|
+
if (drag && e.pointerId === drag.pointerId)
|
|
224
|
+
endDrag();
|
|
225
|
+
}
|
|
226
|
+
root.addEventListener('keydown', onKeydown);
|
|
227
|
+
root.addEventListener('pointerdown', onPointerDown);
|
|
228
|
+
root.addEventListener('pointermove', onPointerMove);
|
|
229
|
+
root.addEventListener('pointerup', onPointerEnd);
|
|
230
|
+
root.addEventListener('pointercancel', onPointerEnd);
|
|
231
|
+
root.addEventListener('lostpointercapture', onPointerEnd);
|
|
232
|
+
render();
|
|
233
|
+
return {
|
|
234
|
+
get value() {
|
|
235
|
+
return (isRange ? [values[0], values[1]] : values[0]);
|
|
236
|
+
},
|
|
237
|
+
setValue(value) {
|
|
238
|
+
values = normalise(value);
|
|
239
|
+
render();
|
|
240
|
+
},
|
|
241
|
+
update(next = {}) {
|
|
242
|
+
opts = { ...opts, ...next };
|
|
243
|
+
values = normalise((isRange ? values : values[0]));
|
|
244
|
+
render();
|
|
245
|
+
},
|
|
246
|
+
destroy() {
|
|
247
|
+
endDrag(true);
|
|
248
|
+
root.removeEventListener('keydown', onKeydown);
|
|
249
|
+
root.removeEventListener('pointerdown', onPointerDown);
|
|
250
|
+
root.removeEventListener('pointermove', onPointerMove);
|
|
251
|
+
root.removeEventListener('pointerup', onPointerEnd);
|
|
252
|
+
root.removeEventListener('pointercancel', onPointerEnd);
|
|
253
|
+
root.removeEventListener('lostpointercapture', onPointerEnd);
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
}
|