@react-aria/interactions 3.8.4 → 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/useHover.ts CHANGED
@@ -15,17 +15,18 @@
15
15
  // NOTICE file in the root directory of this source tree.
16
16
  // See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
17
17
 
18
+ import {DOMAttributes} from '@react-types/shared';
18
19
  import {HoverEvents} from '@react-types/shared';
19
- import {HTMLAttributes, useEffect, useMemo, useRef, useState} from 'react';
20
+ import {useEffect, useMemo, useRef, useState} from 'react';
20
21
 
21
22
  export interface HoverProps extends HoverEvents {
22
23
  /** Whether the hover events should be disabled. */
23
24
  isDisabled?: boolean
24
25
  }
25
26
 
26
- interface HoverResult {
27
+ export interface HoverResult {
27
28
  /** Props to spread on the target element. */
28
- hoverProps: HTMLAttributes<HTMLElement>,
29
+ hoverProps: DOMAttributes,
29
30
  isHovered: boolean
30
31
  }
31
32
 
@@ -152,7 +153,7 @@ export function useHover(props: HoverProps): HoverResult {
152
153
  setHovered(false);
153
154
  };
154
155
 
155
- let hoverProps: HTMLAttributes<HTMLElement> = {};
156
+ let hoverProps: DOMAttributes = {};
156
157
 
157
158
  if (typeof PointerEvent !== 'undefined') {
158
159
  hoverProps.onPointerEnter = (e) => {
@@ -164,7 +165,7 @@ export function useHover(props: HoverProps): HoverResult {
164
165
  };
165
166
 
166
167
  hoverProps.onPointerLeave = (e) => {
167
- if (!isDisabled && e.currentTarget.contains(e.target as HTMLElement)) {
168
+ if (!isDisabled && e.currentTarget.contains(e.target as Element)) {
168
169
  triggerHoverEnd(e, e.pointerType);
169
170
  }
170
171
  };
@@ -182,7 +183,7 @@ export function useHover(props: HoverProps): HoverResult {
182
183
  };
183
184
 
184
185
  hoverProps.onMouseLeave = (e) => {
185
- if (!isDisabled && e.currentTarget.contains(e.target as HTMLElement)) {
186
+ if (!isDisabled && e.currentTarget.contains(e.target as Element)) {
186
187
  triggerHoverEnd(e, 'mouse');
187
188
  }
188
189
  };
@@ -17,7 +17,7 @@
17
17
 
18
18
  import {RefObject, SyntheticEvent, useEffect, useRef} from 'react';
19
19
 
20
- interface InteractOutsideProps {
20
+ export interface InteractOutsideProps {
21
21
  ref: RefObject<Element>,
22
22
  onInteractOutside?: (e: SyntheticEvent) => void,
23
23
  onInteractOutsideStart?: (e: SyntheticEvent) => void,
@@ -11,17 +11,16 @@
11
11
  */
12
12
 
13
13
  import {createEventHandler} from './createEventHandler';
14
- import {HTMLAttributes} from 'react';
15
- import {KeyboardEvents} from '@react-types/shared';
14
+ import {DOMAttributes, KeyboardEvents} from '@react-types/shared';
16
15
 
17
16
  export interface KeyboardProps extends KeyboardEvents {
18
17
  /** Whether the keyboard events should be disabled. */
19
18
  isDisabled?: boolean
20
19
  }
21
20
 
22
- interface KeyboardResult {
21
+ export interface KeyboardResult {
23
22
  /** Props to spread onto the target element. */
24
- keyboardProps: HTMLAttributes<HTMLElement>
23
+ keyboardProps: DOMAttributes
25
24
  }
26
25
 
27
26
  /**
@@ -10,12 +10,12 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {HTMLAttributes, useRef} from 'react';
14
- import {LongPressEvent} from '@react-types/shared';
13
+ import {DOMAttributes, LongPressEvent} from '@react-types/shared';
15
14
  import {mergeProps, useDescription, useGlobalListeners} from '@react-aria/utils';
16
15
  import {usePress} from './usePress';
16
+ import {useRef} from 'react';
17
17
 
18
- interface LongPressProps {
18
+ export interface LongPressProps {
19
19
  /** Whether long press events should be disabled. */
20
20
  isDisabled?: boolean,
21
21
  /** Handler that is called when a long press interaction starts. */
@@ -42,9 +42,9 @@ interface LongPressProps {
42
42
  accessibilityDescription?: string
43
43
  }
44
44
 
45
- interface LongPressResult {
45
+ export interface LongPressResult {
46
46
  /** Props to spread on the target element. */
47
- longPressProps: HTMLAttributes<HTMLElement>
47
+ longPressProps: DOMAttributes
48
48
  }
49
49
 
50
50
  const DEFAULT_THRESHOLD = 500;
package/src/useMove.ts CHANGED
@@ -11,13 +11,13 @@
11
11
  */
12
12
 
13
13
  import {disableTextSelection, restoreTextSelection} from './textSelection';
14
- import {MoveEvents, PointerType} from '@react-types/shared';
15
- import React, {HTMLAttributes, useMemo, useRef} from 'react';
14
+ import {DOMAttributes, MoveEvents, PointerType} from '@react-types/shared';
15
+ import React, {useMemo, useRef} from 'react';
16
16
  import {useGlobalListeners} from '@react-aria/utils';
17
17
 
18
- interface MoveResult {
18
+ export interface MoveResult {
19
19
  /** Props to spread on the target element. */
20
- moveProps: HTMLAttributes<HTMLElement>
20
+ moveProps: DOMAttributes
21
21
  }
22
22
 
23
23
  interface EventBase {
@@ -44,7 +44,7 @@ export function useMove(props: MoveEvents): MoveResult {
44
44
  let {addGlobalListener, removeGlobalListener} = useGlobalListeners();
45
45
 
46
46
  let moveProps = useMemo(() => {
47
- let moveProps: HTMLAttributes<HTMLElement> = {};
47
+ let moveProps: DOMAttributes = {};
48
48
 
49
49
  let start = () => {
50
50
  disableTextSelection();
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) {
@@ -778,13 +778,15 @@ function isVirtualPointerEvent(event: PointerEvent) {
778
778
  // Android TalkBack double tap will sometimes return a event with width and height of 1
779
779
  // and pointerType === 'mouse' so we need to check for a specific combination of event attributes.
780
780
  // Cannot use "event.pressure === 0" as the sole check due to Safari pointer events always returning pressure === 0
781
- // instead of .5, see https://bugs.webkit.org/show_bug.cgi?id=206216
781
+ // instead of .5, see https://bugs.webkit.org/show_bug.cgi?id=206216. event.pointerType === 'mouse' is to distingush
782
+ // Talkback double tap from Windows Firefox touch screen press
782
783
  return (
783
784
  (event.width === 0 && event.height === 0) ||
784
785
  (event.width === 1 &&
785
786
  event.height === 1 &&
786
787
  event.pressure === 0 &&
787
- event.detail === 0
788
+ event.detail === 0 &&
789
+ event.pointerType === 'mouse'
788
790
  )
789
791
  );
790
792
  }
package/src/utils.ts CHANGED
@@ -10,7 +10,7 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {FocusEvent as ReactFocusEvent, useRef} from 'react';
13
+ import {FocusEvent as ReactFocusEvent, useCallback, useRef} from 'react';
14
14
  import {useLayoutEffect} from '@react-aria/utils';
15
15
 
16
16
  // Original licensing for the following method can be found in the
@@ -87,22 +87,22 @@ export function useSyntheticBlurEvent(onBlur: (e: ReactFocusEvent) => void) {
87
87
  onBlur,
88
88
  observer: null as MutationObserver
89
89
  });
90
- let state = stateRef.current;
91
- state.onBlur = onBlur;
90
+ stateRef.current.onBlur = onBlur;
92
91
 
93
92
  // Clean up MutationObserver on unmount. See below.
94
93
  // eslint-disable-next-line arrow-body-style
95
94
  useLayoutEffect(() => {
95
+ const state = stateRef.current;
96
96
  return () => {
97
97
  if (state.observer) {
98
98
  state.observer.disconnect();
99
99
  state.observer = null;
100
100
  }
101
101
  };
102
- }, [state]);
102
+ }, []);
103
103
 
104
104
  // This function is called during a React onFocus event.
105
- return (e: ReactFocusEvent) => {
105
+ return useCallback((e: ReactFocusEvent) => {
106
106
  // React does not fire onBlur when an element is disabled. https://github.com/facebook/react/issues/9142
107
107
  // Most browsers fire a native focusout event in this case, except for Firefox. In that case, we use a
108
108
  // MutationObserver to watch for the disabled attribute, and dispatch these events ourselves.
@@ -113,36 +113,35 @@ export function useSyntheticBlurEvent(onBlur: (e: ReactFocusEvent) => void) {
113
113
  e.target instanceof HTMLTextAreaElement ||
114
114
  e.target instanceof HTMLSelectElement
115
115
  ) {
116
- state.isFocused = true;
116
+ stateRef.current.isFocused = true;
117
117
 
118
118
  let target = e.target;
119
119
  let onBlurHandler = (e: FocusEvent) => {
120
- let state = stateRef.current;
121
- state.isFocused = false;
120
+ stateRef.current.isFocused = false;
122
121
 
123
122
  if (target.disabled) {
124
123
  // For backward compatibility, dispatch a (fake) React synthetic event.
125
- state.onBlur?.(new SyntheticFocusEvent('blur', e));
124
+ stateRef.current.onBlur?.(new SyntheticFocusEvent('blur', e));
126
125
  }
127
126
 
128
127
  // We no longer need the MutationObserver once the target is blurred.
129
- if (state.observer) {
130
- state.observer.disconnect();
131
- state.observer = null;
128
+ if (stateRef.current.observer) {
129
+ stateRef.current.observer.disconnect();
130
+ stateRef.current.observer = null;
132
131
  }
133
132
  };
134
133
 
135
134
  target.addEventListener('focusout', onBlurHandler, {once: true});
136
135
 
137
- state.observer = new MutationObserver(() => {
138
- if (state.isFocused && target.disabled) {
139
- state.observer.disconnect();
136
+ stateRef.current.observer = new MutationObserver(() => {
137
+ if (stateRef.current.isFocused && target.disabled) {
138
+ stateRef.current.observer.disconnect();
140
139
  target.dispatchEvent(new FocusEvent('blur'));
141
140
  target.dispatchEvent(new FocusEvent('focusout', {bubbles: true}));
142
141
  }
143
142
  });
144
143
 
145
- state.observer.observe(target, {attributes: true, attributeFilter: ['disabled']});
144
+ stateRef.current.observer.observe(target, {attributes: true, attributeFilter: ['disabled']});
146
145
  }
147
- };
146
+ }, []);
148
147
  }