@barqjs/aria 0.0.0-stage → 0.2.0-next.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 +21 -0
- package/README.md +293 -2
- package/package.json +347 -4
- package/src/breadcrumbs.tsx +296 -0
- package/src/button.tsx +342 -0
- package/src/calendar.tsx +1389 -0
- package/src/checkbox.tsx +570 -0
- package/src/collections.ts +976 -0
- package/src/color.ts +562 -0
- package/src/colorpicker.tsx +1478 -0
- package/src/combobox.tsx +2481 -0
- package/src/command.tsx +1184 -0
- package/src/date.ts +652 -0
- package/src/datefield.tsx +951 -0
- package/src/datepicker.tsx +556 -0
- package/src/dialog.tsx +890 -0
- package/src/disclosure.tsx +659 -0
- package/src/dom.ts +706 -0
- package/src/drawer.ts +674 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +563 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1270 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +218 -0
- package/src/interactions/focusable.ts +86 -0
- package/src/interactions/hover.ts +213 -0
- package/src/interactions/interact-outside.ts +123 -0
- package/src/interactions/keyboard.ts +105 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +416 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +193 -0
- package/src/interactions/press.ts +898 -0
- package/src/interactions/prevent-focus.ts +102 -0
- package/src/interactions/scroll-wheel.ts +43 -0
- package/src/interactions/text-selection.ts +77 -0
- package/src/interactions.ts +81 -0
- package/src/label.ts +128 -0
- package/src/link.tsx +381 -0
- package/src/listbox.tsx +721 -0
- package/src/live.ts +204 -0
- package/src/menu.tsx +2290 -0
- package/src/menubar.tsx +575 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +838 -0
- package/src/otp.ts +430 -0
- package/src/overlays.ts +1247 -0
- package/src/platform.ts +74 -0
- package/src/presence.ts +164 -0
- package/src/questionnaire.tsx +1712 -0
- package/src/radio.tsx +567 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1665 -0
- package/src/selection.ts +1415 -0
- package/src/slider.tsx +1042 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +186 -0
- package/src/table.tsx +0 -0
- package/src/tabs.tsx +1018 -0
- package/src/tag.tsx +452 -0
- package/src/textfield.tsx +417 -0
- package/src/toast.ts +658 -0
- package/src/toggle-input.tsx +48 -0
- package/src/toggle.ts +321 -0
- package/src/togglegroup.ts +365 -0
- package/src/toolbar.tsx +179 -0
- package/src/tooltip.tsx +657 -0
- package/src/utils.ts +758 -0
- package/src/validation.ts +447 -0
- package/src/virtualizer.tsx +674 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the user is driving the page with, right now.
|
|
3
|
+
*
|
|
4
|
+
* `:focus-visible` answers a narrower question than a widget needs. It is a
|
|
5
|
+
* per-element pseudo-class the engine decides, so a component cannot read it
|
|
6
|
+
* to choose an ARIA attribute, cannot apply it to a wrapper that is not the
|
|
7
|
+
* focused element, and gets no say in the one case the heuristic gets wrong:
|
|
8
|
+
* a text input, where only Tab and Escape should raise a ring and every other
|
|
9
|
+
* key should not.
|
|
10
|
+
*
|
|
11
|
+
* So the modality is tracked once for the page, from listeners installed on
|
|
12
|
+
* the first call rather than at import, because this package declares
|
|
13
|
+
* `sideEffects: false` and a module that installs listeners on evaluation
|
|
14
|
+
* makes that a lie.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { type Accessor, createSignal, untrack } from "@barqjs/core";
|
|
18
|
+
import { isServer } from "@barqjs/web";
|
|
19
|
+
import { activeElement, eventTarget, ownerDocument, ownerWindow } from "../dom.ts";
|
|
20
|
+
import { isAndroid, isMac } from "../platform.ts";
|
|
21
|
+
import { tryCleanup } from "@barqjs/primitives/utils";
|
|
22
|
+
import { isIgnoringFocus, isOpeningLink } from "./flags.ts";
|
|
23
|
+
|
|
24
|
+
/** How the user reached the element. */
|
|
25
|
+
export type Modality = "keyboard" | "pointer" | "virtual";
|
|
26
|
+
|
|
27
|
+
/** What produced the interaction. `virtual` is a screen reader or `.click()`. */
|
|
28
|
+
export type PointerType = "mouse" | "pen" | "touch" | "keyboard" | "virtual";
|
|
29
|
+
|
|
30
|
+
type ModalityEvent = PointerEvent | MouseEvent | KeyboardEvent | FocusEvent | null;
|
|
31
|
+
|
|
32
|
+
export type ModalityHandler = (modality: Modality, event: ModalityEvent) => void;
|
|
33
|
+
|
|
34
|
+
let currentModality: Modality | null = null;
|
|
35
|
+
let currentPointerType: PointerType = "keyboard";
|
|
36
|
+
const handlers = new Set<ModalityHandler>();
|
|
37
|
+
const trackedWindows = new Map<Window, { focus: (options?: FocusOptions) => void }>();
|
|
38
|
+
|
|
39
|
+
let hasEventBeforeFocus = false;
|
|
40
|
+
let hasBlurredWindowRecently = false;
|
|
41
|
+
|
|
42
|
+
// Only these two make a ring appear on a text input. Every other key is the
|
|
43
|
+
// user typing, and a ring that appears while typing is noise.
|
|
44
|
+
const TEXT_INPUT_FOCUS_KEYS = new Set(["Tab", "Escape"]);
|
|
45
|
+
|
|
46
|
+
const NON_TEXT_INPUT_TYPES = new Set([
|
|
47
|
+
"checkbox",
|
|
48
|
+
"radio",
|
|
49
|
+
"range",
|
|
50
|
+
"color",
|
|
51
|
+
"file",
|
|
52
|
+
"image",
|
|
53
|
+
"button",
|
|
54
|
+
"submit",
|
|
55
|
+
"reset",
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
function notify(next: Modality, event: ModalityEvent): void {
|
|
59
|
+
for (const handler of handlers) handler(next, event);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether the key could plausibly be a navigation the user wants a ring for.
|
|
64
|
+
*
|
|
65
|
+
* Control and Shift arrive on their own when the user tabs back into the
|
|
66
|
+
* window from browser chrome, and a modifier held down is a shortcut rather
|
|
67
|
+
* than navigation.
|
|
68
|
+
*/
|
|
69
|
+
function isModalityKey(event: KeyboardEvent): boolean {
|
|
70
|
+
return !(
|
|
71
|
+
event.metaKey ||
|
|
72
|
+
(!isMac() && event.altKey) ||
|
|
73
|
+
event.ctrlKey ||
|
|
74
|
+
event.key === "Control" ||
|
|
75
|
+
event.key === "Shift" ||
|
|
76
|
+
event.key === "Meta"
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Whether a click came from a keyboard, a screen reader, or `element.click()`.
|
|
82
|
+
*
|
|
83
|
+
* Every engine but one reports `detail === 0` only for such clicks. Android
|
|
84
|
+
* TalkBack is the exception: its double tap arrives through a `click` listener
|
|
85
|
+
* with a `pointerType` set, so the button mask is what separates it.
|
|
86
|
+
*/
|
|
87
|
+
export function isVirtualClick(event: MouseEvent | PointerEvent): boolean {
|
|
88
|
+
// JAWS and NVDA under Firefox.
|
|
89
|
+
if ((event as PointerEvent).pointerType === "" && event.isTrusted) return true;
|
|
90
|
+
|
|
91
|
+
if (isAndroid() && Boolean((event as PointerEvent).pointerType)) {
|
|
92
|
+
return event.type === "click" && event.buttons === 1;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return event.detail === 0 && !(event as PointerEvent).pointerType;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Whether a pointer event was fabricated by a screen reader.
|
|
100
|
+
*
|
|
101
|
+
* A real pointer has a size. VoiceOver's has none, and TalkBack's double tap
|
|
102
|
+
* reports a one-pixel `mouse` pointer at zero pressure.
|
|
103
|
+
*/
|
|
104
|
+
export function isVirtualPointerEvent(event: PointerEvent): boolean {
|
|
105
|
+
const android = isAndroid();
|
|
106
|
+
return (
|
|
107
|
+
(!android && event.width === 0 && event.height === 0) ||
|
|
108
|
+
(android &&
|
|
109
|
+
event.width === 1 &&
|
|
110
|
+
event.height === 1 &&
|
|
111
|
+
event.pressure === 0 &&
|
|
112
|
+
event.detail === 0 &&
|
|
113
|
+
event.pointerType === "mouse")
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function onKeyboardEvent(event: Event): void {
|
|
118
|
+
hasEventBeforeFocus = true;
|
|
119
|
+
if (!isOpeningLink() && isModalityKey(event as KeyboardEvent)) {
|
|
120
|
+
currentModality = "keyboard";
|
|
121
|
+
currentPointerType = "keyboard";
|
|
122
|
+
notify("keyboard", event as KeyboardEvent);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function onPointerEvent(event: Event): void {
|
|
127
|
+
const pointer = event as PointerEvent;
|
|
128
|
+
currentModality = "pointer";
|
|
129
|
+
currentPointerType = "pointerType" in pointer ? (pointer.pointerType as PointerType) : "mouse";
|
|
130
|
+
if (event.type === "mousedown" || event.type === "pointerdown") {
|
|
131
|
+
hasEventBeforeFocus = true;
|
|
132
|
+
notify("pointer", pointer);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function onClickEvent(event: Event): void {
|
|
137
|
+
if (!isOpeningLink() && isVirtualClick(event as MouseEvent)) {
|
|
138
|
+
hasEventBeforeFocus = true;
|
|
139
|
+
currentModality = "virtual";
|
|
140
|
+
currentPointerType = "virtual";
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function onFocusEvent(event: Event): void {
|
|
145
|
+
if (isIgnoringFocus()) return;
|
|
146
|
+
|
|
147
|
+
const target = eventTarget(event);
|
|
148
|
+
const view = ownerWindow(target);
|
|
149
|
+
const doc = ownerDocument(target);
|
|
150
|
+
|
|
151
|
+
// Returning to the tab restores focus to where it was, firing a focus event
|
|
152
|
+
// nobody initiated. `hasBlurredWindowRecently` keeps that from reading as
|
|
153
|
+
// virtual modality, but WebKit fires the window/element pair twice and the
|
|
154
|
+
// first element focus clears the flag, so re-arm it on the window's own.
|
|
155
|
+
if ((target as unknown) === view) {
|
|
156
|
+
hasBlurredWindowRecently = true;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Firefox fires two extra focus events, on the window then the document,
|
|
161
|
+
// when the user first clicks into an iframe.
|
|
162
|
+
if ((target as unknown) === doc || !event.isTrusted) return;
|
|
163
|
+
|
|
164
|
+
// A focus with no keyboard or pointer event before it is a virtual cursor:
|
|
165
|
+
// the next/previous buttons above the iOS keyboard, or a screen reader.
|
|
166
|
+
if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
|
|
167
|
+
currentModality = "virtual";
|
|
168
|
+
currentPointerType = "virtual";
|
|
169
|
+
notify("virtual", event as FocusEvent);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
hasEventBeforeFocus = false;
|
|
173
|
+
hasBlurredWindowRecently = false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function onWindowBlur(): void {
|
|
177
|
+
if (isIgnoringFocus()) return;
|
|
178
|
+
// Tabbing out of the window fires no subsequent focus event, so the state
|
|
179
|
+
// has to be reset here or the next focus reads as virtual.
|
|
180
|
+
hasEventBeforeFocus = false;
|
|
181
|
+
hasBlurredWindowRecently = true;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Install the page-level listeners, once per window.
|
|
186
|
+
*
|
|
187
|
+
* Called by every hook that reads the modality rather than at import time.
|
|
188
|
+
* Idempotent, and cheap after the first call.
|
|
189
|
+
*/
|
|
190
|
+
export function trackModality(element?: Element | null): () => void {
|
|
191
|
+
if (isServer || typeof window === "undefined") return () => {};
|
|
192
|
+
|
|
193
|
+
const doc = ownerDocument(element);
|
|
194
|
+
const setup = (): void => {
|
|
195
|
+
setupModalityListeners(element);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
if (doc.readyState !== "loading") {
|
|
199
|
+
setup();
|
|
200
|
+
return () => untrackModality(element);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
doc.addEventListener("DOMContentLoaded", setup);
|
|
204
|
+
return () => {
|
|
205
|
+
doc.removeEventListener("DOMContentLoaded", setup);
|
|
206
|
+
untrackModality(element);
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function setupModalityListeners(element?: Element | null): void {
|
|
211
|
+
const view = ownerWindow(element);
|
|
212
|
+
const doc = ownerDocument(element);
|
|
213
|
+
if (trackedWindows.has(view)) return;
|
|
214
|
+
|
|
215
|
+
// A programmatic `focus()` must not change the modality, but a focus with no
|
|
216
|
+
// preceding user event must be read as virtual. Telling the two apart needs
|
|
217
|
+
// to know that `focus()` was called, and the prototype is the only place
|
|
218
|
+
// that fact exists.
|
|
219
|
+
//
|
|
220
|
+
// `defineProperty` rather than assignment: `@testing-library/user-event`
|
|
221
|
+
// instruments `focus` as a getter, and assigning to one throws.
|
|
222
|
+
//
|
|
223
|
+
// Capturing the unbound method is the point: `focusWithModality` re-applies
|
|
224
|
+
// the `this` it was called on.
|
|
225
|
+
//
|
|
226
|
+
// Untracked because the focus and blur listeners run synchronously inside
|
|
227
|
+
// the caller: an effect callback that moves focus has not read what its
|
|
228
|
+
// listeners read, and the engine's dev build would say it had.
|
|
229
|
+
const original = view.HTMLElement.prototype.focus;
|
|
230
|
+
Reflect.defineProperty(view.HTMLElement.prototype, "focus", {
|
|
231
|
+
configurable: true,
|
|
232
|
+
writable: true,
|
|
233
|
+
value: function focusWithModality(this: HTMLElement, ...args: [FocusOptions?]): void {
|
|
234
|
+
hasEventBeforeFocus = true;
|
|
235
|
+
untrack(() => original.apply(this, args));
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
doc.addEventListener("keydown", onKeyboardEvent, true);
|
|
240
|
+
doc.addEventListener("keyup", onKeyboardEvent, true);
|
|
241
|
+
doc.addEventListener("click", onClickEvent, true);
|
|
242
|
+
|
|
243
|
+
// On the window, so these run before any listener bound to the document.
|
|
244
|
+
view.addEventListener("focus", onFocusEvent, true);
|
|
245
|
+
view.addEventListener("blur", onWindowBlur, false);
|
|
246
|
+
|
|
247
|
+
if (typeof PointerEvent !== "undefined") {
|
|
248
|
+
doc.addEventListener("pointerdown", onPointerEvent, true);
|
|
249
|
+
doc.addEventListener("pointermove", onPointerEvent, true);
|
|
250
|
+
doc.addEventListener("pointerup", onPointerEvent, true);
|
|
251
|
+
} else {
|
|
252
|
+
doc.addEventListener("mousedown", onPointerEvent, true);
|
|
253
|
+
doc.addEventListener("mousemove", onPointerEvent, true);
|
|
254
|
+
doc.addEventListener("mouseup", onPointerEvent, true);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
trackedWindows.set(view, { focus: original });
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Undo {@link trackModality} for a window, including the prototype patch. */
|
|
261
|
+
export function untrackModality(element?: Element | null): void {
|
|
262
|
+
if (typeof window === "undefined") return;
|
|
263
|
+
const view = ownerWindow(element);
|
|
264
|
+
const doc = ownerDocument(element);
|
|
265
|
+
const tracked = trackedWindows.get(view);
|
|
266
|
+
if (tracked === undefined) return;
|
|
267
|
+
|
|
268
|
+
Reflect.defineProperty(view.HTMLElement.prototype, "focus", {
|
|
269
|
+
configurable: true,
|
|
270
|
+
writable: true,
|
|
271
|
+
value: tracked.focus,
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
doc.removeEventListener("keydown", onKeyboardEvent, true);
|
|
275
|
+
doc.removeEventListener("keyup", onKeyboardEvent, true);
|
|
276
|
+
doc.removeEventListener("click", onClickEvent, true);
|
|
277
|
+
view.removeEventListener("focus", onFocusEvent, true);
|
|
278
|
+
view.removeEventListener("blur", onWindowBlur, false);
|
|
279
|
+
|
|
280
|
+
if (typeof PointerEvent !== "undefined") {
|
|
281
|
+
doc.removeEventListener("pointerdown", onPointerEvent, true);
|
|
282
|
+
doc.removeEventListener("pointermove", onPointerEvent, true);
|
|
283
|
+
doc.removeEventListener("pointerup", onPointerEvent, true);
|
|
284
|
+
} else {
|
|
285
|
+
doc.removeEventListener("mousedown", onPointerEvent, true);
|
|
286
|
+
doc.removeEventListener("mousemove", onPointerEvent, true);
|
|
287
|
+
doc.removeEventListener("mouseup", onPointerEvent, true);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
trackedWindows.delete(view);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Whether a focus ring should be shown for whatever takes focus next. */
|
|
294
|
+
export function isFocusVisible(): boolean {
|
|
295
|
+
return currentModality !== "pointer";
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** The last modality observed, or `null` before the user has done anything. */
|
|
299
|
+
export function getInteractionModality(): Modality | null {
|
|
300
|
+
return currentModality;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Declare the modality, as if the user had produced it.
|
|
305
|
+
*
|
|
306
|
+
* For a component that moves focus itself and knows why: a menu opened from a
|
|
307
|
+
* keyboard shortcut must show its ring even though no key reached the menu.
|
|
308
|
+
*/
|
|
309
|
+
export function setInteractionModality(next: Modality): void {
|
|
310
|
+
currentModality = next;
|
|
311
|
+
currentPointerType = next === "pointer" ? "mouse" : next;
|
|
312
|
+
notify(next, null);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Forget what was observed, as on a page nobody has touched yet.
|
|
317
|
+
*
|
|
318
|
+
* Modality is the one piece of interaction state that outlives the elements it
|
|
319
|
+
* was produced on. A test harness calls this between tests, or the first focus
|
|
320
|
+
* of one test is read through the last press of another.
|
|
321
|
+
*/
|
|
322
|
+
export function resetInteractionModality(): void {
|
|
323
|
+
currentModality = null;
|
|
324
|
+
currentPointerType = "keyboard";
|
|
325
|
+
hasEventBeforeFocus = false;
|
|
326
|
+
hasBlurredWindowRecently = false;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** The last pointer type observed. */
|
|
330
|
+
export function getPointerType(): PointerType {
|
|
331
|
+
return currentPointerType;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Subscribe to modality changes. Returns the unsubscribe.
|
|
336
|
+
*
|
|
337
|
+
* `isTextInput` narrows to the two keys that should raise a ring on a field.
|
|
338
|
+
*/
|
|
339
|
+
export function onModalityChange(
|
|
340
|
+
fn: ModalityHandler,
|
|
341
|
+
options: { isTextInput?: boolean } = {},
|
|
342
|
+
): () => void {
|
|
343
|
+
const handler: ModalityHandler = (next, event) => {
|
|
344
|
+
if (!shouldReportKeyboardFocus(options.isTextInput === true, next, event)) return;
|
|
345
|
+
fn(next, event);
|
|
346
|
+
};
|
|
347
|
+
handlers.add(handler);
|
|
348
|
+
return () => {
|
|
349
|
+
handlers.delete(handler);
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function shouldReportKeyboardFocus(
|
|
354
|
+
isTextInput: boolean,
|
|
355
|
+
next: Modality,
|
|
356
|
+
event: ModalityEvent,
|
|
357
|
+
): boolean {
|
|
358
|
+
const target = event !== null ? (eventTarget(event) as Element | null) : null;
|
|
359
|
+
const doc = ownerDocument(target);
|
|
360
|
+
const view = ownerWindow(target);
|
|
361
|
+
const active = activeElement(doc);
|
|
362
|
+
|
|
363
|
+
// The caller passes `isTextInput` for the case the DOM cannot answer: a key
|
|
364
|
+
// pressed on a button that is about to move focus INTO a field, where the
|
|
365
|
+
// active element at this moment is still the button.
|
|
366
|
+
const intoText =
|
|
367
|
+
isTextInput ||
|
|
368
|
+
(active instanceof view.HTMLInputElement && !NON_TEXT_INPUT_TYPES.has(active.type)) ||
|
|
369
|
+
active instanceof view.HTMLTextAreaElement ||
|
|
370
|
+
(active instanceof view.HTMLElement && active.isContentEditable);
|
|
371
|
+
|
|
372
|
+
return !(
|
|
373
|
+
intoText &&
|
|
374
|
+
next === "keyboard" &&
|
|
375
|
+
event instanceof view.KeyboardEvent &&
|
|
376
|
+
!TEXT_INPUT_FOCUS_KEYS.has(event.key)
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* The current modality, as a signal.
|
|
382
|
+
*
|
|
383
|
+
* ```tsx
|
|
384
|
+
* const how = modality();
|
|
385
|
+
* <div data-modality={how} />
|
|
386
|
+
* ```
|
|
387
|
+
*/
|
|
388
|
+
export function modality(): Accessor<Modality | null> {
|
|
389
|
+
const [current, setCurrent] = createSignal(currentModality);
|
|
390
|
+
if (isServer) return current;
|
|
391
|
+
|
|
392
|
+
trackModality();
|
|
393
|
+
tryCleanup(onModalityChange(() => setCurrent(currentModality)));
|
|
394
|
+
|
|
395
|
+
return current;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Whether a focus ring should be visible for the page, as a signal.
|
|
400
|
+
*
|
|
401
|
+
* This is the global answer. A component wants {@link focusRing} in
|
|
402
|
+
* `../focus.ts`, which combines it with whether the element is focused at all.
|
|
403
|
+
*/
|
|
404
|
+
export function focusVisible(
|
|
405
|
+
options: { isTextInput?: boolean; autoFocus?: boolean } = {},
|
|
406
|
+
): Accessor<boolean> {
|
|
407
|
+
const [current, setCurrent] = createSignal(options.autoFocus === true || isFocusVisible());
|
|
408
|
+
if (isServer) return current;
|
|
409
|
+
|
|
410
|
+
trackModality();
|
|
411
|
+
tryCleanup(
|
|
412
|
+
onModalityChange(() => setCurrent(isFocusVisible()), { isTextInput: options.isTextInput }),
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
return current;
|
|
416
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dragging a value: a slider thumb, a colour area, a resize handle.
|
|
3
|
+
*
|
|
4
|
+
* Deltas rather than positions, and the same deltas from the arrow keys as
|
|
5
|
+
* from a pointer, so the consumer writes the value update once and gets
|
|
6
|
+
* keyboard support for free.
|
|
7
|
+
*
|
|
8
|
+
* `PointerEvent.movementX` would seem to be the answer and is not: it is
|
|
9
|
+
* always zero in WebKit on macOS, and Chrome on Android scales it by the
|
|
10
|
+
* device pixel ratio while Chrome on macOS does not. The difference between
|
|
11
|
+
* successive `pageX` values is the only portable delta.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { ownerWindow, targetElement } from "../dom.ts";
|
|
15
|
+
import type { DOMProps } from "../utils.ts";
|
|
16
|
+
import { globalListeners } from "./listeners.ts";
|
|
17
|
+
import type { PointerType } from "./modality.ts";
|
|
18
|
+
import { disableTextSelection, restoreTextSelection } from "./text-selection.ts";
|
|
19
|
+
|
|
20
|
+
interface Modifiers {
|
|
21
|
+
shiftKey: boolean;
|
|
22
|
+
ctrlKey: boolean;
|
|
23
|
+
metaKey: boolean;
|
|
24
|
+
altKey: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface MoveStartEvent extends Modifiers {
|
|
28
|
+
type: "movestart";
|
|
29
|
+
pointerType: PointerType;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface MoveMoveEvent extends Modifiers {
|
|
33
|
+
type: "move";
|
|
34
|
+
pointerType: PointerType;
|
|
35
|
+
deltaX: number;
|
|
36
|
+
deltaY: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface MoveEndEvent extends Modifiers {
|
|
40
|
+
type: "moveend";
|
|
41
|
+
pointerType: PointerType;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface MoveOptions {
|
|
45
|
+
onMoveStart?: (event: MoveStartEvent) => void;
|
|
46
|
+
onMove?: (event: MoveMoveEvent) => void;
|
|
47
|
+
onMoveEnd?: (event: MoveEndEvent) => void;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface MoveResult {
|
|
51
|
+
moveProps: DOMProps;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function modifiers(event: Modifiers): Modifiers {
|
|
55
|
+
return {
|
|
56
|
+
shiftKey: event.shiftKey,
|
|
57
|
+
ctrlKey: event.ctrlKey,
|
|
58
|
+
metaKey: event.metaKey,
|
|
59
|
+
altKey: event.altKey,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Move handling for one element.
|
|
65
|
+
*
|
|
66
|
+
* ```tsx
|
|
67
|
+
* const { moveProps } = move({ onMove: (e) => value.update((v) => v + e.deltaX) });
|
|
68
|
+
* <div {...moveProps} role="slider" tabIndex={0} />
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export function move(options: MoveOptions): MoveResult {
|
|
72
|
+
const listeners = globalListeners();
|
|
73
|
+
const state = {
|
|
74
|
+
didMove: false,
|
|
75
|
+
last: null as { pageX: number; pageY: number } | null,
|
|
76
|
+
id: null as number | null,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const start = (): void => {
|
|
80
|
+
disableTextSelection();
|
|
81
|
+
state.didMove = false;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const emit = (
|
|
85
|
+
event: Modifiers,
|
|
86
|
+
pointerType: PointerType,
|
|
87
|
+
deltaX: number,
|
|
88
|
+
deltaY: number,
|
|
89
|
+
): void => {
|
|
90
|
+
if (deltaX === 0 && deltaY === 0) return;
|
|
91
|
+
|
|
92
|
+
if (!state.didMove) {
|
|
93
|
+
state.didMove = true;
|
|
94
|
+
options.onMoveStart?.({ type: "movestart", pointerType, ...modifiers(event) });
|
|
95
|
+
}
|
|
96
|
+
options.onMove?.({ type: "move", pointerType, deltaX, deltaY, ...modifiers(event) });
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const end = (event: Modifiers, pointerType: PointerType): void => {
|
|
100
|
+
restoreTextSelection();
|
|
101
|
+
if (state.didMove) options.onMoveEnd?.({ type: "moveend", pointerType, ...modifiers(event) });
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const moveProps: DOMProps = {};
|
|
105
|
+
|
|
106
|
+
if (typeof PointerEvent !== "undefined") {
|
|
107
|
+
const onPointerMove = (raw: Event): void => {
|
|
108
|
+
const event = raw as PointerEvent;
|
|
109
|
+
if (event.pointerId !== state.id) return;
|
|
110
|
+
const pointerType = (event.pointerType || "mouse") as PointerType;
|
|
111
|
+
emit(
|
|
112
|
+
event,
|
|
113
|
+
pointerType,
|
|
114
|
+
event.pageX - (state.last?.pageX ?? 0),
|
|
115
|
+
event.pageY - (state.last?.pageY ?? 0),
|
|
116
|
+
);
|
|
117
|
+
state.last = { pageX: event.pageX, pageY: event.pageY };
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const onPointerUp = (raw: Event): void => {
|
|
121
|
+
const event = raw as PointerEvent;
|
|
122
|
+
if (event.pointerId !== state.id) return;
|
|
123
|
+
end(event, (event.pointerType || "mouse") as PointerType);
|
|
124
|
+
state.id = null;
|
|
125
|
+
listeners.removeAll();
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
moveProps.onPointerDown = (event: PointerEvent): void => {
|
|
129
|
+
if (event.button !== 0 || state.id !== null) return;
|
|
130
|
+
start();
|
|
131
|
+
event.stopPropagation();
|
|
132
|
+
event.preventDefault();
|
|
133
|
+
state.last = { pageX: event.pageX, pageY: event.pageY };
|
|
134
|
+
state.id = event.pointerId;
|
|
135
|
+
|
|
136
|
+
const view = ownerWindow(targetElement(event));
|
|
137
|
+
listeners.add(view, "pointermove", onPointerMove, false);
|
|
138
|
+
listeners.add(view, "pointerup", onPointerUp, false);
|
|
139
|
+
listeners.add(view, "pointercancel", onPointerUp, false);
|
|
140
|
+
};
|
|
141
|
+
} else {
|
|
142
|
+
const onMouseMove = (raw: Event): void => {
|
|
143
|
+
const event = raw as MouseEvent;
|
|
144
|
+
if (event.button !== 0) return;
|
|
145
|
+
emit(
|
|
146
|
+
event,
|
|
147
|
+
"mouse",
|
|
148
|
+
event.pageX - (state.last?.pageX ?? 0),
|
|
149
|
+
event.pageY - (state.last?.pageY ?? 0),
|
|
150
|
+
);
|
|
151
|
+
state.last = { pageX: event.pageX, pageY: event.pageY };
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const onMouseUp = (raw: Event): void => {
|
|
155
|
+
const event = raw as MouseEvent;
|
|
156
|
+
if (event.button !== 0) return;
|
|
157
|
+
end(event, "mouse");
|
|
158
|
+
listeners.removeAll();
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
moveProps.onMouseDown = (event: MouseEvent): void => {
|
|
162
|
+
if (event.button !== 0) return;
|
|
163
|
+
start();
|
|
164
|
+
event.stopPropagation();
|
|
165
|
+
event.preventDefault();
|
|
166
|
+
state.last = { pageX: event.pageX, pageY: event.pageY };
|
|
167
|
+
|
|
168
|
+
const view = ownerWindow(targetElement(event));
|
|
169
|
+
listeners.add(view, "mousemove", onMouseMove, false);
|
|
170
|
+
listeners.add(view, "mouseup", onMouseUp, false);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const findTouch = (event: TouchEvent): Touch | undefined => {
|
|
174
|
+
for (let i = 0; i < event.changedTouches.length; i++) {
|
|
175
|
+
const touch = event.changedTouches[i];
|
|
176
|
+
if (touch !== undefined && touch.identifier === state.id) return touch;
|
|
177
|
+
}
|
|
178
|
+
return undefined;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const onTouchMove = (raw: Event): void => {
|
|
182
|
+
const event = raw as TouchEvent;
|
|
183
|
+
const touch = findTouch(event);
|
|
184
|
+
if (touch === undefined) return;
|
|
185
|
+
emit(
|
|
186
|
+
event,
|
|
187
|
+
"touch",
|
|
188
|
+
touch.pageX - (state.last?.pageX ?? 0),
|
|
189
|
+
touch.pageY - (state.last?.pageY ?? 0),
|
|
190
|
+
);
|
|
191
|
+
state.last = { pageX: touch.pageX, pageY: touch.pageY };
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const onTouchEnd = (raw: Event): void => {
|
|
195
|
+
const event = raw as TouchEvent;
|
|
196
|
+
if (findTouch(event) === undefined) return;
|
|
197
|
+
end(event, "touch");
|
|
198
|
+
state.id = null;
|
|
199
|
+
listeners.removeAll();
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
moveProps.onTouchStart = (event: TouchEvent): void => {
|
|
203
|
+
const touch = event.changedTouches[0];
|
|
204
|
+
if (touch === undefined || state.id !== null) return;
|
|
205
|
+
|
|
206
|
+
start();
|
|
207
|
+
event.stopPropagation();
|
|
208
|
+
event.preventDefault();
|
|
209
|
+
state.last = { pageX: touch.pageX, pageY: touch.pageY };
|
|
210
|
+
state.id = touch.identifier;
|
|
211
|
+
|
|
212
|
+
const view = ownerWindow(targetElement(event));
|
|
213
|
+
listeners.add(view, "touchmove", onTouchMove, false);
|
|
214
|
+
listeners.add(view, "touchend", onTouchEnd, false);
|
|
215
|
+
listeners.add(view, "touchcancel", onTouchEnd, false);
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const keyboardMove = (event: KeyboardEvent, deltaX: number, deltaY: number): void => {
|
|
220
|
+
start();
|
|
221
|
+
emit(event, "keyboard", deltaX, deltaY);
|
|
222
|
+
end(event, "keyboard");
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
moveProps.onKeyDown = (event: KeyboardEvent): void => {
|
|
226
|
+
switch (event.key) {
|
|
227
|
+
case "Left":
|
|
228
|
+
case "ArrowLeft":
|
|
229
|
+
event.preventDefault();
|
|
230
|
+
event.stopPropagation();
|
|
231
|
+
keyboardMove(event, -1, 0);
|
|
232
|
+
break;
|
|
233
|
+
case "Right":
|
|
234
|
+
case "ArrowRight":
|
|
235
|
+
event.preventDefault();
|
|
236
|
+
event.stopPropagation();
|
|
237
|
+
keyboardMove(event, 1, 0);
|
|
238
|
+
break;
|
|
239
|
+
case "Up":
|
|
240
|
+
case "ArrowUp":
|
|
241
|
+
event.preventDefault();
|
|
242
|
+
event.stopPropagation();
|
|
243
|
+
keyboardMove(event, 0, -1);
|
|
244
|
+
break;
|
|
245
|
+
case "Down":
|
|
246
|
+
case "ArrowDown":
|
|
247
|
+
event.preventDefault();
|
|
248
|
+
event.stopPropagation();
|
|
249
|
+
keyboardMove(event, 0, 1);
|
|
250
|
+
break;
|
|
251
|
+
default:
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
return { moveProps };
|
|
257
|
+
}
|