motion-dom 11.13.0-beta.0 → 11.14.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/.turbo/turbo-build.log +9 -9
- package/dist/cjs/index.js +229 -14
- package/dist/es/gestures/drag/state/is-active.mjs +9 -0
- package/dist/es/gestures/drag/state/set-active.mjs +28 -0
- package/dist/es/gestures/hover.mjs +32 -17
- package/dist/es/gestures/press/index.mjs +75 -0
- package/dist/es/gestures/press/utils/is-keyboard-accessible.mjs +12 -0
- package/dist/es/gestures/press/utils/keyboard.mjs +38 -0
- package/dist/es/gestures/press/utils/state.mjs +3 -0
- package/dist/es/gestures/utils/is-node-or-child.mjs +20 -0
- package/dist/es/gestures/utils/is-primary-pointer.mjs +18 -0
- package/dist/es/gestures/utils/setup.mjs +15 -0
- package/dist/es/index.mjs +5 -0
- package/dist/index.d.ts +92 -2
- package/lib/gestures/drag/state/is-active.js +8 -0
- package/lib/gestures/drag/state/is-active.js.map +1 -0
- package/lib/gestures/drag/state/set-active.js +26 -0
- package/lib/gestures/drag/state/set-active.js.map +1 -0
- package/lib/gestures/hover.js +32 -17
- package/lib/gestures/hover.js.map +1 -1
- package/lib/gestures/press/index.js +73 -0
- package/lib/gestures/press/index.js.map +1 -0
- package/lib/gestures/press/types.js +2 -0
- package/lib/gestures/press/types.js.map +1 -0
- package/lib/gestures/press/utils/is-keyboard-accessible.js +11 -0
- package/lib/gestures/press/utils/is-keyboard-accessible.js.map +1 -0
- package/lib/gestures/press/utils/keyboard.js +36 -0
- package/lib/gestures/press/utils/keyboard.js.map +1 -0
- package/lib/gestures/press/utils/state.js +2 -0
- package/lib/gestures/press/utils/state.js.map +1 -0
- package/lib/gestures/types.js +2 -0
- package/lib/gestures/types.js.map +1 -0
- package/lib/gestures/utils/is-node-or-child.js +19 -0
- package/lib/gestures/utils/is-node-or-child.js.map +1 -0
- package/lib/gestures/utils/is-primary-pointer.js +17 -0
- package/lib/gestures/utils/is-primary-pointer.js.map +1 -0
- package/lib/gestures/utils/setup.js +13 -0
- package/lib/gestures/utils/setup.js.map +1 -0
- package/lib/index.js +7 -0
- package/lib/index.js.map +1 -1
- package/package.json +2 -2
- package/src/gestures/drag/state/is-active.ts +8 -0
- package/src/gestures/drag/state/set-active.ts +23 -0
- package/src/gestures/hover.ts +54 -20
- package/src/gestures/press/index.ts +109 -0
- package/src/gestures/press/types.ts +10 -0
- package/src/gestures/press/utils/is-keyboard-accessible.ts +11 -0
- package/src/gestures/press/utils/keyboard.ts +51 -0
- package/src/gestures/press/utils/state.ts +1 -0
- package/src/gestures/types.ts +21 -0
- package/src/gestures/utils/__tests__/is-primary-pointer.test.ts +48 -0
- package/src/gestures/utils/is-node-or-child.ts +19 -0
- package/src/gestures/utils/is-primary-pointer.ts +15 -0
- package/src/gestures/utils/setup.ts +23 -0
- package/src/index.ts +8 -0
- package/types/gestures/drag/state/is-active.d.ts +5 -0
- package/types/gestures/drag/state/set-active.d.ts +1 -0
- package/types/gestures/hover.d.ts +22 -1
- package/types/gestures/press/index.d.ts +26 -0
- package/types/gestures/press/types.d.ts +5 -0
- package/types/gestures/press/utils/is-keyboard-accessible.d.ts +1 -0
- package/types/gestures/press/utils/keyboard.d.ts +1 -0
- package/types/gestures/press/utils/state.d.ts +1 -0
- package/types/gestures/types.d.ts +20 -0
- package/types/gestures/utils/is-node-or-child.d.ts +8 -0
- package/types/gestures/utils/is-primary-pointer.d.ts +1 -0
- package/types/gestures/utils/setup.d.ts +3 -0
- package/types/index.d.ts +7 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
[36m
|
|
2
|
-
[1mlib/index.js[22m → [1mdist/cjs[22m...[39m
|
|
3
|
-
[32mcreated [1mdist/cjs[22m in [
|
|
4
|
-
[36m
|
|
5
|
-
[1mlib/index.js[22m → [1mdist/es[22m...[39m
|
|
6
|
-
[32mcreated [1mdist/es[22m in [
|
|
7
|
-
[36m
|
|
8
|
-
[1mtypes/index.d.ts[22m → [1mdist/index.d.ts[22m...[39m
|
|
9
|
-
[32mcreated [1mdist/index.d.ts[22m in [
|
|
1
|
+
[36m
|
|
2
|
+
[1mlib/index.js[22m → [1mdist/cjs[22m...[39m
|
|
3
|
+
[32mcreated [1mdist/cjs[22m in [1m34ms[22m[39m
|
|
4
|
+
[36m
|
|
5
|
+
[1mlib/index.js[22m → [1mdist/es[22m...[39m
|
|
6
|
+
[32mcreated [1mdist/es[22m in [1m20ms[22m[39m
|
|
7
|
+
[36m
|
|
8
|
+
[1mtypes/index.d.ts[22m → [1mdist/index.d.ts[22m...[39m
|
|
9
|
+
[32mcreated [1mdist/index.d.ts[22m in [1m25ms[22m[39m
|
package/dist/cjs/index.js
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
const isDragging = {
|
|
6
|
+
x: false,
|
|
7
|
+
y: false,
|
|
8
|
+
};
|
|
9
|
+
function isDragActive() {
|
|
10
|
+
return isDragging.x || isDragging.y;
|
|
11
|
+
}
|
|
12
|
+
|
|
5
13
|
function resolveElements(elementOrSelector, scope, selectorCache) {
|
|
6
14
|
var _a;
|
|
7
15
|
if (elementOrSelector instanceof Element) {
|
|
@@ -23,28 +31,235 @@ function resolveElements(elementOrSelector, scope, selectorCache) {
|
|
|
23
31
|
return Array.from(elementOrSelector);
|
|
24
32
|
}
|
|
25
33
|
|
|
26
|
-
function
|
|
34
|
+
function setupGesture(elementOrSelector, options) {
|
|
27
35
|
const elements = resolveElements(elementOrSelector);
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
36
|
+
const gestureAbortController = new AbortController();
|
|
37
|
+
const eventOptions = {
|
|
38
|
+
passive: true,
|
|
39
|
+
...options,
|
|
40
|
+
signal: gestureAbortController.signal,
|
|
41
|
+
};
|
|
42
|
+
const cancel = () => gestureAbortController.abort();
|
|
43
|
+
return [elements, eventOptions, cancel];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Filter out events that are not pointer events, or are triggering
|
|
48
|
+
* while a Motion gesture is active.
|
|
49
|
+
*/
|
|
50
|
+
function filterEvents$1(callback) {
|
|
51
|
+
return (event) => {
|
|
52
|
+
if (event.pointerType === "touch" || isDragActive())
|
|
32
53
|
return;
|
|
54
|
+
callback(event);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Create a hover gesture. hover() is different to .addEventListener("pointerenter")
|
|
59
|
+
* in that it has an easier syntax, filters out polyfilled touch events, interoperates
|
|
60
|
+
* with drag gestures, and automatically removes the "pointerennd" event listener when the hover ends.
|
|
61
|
+
*
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
function hover(elementOrSelector, onHoverStart, options = {}) {
|
|
65
|
+
const [elements, eventOptions, cancel] = setupGesture(elementOrSelector, options);
|
|
66
|
+
const onPointerEnter = filterEvents$1((enterEvent) => {
|
|
33
67
|
const { target } = enterEvent;
|
|
34
68
|
const onHoverEnd = onHoverStart(enterEvent);
|
|
35
|
-
if (onHoverEnd
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
69
|
+
if (!onHoverEnd || !target)
|
|
70
|
+
return;
|
|
71
|
+
const onPointerLeave = filterEvents$1((leaveEvent) => {
|
|
72
|
+
onHoverEnd(leaveEvent);
|
|
73
|
+
target.removeEventListener("pointerleave", onPointerLeave);
|
|
74
|
+
});
|
|
75
|
+
target.addEventListener("pointerleave", onPointerLeave, eventOptions);
|
|
76
|
+
});
|
|
77
|
+
elements.forEach((element) => {
|
|
78
|
+
element.addEventListener("pointerenter", onPointerEnter, eventOptions);
|
|
79
|
+
});
|
|
80
|
+
return cancel;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const isPrimaryPointer = (event) => {
|
|
84
|
+
if (event.pointerType === "mouse") {
|
|
85
|
+
return typeof event.button !== "number" || event.button <= 0;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
/**
|
|
89
|
+
* isPrimary is true for all mice buttons, whereas every touch point
|
|
90
|
+
* is regarded as its own input. So subsequent concurrent touch points
|
|
91
|
+
* will be false.
|
|
92
|
+
*
|
|
93
|
+
* Specifically match against false here as incomplete versions of
|
|
94
|
+
* PointerEvents in very old browser might have it set as undefined.
|
|
95
|
+
*/
|
|
96
|
+
return event.isPrimary !== false;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const isPressing = new WeakSet();
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Filter out events that are not "Enter" keys.
|
|
104
|
+
*/
|
|
105
|
+
function filterEvents(callback) {
|
|
106
|
+
return (event) => {
|
|
107
|
+
if (event.key !== "Enter")
|
|
108
|
+
return;
|
|
109
|
+
callback(event);
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function firePointerEvent(target, type) {
|
|
113
|
+
target.dispatchEvent(new PointerEvent("pointer" + type, { isPrimary: true, bubbles: true }));
|
|
114
|
+
}
|
|
115
|
+
const enableKeyboardPress = (focusEvent, eventOptions) => {
|
|
116
|
+
const element = focusEvent.currentTarget;
|
|
117
|
+
if (!element)
|
|
118
|
+
return;
|
|
119
|
+
const handleKeydown = filterEvents(() => {
|
|
120
|
+
if (isPressing.has(element))
|
|
121
|
+
return;
|
|
122
|
+
firePointerEvent(element, "down");
|
|
123
|
+
const handleKeyup = filterEvents(() => {
|
|
124
|
+
firePointerEvent(element, "up");
|
|
125
|
+
});
|
|
126
|
+
const handleBlur = () => firePointerEvent(element, "cancel");
|
|
127
|
+
element.addEventListener("keyup", handleKeyup, eventOptions);
|
|
128
|
+
element.addEventListener("blur", handleBlur, eventOptions);
|
|
129
|
+
});
|
|
130
|
+
element.addEventListener("keydown", handleKeydown, eventOptions);
|
|
131
|
+
/**
|
|
132
|
+
* Add an event listener that fires on blur to remove the keydown events.
|
|
133
|
+
*/
|
|
134
|
+
element.addEventListener("blur", () => element.removeEventListener("keydown", handleKeydown), eventOptions);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const focusableElements = new Set([
|
|
138
|
+
"BUTTON",
|
|
139
|
+
"INPUT",
|
|
140
|
+
"SELECT",
|
|
141
|
+
"TEXTAREA",
|
|
142
|
+
"A",
|
|
143
|
+
]);
|
|
144
|
+
function isElementKeyboardAccessible(element) {
|
|
145
|
+
return focusableElements.has(element.tagName) || element.tabIndex !== -1;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Recursively traverse up the tree to check whether the provided child node
|
|
150
|
+
* is the parent or a descendant of it.
|
|
151
|
+
*
|
|
152
|
+
* @param parent - Element to find
|
|
153
|
+
* @param child - Element to test against parent
|
|
154
|
+
*/
|
|
155
|
+
const isNodeOrChild = (parent, child) => {
|
|
156
|
+
if (!child) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
else if (parent === child) {
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
return isNodeOrChild(parent, child.parentElement);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Filter out events that are not primary pointer events, or are triggering
|
|
169
|
+
* while a Motion gesture is active.
|
|
170
|
+
*/
|
|
171
|
+
function isValidPressEvent(event) {
|
|
172
|
+
return isPrimaryPointer(event) && !isDragActive();
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Create a press gesture.
|
|
176
|
+
*
|
|
177
|
+
* Press is different to `"pointerdown"`, `"pointerup"` in that it
|
|
178
|
+
* automatically filters out secondary pointer events like right
|
|
179
|
+
* click and multitouch.
|
|
180
|
+
*
|
|
181
|
+
* It also adds accessibility support for keyboards, where
|
|
182
|
+
* an element with a press gesture will receive focus and
|
|
183
|
+
* trigger on Enter `"keydown"` and `"keyup"` events.
|
|
184
|
+
*
|
|
185
|
+
* This is different to a browser's `"click"` event, which does
|
|
186
|
+
* respond to keyboards but only for the `"click"` itself, rather
|
|
187
|
+
* than the press start and end/cancel. The element also needs
|
|
188
|
+
* to be focusable for this to work, whereas a press gesture will
|
|
189
|
+
* make an element focusable by default.
|
|
190
|
+
*
|
|
191
|
+
* @public
|
|
192
|
+
*/
|
|
193
|
+
function press(elementOrSelector, onPressStart, options = {}) {
|
|
194
|
+
const [elements, eventOptions, cancelEvents] = setupGesture(elementOrSelector, options);
|
|
195
|
+
const startPress = (startEvent) => {
|
|
196
|
+
const element = startEvent.currentTarget;
|
|
197
|
+
if (!isValidPressEvent(startEvent) || isPressing.has(element))
|
|
198
|
+
return;
|
|
199
|
+
isPressing.add(element);
|
|
200
|
+
const onPressEnd = onPressStart(startEvent);
|
|
201
|
+
const onPointerEnd = (endEvent, success) => {
|
|
202
|
+
window.removeEventListener("pointerup", onPointerUp);
|
|
203
|
+
window.removeEventListener("pointercancel", onPointerCancel);
|
|
204
|
+
if (!isValidPressEvent(endEvent) || !isPressing.has(element)) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
isPressing.delete(element);
|
|
208
|
+
if (onPressEnd) {
|
|
209
|
+
onPressEnd(endEvent, { success });
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
const onPointerUp = (upEvent) => {
|
|
213
|
+
onPointerEnd(upEvent, options.useGlobalTarget ||
|
|
214
|
+
isNodeOrChild(element, upEvent.target));
|
|
215
|
+
};
|
|
216
|
+
const onPointerCancel = (cancelEvent) => {
|
|
217
|
+
onPointerEnd(cancelEvent, false);
|
|
218
|
+
};
|
|
219
|
+
window.addEventListener("pointerup", onPointerUp, eventOptions);
|
|
220
|
+
window.addEventListener("pointercancel", onPointerCancel, eventOptions);
|
|
42
221
|
};
|
|
43
222
|
elements.forEach((element) => {
|
|
44
|
-
element
|
|
223
|
+
if (!isElementKeyboardAccessible(element)) {
|
|
224
|
+
element.tabIndex = 0;
|
|
225
|
+
}
|
|
226
|
+
const target = options.useGlobalTarget ? window : element;
|
|
227
|
+
target.addEventListener("pointerdown", startPress, eventOptions);
|
|
228
|
+
element.addEventListener("focus", (event) => enableKeyboardPress(event, eventOptions), eventOptions);
|
|
45
229
|
});
|
|
46
|
-
return
|
|
230
|
+
return cancelEvents;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function setDragLock(axis) {
|
|
234
|
+
if (axis === "x" || axis === "y") {
|
|
235
|
+
if (isDragging[axis]) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
isDragging[axis] = true;
|
|
240
|
+
return () => {
|
|
241
|
+
isDragging[axis] = false;
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
if (isDragging.x || isDragging.y) {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
isDragging.x = isDragging.y = true;
|
|
251
|
+
return () => {
|
|
252
|
+
isDragging.x = isDragging.y = false;
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
}
|
|
47
256
|
}
|
|
48
257
|
|
|
49
258
|
exports.hover = hover;
|
|
259
|
+
exports.isDragActive = isDragActive;
|
|
260
|
+
exports.isDragging = isDragging;
|
|
261
|
+
exports.isNodeOrChild = isNodeOrChild;
|
|
262
|
+
exports.isPrimaryPointer = isPrimaryPointer;
|
|
263
|
+
exports.press = press;
|
|
50
264
|
exports.resolveElements = resolveElements;
|
|
265
|
+
exports.setDragLock = setDragLock;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { isDragging } from './is-active.mjs';
|
|
2
|
+
|
|
3
|
+
function setDragLock(axis) {
|
|
4
|
+
if (axis === "x" || axis === "y") {
|
|
5
|
+
if (isDragging[axis]) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
isDragging[axis] = true;
|
|
10
|
+
return () => {
|
|
11
|
+
isDragging[axis] = false;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
if (isDragging.x || isDragging.y) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
isDragging.x = isDragging.y = true;
|
|
21
|
+
return () => {
|
|
22
|
+
isDragging.x = isDragging.y = false;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { setDragLock };
|
|
@@ -1,26 +1,41 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isDragActive } from './drag/state/is-active.mjs';
|
|
2
|
+
import { setupGesture } from './utils/setup.mjs';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Filter out events that are not pointer events, or are triggering
|
|
6
|
+
* while a Motion gesture is active.
|
|
7
|
+
*/
|
|
8
|
+
function filterEvents(callback) {
|
|
9
|
+
return (event) => {
|
|
10
|
+
if (event.pointerType === "touch" || isDragActive())
|
|
9
11
|
return;
|
|
12
|
+
callback(event);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Create a hover gesture. hover() is different to .addEventListener("pointerenter")
|
|
17
|
+
* in that it has an easier syntax, filters out polyfilled touch events, interoperates
|
|
18
|
+
* with drag gestures, and automatically removes the "pointerennd" event listener when the hover ends.
|
|
19
|
+
*
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
function hover(elementOrSelector, onHoverStart, options = {}) {
|
|
23
|
+
const [elements, eventOptions, cancel] = setupGesture(elementOrSelector, options);
|
|
24
|
+
const onPointerEnter = filterEvents((enterEvent) => {
|
|
10
25
|
const { target } = enterEvent;
|
|
11
26
|
const onHoverEnd = onHoverStart(enterEvent);
|
|
12
|
-
if (onHoverEnd
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
27
|
+
if (!onHoverEnd || !target)
|
|
28
|
+
return;
|
|
29
|
+
const onPointerLeave = filterEvents((leaveEvent) => {
|
|
30
|
+
onHoverEnd(leaveEvent);
|
|
31
|
+
target.removeEventListener("pointerleave", onPointerLeave);
|
|
32
|
+
});
|
|
33
|
+
target.addEventListener("pointerleave", onPointerLeave, eventOptions);
|
|
34
|
+
});
|
|
20
35
|
elements.forEach((element) => {
|
|
21
|
-
element.addEventListener("pointerenter", onPointerEnter,
|
|
36
|
+
element.addEventListener("pointerenter", onPointerEnter, eventOptions);
|
|
22
37
|
});
|
|
23
|
-
return
|
|
38
|
+
return cancel;
|
|
24
39
|
}
|
|
25
40
|
|
|
26
41
|
export { hover };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { isDragActive } from '../drag/state/is-active.mjs';
|
|
2
|
+
import { isPrimaryPointer } from '../utils/is-primary-pointer.mjs';
|
|
3
|
+
import { setupGesture } from '../utils/setup.mjs';
|
|
4
|
+
import { enableKeyboardPress } from './utils/keyboard.mjs';
|
|
5
|
+
import { isElementKeyboardAccessible } from './utils/is-keyboard-accessible.mjs';
|
|
6
|
+
import { isNodeOrChild } from '../utils/is-node-or-child.mjs';
|
|
7
|
+
import { isPressing } from './utils/state.mjs';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Filter out events that are not primary pointer events, or are triggering
|
|
11
|
+
* while a Motion gesture is active.
|
|
12
|
+
*/
|
|
13
|
+
function isValidPressEvent(event) {
|
|
14
|
+
return isPrimaryPointer(event) && !isDragActive();
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create a press gesture.
|
|
18
|
+
*
|
|
19
|
+
* Press is different to `"pointerdown"`, `"pointerup"` in that it
|
|
20
|
+
* automatically filters out secondary pointer events like right
|
|
21
|
+
* click and multitouch.
|
|
22
|
+
*
|
|
23
|
+
* It also adds accessibility support for keyboards, where
|
|
24
|
+
* an element with a press gesture will receive focus and
|
|
25
|
+
* trigger on Enter `"keydown"` and `"keyup"` events.
|
|
26
|
+
*
|
|
27
|
+
* This is different to a browser's `"click"` event, which does
|
|
28
|
+
* respond to keyboards but only for the `"click"` itself, rather
|
|
29
|
+
* than the press start and end/cancel. The element also needs
|
|
30
|
+
* to be focusable for this to work, whereas a press gesture will
|
|
31
|
+
* make an element focusable by default.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
function press(elementOrSelector, onPressStart, options = {}) {
|
|
36
|
+
const [elements, eventOptions, cancelEvents] = setupGesture(elementOrSelector, options);
|
|
37
|
+
const startPress = (startEvent) => {
|
|
38
|
+
const element = startEvent.currentTarget;
|
|
39
|
+
if (!isValidPressEvent(startEvent) || isPressing.has(element))
|
|
40
|
+
return;
|
|
41
|
+
isPressing.add(element);
|
|
42
|
+
const onPressEnd = onPressStart(startEvent);
|
|
43
|
+
const onPointerEnd = (endEvent, success) => {
|
|
44
|
+
window.removeEventListener("pointerup", onPointerUp);
|
|
45
|
+
window.removeEventListener("pointercancel", onPointerCancel);
|
|
46
|
+
if (!isValidPressEvent(endEvent) || !isPressing.has(element)) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
isPressing.delete(element);
|
|
50
|
+
if (onPressEnd) {
|
|
51
|
+
onPressEnd(endEvent, { success });
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const onPointerUp = (upEvent) => {
|
|
55
|
+
onPointerEnd(upEvent, options.useGlobalTarget ||
|
|
56
|
+
isNodeOrChild(element, upEvent.target));
|
|
57
|
+
};
|
|
58
|
+
const onPointerCancel = (cancelEvent) => {
|
|
59
|
+
onPointerEnd(cancelEvent, false);
|
|
60
|
+
};
|
|
61
|
+
window.addEventListener("pointerup", onPointerUp, eventOptions);
|
|
62
|
+
window.addEventListener("pointercancel", onPointerCancel, eventOptions);
|
|
63
|
+
};
|
|
64
|
+
elements.forEach((element) => {
|
|
65
|
+
if (!isElementKeyboardAccessible(element)) {
|
|
66
|
+
element.tabIndex = 0;
|
|
67
|
+
}
|
|
68
|
+
const target = options.useGlobalTarget ? window : element;
|
|
69
|
+
target.addEventListener("pointerdown", startPress, eventOptions);
|
|
70
|
+
element.addEventListener("focus", (event) => enableKeyboardPress(event, eventOptions), eventOptions);
|
|
71
|
+
});
|
|
72
|
+
return cancelEvents;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { press };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const focusableElements = new Set([
|
|
2
|
+
"BUTTON",
|
|
3
|
+
"INPUT",
|
|
4
|
+
"SELECT",
|
|
5
|
+
"TEXTAREA",
|
|
6
|
+
"A",
|
|
7
|
+
]);
|
|
8
|
+
function isElementKeyboardAccessible(element) {
|
|
9
|
+
return focusableElements.has(element.tagName) || element.tabIndex !== -1;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { isElementKeyboardAccessible };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { isPressing } from './state.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Filter out events that are not "Enter" keys.
|
|
5
|
+
*/
|
|
6
|
+
function filterEvents(callback) {
|
|
7
|
+
return (event) => {
|
|
8
|
+
if (event.key !== "Enter")
|
|
9
|
+
return;
|
|
10
|
+
callback(event);
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function firePointerEvent(target, type) {
|
|
14
|
+
target.dispatchEvent(new PointerEvent("pointer" + type, { isPrimary: true, bubbles: true }));
|
|
15
|
+
}
|
|
16
|
+
const enableKeyboardPress = (focusEvent, eventOptions) => {
|
|
17
|
+
const element = focusEvent.currentTarget;
|
|
18
|
+
if (!element)
|
|
19
|
+
return;
|
|
20
|
+
const handleKeydown = filterEvents(() => {
|
|
21
|
+
if (isPressing.has(element))
|
|
22
|
+
return;
|
|
23
|
+
firePointerEvent(element, "down");
|
|
24
|
+
const handleKeyup = filterEvents(() => {
|
|
25
|
+
firePointerEvent(element, "up");
|
|
26
|
+
});
|
|
27
|
+
const handleBlur = () => firePointerEvent(element, "cancel");
|
|
28
|
+
element.addEventListener("keyup", handleKeyup, eventOptions);
|
|
29
|
+
element.addEventListener("blur", handleBlur, eventOptions);
|
|
30
|
+
});
|
|
31
|
+
element.addEventListener("keydown", handleKeydown, eventOptions);
|
|
32
|
+
/**
|
|
33
|
+
* Add an event listener that fires on blur to remove the keydown events.
|
|
34
|
+
*/
|
|
35
|
+
element.addEventListener("blur", () => element.removeEventListener("keydown", handleKeydown), eventOptions);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export { enableKeyboardPress };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively traverse up the tree to check whether the provided child node
|
|
3
|
+
* is the parent or a descendant of it.
|
|
4
|
+
*
|
|
5
|
+
* @param parent - Element to find
|
|
6
|
+
* @param child - Element to test against parent
|
|
7
|
+
*/
|
|
8
|
+
const isNodeOrChild = (parent, child) => {
|
|
9
|
+
if (!child) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
else if (parent === child) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
return isNodeOrChild(parent, child.parentElement);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export { isNodeOrChild };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const isPrimaryPointer = (event) => {
|
|
2
|
+
if (event.pointerType === "mouse") {
|
|
3
|
+
return typeof event.button !== "number" || event.button <= 0;
|
|
4
|
+
}
|
|
5
|
+
else {
|
|
6
|
+
/**
|
|
7
|
+
* isPrimary is true for all mice buttons, whereas every touch point
|
|
8
|
+
* is regarded as its own input. So subsequent concurrent touch points
|
|
9
|
+
* will be false.
|
|
10
|
+
*
|
|
11
|
+
* Specifically match against false here as incomplete versions of
|
|
12
|
+
* PointerEvents in very old browser might have it set as undefined.
|
|
13
|
+
*/
|
|
14
|
+
return event.isPrimary !== false;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { isPrimaryPointer };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { resolveElements } from '../../utils/resolve-elements.mjs';
|
|
2
|
+
|
|
3
|
+
function setupGesture(elementOrSelector, options) {
|
|
4
|
+
const elements = resolveElements(elementOrSelector);
|
|
5
|
+
const gestureAbortController = new AbortController();
|
|
6
|
+
const eventOptions = {
|
|
7
|
+
passive: true,
|
|
8
|
+
...options,
|
|
9
|
+
signal: gestureAbortController.signal,
|
|
10
|
+
};
|
|
11
|
+
const cancel = () => gestureAbortController.abort();
|
|
12
|
+
return [elements, eventOptions, cancel];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { setupGesture };
|
package/dist/es/index.mjs
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
1
|
export { hover } from './gestures/hover.mjs';
|
|
2
|
+
export { press } from './gestures/press/index.mjs';
|
|
2
3
|
export { resolveElements } from './utils/resolve-elements.mjs';
|
|
4
|
+
export { isDragActive, isDragging } from './gestures/drag/state/is-active.mjs';
|
|
5
|
+
export { setDragLock } from './gestures/drag/state/set-active.mjs';
|
|
6
|
+
export { isPrimaryPointer } from './gestures/utils/is-primary-pointer.mjs';
|
|
7
|
+
export { isNodeOrChild } from './gestures/utils/is-node-or-child.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -11,8 +11,98 @@ interface SelectorCache {
|
|
|
11
11
|
}
|
|
12
12
|
declare function resolveElements(elementOrSelector: ElementOrSelector, scope?: AnimationScope, selectorCache?: SelectorCache): Element[];
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Options for the hover gesture.
|
|
16
|
+
*
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
interface EventOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Use passive event listeners. Doing so allows the browser to optimize
|
|
22
|
+
* scrolling performance by not allowing the use of `preventDefault()`.
|
|
23
|
+
*
|
|
24
|
+
* @default true
|
|
25
|
+
*/
|
|
26
|
+
passive?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Remove the event listener after the first event.
|
|
29
|
+
*
|
|
30
|
+
* @default false
|
|
31
|
+
*/
|
|
32
|
+
once?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A function to be called when a hover gesture starts.
|
|
37
|
+
*
|
|
38
|
+
* This function can optionally return a function that will be called
|
|
39
|
+
* when the hover gesture ends.
|
|
40
|
+
*
|
|
41
|
+
* @public
|
|
42
|
+
*/
|
|
14
43
|
type OnHoverStartEvent = (event: PointerEvent) => void | OnHoverEndEvent;
|
|
44
|
+
/**
|
|
45
|
+
* A function to be called when a hover gesture ends.
|
|
46
|
+
*
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
15
49
|
type OnHoverEndEvent = (event: PointerEvent) => void;
|
|
16
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Create a hover gesture. hover() is different to .addEventListener("pointerenter")
|
|
52
|
+
* in that it has an easier syntax, filters out polyfilled touch events, interoperates
|
|
53
|
+
* with drag gestures, and automatically removes the "pointerennd" event listener when the hover ends.
|
|
54
|
+
*
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
57
|
+
declare function hover(elementOrSelector: ElementOrSelector, onHoverStart: OnHoverStartEvent, options?: EventOptions): VoidFunction;
|
|
58
|
+
|
|
59
|
+
interface PressGestureInfo {
|
|
60
|
+
success: boolean;
|
|
61
|
+
}
|
|
62
|
+
type OnPressEndEvent = (event: PointerEvent, info: PressGestureInfo) => void;
|
|
63
|
+
type OnPressStartEvent = (event: PointerEvent) => OnPressEndEvent | void;
|
|
64
|
+
|
|
65
|
+
interface PointerEventOptions extends EventOptions {
|
|
66
|
+
useGlobalTarget?: boolean;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Create a press gesture.
|
|
70
|
+
*
|
|
71
|
+
* Press is different to `"pointerdown"`, `"pointerup"` in that it
|
|
72
|
+
* automatically filters out secondary pointer events like right
|
|
73
|
+
* click and multitouch.
|
|
74
|
+
*
|
|
75
|
+
* It also adds accessibility support for keyboards, where
|
|
76
|
+
* an element with a press gesture will receive focus and
|
|
77
|
+
* trigger on Enter `"keydown"` and `"keyup"` events.
|
|
78
|
+
*
|
|
79
|
+
* This is different to a browser's `"click"` event, which does
|
|
80
|
+
* respond to keyboards but only for the `"click"` itself, rather
|
|
81
|
+
* than the press start and end/cancel. The element also needs
|
|
82
|
+
* to be focusable for this to work, whereas a press gesture will
|
|
83
|
+
* make an element focusable by default.
|
|
84
|
+
*
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
declare function press(elementOrSelector: ElementOrSelector, onPressStart: OnPressStartEvent, options?: PointerEventOptions): VoidFunction;
|
|
88
|
+
|
|
89
|
+
declare const isDragging: {
|
|
90
|
+
x: boolean;
|
|
91
|
+
y: boolean;
|
|
92
|
+
};
|
|
93
|
+
declare function isDragActive(): boolean;
|
|
94
|
+
|
|
95
|
+
declare function setDragLock(axis: boolean | "x" | "y" | "lockDirection"): (() => void) | null;
|
|
96
|
+
|
|
97
|
+
declare const isPrimaryPointer: (event: PointerEvent) => boolean;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Recursively traverse up the tree to check whether the provided child node
|
|
101
|
+
* is the parent or a descendant of it.
|
|
102
|
+
*
|
|
103
|
+
* @param parent - Element to find
|
|
104
|
+
* @param child - Element to test against parent
|
|
105
|
+
*/
|
|
106
|
+
declare const isNodeOrChild: (parent: Element | null, child?: Element | null) => boolean;
|
|
17
107
|
|
|
18
|
-
export { type AnimationScope, type ElementOrSelector, type OnHoverEndEvent, type OnHoverStartEvent, type SelectorCache, type WithQuerySelectorAll, hover, resolveElements };
|
|
108
|
+
export { type AnimationScope, type ElementOrSelector, type EventOptions, type OnHoverEndEvent, type OnHoverStartEvent, type OnPressEndEvent, type OnPressStartEvent, type PointerEventOptions, type PressGestureInfo, type SelectorCache, type WithQuerySelectorAll, hover, isDragActive, isDragging, isNodeOrChild, isPrimaryPointer, press, resolveElements, setDragLock };
|