1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | import {FocusEvent as ReactFocusEvent, useCallback, useRef} from 'react';
|
14 | import {useEffectEvent, useLayoutEffect} from '@react-aria/utils';
|
15 |
|
16 | export class SyntheticFocusEvent<Target = Element> implements ReactFocusEvent<Target> {
|
17 | nativeEvent: FocusEvent;
|
18 | target: EventTarget & Target;
|
19 | currentTarget: EventTarget & Target;
|
20 | relatedTarget: Element;
|
21 | bubbles: boolean;
|
22 | cancelable: boolean;
|
23 | defaultPrevented: boolean;
|
24 | eventPhase: number;
|
25 | isTrusted: boolean;
|
26 | timeStamp: number;
|
27 | type: string;
|
28 |
|
29 | constructor(type: string, nativeEvent: FocusEvent) {
|
30 | this.nativeEvent = nativeEvent;
|
31 | this.target = nativeEvent.target as EventTarget & Target;
|
32 | this.currentTarget = nativeEvent.currentTarget as EventTarget & Target;
|
33 | this.relatedTarget = nativeEvent.relatedTarget as Element;
|
34 | this.bubbles = nativeEvent.bubbles;
|
35 | this.cancelable = nativeEvent.cancelable;
|
36 | this.defaultPrevented = nativeEvent.defaultPrevented;
|
37 | this.eventPhase = nativeEvent.eventPhase;
|
38 | this.isTrusted = nativeEvent.isTrusted;
|
39 | this.timeStamp = nativeEvent.timeStamp;
|
40 | this.type = type;
|
41 | }
|
42 |
|
43 | isDefaultPrevented(): boolean {
|
44 | return this.nativeEvent.defaultPrevented;
|
45 | }
|
46 |
|
47 | preventDefault(): void {
|
48 | this.defaultPrevented = true;
|
49 | this.nativeEvent.preventDefault();
|
50 | }
|
51 |
|
52 | stopPropagation(): void {
|
53 | this.nativeEvent.stopPropagation();
|
54 | this.isPropagationStopped = () => true;
|
55 | }
|
56 |
|
57 | isPropagationStopped(): boolean {
|
58 | return false;
|
59 | }
|
60 |
|
61 | persist() {}
|
62 | }
|
63 |
|
64 | export function useSyntheticBlurEvent<Target = Element>(onBlur: (e: ReactFocusEvent<Target>) => void) {
|
65 | let stateRef = useRef({
|
66 | isFocused: false,
|
67 | observer: null as MutationObserver | null
|
68 | });
|
69 |
|
70 |
|
71 |
|
72 | useLayoutEffect(() => {
|
73 | const state = stateRef.current;
|
74 | return () => {
|
75 | if (state.observer) {
|
76 | state.observer.disconnect();
|
77 | state.observer = null;
|
78 | }
|
79 | };
|
80 | }, []);
|
81 |
|
82 | let dispatchBlur = useEffectEvent((e: SyntheticFocusEvent<Target>) => {
|
83 | onBlur?.(e);
|
84 | });
|
85 |
|
86 |
|
87 | return useCallback((e: ReactFocusEvent<Target>) => {
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | if (
|
93 | e.target instanceof HTMLButtonElement ||
|
94 | e.target instanceof HTMLInputElement ||
|
95 | e.target instanceof HTMLTextAreaElement ||
|
96 | e.target instanceof HTMLSelectElement
|
97 | ) {
|
98 | stateRef.current.isFocused = true;
|
99 |
|
100 | let target = e.target;
|
101 | let onBlurHandler: EventListenerOrEventListenerObject | null = (e) => {
|
102 | stateRef.current.isFocused = false;
|
103 |
|
104 | if (target.disabled) {
|
105 |
|
106 | dispatchBlur(new SyntheticFocusEvent('blur', e as FocusEvent));
|
107 | }
|
108 |
|
109 |
|
110 | if (stateRef.current.observer) {
|
111 | stateRef.current.observer.disconnect();
|
112 | stateRef.current.observer = null;
|
113 | }
|
114 | };
|
115 |
|
116 | target.addEventListener('focusout', onBlurHandler, {once: true});
|
117 |
|
118 | stateRef.current.observer = new MutationObserver(() => {
|
119 | if (stateRef.current.isFocused && target.disabled) {
|
120 | stateRef.current.observer?.disconnect();
|
121 | let relatedTargetEl = target === document.activeElement ? null : document.activeElement;
|
122 | target.dispatchEvent(new FocusEvent('blur', {relatedTarget: relatedTargetEl}));
|
123 | target.dispatchEvent(new FocusEvent('focusout', {bubbles: true, relatedTarget: relatedTargetEl}));
|
124 | }
|
125 | });
|
126 |
|
127 | stateRef.current.observer.observe(target, {attributes: true, attributeFilter: ['disabled']});
|
128 | }
|
129 | }, [dispatchBlur]);
|
130 | }
|