-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathindex.ts
907 lines (745 loc) · 26.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/**
* TODO: Remove this package once the react-aria team publishes a new version
* with the fix https://github.com/adobe/react-spectrum/pull/5291
*/
// Portions of the code in this file are based on code from react.
// Original licensing for the following can be found in the
// NOTICE file in the root directory of this source tree.
// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
import {DOMAttributes, FocusableElement, PointerType} from "@react-types/shared";
import {
focusWithoutScrolling,
isMac,
isVirtualClick,
isVirtualPointerEvent,
mergeProps,
openLink,
useEffectEvent,
useGlobalListeners,
useSyncRef,
} from "@react-aria/utils";
import {RefObject, useContext, useEffect, useMemo, useRef, useState} from "react";
import {getOwnerDocument, getOwnerWindow} from "./dom-helpers";
import {PressHookProps, PressResult, PressState, EventBase} from "./types";
import {disableTextSelection, restoreTextSelection} from "./text-selection";
import {PressEvent} from "./events";
import {PressResponderContext} from "./context";
function usePressResponderContext(props: PressHookProps): PressHookProps {
// Consume context from <PressResponder> and merge with props.
let context = useContext(PressResponderContext);
if (context) {
let {register, ...contextProps} = context;
props = mergeProps(contextProps, props) as PressHookProps;
register();
}
useSyncRef(context, props.ref as RefObject<FocusableElement>);
return props;
}
const LINK_CLICKED = Symbol("linkClicked");
/**
* Handles press interactions across mouse, touch, keyboard, and screen readers.
* It normalizes behavior across browsers and platforms, and handles many nuances
* of dealing with pointer and keyboard events.
*/
export function usePress(props: PressHookProps): PressResult {
let {
onPress,
onPressChange,
onPressStart,
onPressEnd,
onPressUp,
isDisabled,
isPressed: isPressedProp,
preventFocusOnPress,
shouldCancelOnPointerExit,
allowTextSelectionOnPress,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ref: _, // Removing `ref` from `domProps` because TypeScript is dumb
...domProps
} = usePressResponderContext(props);
let [isPressed, setPressed] = useState(false);
let ref = useRef<PressState>({
isPressed: false,
ignoreEmulatedMouseEvents: false,
ignoreClickAfterPress: false,
didFirePressStart: false,
isTriggeringEvent: false,
activePointerId: null,
target: null,
isOverTarget: false,
pointerType: null,
});
let {addGlobalListener, removeAllGlobalListeners} = useGlobalListeners();
let triggerPressStart = useEffectEvent((originalEvent: EventBase, pointerType: PointerType) => {
let state = ref.current;
if (isDisabled || state.didFirePressStart) {
return;
}
let shouldStopPropagation = true;
state.isTriggeringEvent = true;
if (onPressStart) {
let event = new PressEvent("pressstart", pointerType, originalEvent);
onPressStart(event);
shouldStopPropagation = event.shouldStopPropagation;
}
if (onPressChange) {
onPressChange(true);
}
state.isTriggeringEvent = false;
state.didFirePressStart = true;
setPressed(true);
return shouldStopPropagation;
});
let triggerPressEnd = useEffectEvent(
(originalEvent: EventBase, pointerType: PointerType, wasPressed = true) => {
let state = ref.current;
if (!state.didFirePressStart) {
return;
}
state.ignoreClickAfterPress = true;
state.didFirePressStart = false;
state.isTriggeringEvent = true;
let shouldStopPropagation = true;
if (onPressEnd) {
let event = new PressEvent("pressend", pointerType, originalEvent);
onPressEnd(event);
shouldStopPropagation = event.shouldStopPropagation;
}
if (onPressChange) {
onPressChange(false);
}
setPressed(false);
if (onPress && wasPressed && !isDisabled) {
let event = new PressEvent("press", pointerType, originalEvent);
onPress(event);
shouldStopPropagation &&= event.shouldStopPropagation;
}
state.isTriggeringEvent = false;
return shouldStopPropagation;
},
);
let triggerPressUp = useEffectEvent((originalEvent: EventBase, pointerType: PointerType) => {
let state = ref.current;
if (isDisabled) {
return;
}
if (onPressUp) {
state.isTriggeringEvent = true;
let event = new PressEvent("pressup", pointerType, originalEvent);
onPressUp(event);
state.isTriggeringEvent = false;
return event.shouldStopPropagation;
}
return true;
});
let cancel = useEffectEvent((e: EventBase) => {
let state = ref.current;
if (state.isPressed) {
if (state.isOverTarget && state.target) {
triggerPressEnd(createEvent(state.target, e), state.pointerType, false);
}
state.isPressed = false;
state.isOverTarget = false;
state.activePointerId = null;
state.pointerType = null;
removeAllGlobalListeners();
if (!allowTextSelectionOnPress && state.target) {
restoreTextSelection(state.target);
}
}
});
let cancelOnPointerExit = useEffectEvent((e: EventBase) => {
if (shouldCancelOnPointerExit) {
cancel(e);
}
});
let pressProps = useMemo(() => {
let state = ref.current;
let pressProps: DOMAttributes = {
onKeyDown(e) {
if (
isValidKeyboardEvent(e.nativeEvent, e.currentTarget) &&
e.currentTarget.contains(e.target as Element)
) {
if (shouldPreventDefaultKeyboard(e.target as Element, e.key)) {
e.preventDefault();
}
// If the event is repeating, it may have started on a different element
// after which focus moved to the current element. Ignore these events and
// only handle the first key down event.
let shouldStopPropagation = true;
if (!state.isPressed && !e.repeat) {
state.target = e.currentTarget;
state.isPressed = true;
shouldStopPropagation = triggerPressStart(e, "keyboard");
// Focus may move before the key up event, so register the event on the document
// instead of the same element where the key down event occurred.
addGlobalListener(getOwnerDocument(e.currentTarget), "keyup", onKeyUp, false);
}
if (shouldStopPropagation) {
e.stopPropagation();
}
// Keep track of the keydown events that occur while the Meta (e.g. Command) key is held.
// macOS has a bug where keyup events are not fired while the Meta key is down.
// When the Meta key itself is released we will get an event for that, and we'll act as if
// all of these other keys were released as well.
// https://bugs.chromium.org/p/chromium/issues/detail?id=1393524
// https://bugs.webkit.org/show_bug.cgi?id=55291
// https://bugzilla.mozilla.org/show_bug.cgi?id=1299553
if (e.metaKey && isMac()) {
state.metaKeyEvents?.set(e.key, e.nativeEvent);
}
} else if (e.key === "Meta") {
state.metaKeyEvents = new Map();
}
},
onKeyUp(e) {
if (
state.target &&
isValidKeyboardEvent(e.nativeEvent, e.currentTarget) &&
!e.repeat &&
e.currentTarget.contains(e.target as Element)
) {
triggerPressUp(createEvent(state.target, e), "keyboard");
}
},
onClick(e) {
if (e && !e.currentTarget.contains(e.target as Element)) {
return;
}
// @ts-ignore
if (e && e.button === 0 && !state.isTriggeringEvent && !openLink.isOpening) {
let shouldStopPropagation = true;
if (isDisabled) {
e.preventDefault();
}
// If triggered from a screen reader or by using element.click(),
// trigger as if it were a keyboard click.
if (
!state.ignoreClickAfterPress &&
!state.ignoreEmulatedMouseEvents &&
!state.isPressed &&
(state.pointerType === "virtual" || isVirtualClick(e.nativeEvent))
) {
// Ensure the element receives focus (VoiceOver on iOS does not do this)
if (!isDisabled && !preventFocusOnPress) {
focusWithoutScrolling(e.currentTarget);
}
let stopPressStart = triggerPressStart(e, "virtual");
let stopPressUp = triggerPressUp(e, "virtual");
let stopPressEnd = triggerPressEnd(e, "virtual");
shouldStopPropagation = stopPressStart && stopPressUp && stopPressEnd;
}
state.ignoreEmulatedMouseEvents = false;
state.ignoreClickAfterPress = false;
if (shouldStopPropagation) {
e.stopPropagation();
}
}
},
};
let onKeyUp = (e: KeyboardEvent) => {
if (state.isPressed && state.target && isValidKeyboardEvent(e, state.target)) {
if (shouldPreventDefaultKeyboard(e.target as Element, e.key)) {
e.preventDefault();
}
let target = e.target as Element;
let shouldStopPropagation = triggerPressEnd(
createEvent(state.target, e as EventBase),
"keyboard",
state.target.contains(target),
);
removeAllGlobalListeners();
if (shouldStopPropagation) {
e.stopPropagation();
}
// If a link was triggered with a key other than Enter, open the URL ourselves.
// This means the link has a role override, and the default browser behavior
// only applies when using the Enter key.
if (
e.key !== "Enter" &&
isHTMLAnchorLink(state.target) &&
state.target.contains(target) &&
// @ts-ignore
!e[LINK_CLICKED]
) {
// Store a hidden property on the event so we only trigger link click once,
// even if there are multiple usePress instances attached to the element.
// @ts-ignore
e[LINK_CLICKED] = true;
openLink(state.target, e, false);
}
state.isPressed = false;
state.metaKeyEvents?.delete(e.key);
} else if (e.key === "Meta" && state.metaKeyEvents?.size) {
// If we recorded keydown events that occurred while the Meta key was pressed,
// and those haven't received keyup events already, fire keyup events ourselves.
// See comment above for more info about the macOS bug causing this.
let events = state.metaKeyEvents;
state.metaKeyEvents = null;
for (let event of events.values()) {
state.target && state.target.dispatchEvent(new KeyboardEvent("keyup", event));
}
}
};
if (typeof PointerEvent !== "undefined") {
pressProps.onPointerDown = (e) => {
// Only handle left clicks, and ignore events that bubbled through portals.
if (e.button !== 0 || !e.currentTarget.contains(e.target as Element)) {
return;
}
// iOS safari fires pointer events from VoiceOver with incorrect coordinates/target.
// Ignore and let the onClick handler take care of it instead.
// https://bugs.webkit.org/show_bug.cgi?id=222627
// https://bugs.webkit.org/show_bug.cgi?id=223202
if (isVirtualPointerEvent(e.nativeEvent)) {
state.pointerType = "virtual";
return;
}
// Due to browser inconsistencies, especially on mobile browsers, we prevent
// default on pointer down and handle focusing the pressable element ourselves.
if (shouldPreventDefault(e.currentTarget as Element)) {
e.preventDefault();
}
state.pointerType = e.pointerType;
let shouldStopPropagation = true;
if (!state.isPressed) {
state.isPressed = true;
state.isOverTarget = true;
state.activePointerId = e.pointerId;
state.target = e.currentTarget;
if (!isDisabled && !preventFocusOnPress) {
focusWithoutScrolling(e.currentTarget);
}
if (!allowTextSelectionOnPress) {
disableTextSelection(state.target);
}
shouldStopPropagation = triggerPressStart(e, state.pointerType);
addGlobalListener(getOwnerDocument(e.currentTarget), "pointermove", onPointerMove, false);
addGlobalListener(getOwnerDocument(e.currentTarget), "pointerup", onPointerUp, false);
addGlobalListener(
getOwnerDocument(e.currentTarget),
"pointercancel",
onPointerCancel,
false,
);
}
if (shouldStopPropagation) {
e.stopPropagation();
}
};
pressProps.onMouseDown = (e) => {
if (!e.currentTarget.contains(e.target as Element)) {
return;
}
if (e.button === 0) {
// Chrome and Firefox on touch Windows devices require mouse down events
// to be canceled in addition to pointer events, or an extra asynchronous
// focus event will be fired.
if (shouldPreventDefault(e.currentTarget as Element)) {
e.preventDefault();
}
e.stopPropagation();
}
};
pressProps.onPointerUp = (e) => {
// iOS fires pointerup with zero width and height, so check the pointerType recorded during pointerdown.
if (!e.currentTarget.contains(e.target as Element) || state.pointerType === "virtual") {
return;
}
// Only handle left clicks
// Safari on iOS sometimes fires pointerup events, even
// when the touch isn't over the target, so double check.
if (e.button === 0 && isOverTarget(e, e.currentTarget)) {
triggerPressUp(e, state.pointerType || e.pointerType);
}
};
// Safari on iOS < 13.2 does not implement pointerenter/pointerleave events correctly.
// Use pointer move events instead to implement our own hit testing.
// See https://bugs.webkit.org/show_bug.cgi?id=199803
let onPointerMove = (e: PointerEvent) => {
if (e.pointerId !== state.activePointerId || !state.target) {
return;
}
if (isOverTarget(e, state.target)) {
if (!state.isOverTarget) {
state.isOverTarget = true;
triggerPressStart(createEvent(state.target, e as EventBase), state.pointerType);
}
} else if (state.isOverTarget) {
state.isOverTarget = false;
triggerPressEnd(createEvent(state.target, e as EventBase), state.pointerType, false);
cancelOnPointerExit(e);
}
};
let onPointerUp = (e: PointerEvent) => {
if (
e.pointerId === state.activePointerId &&
state.isPressed &&
e.button === 0 &&
state.target
) {
if (isOverTarget(e, state.target)) {
triggerPressEnd(createEvent(state.target, e as EventBase), state.pointerType);
} else if (state.isOverTarget) {
triggerPressEnd(createEvent(state.target, e as EventBase), state.pointerType);
}
state.isPressed = false;
state.isOverTarget = false;
state.activePointerId = null;
state.pointerType = null;
removeAllGlobalListeners();
if (!allowTextSelectionOnPress) {
restoreTextSelection(state.target);
}
}
};
let onPointerCancel = (e: PointerEvent) => {
cancel(e);
};
pressProps.onDragStart = (e) => {
if (!e.currentTarget.contains(e.target as Element)) {
return;
}
// Safari does not call onPointerCancel when a drag starts, whereas Chrome and Firefox do.
cancel(e);
};
} else {
pressProps.onMouseDown = (e) => {
// Only handle left clicks
if (e.button !== 0 || !e.currentTarget.contains(e.target as Element)) {
return;
}
// Due to browser inconsistencies, especially on mobile browsers, we prevent
// default on mouse down and handle focusing the pressable element ourselves.
if (shouldPreventDefault(e.currentTarget)) {
e.preventDefault();
}
if (state.ignoreEmulatedMouseEvents) {
e.stopPropagation();
return;
}
state.isPressed = true;
state.isOverTarget = true;
state.target = e.currentTarget;
state.pointerType = isVirtualClick(e.nativeEvent) ? "virtual" : "mouse";
if (!isDisabled && !preventFocusOnPress) {
focusWithoutScrolling(e.currentTarget);
}
let shouldStopPropagation = triggerPressStart(e, state.pointerType);
if (shouldStopPropagation) {
e.stopPropagation();
}
addGlobalListener(getOwnerDocument(e.currentTarget), "mouseup", onMouseUp, false);
};
pressProps.onMouseEnter = (e) => {
if (!e.currentTarget.contains(e.target as Element)) {
return;
}
let shouldStopPropagation = true;
if (state.isPressed && !state.ignoreEmulatedMouseEvents) {
state.isOverTarget = true;
shouldStopPropagation = triggerPressStart(e, state.pointerType);
}
if (shouldStopPropagation) {
e.stopPropagation();
}
};
pressProps.onMouseLeave = (e) => {
if (!e.currentTarget.contains(e.target as Element)) {
return;
}
let shouldStopPropagation = true;
if (state.isPressed && !state.ignoreEmulatedMouseEvents) {
state.isOverTarget = false;
shouldStopPropagation = triggerPressEnd(e, state.pointerType, false);
cancelOnPointerExit(e);
}
if (shouldStopPropagation) {
e.stopPropagation();
}
};
pressProps.onMouseUp = (e) => {
if (!e.currentTarget.contains(e.target as Element)) {
return;
}
if (!state.ignoreEmulatedMouseEvents && e.button === 0) {
triggerPressUp(e, state.pointerType || "mouse");
}
};
let onMouseUp = (e: MouseEvent) => {
// Only handle left clicks
if (e.button !== 0) {
return;
}
state.isPressed = false;
removeAllGlobalListeners();
if (state.ignoreEmulatedMouseEvents) {
state.ignoreEmulatedMouseEvents = false;
return;
}
if (!state.target) {
return;
}
if (isOverTarget(e, state.target)) {
triggerPressEnd(createEvent(state.target, e as EventBase), state.pointerType);
} else if (state.isOverTarget) {
triggerPressEnd(createEvent(state.target, e as EventBase), state.pointerType, false);
}
state.isOverTarget = false;
};
pressProps.onTouchStart = (e) => {
if (!e.currentTarget.contains(e.target as Element)) {
return;
}
let touch = getTouchFromEvent(e.nativeEvent);
if (!touch) {
return;
}
state.activePointerId = touch.identifier;
state.ignoreEmulatedMouseEvents = true;
state.isOverTarget = true;
state.isPressed = true;
state.target = e.currentTarget;
state.pointerType = "touch";
// Due to browser inconsistencies, especially on mobile browsers, we prevent default
// on the emulated mouse event and handle focusing the pressable element ourselves.
if (!isDisabled && !preventFocusOnPress) {
focusWithoutScrolling(e.currentTarget);
}
if (!allowTextSelectionOnPress) {
disableTextSelection(state.target);
}
let shouldStopPropagation = triggerPressStart(e, state.pointerType);
if (shouldStopPropagation) {
e.stopPropagation();
}
addGlobalListener(getOwnerWindow(e.currentTarget), "scroll", onScroll, true);
};
pressProps.onTouchMove = (e) => {
if (!e.currentTarget.contains(e.target as Element)) {
return;
}
if (!state.isPressed) {
e.stopPropagation();
return;
}
let touch = getTouchById(e.nativeEvent, state.activePointerId);
let shouldStopPropagation = true;
if (touch && isOverTarget(touch, e.currentTarget)) {
if (!state.isOverTarget) {
state.isOverTarget = true;
shouldStopPropagation = triggerPressStart(e, state.pointerType);
}
} else if (state.isOverTarget) {
state.isOverTarget = false;
shouldStopPropagation = triggerPressEnd(e, state.pointerType, false);
cancelOnPointerExit(e);
}
if (shouldStopPropagation) {
e.stopPropagation();
}
};
pressProps.onTouchEnd = (e) => {
if (!e.currentTarget.contains(e.target as Element)) {
return;
}
if (!state.isPressed) {
e.stopPropagation();
return;
}
let touch = getTouchById(e.nativeEvent, state.activePointerId);
let shouldStopPropagation = true;
if (touch && isOverTarget(touch, e.currentTarget)) {
triggerPressUp(e, state.pointerType);
shouldStopPropagation = triggerPressEnd(e, state.pointerType);
} else if (state.isOverTarget) {
shouldStopPropagation = triggerPressEnd(e, state.pointerType, false);
}
if (shouldStopPropagation) {
e.stopPropagation();
}
state.isPressed = false;
state.activePointerId = null;
state.isOverTarget = false;
state.ignoreEmulatedMouseEvents = true;
if (!allowTextSelectionOnPress && state.target) {
restoreTextSelection(state.target);
}
removeAllGlobalListeners();
};
pressProps.onTouchCancel = (e) => {
if (!e.currentTarget.contains(e.target as Element)) {
return;
}
e.stopPropagation();
if (state.isPressed) {
cancel(e);
}
};
let onScroll = (e: Event) => {
if (state.isPressed && (e.target as Element).contains(state.target)) {
cancel({
currentTarget: state.target,
shiftKey: false,
ctrlKey: false,
metaKey: false,
altKey: false,
});
}
};
pressProps.onDragStart = (e) => {
if (!e.currentTarget.contains(e.target as Element)) {
return;
}
cancel(e);
};
}
return pressProps;
}, [
addGlobalListener,
isDisabled,
preventFocusOnPress,
removeAllGlobalListeners,
allowTextSelectionOnPress,
cancel,
cancelOnPointerExit,
triggerPressEnd,
triggerPressStart,
triggerPressUp,
]);
// Remove user-select: none in case component unmounts immediately after pressStart
// eslint-disable-next-line arrow-body-style
useEffect(() => {
return () => {
if (!allowTextSelectionOnPress && ref.current.target) {
// eslint-disable-next-line react-hooks/exhaustive-deps
restoreTextSelection(ref.current.target);
}
};
}, [allowTextSelectionOnPress]);
return {
isPressed: isPressedProp || isPressed,
pressProps: mergeProps(domProps, pressProps),
};
}
function isHTMLAnchorLink(target: Element): target is HTMLAnchorElement {
return target.tagName === "A" && target.hasAttribute("href");
}
function isValidKeyboardEvent(event: KeyboardEvent, currentTarget: Element): boolean {
const {key, code} = event;
const element = currentTarget as HTMLElement;
const role = element.getAttribute("role");
// Accessibility for keyboards. Space and Enter only.
// "Spacebar" is for IE 11
return (
(key === "Enter" || key === " " || key === "Spacebar" || code === "Space") &&
!(
(element instanceof getOwnerWindow(element).HTMLInputElement &&
!isValidInputKey(element as HTMLInputElement, key)) ||
element instanceof getOwnerWindow(element).HTMLTextAreaElement ||
element.isContentEditable
) &&
// Links should only trigger with Enter key
!((role === "link" || (!role && isHTMLAnchorLink(element))) && key !== "Enter")
);
}
function getTouchFromEvent(event: TouchEvent): Touch | null {
const {targetTouches} = event;
if (targetTouches.length > 0) {
return targetTouches[0];
}
return null;
}
function getTouchById(event: TouchEvent, pointerId: null | number): null | Touch {
const changedTouches = event.changedTouches;
for (let i = 0; i < changedTouches.length; i++) {
const touch = changedTouches[i];
if (touch.identifier === pointerId) {
return touch;
}
}
return null;
}
function createEvent(target: FocusableElement, e: EventBase): EventBase {
return {
currentTarget: target,
shiftKey: e.shiftKey,
ctrlKey: e.ctrlKey,
metaKey: e.metaKey,
altKey: e.altKey,
};
}
interface Rect {
top: number;
right: number;
bottom: number;
left: number;
}
interface EventPoint {
clientX: number;
clientY: number;
width?: number;
height?: number;
radiusX?: number;
radiusY?: number;
}
function getPointClientRect(point: EventPoint): Rect {
let offsetX = !!point.width ? point.width / 2 : point.radiusX || 0;
let offsetY = !!point.height ? point.height / 2 : point.radiusY || 0;
return {
top: point.clientY - offsetY,
right: point.clientX + offsetX,
bottom: point.clientY + offsetY,
left: point.clientX - offsetX,
};
}
function areRectanglesOverlapping(a: Rect, b: Rect) {
// check if they cannot overlap on x axis
if (a.left > b.right || b.left > a.right) {
return false;
}
// check if they cannot overlap on y axis
if (a.top > b.bottom || b.top > a.bottom) {
return false;
}
return true;
}
function isOverTarget(point: EventPoint, target: Element) {
let rect = target.getBoundingClientRect();
let pointRect = getPointClientRect(point);
return areRectanglesOverlapping(rect, pointRect);
}
function shouldPreventDefault(target: Element) {
// We cannot prevent default if the target is a draggable element.
return !(target instanceof HTMLElement) || !target.hasAttribute("draggable");
}
function shouldPreventDefaultKeyboard(target: Element, key: string) {
if (target instanceof HTMLInputElement) {
return !isValidInputKey(target, key);
}
if (target instanceof HTMLButtonElement) {
return target.type !== "submit" && target.type !== "reset";
}
if (isHTMLAnchorLink(target)) {
return false;
}
return true;
}
const nonTextInputTypes = new Set([
"checkbox",
"radio",
"range",
"color",
"file",
"image",
"button",
"submit",
"reset",
]);
function isValidInputKey(target: HTMLInputElement, key: string) {
// Only space should toggle checkboxes and radios, not enter.
return target.type === "checkbox" || target.type === "radio"
? key === " "
: nonTextInputTypes.has(target.type);
}