@react-aria/interactions 3.0.0-nightly-641446f65-240905

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 (109) hide show
  1. package/README.md +3 -0
  2. package/dist/PressResponder.main.js +62 -0
  3. package/dist/PressResponder.main.js.map +1 -0
  4. package/dist/PressResponder.mjs +52 -0
  5. package/dist/PressResponder.module.js +52 -0
  6. package/dist/PressResponder.module.js.map +1 -0
  7. package/dist/Pressable.main.js +43 -0
  8. package/dist/Pressable.main.js.map +1 -0
  9. package/dist/Pressable.mjs +34 -0
  10. package/dist/Pressable.module.js +34 -0
  11. package/dist/Pressable.module.js.map +1 -0
  12. package/dist/context.main.js +30 -0
  13. package/dist/context.main.js.map +1 -0
  14. package/dist/context.mjs +21 -0
  15. package/dist/context.module.js +21 -0
  16. package/dist/context.module.js.map +1 -0
  17. package/dist/createEventHandler.main.js +42 -0
  18. package/dist/createEventHandler.main.js.map +1 -0
  19. package/dist/createEventHandler.mjs +37 -0
  20. package/dist/createEventHandler.module.js +37 -0
  21. package/dist/createEventHandler.module.js.map +1 -0
  22. package/dist/import.mjs +39 -0
  23. package/dist/main.js +62 -0
  24. package/dist/main.js.map +1 -0
  25. package/dist/module.js +39 -0
  26. package/dist/module.js.map +1 -0
  27. package/dist/textSelection.main.js +77 -0
  28. package/dist/textSelection.main.js.map +1 -0
  29. package/dist/textSelection.mjs +71 -0
  30. package/dist/textSelection.module.js +71 -0
  31. package/dist/textSelection.module.js.map +1 -0
  32. package/dist/types.d.ts +218 -0
  33. package/dist/types.d.ts.map +1 -0
  34. package/dist/useFocus.main.js +64 -0
  35. package/dist/useFocus.main.js.map +1 -0
  36. package/dist/useFocus.mjs +59 -0
  37. package/dist/useFocus.module.js +59 -0
  38. package/dist/useFocus.module.js.map +1 -0
  39. package/dist/useFocusVisible.main.js +248 -0
  40. package/dist/useFocusVisible.main.js.map +1 -0
  41. package/dist/useFocusVisible.mjs +237 -0
  42. package/dist/useFocusVisible.module.js +237 -0
  43. package/dist/useFocusVisible.module.js.map +1 -0
  44. package/dist/useFocusWithin.main.js +76 -0
  45. package/dist/useFocusWithin.main.js.map +1 -0
  46. package/dist/useFocusWithin.mjs +71 -0
  47. package/dist/useFocusWithin.module.js +71 -0
  48. package/dist/useFocusWithin.module.js.map +1 -0
  49. package/dist/useHover.main.js +142 -0
  50. package/dist/useHover.main.js.map +1 -0
  51. package/dist/useHover.mjs +137 -0
  52. package/dist/useHover.module.js +137 -0
  53. package/dist/useHover.module.js.map +1 -0
  54. package/dist/useInteractOutside.main.js +101 -0
  55. package/dist/useInteractOutside.main.js.map +1 -0
  56. package/dist/useInteractOutside.mjs +96 -0
  57. package/dist/useInteractOutside.module.js +96 -0
  58. package/dist/useInteractOutside.module.js.map +1 -0
  59. package/dist/useKeyboard.main.js +30 -0
  60. package/dist/useKeyboard.main.js.map +1 -0
  61. package/dist/useKeyboard.mjs +25 -0
  62. package/dist/useKeyboard.module.js +25 -0
  63. package/dist/useKeyboard.module.js.map +1 -0
  64. package/dist/useLongPress.main.js +84 -0
  65. package/dist/useLongPress.main.js.map +1 -0
  66. package/dist/useLongPress.mjs +79 -0
  67. package/dist/useLongPress.module.js +79 -0
  68. package/dist/useLongPress.module.js.map +1 -0
  69. package/dist/useMove.main.js +236 -0
  70. package/dist/useMove.main.js.map +1 -0
  71. package/dist/useMove.mjs +231 -0
  72. package/dist/useMove.module.js +231 -0
  73. package/dist/useMove.module.js.map +1 -0
  74. package/dist/usePress.main.js +612 -0
  75. package/dist/usePress.main.js.map +1 -0
  76. package/dist/usePress.mjs +607 -0
  77. package/dist/usePress.module.js +607 -0
  78. package/dist/usePress.module.js.map +1 -0
  79. package/dist/useScrollWheel.main.js +41 -0
  80. package/dist/useScrollWheel.main.js.map +1 -0
  81. package/dist/useScrollWheel.mjs +36 -0
  82. package/dist/useScrollWheel.module.js +36 -0
  83. package/dist/useScrollWheel.module.js.map +1 -0
  84. package/dist/utils.main.js +120 -0
  85. package/dist/utils.main.js.map +1 -0
  86. package/dist/utils.mjs +115 -0
  87. package/dist/utils.module.js +115 -0
  88. package/dist/utils.module.js.map +1 -0
  89. package/package.json +37 -0
  90. package/src/DOMPropsContext.ts +39 -0
  91. package/src/DOMPropsResponder.tsx +47 -0
  92. package/src/PressResponder.tsx +64 -0
  93. package/src/Pressable.tsx +31 -0
  94. package/src/context.ts +23 -0
  95. package/src/createEventHandler.ts +48 -0
  96. package/src/index.ts +44 -0
  97. package/src/textSelection.ts +99 -0
  98. package/src/useDOMPropsResponder.ts +27 -0
  99. package/src/useFocus.ts +87 -0
  100. package/src/useFocusVisible.ts +331 -0
  101. package/src/useFocusWithin.ts +103 -0
  102. package/src/useHover.ts +206 -0
  103. package/src/useInteractOutside.ts +134 -0
  104. package/src/useKeyboard.ts +36 -0
  105. package/src/useLongPress.ts +129 -0
  106. package/src/useMove.ts +231 -0
  107. package/src/usePress.ts +955 -0
  108. package/src/useScrollWheel.ts +41 -0
  109. package/src/utils.ts +130 -0
@@ -0,0 +1,134 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ // Portions of the code in this file are based on code from react.
14
+ // Original licensing for the following can be found in the
15
+ // NOTICE file in the root directory of this source tree.
16
+ // See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
17
+
18
+ import {getOwnerDocument, useEffectEvent} from '@react-aria/utils';
19
+ import {RefObject} from '@react-types/shared';
20
+ import {useEffect, useRef} from 'react';
21
+
22
+ export interface InteractOutsideProps {
23
+ ref: RefObject<Element | null>,
24
+ onInteractOutside?: (e: PointerEvent) => void,
25
+ onInteractOutsideStart?: (e: PointerEvent) => void,
26
+ /** Whether the interact outside events should be disabled. */
27
+ isDisabled?: boolean
28
+ }
29
+
30
+ /**
31
+ * Example, used in components like Dialogs and Popovers so they can close
32
+ * when a user clicks outside them.
33
+ */
34
+ export function useInteractOutside(props: InteractOutsideProps) {
35
+ let {ref, onInteractOutside, isDisabled, onInteractOutsideStart} = props;
36
+ let stateRef = useRef({
37
+ isPointerDown: false,
38
+ ignoreEmulatedMouseEvents: false
39
+ });
40
+
41
+ let onPointerDown = useEffectEvent((e) => {
42
+ if (onInteractOutside && isValidEvent(e, ref)) {
43
+ if (onInteractOutsideStart) {
44
+ onInteractOutsideStart(e);
45
+ }
46
+ stateRef.current.isPointerDown = true;
47
+ }
48
+ });
49
+
50
+ let triggerInteractOutside = useEffectEvent((e: PointerEvent) => {
51
+ if (onInteractOutside) {
52
+ onInteractOutside(e);
53
+ }
54
+ });
55
+
56
+ useEffect(() => {
57
+ let state = stateRef.current;
58
+ if (isDisabled) {
59
+ return;
60
+ }
61
+
62
+ const element = ref.current;
63
+ const documentObject = getOwnerDocument(element);
64
+
65
+ // Use pointer events if available. Otherwise, fall back to mouse and touch events.
66
+ if (typeof PointerEvent !== 'undefined') {
67
+ let onPointerUp = (e) => {
68
+ if (state.isPointerDown && isValidEvent(e, ref)) {
69
+ triggerInteractOutside(e);
70
+ }
71
+ state.isPointerDown = false;
72
+ };
73
+
74
+ // changing these to capture phase fixed combobox
75
+ documentObject.addEventListener('pointerdown', onPointerDown, true);
76
+ documentObject.addEventListener('pointerup', onPointerUp, true);
77
+
78
+ return () => {
79
+ documentObject.removeEventListener('pointerdown', onPointerDown, true);
80
+ documentObject.removeEventListener('pointerup', onPointerUp, true);
81
+ };
82
+ } else {
83
+ let onMouseUp = (e) => {
84
+ if (state.ignoreEmulatedMouseEvents) {
85
+ state.ignoreEmulatedMouseEvents = false;
86
+ } else if (state.isPointerDown && isValidEvent(e, ref)) {
87
+ triggerInteractOutside(e);
88
+ }
89
+ state.isPointerDown = false;
90
+ };
91
+
92
+ let onTouchEnd = (e) => {
93
+ state.ignoreEmulatedMouseEvents = true;
94
+ if (state.isPointerDown && isValidEvent(e, ref)) {
95
+ triggerInteractOutside(e);
96
+ }
97
+ state.isPointerDown = false;
98
+ };
99
+
100
+ documentObject.addEventListener('mousedown', onPointerDown, true);
101
+ documentObject.addEventListener('mouseup', onMouseUp, true);
102
+ documentObject.addEventListener('touchstart', onPointerDown, true);
103
+ documentObject.addEventListener('touchend', onTouchEnd, true);
104
+
105
+ return () => {
106
+ documentObject.removeEventListener('mousedown', onPointerDown, true);
107
+ documentObject.removeEventListener('mouseup', onMouseUp, true);
108
+ documentObject.removeEventListener('touchstart', onPointerDown, true);
109
+ documentObject.removeEventListener('touchend', onTouchEnd, true);
110
+ };
111
+ }
112
+ }, [ref, isDisabled, onPointerDown, triggerInteractOutside]);
113
+ }
114
+
115
+ function isValidEvent(event, ref) {
116
+ if (event.button > 0) {
117
+ return false;
118
+ }
119
+
120
+ if (event.target) {
121
+ // if the event target is no longer in the document, ignore
122
+ const ownerDocument = event.target.ownerDocument;
123
+ if (!ownerDocument || !ownerDocument.documentElement.contains(event.target)) {
124
+ return false;
125
+ }
126
+
127
+ // If the target is within a top layer element (e.g. toasts), ignore.
128
+ if (event.target.closest('[data-react-aria-top-layer]')) {
129
+ return false;
130
+ }
131
+ }
132
+
133
+ return ref.current && !ref.current.contains(event.target);
134
+ }
@@ -0,0 +1,36 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {createEventHandler} from './createEventHandler';
14
+ import {DOMAttributes, KeyboardEvents} from '@react-types/shared';
15
+
16
+ export interface KeyboardProps extends KeyboardEvents {
17
+ /** Whether the keyboard events should be disabled. */
18
+ isDisabled?: boolean
19
+ }
20
+
21
+ export interface KeyboardResult {
22
+ /** Props to spread onto the target element. */
23
+ keyboardProps: DOMAttributes
24
+ }
25
+
26
+ /**
27
+ * Handles keyboard interactions for a focusable element.
28
+ */
29
+ export function useKeyboard(props: KeyboardProps): KeyboardResult {
30
+ return {
31
+ keyboardProps: props.isDisabled ? {} : {
32
+ onKeyDown: createEventHandler(props.onKeyDown),
33
+ onKeyUp: createEventHandler(props.onKeyUp)
34
+ }
35
+ };
36
+ }
@@ -0,0 +1,129 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {DOMAttributes, LongPressEvent} from '@react-types/shared';
14
+ import {mergeProps, useDescription, useGlobalListeners} from '@react-aria/utils';
15
+ import {usePress} from './usePress';
16
+ import {useRef} from 'react';
17
+
18
+ export interface LongPressProps {
19
+ /** Whether long press events should be disabled. */
20
+ isDisabled?: boolean,
21
+ /** Handler that is called when a long press interaction starts. */
22
+ onLongPressStart?: (e: LongPressEvent) => void,
23
+ /**
24
+ * Handler that is called when a long press interaction ends, either
25
+ * over the target or when the pointer leaves the target.
26
+ */
27
+ onLongPressEnd?: (e: LongPressEvent) => void,
28
+ /**
29
+ * Handler that is called when the threshold time is met while
30
+ * the press is over the target.
31
+ */
32
+ onLongPress?: (e: LongPressEvent) => void,
33
+ /**
34
+ * The amount of time in milliseconds to wait before triggering a long press.
35
+ * @default 500ms
36
+ */
37
+ threshold?: number,
38
+ /**
39
+ * A description for assistive techology users indicating that a long press
40
+ * action is available, e.g. "Long press to open menu".
41
+ */
42
+ accessibilityDescription?: string
43
+ }
44
+
45
+ export interface LongPressResult {
46
+ /** Props to spread on the target element. */
47
+ longPressProps: DOMAttributes
48
+ }
49
+
50
+ const DEFAULT_THRESHOLD = 500;
51
+
52
+ /**
53
+ * Handles long press interactions across mouse and touch devices. Supports a customizable time threshold,
54
+ * accessibility description, and normalizes behavior across browsers and devices.
55
+ */
56
+ export function useLongPress(props: LongPressProps): LongPressResult {
57
+ let {
58
+ isDisabled,
59
+ onLongPressStart,
60
+ onLongPressEnd,
61
+ onLongPress,
62
+ threshold = DEFAULT_THRESHOLD,
63
+ accessibilityDescription
64
+ } = props;
65
+
66
+ const timeRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
67
+ let {addGlobalListener, removeGlobalListener} = useGlobalListeners();
68
+
69
+ let {pressProps} = usePress({
70
+ isDisabled,
71
+ onPressStart(e) {
72
+ e.continuePropagation();
73
+ if (e.pointerType === 'mouse' || e.pointerType === 'touch') {
74
+ if (onLongPressStart) {
75
+ onLongPressStart({
76
+ ...e,
77
+ type: 'longpressstart'
78
+ });
79
+ }
80
+
81
+ timeRef.current = setTimeout(() => {
82
+ // Prevent other usePress handlers from also handling this event.
83
+ e.target.dispatchEvent(new PointerEvent('pointercancel', {bubbles: true}));
84
+ if (onLongPress) {
85
+ onLongPress({
86
+ ...e,
87
+ type: 'longpress'
88
+ });
89
+ }
90
+ timeRef.current = undefined;
91
+ }, threshold);
92
+
93
+ // Prevent context menu, which may be opened on long press on touch devices
94
+ if (e.pointerType === 'touch') {
95
+ let onContextMenu = e => {
96
+ e.preventDefault();
97
+ };
98
+
99
+ addGlobalListener(e.target, 'contextmenu', onContextMenu, {once: true});
100
+ addGlobalListener(window, 'pointerup', () => {
101
+ // If no contextmenu event is fired quickly after pointerup, remove the handler
102
+ // so future context menu events outside a long press are not prevented.
103
+ setTimeout(() => {
104
+ removeGlobalListener(e.target, 'contextmenu', onContextMenu);
105
+ }, 30);
106
+ }, {once: true});
107
+ }
108
+ }
109
+ },
110
+ onPressEnd(e) {
111
+ if (timeRef.current) {
112
+ clearTimeout(timeRef.current);
113
+ }
114
+
115
+ if (onLongPressEnd && (e.pointerType === 'mouse' || e.pointerType === 'touch')) {
116
+ onLongPressEnd({
117
+ ...e,
118
+ type: 'longpressend'
119
+ });
120
+ }
121
+ }
122
+ });
123
+
124
+ let descriptionProps = useDescription(onLongPress && !isDisabled ? accessibilityDescription : undefined);
125
+
126
+ return {
127
+ longPressProps: mergeProps(pressProps, descriptionProps)
128
+ };
129
+ }
package/src/useMove.ts ADDED
@@ -0,0 +1,231 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {disableTextSelection, restoreTextSelection} from './textSelection';
14
+ import {DOMAttributes, MoveEvents, PointerType} from '@react-types/shared';
15
+ import React, {useMemo, useRef} from 'react';
16
+ import {useEffectEvent, useGlobalListeners} from '@react-aria/utils';
17
+
18
+ export interface MoveResult {
19
+ /** Props to spread on the target element. */
20
+ moveProps: DOMAttributes
21
+ }
22
+
23
+ interface EventBase {
24
+ shiftKey: boolean,
25
+ ctrlKey: boolean,
26
+ metaKey: boolean,
27
+ altKey: boolean
28
+ }
29
+
30
+ /**
31
+ * Handles move interactions across mouse, touch, and keyboard, including dragging with
32
+ * the mouse or touch, and using the arrow keys. Normalizes behavior across browsers and
33
+ * platforms, and ignores emulated mouse events on touch devices.
34
+ */
35
+ export function useMove(props: MoveEvents): MoveResult {
36
+ let {onMoveStart, onMove, onMoveEnd} = props;
37
+
38
+ let state = useRef<{
39
+ didMove: boolean,
40
+ lastPosition: {pageX: number, pageY: number} | null,
41
+ id: number | null
42
+ }>({didMove: false, lastPosition: null, id: null});
43
+
44
+ let {addGlobalListener, removeGlobalListener} = useGlobalListeners();
45
+
46
+ let move = useEffectEvent((originalEvent: EventBase, pointerType: PointerType, deltaX: number, deltaY: number) => {
47
+ if (deltaX === 0 && deltaY === 0) {
48
+ return;
49
+ }
50
+
51
+ if (!state.current.didMove) {
52
+ state.current.didMove = true;
53
+ onMoveStart?.({
54
+ type: 'movestart',
55
+ pointerType,
56
+ shiftKey: originalEvent.shiftKey,
57
+ metaKey: originalEvent.metaKey,
58
+ ctrlKey: originalEvent.ctrlKey,
59
+ altKey: originalEvent.altKey
60
+ });
61
+ }
62
+ onMove?.({
63
+ type: 'move',
64
+ pointerType,
65
+ deltaX: deltaX,
66
+ deltaY: deltaY,
67
+ shiftKey: originalEvent.shiftKey,
68
+ metaKey: originalEvent.metaKey,
69
+ ctrlKey: originalEvent.ctrlKey,
70
+ altKey: originalEvent.altKey
71
+ });
72
+ });
73
+
74
+ let end = useEffectEvent((originalEvent: EventBase, pointerType: PointerType) => {
75
+ restoreTextSelection();
76
+ if (state.current.didMove) {
77
+ onMoveEnd?.({
78
+ type: 'moveend',
79
+ pointerType,
80
+ shiftKey: originalEvent.shiftKey,
81
+ metaKey: originalEvent.metaKey,
82
+ ctrlKey: originalEvent.ctrlKey,
83
+ altKey: originalEvent.altKey
84
+ });
85
+ }
86
+ });
87
+
88
+ let moveProps = useMemo(() => {
89
+ let moveProps: DOMAttributes = {};
90
+
91
+ let start = () => {
92
+ disableTextSelection();
93
+ state.current.didMove = false;
94
+ };
95
+
96
+ if (typeof PointerEvent === 'undefined') {
97
+ let onMouseMove = (e: MouseEvent) => {
98
+ if (e.button === 0) {
99
+ move(e, 'mouse', e.pageX - (state.current.lastPosition?.pageX ?? 0), e.pageY - (state.current.lastPosition?.pageY ?? 0));
100
+ state.current.lastPosition = {pageX: e.pageX, pageY: e.pageY};
101
+ }
102
+ };
103
+ let onMouseUp = (e: MouseEvent) => {
104
+ if (e.button === 0) {
105
+ end(e, 'mouse');
106
+ removeGlobalListener(window, 'mousemove', onMouseMove, false);
107
+ removeGlobalListener(window, 'mouseup', onMouseUp, false);
108
+ }
109
+ };
110
+ moveProps.onMouseDown = (e: React.MouseEvent) => {
111
+ if (e.button === 0) {
112
+ start();
113
+ e.stopPropagation();
114
+ e.preventDefault();
115
+ state.current.lastPosition = {pageX: e.pageX, pageY: e.pageY};
116
+ addGlobalListener(window, 'mousemove', onMouseMove, false);
117
+ addGlobalListener(window, 'mouseup', onMouseUp, false);
118
+ }
119
+ };
120
+
121
+ let onTouchMove = (e: TouchEvent) => {
122
+ let touch = [...e.changedTouches].findIndex(({identifier}) => identifier === state.current.id);
123
+ if (touch >= 0) {
124
+ let {pageX, pageY} = e.changedTouches[touch];
125
+ move(e, 'touch', pageX - (state.current.lastPosition?.pageX ?? 0), pageY - (state.current.lastPosition?.pageY ?? 0));
126
+ state.current.lastPosition = {pageX, pageY};
127
+ }
128
+ };
129
+ let onTouchEnd = (e: TouchEvent) => {
130
+ let touch = [...e.changedTouches].findIndex(({identifier}) => identifier === state.current.id);
131
+ if (touch >= 0) {
132
+ end(e, 'touch');
133
+ state.current.id = null;
134
+ removeGlobalListener(window, 'touchmove', onTouchMove);
135
+ removeGlobalListener(window, 'touchend', onTouchEnd);
136
+ removeGlobalListener(window, 'touchcancel', onTouchEnd);
137
+ }
138
+ };
139
+ moveProps.onTouchStart = (e: React.TouchEvent) => {
140
+ if (e.changedTouches.length === 0 || state.current.id != null) {
141
+ return;
142
+ }
143
+
144
+ let {pageX, pageY, identifier} = e.changedTouches[0];
145
+ start();
146
+ e.stopPropagation();
147
+ e.preventDefault();
148
+ state.current.lastPosition = {pageX, pageY};
149
+ state.current.id = identifier;
150
+ addGlobalListener(window, 'touchmove', onTouchMove, false);
151
+ addGlobalListener(window, 'touchend', onTouchEnd, false);
152
+ addGlobalListener(window, 'touchcancel', onTouchEnd, false);
153
+ };
154
+ } else {
155
+ let onPointerMove = (e: PointerEvent) => {
156
+ if (e.pointerId === state.current.id) {
157
+ let pointerType = (e.pointerType || 'mouse') as PointerType;
158
+
159
+ // Problems with PointerEvent#movementX/movementY:
160
+ // 1. it is always 0 on macOS Safari.
161
+ // 2. On Chrome Android, it's scaled by devicePixelRatio, but not on Chrome macOS
162
+ move(e, pointerType, e.pageX - (state.current.lastPosition?.pageX ?? 0), e.pageY - (state.current.lastPosition?.pageY ?? 0));
163
+ state.current.lastPosition = {pageX: e.pageX, pageY: e.pageY};
164
+ }
165
+ };
166
+
167
+ let onPointerUp = (e: PointerEvent) => {
168
+ if (e.pointerId === state.current.id) {
169
+ let pointerType = (e.pointerType || 'mouse') as PointerType;
170
+ end(e, pointerType);
171
+ state.current.id = null;
172
+ removeGlobalListener(window, 'pointermove', onPointerMove, false);
173
+ removeGlobalListener(window, 'pointerup', onPointerUp, false);
174
+ removeGlobalListener(window, 'pointercancel', onPointerUp, false);
175
+ }
176
+ };
177
+
178
+ moveProps.onPointerDown = (e: React.PointerEvent) => {
179
+ if (e.button === 0 && state.current.id == null) {
180
+ start();
181
+ e.stopPropagation();
182
+ e.preventDefault();
183
+ state.current.lastPosition = {pageX: e.pageX, pageY: e.pageY};
184
+ state.current.id = e.pointerId;
185
+ addGlobalListener(window, 'pointermove', onPointerMove, false);
186
+ addGlobalListener(window, 'pointerup', onPointerUp, false);
187
+ addGlobalListener(window, 'pointercancel', onPointerUp, false);
188
+ }
189
+ };
190
+ }
191
+
192
+ let triggerKeyboardMove = (e: EventBase, deltaX: number, deltaY: number) => {
193
+ start();
194
+ move(e, 'keyboard', deltaX, deltaY);
195
+ end(e, 'keyboard');
196
+ };
197
+
198
+ moveProps.onKeyDown = (e) => {
199
+ switch (e.key) {
200
+ case 'Left':
201
+ case 'ArrowLeft':
202
+ e.preventDefault();
203
+ e.stopPropagation();
204
+ triggerKeyboardMove(e, -1, 0);
205
+ break;
206
+ case 'Right':
207
+ case 'ArrowRight':
208
+ e.preventDefault();
209
+ e.stopPropagation();
210
+ triggerKeyboardMove(e, 1, 0);
211
+ break;
212
+ case 'Up':
213
+ case 'ArrowUp':
214
+ e.preventDefault();
215
+ e.stopPropagation();
216
+ triggerKeyboardMove(e, 0, -1);
217
+ break;
218
+ case 'Down':
219
+ case 'ArrowDown':
220
+ e.preventDefault();
221
+ e.stopPropagation();
222
+ triggerKeyboardMove(e, 0, 1);
223
+ break;
224
+ }
225
+ };
226
+
227
+ return moveProps;
228
+ }, [state, addGlobalListener, removeGlobalListener, move, end]);
229
+
230
+ return {moveProps};
231
+ }