1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | import {FocusEvent as ReactFocusEvent, useCallback, useRef} from 'react';
|
14 | import {useLayoutEffect} from '@react-aria/utils';
|
15 |
|
16 | export class SyntheticFocusEvent implements ReactFocusEvent {
|
17 | nativeEvent: FocusEvent;
|
18 | target: Element;
|
19 | currentTarget: Element;
|
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 Element;
|
32 | this.currentTarget = nativeEvent.currentTarget as Element;
|
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(onBlur: (e: ReactFocusEvent) => void) {
|
65 | let stateRef = useRef({
|
66 | isFocused: false,
|
67 | onBlur,
|
68 | observer: null as MutationObserver
|
69 | });
|
70 | stateRef.current.onBlur = onBlur;
|
71 |
|
72 |
|
73 |
|
74 | useLayoutEffect(() => {
|
75 | const state = stateRef.current;
|
76 | return () => {
|
77 | if (state.observer) {
|
78 | state.observer.disconnect();
|
79 | state.observer = null;
|
80 | }
|
81 | };
|
82 | }, []);
|
83 |
|
84 |
|
85 | return useCallback((e: ReactFocusEvent) => {
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | if (
|
91 | e.target instanceof HTMLButtonElement ||
|
92 | e.target instanceof HTMLInputElement ||
|
93 | e.target instanceof HTMLTextAreaElement ||
|
94 | e.target instanceof HTMLSelectElement
|
95 | ) {
|
96 | stateRef.current.isFocused = true;
|
97 |
|
98 | let target = e.target;
|
99 | let onBlurHandler = (e: FocusEvent) => {
|
100 | stateRef.current.isFocused = false;
|
101 |
|
102 | if (target.disabled) {
|
103 |
|
104 | stateRef.current.onBlur?.(new SyntheticFocusEvent('blur', e));
|
105 | }
|
106 |
|
107 |
|
108 | if (stateRef.current.observer) {
|
109 | stateRef.current.observer.disconnect();
|
110 | stateRef.current.observer = null;
|
111 | }
|
112 | };
|
113 |
|
114 | target.addEventListener('focusout', onBlurHandler, {once: true});
|
115 |
|
116 | stateRef.current.observer = new MutationObserver(() => {
|
117 | if (stateRef.current.isFocused && target.disabled) {
|
118 | stateRef.current.observer.disconnect();
|
119 | target.dispatchEvent(new FocusEvent('blur'));
|
120 | target.dispatchEvent(new FocusEvent('focusout', {bubbles: true}));
|
121 | }
|
122 | });
|
123 |
|
124 | stateRef.current.observer.observe(target, {attributes: true, attributeFilter: ['disabled']});
|
125 | }
|
126 | }, []);
|
127 | }
|