motion-dom 11.13.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.
Files changed (58) hide show
  1. package/.turbo/turbo-build.log +3 -3
  2. package/dist/cjs/index.js +177 -17
  3. package/dist/es/gestures/hover.mjs +4 -9
  4. package/dist/es/gestures/press/index.mjs +75 -0
  5. package/dist/es/gestures/press/utils/is-keyboard-accessible.mjs +12 -0
  6. package/dist/es/gestures/press/utils/keyboard.mjs +38 -0
  7. package/dist/es/gestures/press/utils/state.mjs +3 -0
  8. package/dist/es/gestures/utils/is-node-or-child.mjs +20 -0
  9. package/dist/es/gestures/utils/is-primary-pointer.mjs +18 -0
  10. package/dist/es/gestures/utils/setup.mjs +15 -0
  11. package/dist/es/index.mjs +3 -0
  12. package/dist/index.d.ts +45 -3
  13. package/lib/gestures/hover.js +4 -9
  14. package/lib/gestures/hover.js.map +1 -1
  15. package/lib/gestures/press/index.js +73 -0
  16. package/lib/gestures/press/index.js.map +1 -0
  17. package/lib/gestures/press/types.js +2 -0
  18. package/lib/gestures/press/types.js.map +1 -0
  19. package/lib/gestures/press/utils/is-keyboard-accessible.js +11 -0
  20. package/lib/gestures/press/utils/is-keyboard-accessible.js.map +1 -0
  21. package/lib/gestures/press/utils/keyboard.js +36 -0
  22. package/lib/gestures/press/utils/keyboard.js.map +1 -0
  23. package/lib/gestures/press/utils/state.js +2 -0
  24. package/lib/gestures/press/utils/state.js.map +1 -0
  25. package/lib/gestures/types.js +2 -0
  26. package/lib/gestures/types.js.map +1 -0
  27. package/lib/gestures/utils/is-node-or-child.js +19 -0
  28. package/lib/gestures/utils/is-node-or-child.js.map +1 -0
  29. package/lib/gestures/utils/is-primary-pointer.js +17 -0
  30. package/lib/gestures/utils/is-primary-pointer.js.map +1 -0
  31. package/lib/gestures/utils/setup.js +13 -0
  32. package/lib/gestures/utils/setup.js.map +1 -0
  33. package/lib/index.js +5 -0
  34. package/lib/index.js.map +1 -1
  35. package/package.json +2 -2
  36. package/src/gestures/hover.ts +11 -34
  37. package/src/gestures/press/index.ts +109 -0
  38. package/src/gestures/press/types.ts +10 -0
  39. package/src/gestures/press/utils/is-keyboard-accessible.ts +11 -0
  40. package/src/gestures/press/utils/keyboard.ts +51 -0
  41. package/src/gestures/press/utils/state.ts +1 -0
  42. package/src/gestures/types.ts +21 -0
  43. package/src/gestures/utils/__tests__/is-primary-pointer.test.ts +48 -0
  44. package/src/gestures/utils/is-node-or-child.ts +19 -0
  45. package/src/gestures/utils/is-primary-pointer.ts +15 -0
  46. package/src/gestures/utils/setup.ts +23 -0
  47. package/src/index.ts +5 -0
  48. package/types/gestures/hover.d.ts +2 -21
  49. package/types/gestures/press/index.d.ts +26 -0
  50. package/types/gestures/press/types.d.ts +5 -0
  51. package/types/gestures/press/utils/is-keyboard-accessible.d.ts +1 -0
  52. package/types/gestures/press/utils/keyboard.d.ts +1 -0
  53. package/types/gestures/press/utils/state.d.ts +1 -0
  54. package/types/gestures/types.d.ts +20 -0
  55. package/types/gestures/utils/is-node-or-child.d.ts +8 -0
  56. package/types/gestures/utils/is-primary-pointer.d.ts +1 -0
  57. package/types/gestures/utils/setup.d.ts +3 -0
  58. package/types/index.d.ts +5 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/gestures/press/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAEtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAA;AAEtD,OAAO,EAAE,2BAA2B,EAAE,MAAM,gCAAgC,CAAA;AAC5E,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAE1C;;;GAG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C,OAAO,EAAE,OAAO,CAAC,MAAiB,CAAC,CACxD,CAAA;QACL,CAAC,CAAA;QAED,MAAM,eAAe,GAAG,CAAC,WAAyB,EAAE,EAAE;YAClD,YAAY,CAAC,WAAW,EAAE,KAAK,CAAC,CAAA;QACpC,CAAC,CAAA;QAED,MAAM,CAAC,gBAAgB,CAAC,WAAW,EAAE,WAAW,EAAE,YAAY,CAAC,CAAA;QAC/D,MAAM,CAAC,gBAAgB,CAAC,eAAe,EAAE,eAAe,EAAE,YAAY,CAAC,CAAA;IAC3E,CAAC,CAAA;IAED,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAoB,EAAE,EAAE;QACtC,IAAI,CAAC,2BAA2B,CAAC,OAAO,CAAC,EAAE,CAAC;YACxC,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAA;QACxB,CAAC;QAED,MAAM,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAA;QACzD,MAAM,CAAC,gBAAgB,CAAC,aAAa,EAAE,UAAU,EAAE,YAAY,CAAC,CAAA;QAEhE,OAAO,CAAC,gBAAgB,CACpB,OAAO,EACP,CAAC,KAAK,EAAE,EAAE,CAAC,mBAAmB,CAAC,KAAK,EAAE,YAAY,CAAC,EACnD,YAAY,CACf,CAAA;IACL,CAAC,CAAC,CAAA;IAEF,OAAO,YAAY,CAAA;AACvB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/gestures/press/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,11 @@
1
+ const focusableElements = new Set([
2
+ "BUTTON",
3
+ "INPUT",
4
+ "SELECT",
5
+ "TEXTAREA",
6
+ "A",
7
+ ]);
8
+ export function isElementKeyboardAccessible(element) {
9
+ return focusableElements.has(element.tagName) || element.tabIndex !== -1;
10
+ }
11
+ //# sourceMappingURL=is-keyboard-accessible.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-keyboard-accessible.js","sourceRoot":"","sources":["../../../../src/gestures/press/utils/is-keyboard-accessible.ts"],"names":[],"mappings":"AAAA,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC;IAC9B,QAAQ;IACR,OAAO;IACP,QAAQ;IACR,UAAU;IACV,GAAG;CACN,CAAC,CAAA;AAEF,MAAM,UAAU,2BAA2B,CAAC,OAAoB;IAC5D,OAAO,iBAAiB,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,QAAQ,KAAK,CAAC,CAAC,CAAA;AAC5E,CAAC"}
@@ -0,0 +1,36 @@
1
+ import { isPressing } from "./state";
2
+ /**
3
+ * Filter out events that are not "Enter" keys.
4
+ */
5
+ function filterEvents(callback) {
6
+ return (event) => {
7
+ if (event.key !== "Enter")
8
+ return;
9
+ callback(event);
10
+ };
11
+ }
12
+ function firePointerEvent(target, type) {
13
+ target.dispatchEvent(new PointerEvent("pointer" + type, { isPrimary: true, bubbles: true }));
14
+ }
15
+ export const enableKeyboardPress = (focusEvent, eventOptions) => {
16
+ const element = focusEvent.currentTarget;
17
+ if (!element)
18
+ return;
19
+ const handleKeydown = filterEvents(() => {
20
+ if (isPressing.has(element))
21
+ return;
22
+ firePointerEvent(element, "down");
23
+ const handleKeyup = filterEvents(() => {
24
+ firePointerEvent(element, "up");
25
+ });
26
+ const handleBlur = () => firePointerEvent(element, "cancel");
27
+ element.addEventListener("keyup", handleKeyup, eventOptions);
28
+ element.addEventListener("blur", handleBlur, eventOptions);
29
+ });
30
+ element.addEventListener("keydown", handleKeydown, eventOptions);
31
+ /**
32
+ * Add an event listener that fires on blur to remove the keydown events.
33
+ */
34
+ element.addEventListener("blur", () => element.removeEventListener("keydown", handleKeydown), eventOptions);
35
+ };
36
+ //# sourceMappingURL=keyboard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keyboard.js","sourceRoot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,gBAAgB,CAAC,SAAS,EAAE,aAAa,EAAE,YAAY,CAAC,CAAA;IAEhE;;OAEG;IACH,OAAO,CAAC,gBAAgB,CACpB,MAAM,EACN,GAAG,EAAE,CAAC,OAAO,CAAC,mBAAmB,CAAC,SAAS,EAAE,aAAa,CAAC,EAC3D,YAAY,CACf,CAAA;AACL,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ export const isPressing = new WeakSet();
2
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../../../../src/gestures/press/utils/state.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,OAAO,EAAW,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/gestures/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,19 @@
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
+ export 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
+ //# sourceMappingURL=is-node-or-child.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-node-or-child.js","sourceRoot":"","sources":["../../../src/gestures/utils/is-node-or-child.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CACzB,MAAsB,EACtB,KAAsB,EACf,EAAE;IACT,IAAI,CAAC,KAAK,EAAE,CAAC;QACT,OAAO,KAAK,CAAA;IAChB,CAAC;SAAM,IAAI,MAAM,KAAK,KAAK,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAA;IACf,CAAC;SAAM,CAAC;QACJ,OAAO,aAAa,CAAC,MAAM,EAAE,KAAK,CAAC,aAAa,CAAC,CAAA;IACrD,CAAC;AACL,CAAC,CAAA"}
@@ -0,0 +1,17 @@
1
+ export 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
+ //# sourceMappingURL=is-primary-pointer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-primary-pointer.js","sourceRoot":"","sources":["../../../src/gestures/utils/is-primary-pointer.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,KAAmB,EAAE,EAAE;IACpD,IAAI,KAAK,CAAC,WAAW,KAAK,OAAO,EAAE,CAAC;QAChC,OAAO,OAAO,KAAK,CAAC,MAAM,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,IAAI,CAAC,CAAA;IAChE,CAAC;SAAM,CAAC;QACJ;;;;;;;WAOG;QACH,OAAO,KAAK,CAAC,SAAS,KAAK,KAAK,CAAA;IACpC,CAAC;AACL,CAAC,CAAA"}
@@ -0,0 +1,13 @@
1
+ import { resolveElements, } from "../../utils/resolve-elements";
2
+ export function setupGesture(elementOrSelector, options) {
3
+ const elements = resolveElements(elementOrSelector);
4
+ const gestureAbortController = new AbortController();
5
+ const eventOptions = {
6
+ passive: true,
7
+ ...options,
8
+ signal: gestureAbortController.signal,
9
+ };
10
+ const cancel = () => gestureAbortController.abort();
11
+ return [elements, eventOptions, cancel];
12
+ }
13
+ //# sourceMappingURL=setup.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setup.js","sourceRoot":"","sources":["../../../src/gestures/utils/setup.ts"],"names":[],"mappings":"AAAA,OAAO,EAEH,eAAe,GAClB,MAAM,8BAA8B,CAAA;AAErC,MAAM,UAAU,YAAY,CACxB,iBAAoC,EACpC,OAAqB;IAErB,MAAM,QAAQ,GAAG,eAAe,CAAC,iBAAiB,CAAC,CAAA;IAEnD,MAAM,sBAAsB,GAAG,IAAI,eAAe,EAAE,CAAA;IAEpD,MAAM,YAAY,GAAG;QACjB,OAAO,EAAE,IAAI;QACb,GAAG,OAAO;QACV,MAAM,EAAE,sBAAsB,CAAC,MAAM;KACxC,CAAA;IAED,MAAM,MAAM,GAAG,GAAG,EAAE,CAAC,sBAAsB,CAAC,KAAK,EAAE,CAAA;IAEnD,OAAO,CAAC,QAAQ,EAAE,YAAY,EAAE,MAAM,CAAC,CAAA;AAC3C,CAAC"}
package/lib/index.js CHANGED
@@ -1,5 +1,10 @@
1
1
  export * from "./gestures/hover";
2
+ export * from "./gestures/press";
3
+ export * from "./gestures/press/types";
4
+ export * from "./gestures/types";
2
5
  export * from "./utils/resolve-elements";
3
6
  export * from "./gestures/drag/state/is-active";
4
7
  export * from "./gestures/drag/state/set-active";
8
+ export * from "./gestures/utils/is-primary-pointer";
9
+ export * from "./gestures/utils/is-node-or-child";
5
10
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAEhC,cAAc,0BAA0B,CAAA;AACxC,cAAc,iCAAiC,CAAA;AAC/C,cAAc,kCAAkC,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,wBAAwB,CAAA;AACtC,cAAc,kBAAkB,CAAA;AAEhC,cAAc,0BAA0B,CAAA;AACxC,cAAc,iCAAiC,CAAA;AAC/C,cAAc,kCAAkC,CAAA;AAChD,cAAc,qCAAqC,CAAA;AACnD,cAAc,mCAAmC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "motion-dom",
3
- "version": "11.13.0",
3
+ "version": "11.14.0",
4
4
  "main": "./lib/index.js",
5
5
  "types": "./types/index.d.ts",
6
6
  "module": "./lib/index.js",
@@ -23,5 +23,5 @@
23
23
  "build": "yarn clean && tsc -p . && rollup -c",
24
24
  "dev": "concurrently -c blue,red -n tsc,rollup --kill-others \"tsc --watch -p . --preserveWatchOutput\" \"rollup --config --watch --no-watch.clearScreen\""
25
25
  },
26
- "gitHead": "c6ea900aecfec3e946a0f33db4672e0194f3048b"
26
+ "gitHead": "9d50c4ee2443802c9ca392d8f58db0146ab83983"
27
27
  }
@@ -1,27 +1,7 @@
1
- import { ElementOrSelector, resolveElements } from "../utils/resolve-elements"
1
+ import { ElementOrSelector } from "../utils/resolve-elements"
2
2
  import { isDragActive } from "./drag/state/is-active"
3
-
4
- /**
5
- * Options for the hover gesture.
6
- *
7
- * @public
8
- */
9
- export interface HoverOptions {
10
- /**
11
- * Use passive event listeners. Doing so allows the browser to optimize
12
- * scrolling performance by not allowing the use of `preventDefault()`.
13
- *
14
- * @default true
15
- */
16
- passive?: boolean
17
-
18
- /**
19
- * Remove the event listener after the first event.
20
- *
21
- * @default false
22
- */
23
- once?: boolean
24
- }
3
+ import { EventOptions } from "./types"
4
+ import { setupGesture } from "./utils/setup"
25
5
 
26
6
  /**
27
7
  * A function to be called when a hover gesture starts.
@@ -61,15 +41,12 @@ function filterEvents(callback: OnHoverStartEvent) {
61
41
  export function hover(
62
42
  elementOrSelector: ElementOrSelector,
63
43
  onHoverStart: OnHoverStartEvent,
64
- options: HoverOptions = {}
65
- ) {
66
- const gestureAbortController = new AbortController()
67
-
68
- const eventOptions = {
69
- passive: true,
70
- ...options,
71
- signal: gestureAbortController.signal,
72
- }
44
+ options: EventOptions = {}
45
+ ): VoidFunction {
46
+ const [elements, eventOptions, cancel] = setupGesture(
47
+ elementOrSelector,
48
+ options
49
+ )
73
50
 
74
51
  const onPointerEnter = filterEvents((enterEvent: PointerEvent) => {
75
52
  const { target } = enterEvent
@@ -85,9 +62,9 @@ export function hover(
85
62
  target.addEventListener("pointerleave", onPointerLeave, eventOptions)
86
63
  })
87
64
 
88
- resolveElements(elementOrSelector).forEach((element) => {
65
+ elements.forEach((element) => {
89
66
  element.addEventListener("pointerenter", onPointerEnter, eventOptions)
90
67
  })
91
68
 
92
- return () => gestureAbortController.abort()
69
+ return cancel
93
70
  }
@@ -0,0 +1,109 @@
1
+ import { ElementOrSelector } from "../../utils/resolve-elements"
2
+ import { isDragActive } from "../drag/state/is-active"
3
+ import { EventOptions } from "../types"
4
+ import { isPrimaryPointer } from "../utils/is-primary-pointer"
5
+ import { setupGesture } from "../utils/setup"
6
+ import { enableKeyboardPress } from "./utils/keyboard"
7
+ import { OnPressStartEvent } from "./types"
8
+ import { isElementKeyboardAccessible } from "./utils/is-keyboard-accessible"
9
+ import { isNodeOrChild } from "../utils/is-node-or-child"
10
+ import { isPressing } from "./utils/state"
11
+
12
+ /**
13
+ * Filter out events that are not primary pointer events, or are triggering
14
+ * while a Motion gesture is active.
15
+ */
16
+ function isValidPressEvent(event: PointerEvent) {
17
+ return isPrimaryPointer(event) && !isDragActive()
18
+ }
19
+
20
+ export interface PointerEventOptions extends EventOptions {
21
+ useGlobalTarget?: boolean
22
+ }
23
+
24
+ /**
25
+ * Create a press gesture.
26
+ *
27
+ * Press is different to `"pointerdown"`, `"pointerup"` in that it
28
+ * automatically filters out secondary pointer events like right
29
+ * click and multitouch.
30
+ *
31
+ * It also adds accessibility support for keyboards, where
32
+ * an element with a press gesture will receive focus and
33
+ * trigger on Enter `"keydown"` and `"keyup"` events.
34
+ *
35
+ * This is different to a browser's `"click"` event, which does
36
+ * respond to keyboards but only for the `"click"` itself, rather
37
+ * than the press start and end/cancel. The element also needs
38
+ * to be focusable for this to work, whereas a press gesture will
39
+ * make an element focusable by default.
40
+ *
41
+ * @public
42
+ */
43
+ export function press(
44
+ elementOrSelector: ElementOrSelector,
45
+ onPressStart: OnPressStartEvent,
46
+ options: PointerEventOptions = {}
47
+ ): VoidFunction {
48
+ const [elements, eventOptions, cancelEvents] = setupGesture(
49
+ elementOrSelector,
50
+ options
51
+ )
52
+
53
+ const startPress = (startEvent: PointerEvent) => {
54
+ const element = startEvent.currentTarget as Element
55
+
56
+ if (!isValidPressEvent(startEvent) || isPressing.has(element)) return
57
+
58
+ isPressing.add(element)
59
+
60
+ const onPressEnd = onPressStart(startEvent)
61
+
62
+ const onPointerEnd = (endEvent: PointerEvent, success: boolean) => {
63
+ window.removeEventListener("pointerup", onPointerUp)
64
+ window.removeEventListener("pointercancel", onPointerCancel)
65
+
66
+ if (!isValidPressEvent(endEvent) || !isPressing.has(element)) {
67
+ return
68
+ }
69
+
70
+ isPressing.delete(element)
71
+
72
+ if (onPressEnd) {
73
+ onPressEnd(endEvent, { success })
74
+ }
75
+ }
76
+
77
+ const onPointerUp = (upEvent: PointerEvent) => {
78
+ onPointerEnd(
79
+ upEvent,
80
+ options.useGlobalTarget ||
81
+ isNodeOrChild(element, upEvent.target as Element)
82
+ )
83
+ }
84
+
85
+ const onPointerCancel = (cancelEvent: PointerEvent) => {
86
+ onPointerEnd(cancelEvent, false)
87
+ }
88
+
89
+ window.addEventListener("pointerup", onPointerUp, eventOptions)
90
+ window.addEventListener("pointercancel", onPointerCancel, eventOptions)
91
+ }
92
+
93
+ elements.forEach((element: HTMLElement) => {
94
+ if (!isElementKeyboardAccessible(element)) {
95
+ element.tabIndex = 0
96
+ }
97
+
98
+ const target = options.useGlobalTarget ? window : element
99
+ target.addEventListener("pointerdown", startPress, eventOptions)
100
+
101
+ element.addEventListener(
102
+ "focus",
103
+ (event) => enableKeyboardPress(event, eventOptions),
104
+ eventOptions
105
+ )
106
+ })
107
+
108
+ return cancelEvents
109
+ }
@@ -0,0 +1,10 @@
1
+ export interface PressGestureInfo {
2
+ success: boolean
3
+ }
4
+
5
+ export type OnPressEndEvent = (
6
+ event: PointerEvent,
7
+ info: PressGestureInfo
8
+ ) => void
9
+
10
+ export type OnPressStartEvent = (event: PointerEvent) => OnPressEndEvent | void
@@ -0,0 +1,11 @@
1
+ const focusableElements = new Set([
2
+ "BUTTON",
3
+ "INPUT",
4
+ "SELECT",
5
+ "TEXTAREA",
6
+ "A",
7
+ ])
8
+
9
+ export function isElementKeyboardAccessible(element: HTMLElement) {
10
+ return focusableElements.has(element.tagName) || element.tabIndex !== -1
11
+ }
@@ -0,0 +1,51 @@
1
+ import { isPressing } from "./state"
2
+
3
+ /**
4
+ * Filter out events that are not "Enter" keys.
5
+ */
6
+ function filterEvents(callback: (event: KeyboardEvent) => void) {
7
+ return (event: KeyboardEvent) => {
8
+ if (event.key !== "Enter") return
9
+ callback(event)
10
+ }
11
+ }
12
+
13
+ function firePointerEvent(target: EventTarget, type: "down" | "up" | "cancel") {
14
+ target.dispatchEvent(
15
+ new PointerEvent("pointer" + type, { isPrimary: true, bubbles: true })
16
+ )
17
+ }
18
+
19
+ export const enableKeyboardPress = (
20
+ focusEvent: FocusEvent,
21
+ eventOptions: AddEventListenerOptions
22
+ ) => {
23
+ const element = focusEvent.currentTarget as HTMLElement
24
+ if (!element) return
25
+
26
+ const handleKeydown = filterEvents(() => {
27
+ if (isPressing.has(element)) return
28
+
29
+ firePointerEvent(element, "down")
30
+
31
+ const handleKeyup = filterEvents(() => {
32
+ firePointerEvent(element, "up")
33
+ })
34
+
35
+ const handleBlur = () => firePointerEvent(element, "cancel")
36
+
37
+ element.addEventListener("keyup", handleKeyup, eventOptions)
38
+ element.addEventListener("blur", handleBlur, eventOptions)
39
+ })
40
+
41
+ element.addEventListener("keydown", handleKeydown, eventOptions)
42
+
43
+ /**
44
+ * Add an event listener that fires on blur to remove the keydown events.
45
+ */
46
+ element.addEventListener(
47
+ "blur",
48
+ () => element.removeEventListener("keydown", handleKeydown),
49
+ eventOptions
50
+ )
51
+ }
@@ -0,0 +1 @@
1
+ export const isPressing = new WeakSet<Element>()
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Options for the hover gesture.
3
+ *
4
+ * @public
5
+ */
6
+ export interface EventOptions {
7
+ /**
8
+ * Use passive event listeners. Doing so allows the browser to optimize
9
+ * scrolling performance by not allowing the use of `preventDefault()`.
10
+ *
11
+ * @default true
12
+ */
13
+ passive?: boolean
14
+
15
+ /**
16
+ * Remove the event listener after the first event.
17
+ *
18
+ * @default false
19
+ */
20
+ once?: boolean
21
+ }
@@ -0,0 +1,48 @@
1
+ import { isPrimaryPointer } from "../is-primary-pointer"
2
+
3
+ describe("isPrimaryPointer", () => {
4
+ test("Identifies primary pointer", () => {
5
+ expect(
6
+ isPrimaryPointer({
7
+ button: 1,
8
+ isPrimary: true,
9
+ pointerType: "mouse",
10
+ } as any)
11
+ ).toEqual(false)
12
+ expect(
13
+ isPrimaryPointer({
14
+ button: 1,
15
+ isPrimary: true,
16
+ pointerType: "touch",
17
+ } as any)
18
+ ).toEqual(true)
19
+ expect(
20
+ isPrimaryPointer({
21
+ button: 0,
22
+ isPrimary: true,
23
+ pointerType: "mouse",
24
+ } as any)
25
+ ).toEqual(true)
26
+ expect(
27
+ isPrimaryPointer({
28
+ button: -1,
29
+ isPrimary: false,
30
+ pointerType: "mouse",
31
+ } as any)
32
+ ).toEqual(true)
33
+ expect(
34
+ isPrimaryPointer({
35
+ button: undefined,
36
+ isPrimary: false,
37
+ pointerType: "mouse",
38
+ } as any)
39
+ ).toEqual(true)
40
+ expect(
41
+ isPrimaryPointer({
42
+ button: null,
43
+ isPrimary: false,
44
+ pointerType: "mouse",
45
+ } as any)
46
+ ).toEqual(true)
47
+ })
48
+ })
@@ -0,0 +1,19 @@
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
+ export const isNodeOrChild = (
9
+ parent: Element | null,
10
+ child?: Element | null
11
+ ): boolean => {
12
+ if (!child) {
13
+ return false
14
+ } else if (parent === child) {
15
+ return true
16
+ } else {
17
+ return isNodeOrChild(parent, child.parentElement)
18
+ }
19
+ }
@@ -0,0 +1,15 @@
1
+ export const isPrimaryPointer = (event: PointerEvent) => {
2
+ if (event.pointerType === "mouse") {
3
+ return typeof event.button !== "number" || event.button <= 0
4
+ } else {
5
+ /**
6
+ * isPrimary is true for all mice buttons, whereas every touch point
7
+ * is regarded as its own input. So subsequent concurrent touch points
8
+ * will be false.
9
+ *
10
+ * Specifically match against false here as incomplete versions of
11
+ * PointerEvents in very old browser might have it set as undefined.
12
+ */
13
+ return event.isPrimary !== false
14
+ }
15
+ }
@@ -0,0 +1,23 @@
1
+ import {
2
+ ElementOrSelector,
3
+ resolveElements,
4
+ } from "../../utils/resolve-elements"
5
+ import { EventOptions } from "../types"
6
+ export function setupGesture(
7
+ elementOrSelector: ElementOrSelector,
8
+ options: EventOptions
9
+ ): [Element[], AddEventListenerOptions, VoidFunction] {
10
+ const elements = resolveElements(elementOrSelector)
11
+
12
+ const gestureAbortController = new AbortController()
13
+
14
+ const eventOptions = {
15
+ passive: true,
16
+ ...options,
17
+ signal: gestureAbortController.signal,
18
+ }
19
+
20
+ const cancel = () => gestureAbortController.abort()
21
+
22
+ return [elements, eventOptions, cancel]
23
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  export * from "./gestures/hover"
2
+ export * from "./gestures/press"
3
+ export * from "./gestures/press/types"
4
+ export * from "./gestures/types"
2
5
 
3
6
  export * from "./utils/resolve-elements"
4
7
  export * from "./gestures/drag/state/is-active"
5
8
  export * from "./gestures/drag/state/set-active"
9
+ export * from "./gestures/utils/is-primary-pointer"
10
+ export * from "./gestures/utils/is-node-or-child"
@@ -1,24 +1,5 @@
1
1
  import { ElementOrSelector } from "../utils/resolve-elements";
2
- /**
3
- * Options for the hover gesture.
4
- *
5
- * @public
6
- */
7
- export interface HoverOptions {
8
- /**
9
- * Use passive event listeners. Doing so allows the browser to optimize
10
- * scrolling performance by not allowing the use of `preventDefault()`.
11
- *
12
- * @default true
13
- */
14
- passive?: boolean;
15
- /**
16
- * Remove the event listener after the first event.
17
- *
18
- * @default false
19
- */
20
- once?: boolean;
21
- }
2
+ import { EventOptions } from "./types";
22
3
  /**
23
4
  * A function to be called when a hover gesture starts.
24
5
  *
@@ -41,4 +22,4 @@ export type OnHoverEndEvent = (event: PointerEvent) => void;
41
22
  *
42
23
  * @public
43
24
  */
44
- export declare function hover(elementOrSelector: ElementOrSelector, onHoverStart: OnHoverStartEvent, options?: HoverOptions): () => void;
25
+ export declare function hover(elementOrSelector: ElementOrSelector, onHoverStart: OnHoverStartEvent, options?: EventOptions): VoidFunction;
@@ -0,0 +1,26 @@
1
+ import { ElementOrSelector } from "../../utils/resolve-elements";
2
+ import { EventOptions } from "../types";
3
+ import { OnPressStartEvent } from "./types";
4
+ export interface PointerEventOptions extends EventOptions {
5
+ useGlobalTarget?: boolean;
6
+ }
7
+ /**
8
+ * Create a press gesture.
9
+ *
10
+ * Press is different to `"pointerdown"`, `"pointerup"` in that it
11
+ * automatically filters out secondary pointer events like right
12
+ * click and multitouch.
13
+ *
14
+ * It also adds accessibility support for keyboards, where
15
+ * an element with a press gesture will receive focus and
16
+ * trigger on Enter `"keydown"` and `"keyup"` events.
17
+ *
18
+ * This is different to a browser's `"click"` event, which does
19
+ * respond to keyboards but only for the `"click"` itself, rather
20
+ * than the press start and end/cancel. The element also needs
21
+ * to be focusable for this to work, whereas a press gesture will
22
+ * make an element focusable by default.
23
+ *
24
+ * @public
25
+ */
26
+ export declare function press(elementOrSelector: ElementOrSelector, onPressStart: OnPressStartEvent, options?: PointerEventOptions): VoidFunction;
@@ -0,0 +1,5 @@
1
+ export interface PressGestureInfo {
2
+ success: boolean;
3
+ }
4
+ export type OnPressEndEvent = (event: PointerEvent, info: PressGestureInfo) => void;
5
+ export type OnPressStartEvent = (event: PointerEvent) => OnPressEndEvent | void;
@@ -0,0 +1 @@
1
+ export declare function isElementKeyboardAccessible(element: HTMLElement): boolean;
@@ -0,0 +1 @@
1
+ export declare const enableKeyboardPress: (focusEvent: FocusEvent, eventOptions: AddEventListenerOptions) => void;