1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import {isMac, isVirtualClick} from '@react-aria/utils';
|
19 | import {useEffect, useState} from 'react';
|
20 | import {useIsSSR} from '@react-aria/ssr';
|
21 |
|
22 | export type Modality = 'keyboard' | 'pointer' | 'virtual';
|
23 | type HandlerEvent = PointerEvent | MouseEvent | KeyboardEvent | FocusEvent | null;
|
24 | type Handler = (modality: Modality, e: HandlerEvent) => void;
|
25 | export type FocusVisibleHandler = (isFocusVisible: boolean) => void;
|
26 | export interface FocusVisibleProps {
|
27 |
|
28 | isTextInput?: boolean,
|
29 |
|
30 | autoFocus?: boolean
|
31 | }
|
32 |
|
33 | export interface FocusVisibleResult {
|
34 |
|
35 | isFocusVisible: boolean
|
36 | }
|
37 |
|
38 | let currentModality: null | Modality = null;
|
39 | let changeHandlers = new Set<Handler>();
|
40 | let hasSetupGlobalListeners = false;
|
41 | let hasEventBeforeFocus = false;
|
42 | let hasBlurredWindowRecently = false;
|
43 |
|
44 |
|
45 | const FOCUS_VISIBLE_INPUT_KEYS = {
|
46 | Tab: true,
|
47 | Escape: true
|
48 | };
|
49 |
|
50 | function triggerChangeHandlers(modality: Modality, e: HandlerEvent) {
|
51 | for (let handler of changeHandlers) {
|
52 | handler(modality, e);
|
53 | }
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function isValidKey(e: KeyboardEvent) {
|
60 |
|
61 | return !(e.metaKey || (!isMac() && e.altKey) || e.ctrlKey || e.key === 'Control' || e.key === 'Shift' || e.key === 'Meta');
|
62 | }
|
63 |
|
64 |
|
65 | function handleKeyboardEvent(e: KeyboardEvent) {
|
66 | hasEventBeforeFocus = true;
|
67 | if (isValidKey(e)) {
|
68 | currentModality = 'keyboard';
|
69 | triggerChangeHandlers('keyboard', e);
|
70 | }
|
71 | }
|
72 |
|
73 | function handlePointerEvent(e: PointerEvent | MouseEvent) {
|
74 | currentModality = 'pointer';
|
75 | if (e.type === 'mousedown' || e.type === 'pointerdown') {
|
76 | hasEventBeforeFocus = true;
|
77 | triggerChangeHandlers('pointer', e);
|
78 | }
|
79 | }
|
80 |
|
81 | function handleClickEvent(e: MouseEvent) {
|
82 | if (isVirtualClick(e)) {
|
83 | hasEventBeforeFocus = true;
|
84 | currentModality = 'virtual';
|
85 | }
|
86 | }
|
87 |
|
88 | function handleFocusEvent(e: FocusEvent) {
|
89 |
|
90 |
|
91 |
|
92 | if (e.target === window || e.target === document) {
|
93 | return;
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 | if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
|
99 | currentModality = 'virtual';
|
100 | triggerChangeHandlers('virtual', e);
|
101 | }
|
102 |
|
103 | hasEventBeforeFocus = false;
|
104 | hasBlurredWindowRecently = false;
|
105 | }
|
106 |
|
107 | function handleWindowBlur() {
|
108 |
|
109 |
|
110 | hasEventBeforeFocus = false;
|
111 | hasBlurredWindowRecently = true;
|
112 | }
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | function setupGlobalFocusEvents() {
|
118 | if (typeof window === 'undefined' || hasSetupGlobalListeners) {
|
119 | return;
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | let focus = HTMLElement.prototype.focus;
|
127 | HTMLElement.prototype.focus = function () {
|
128 | hasEventBeforeFocus = true;
|
129 | focus.apply(this, arguments as unknown as [options?: FocusOptions | undefined]);
|
130 | };
|
131 |
|
132 | document.addEventListener('keydown', handleKeyboardEvent, true);
|
133 | document.addEventListener('keyup', handleKeyboardEvent, true);
|
134 | document.addEventListener('click', handleClickEvent, true);
|
135 |
|
136 |
|
137 |
|
138 | window.addEventListener('focus', handleFocusEvent, true);
|
139 | window.addEventListener('blur', handleWindowBlur, false);
|
140 |
|
141 | if (typeof PointerEvent !== 'undefined') {
|
142 | document.addEventListener('pointerdown', handlePointerEvent, true);
|
143 | document.addEventListener('pointermove', handlePointerEvent, true);
|
144 | document.addEventListener('pointerup', handlePointerEvent, true);
|
145 | } else {
|
146 | document.addEventListener('mousedown', handlePointerEvent, true);
|
147 | document.addEventListener('mousemove', handlePointerEvent, true);
|
148 | document.addEventListener('mouseup', handlePointerEvent, true);
|
149 | }
|
150 |
|
151 | hasSetupGlobalListeners = true;
|
152 | }
|
153 |
|
154 | if (typeof document !== 'undefined') {
|
155 | if (document.readyState !== 'loading') {
|
156 | setupGlobalFocusEvents();
|
157 | } else {
|
158 | document.addEventListener('DOMContentLoaded', setupGlobalFocusEvents);
|
159 | }
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | export function isFocusVisible(): boolean {
|
166 | return currentModality !== 'pointer';
|
167 | }
|
168 |
|
169 | export function getInteractionModality(): Modality | null {
|
170 | return currentModality;
|
171 | }
|
172 |
|
173 | export function setInteractionModality(modality: Modality) {
|
174 | currentModality = modality;
|
175 | triggerChangeHandlers(modality, null);
|
176 | }
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | export function useInteractionModality(): Modality | null {
|
182 | setupGlobalFocusEvents();
|
183 |
|
184 | let [modality, setModality] = useState(currentModality);
|
185 | useEffect(() => {
|
186 | let handler = () => {
|
187 | setModality(currentModality);
|
188 | };
|
189 |
|
190 | changeHandlers.add(handler);
|
191 | return () => {
|
192 | changeHandlers.delete(handler);
|
193 | };
|
194 | }, []);
|
195 |
|
196 | return useIsSSR() ? null : modality;
|
197 | }
|
198 |
|
199 | const nonTextInputTypes = new Set([
|
200 | 'checkbox',
|
201 | 'radio',
|
202 | 'range',
|
203 | 'color',
|
204 | 'file',
|
205 | 'image',
|
206 | 'button',
|
207 | 'submit',
|
208 | 'reset'
|
209 | ]);
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | function isKeyboardFocusEvent(isTextInput: boolean, modality: Modality, e: HandlerEvent) {
|
216 | isTextInput = isTextInput ||
|
217 | (e?.target instanceof HTMLInputElement && !nonTextInputTypes.has(e?.target?.type)) ||
|
218 | e?.target instanceof HTMLTextAreaElement ||
|
219 | (e?.target instanceof HTMLElement && e?.target.isContentEditable);
|
220 | return !(isTextInput && modality === 'keyboard' && e instanceof KeyboardEvent && !FOCUS_VISIBLE_INPUT_KEYS[e.key]);
|
221 | }
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | export function useFocusVisible(props: FocusVisibleProps = {}): FocusVisibleResult {
|
227 | let {isTextInput, autoFocus} = props;
|
228 | let [isFocusVisibleState, setFocusVisible] = useState(autoFocus || isFocusVisible());
|
229 | useFocusVisibleListener((isFocusVisible) => {
|
230 | setFocusVisible(isFocusVisible);
|
231 | }, [isTextInput], {isTextInput});
|
232 |
|
233 | return {isFocusVisible: isFocusVisibleState};
|
234 | }
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | export function useFocusVisibleListener(fn: FocusVisibleHandler, deps: ReadonlyArray<any>, opts?: {isTextInput?: boolean}): void {
|
240 | setupGlobalFocusEvents();
|
241 |
|
242 | useEffect(() => {
|
243 | let handler = (modality: Modality, e: HandlerEvent) => {
|
244 | if (!isKeyboardFocusEvent(!!(opts?.isTextInput), modality, e)) {
|
245 | return;
|
246 | }
|
247 | fn(isFocusVisible());
|
248 | };
|
249 | changeHandlers.add(handler);
|
250 | return () => {
|
251 | changeHandlers.delete(handler);
|
252 | };
|
253 |
|
254 | }, deps);
|
255 | }
|