@react-aria/interactions 3.9.1 → 3.10.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/src/usePress.ts CHANGED
@@ -16,11 +16,11 @@
16
16
  // See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
17
17
 
18
18
  import {disableTextSelection, restoreTextSelection} from './textSelection';
19
+ import {DOMAttributes, FocusableElement, PointerType, PressEvents} from '@react-types/shared';
19
20
  import {focusWithoutScrolling, mergeProps, useGlobalListeners, useSyncRef} from '@react-aria/utils';
20
- import {HTMLAttributes, RefObject, useContext, useEffect, useMemo, useRef, useState} from 'react';
21
21
  import {isVirtualClick} from './utils';
22
- import {PointerType, PressEvents} from '@react-types/shared';
23
22
  import {PressResponderContext} from './context';
23
+ import {RefObject, useContext, useEffect, useMemo, useRef, useState} from 'react';
24
24
 
25
25
  export interface PressProps extends PressEvents {
26
26
  /** Whether the target is in a controlled press state (e.g. an overlay it triggers is open). */
@@ -42,7 +42,7 @@ export interface PressProps extends PressEvents {
42
42
 
43
43
  export interface PressHookProps extends PressProps {
44
44
  /** A ref to the target element. */
45
- ref?: RefObject<HTMLElement>
45
+ ref?: RefObject<Element>
46
46
  }
47
47
 
48
48
  interface PressState {
@@ -51,7 +51,7 @@ interface PressState {
51
51
  ignoreClickAfterPress: boolean,
52
52
  didFirePressStart: boolean,
53
53
  activePointerId: any,
54
- target: HTMLElement | null,
54
+ target: FocusableElement | null,
55
55
  isOverTarget: boolean,
56
56
  pointerType: PointerType,
57
57
  userSelect?: string
@@ -69,7 +69,7 @@ export interface PressResult {
69
69
  /** Whether the target is currently pressed. */
70
70
  isPressed: boolean,
71
71
  /** Props to spread on the target element. */
72
- pressProps: HTMLAttributes<HTMLElement>
72
+ pressProps: DOMAttributes
73
73
  }
74
74
 
75
75
  function usePressResponderContext(props: PressHookProps): PressHookProps {
@@ -135,7 +135,7 @@ export function usePress(props: PressHookProps): PressResult {
135
135
  onPressStart({
136
136
  type: 'pressstart',
137
137
  pointerType,
138
- target: originalEvent.currentTarget as HTMLElement,
138
+ target: originalEvent.currentTarget as Element,
139
139
  shiftKey: originalEvent.shiftKey,
140
140
  metaKey: originalEvent.metaKey,
141
141
  ctrlKey: originalEvent.ctrlKey,
@@ -164,7 +164,7 @@ export function usePress(props: PressHookProps): PressResult {
164
164
  onPressEnd({
165
165
  type: 'pressend',
166
166
  pointerType,
167
- target: originalEvent.currentTarget as HTMLElement,
167
+ target: originalEvent.currentTarget as Element,
168
168
  shiftKey: originalEvent.shiftKey,
169
169
  metaKey: originalEvent.metaKey,
170
170
  ctrlKey: originalEvent.ctrlKey,
@@ -182,7 +182,7 @@ export function usePress(props: PressHookProps): PressResult {
182
182
  onPress({
183
183
  type: 'press',
184
184
  pointerType,
185
- target: originalEvent.currentTarget as HTMLElement,
185
+ target: originalEvent.currentTarget as Element,
186
186
  shiftKey: originalEvent.shiftKey,
187
187
  metaKey: originalEvent.metaKey,
188
188
  ctrlKey: originalEvent.ctrlKey,
@@ -201,7 +201,7 @@ export function usePress(props: PressHookProps): PressResult {
201
201
  onPressUp({
202
202
  type: 'pressup',
203
203
  pointerType,
204
- target: originalEvent.currentTarget as HTMLElement,
204
+ target: originalEvent.currentTarget as Element,
205
205
  shiftKey: originalEvent.shiftKey,
206
206
  metaKey: originalEvent.metaKey,
207
207
  ctrlKey: originalEvent.ctrlKey,
@@ -226,9 +226,9 @@ export function usePress(props: PressHookProps): PressResult {
226
226
  }
227
227
  };
228
228
 
229
- let pressProps: HTMLAttributes<HTMLElement> = {
229
+ let pressProps: DOMAttributes = {
230
230
  onKeyDown(e) {
231
- if (isValidKeyboardEvent(e.nativeEvent) && e.currentTarget.contains(e.target as HTMLElement)) {
231
+ if (isValidKeyboardEvent(e.nativeEvent) && e.currentTarget.contains(e.target as Element)) {
232
232
  if (shouldPreventDefaultKeyboard(e.target as Element)) {
233
233
  e.preventDefault();
234
234
  }
@@ -238,7 +238,7 @@ export function usePress(props: PressHookProps): PressResult {
238
238
  // after which focus moved to the current element. Ignore these events and
239
239
  // only handle the first key down event.
240
240
  if (!state.isPressed && !e.repeat) {
241
- state.target = e.currentTarget as HTMLElement;
241
+ state.target = e.currentTarget;
242
242
  state.isPressed = true;
243
243
  triggerPressStart(e, 'keyboard');
244
244
 
@@ -249,12 +249,12 @@ export function usePress(props: PressHookProps): PressResult {
249
249
  }
250
250
  },
251
251
  onKeyUp(e) {
252
- if (isValidKeyboardEvent(e.nativeEvent) && !e.repeat && e.currentTarget.contains(e.target as HTMLElement)) {
252
+ if (isValidKeyboardEvent(e.nativeEvent) && !e.repeat && e.currentTarget.contains(e.target as Element)) {
253
253
  triggerPressUp(createEvent(state.target, e), 'keyboard');
254
254
  }
255
255
  },
256
256
  onClick(e) {
257
- if (e && !e.currentTarget.contains(e.target as HTMLElement)) {
257
+ if (e && !e.currentTarget.contains(e.target as Element)) {
258
258
  return;
259
259
  }
260
260
 
@@ -291,13 +291,13 @@ export function usePress(props: PressHookProps): PressResult {
291
291
  e.stopPropagation();
292
292
 
293
293
  state.isPressed = false;
294
- let target = e.target as HTMLElement;
294
+ let target = e.target as Element;
295
295
  triggerPressEnd(createEvent(state.target, e), 'keyboard', state.target.contains(target));
296
296
  removeAllGlobalListeners();
297
297
 
298
298
  // If the target is a link, trigger the click method to open the URL,
299
299
  // but defer triggering pressEnd until onClick event handler.
300
- if (state.target.contains(target) && isHTMLAnchorLink(state.target) || state.target.getAttribute('role') === 'link') {
300
+ if (state.target instanceof HTMLElement && (state.target.contains(target) && isHTMLAnchorLink(state.target) || state.target.getAttribute('role') === 'link')) {
301
301
  state.target.click();
302
302
  }
303
303
  }
@@ -306,7 +306,7 @@ export function usePress(props: PressHookProps): PressResult {
306
306
  if (typeof PointerEvent !== 'undefined') {
307
307
  pressProps.onPointerDown = (e) => {
308
308
  // Only handle left clicks, and ignore events that bubbled through portals.
309
- if (e.button !== 0 || !e.currentTarget.contains(e.target as HTMLElement)) {
309
+ if (e.button !== 0 || !e.currentTarget.contains(e.target as Element)) {
310
310
  return;
311
311
  }
312
312
 
@@ -321,7 +321,7 @@ export function usePress(props: PressHookProps): PressResult {
321
321
 
322
322
  // Due to browser inconsistencies, especially on mobile browsers, we prevent
323
323
  // default on pointer down and handle focusing the pressable element ourselves.
324
- if (shouldPreventDefault(e.currentTarget as HTMLElement)) {
324
+ if (shouldPreventDefault(e.currentTarget as Element)) {
325
325
  e.preventDefault();
326
326
  }
327
327
 
@@ -351,7 +351,7 @@ export function usePress(props: PressHookProps): PressResult {
351
351
  };
352
352
 
353
353
  pressProps.onMouseDown = (e) => {
354
- if (!e.currentTarget.contains(e.target as HTMLElement)) {
354
+ if (!e.currentTarget.contains(e.target as Element)) {
355
355
  return;
356
356
  }
357
357
 
@@ -359,7 +359,7 @@ export function usePress(props: PressHookProps): PressResult {
359
359
  // Chrome and Firefox on touch Windows devices require mouse down events
360
360
  // to be canceled in addition to pointer events, or an extra asynchronous
361
361
  // focus event will be fired.
362
- if (shouldPreventDefault(e.currentTarget as HTMLElement)) {
362
+ if (shouldPreventDefault(e.currentTarget as Element)) {
363
363
  e.preventDefault();
364
364
  }
365
365
 
@@ -369,7 +369,7 @@ export function usePress(props: PressHookProps): PressResult {
369
369
 
370
370
  pressProps.onPointerUp = (e) => {
371
371
  // iOS fires pointerup with zero width and height, so check the pointerType recorded during pointerdown.
372
- if (!e.currentTarget.contains(e.target as HTMLElement) || state.pointerType === 'virtual') {
372
+ if (!e.currentTarget.contains(e.target as Element) || state.pointerType === 'virtual') {
373
373
  return;
374
374
  }
375
375
 
@@ -427,7 +427,7 @@ export function usePress(props: PressHookProps): PressResult {
427
427
  };
428
428
 
429
429
  pressProps.onDragStart = (e) => {
430
- if (!e.currentTarget.contains(e.target as HTMLElement)) {
430
+ if (!e.currentTarget.contains(e.target as Element)) {
431
431
  return;
432
432
  }
433
433
 
@@ -437,13 +437,13 @@ export function usePress(props: PressHookProps): PressResult {
437
437
  } else {
438
438
  pressProps.onMouseDown = (e) => {
439
439
  // Only handle left clicks
440
- if (e.button !== 0 || !e.currentTarget.contains(e.target as HTMLElement)) {
440
+ if (e.button !== 0 || !e.currentTarget.contains(e.target as Element)) {
441
441
  return;
442
442
  }
443
443
 
444
444
  // Due to browser inconsistencies, especially on mobile browsers, we prevent
445
445
  // default on mouse down and handle focusing the pressable element ourselves.
446
- if (shouldPreventDefault(e.currentTarget as HTMLElement)) {
446
+ if (shouldPreventDefault(e.currentTarget)) {
447
447
  e.preventDefault();
448
448
  }
449
449
 
@@ -467,7 +467,7 @@ export function usePress(props: PressHookProps): PressResult {
467
467
  };
468
468
 
469
469
  pressProps.onMouseEnter = (e) => {
470
- if (!e.currentTarget.contains(e.target as HTMLElement)) {
470
+ if (!e.currentTarget.contains(e.target as Element)) {
471
471
  return;
472
472
  }
473
473
 
@@ -479,7 +479,7 @@ export function usePress(props: PressHookProps): PressResult {
479
479
  };
480
480
 
481
481
  pressProps.onMouseLeave = (e) => {
482
- if (!e.currentTarget.contains(e.target as HTMLElement)) {
482
+ if (!e.currentTarget.contains(e.target as Element)) {
483
483
  return;
484
484
  }
485
485
 
@@ -494,7 +494,7 @@ export function usePress(props: PressHookProps): PressResult {
494
494
  };
495
495
 
496
496
  pressProps.onMouseUp = (e) => {
497
- if (!e.currentTarget.contains(e.target as HTMLElement)) {
497
+ if (!e.currentTarget.contains(e.target as Element)) {
498
498
  return;
499
499
  }
500
500
 
@@ -527,7 +527,7 @@ export function usePress(props: PressHookProps): PressResult {
527
527
  };
528
528
 
529
529
  pressProps.onTouchStart = (e) => {
530
- if (!e.currentTarget.contains(e.target as HTMLElement)) {
530
+ if (!e.currentTarget.contains(e.target as Element)) {
531
531
  return;
532
532
  }
533
533
 
@@ -559,7 +559,7 @@ export function usePress(props: PressHookProps): PressResult {
559
559
  };
560
560
 
561
561
  pressProps.onTouchMove = (e) => {
562
- if (!e.currentTarget.contains(e.target as HTMLElement)) {
562
+ if (!e.currentTarget.contains(e.target as Element)) {
563
563
  return;
564
564
  }
565
565
 
@@ -584,7 +584,7 @@ export function usePress(props: PressHookProps): PressResult {
584
584
  };
585
585
 
586
586
  pressProps.onTouchEnd = (e) => {
587
- if (!e.currentTarget.contains(e.target as HTMLElement)) {
587
+ if (!e.currentTarget.contains(e.target as Element)) {
588
588
  return;
589
589
  }
590
590
 
@@ -612,7 +612,7 @@ export function usePress(props: PressHookProps): PressResult {
612
612
  };
613
613
 
614
614
  pressProps.onTouchCancel = (e) => {
615
- if (!e.currentTarget.contains(e.target as HTMLElement)) {
615
+ if (!e.currentTarget.contains(e.target as Element)) {
616
616
  return;
617
617
  }
618
618
 
@@ -623,7 +623,7 @@ export function usePress(props: PressHookProps): PressResult {
623
623
  };
624
624
 
625
625
  let onScroll = (e: Event) => {
626
- if (state.isPressed && (e.target as HTMLElement).contains(state.target)) {
626
+ if (state.isPressed && (e.target as Element).contains(state.target)) {
627
627
  cancel({
628
628
  currentTarget: state.target,
629
629
  shiftKey: false,
@@ -635,7 +635,7 @@ export function usePress(props: PressHookProps): PressResult {
635
635
  };
636
636
 
637
637
  pressProps.onDragStart = (e) => {
638
- if (!e.currentTarget.contains(e.target as HTMLElement)) {
638
+ if (!e.currentTarget.contains(e.target as Element)) {
639
639
  return;
640
640
  }
641
641
 
@@ -708,7 +708,7 @@ function getTouchById(
708
708
  return null;
709
709
  }
710
710
 
711
- function createEvent(target: HTMLElement, e: EventBase): EventBase {
711
+ function createEvent(target: FocusableElement, e: EventBase): EventBase {
712
712
  return {
713
713
  currentTarget: target,
714
714
  shiftKey: e.shiftKey,
@@ -758,15 +758,15 @@ function areRectanglesOverlapping(a: Rect, b: Rect) {
758
758
  return true;
759
759
  }
760
760
 
761
- function isOverTarget(point: EventPoint, target: HTMLElement) {
761
+ function isOverTarget(point: EventPoint, target: Element) {
762
762
  let rect = target.getBoundingClientRect();
763
763
  let pointRect = getPointClientRect(point);
764
764
  return areRectanglesOverlapping(rect, pointRect);
765
765
  }
766
766
 
767
- function shouldPreventDefault(target: HTMLElement) {
767
+ function shouldPreventDefault(target: Element) {
768
768
  // We cannot prevent default if the target is a draggable element.
769
- return !target.draggable;
769
+ return !(target instanceof HTMLElement) || !target.draggable;
770
770
  }
771
771
 
772
772
  function shouldPreventDefaultKeyboard(target: Element) {