-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathindex.ts
295 lines (232 loc) · 9.22 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* Credit: Huge props to the team at Adobe for inspiring this implementation.
* https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/interactions/src/useFocusVisible.ts
*/
import { getDocument, getEventTarget, getWindow, isMac } from "@zag-js/dom-query"
function isVirtualClick(event: MouseEvent | PointerEvent): boolean {
if ((event as any).mozInputSource === 0 && event.isTrusted) return true
return event.detail === 0 && !(event as PointerEvent).pointerType
}
function isValidKey(e: KeyboardEvent) {
return !(
e.metaKey ||
(!isMac() && e.altKey) ||
e.ctrlKey ||
e.key === "Control" ||
e.key === "Shift" ||
e.key === "Meta"
)
}
const nonTextInputTypes = new Set(["checkbox", "radio", "range", "color", "file", "image", "button", "submit", "reset"])
function isKeyboardFocusEvent(isTextInput: boolean, modality: Modality, e: HandlerEvent) {
const target = e ? getEventTarget<Element>(e) : null
const win = getWindow(target)
isTextInput =
isTextInput ||
(target instanceof win.HTMLInputElement && !nonTextInputTypes.has(target?.type)) ||
target instanceof win.HTMLTextAreaElement ||
(target instanceof win.HTMLElement && target.isContentEditable)
return !(
isTextInput &&
modality === "keyboard" &&
e instanceof win.KeyboardEvent &&
!Reflect.has(FOCUS_VISIBLE_INPUT_KEYS, e.key)
)
}
/////////////////////////////////////////////////////////////////////////////////////////////
export type Modality = "keyboard" | "pointer" | "virtual"
type RootNode = Document | ShadowRoot | Node
type HandlerEvent = PointerEvent | MouseEvent | KeyboardEvent | FocusEvent | null
type Handler = (modality: Modality, e: HandlerEvent) => void
/////////////////////////////////////////////////////////////////////////////////////////////
let currentModality: Modality | null = null
let changeHandlers = new Set<Handler>()
interface GlobalListenerData {
focus: VoidFunction
}
export let listenerMap = new Map<Window, GlobalListenerData>()
let hasEventBeforeFocus = false
let hasBlurredWindowRecently = false
// Only Tab or Esc keys will make focus visible on text input elements
const FOCUS_VISIBLE_INPUT_KEYS = {
Tab: true,
Escape: true,
}
function triggerChangeHandlers(modality: Modality, e: HandlerEvent) {
for (let handler of changeHandlers) {
handler(modality, e)
}
}
function handleKeyboardEvent(e: KeyboardEvent) {
hasEventBeforeFocus = true
if (isValidKey(e)) {
currentModality = "keyboard"
triggerChangeHandlers("keyboard", e)
}
}
function handlePointerEvent(e: PointerEvent | MouseEvent) {
currentModality = "pointer"
if (e.type === "mousedown" || e.type === "pointerdown") {
hasEventBeforeFocus = true
triggerChangeHandlers("pointer", e)
}
}
function handleClickEvent(e: MouseEvent) {
if (isVirtualClick(e)) {
hasEventBeforeFocus = true
currentModality = "virtual"
}
}
function handleFocusEvent(e: FocusEvent) {
// Firefox fires two extra focus events when the user first clicks into an iframe:
// first on the window, then on the document. We ignore these events so they don't
// cause keyboard focus rings to appear.
const target = getEventTarget(e)
if (target === getWindow(target as Element) || target === getDocument(target as Element)) {
return
}
// If a focus event occurs without a preceding keyboard or pointer event, switch to virtual modality.
// This occurs, for example, when navigating a form with the next/previous buttons on iOS.
if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
currentModality = "virtual"
triggerChangeHandlers("virtual", e)
}
hasEventBeforeFocus = false
hasBlurredWindowRecently = false
}
function handleWindowBlur() {
// When the window is blurred, reset state. This is necessary when tabbing out of the window,
// for example, since a subsequent focus event won't be fired.
hasEventBeforeFocus = false
hasBlurredWindowRecently = true
}
/**
* Setup global event listeners to control when keyboard focus style should be visible.
*/
function setupGlobalFocusEvents(root?: RootNode) {
if (typeof window === "undefined" || listenerMap.get(getWindow(root))) {
return
}
const win = getWindow(root)
const doc = getDocument(root)
let focus = win.HTMLElement.prototype.focus
win.HTMLElement.prototype.focus = function () {
// For programmatic focus, we remove the focus visible state to prevent showing focus rings
// When `options.focusVisible` is supported in most browsers, we can remove this
// @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus#focusvisible
currentModality = "virtual"
triggerChangeHandlers("virtual", null)
hasEventBeforeFocus = true
focus.apply(this, arguments as unknown as [options?: FocusOptions | undefined])
}
doc.addEventListener("keydown", handleKeyboardEvent, true)
doc.addEventListener("keyup", handleKeyboardEvent, true)
doc.addEventListener("click", handleClickEvent, true)
win.addEventListener("focus", handleFocusEvent, true)
win.addEventListener("blur", handleWindowBlur, false)
if (typeof win.PointerEvent !== "undefined") {
doc.addEventListener("pointerdown", handlePointerEvent, true)
doc.addEventListener("pointermove", handlePointerEvent, true)
doc.addEventListener("pointerup", handlePointerEvent, true)
} else {
doc.addEventListener("mousedown", handlePointerEvent, true)
doc.addEventListener("mousemove", handlePointerEvent, true)
doc.addEventListener("mouseup", handlePointerEvent, true)
}
// Add unmount handler
win.addEventListener(
"beforeunload",
() => {
tearDownWindowFocusTracking(root)
},
{ once: true },
)
listenerMap.set(win, { focus })
}
const tearDownWindowFocusTracking = (root?: RootNode, loadListener?: () => void) => {
const win = getWindow(root)
const doc = getDocument(root)
if (loadListener) {
doc.removeEventListener("DOMContentLoaded", loadListener)
}
if (!listenerMap.has(win)) {
return
}
win.HTMLElement.prototype.focus = listenerMap.get(win)!.focus
doc.removeEventListener("keydown", handleKeyboardEvent, true)
doc.removeEventListener("keyup", handleKeyboardEvent, true)
doc.removeEventListener("click", handleClickEvent, true)
win.removeEventListener("focus", handleFocusEvent, true)
win.removeEventListener("blur", handleWindowBlur, false)
if (typeof win.PointerEvent !== "undefined") {
doc.removeEventListener("pointerdown", handlePointerEvent, true)
doc.removeEventListener("pointermove", handlePointerEvent, true)
doc.removeEventListener("pointerup", handlePointerEvent, true)
} else {
doc.removeEventListener("mousedown", handlePointerEvent, true)
doc.removeEventListener("mousemove", handlePointerEvent, true)
doc.removeEventListener("mouseup", handlePointerEvent, true)
}
listenerMap.delete(win)
}
/////////////////////////////////////////////////////////////////////////////////////////////
export function getInteractionModality(): Modality | null {
return currentModality
}
export function setInteractionModality(modality: Modality) {
currentModality = modality
triggerChangeHandlers(modality, null)
}
export interface InteractionModalityChangeDetails {
/** The modality of the interaction that caused the focus to be visible. */
modality: Modality | null
}
export interface InteractionModalityProps {
/** The root element to track focus visibility for. */
root?: RootNode | undefined
/** Callback to be called when the interaction modality changes. */
onChange: (details: InteractionModalityChangeDetails) => void
}
export function trackInteractionModality(props: InteractionModalityProps): VoidFunction {
const { onChange, root } = props
setupGlobalFocusEvents(root)
onChange({ modality: currentModality })
const handler = () => onChange({ modality: currentModality })
changeHandlers.add(handler)
return () => {
changeHandlers.delete(handler)
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
export function isFocusVisible(): boolean {
return currentModality === "keyboard"
}
export interface FocusVisibleChangeDetails {
/** Whether keyboard focus is visible globally. */
isFocusVisible: boolean
/** The modality of the interaction that caused the focus to be visible. */
modality: Modality | null
}
export interface FocusVisibleProps {
/** The root element to track focus visibility for. */
root?: RootNode | undefined
/** Whether the element is a text input. */
isTextInput?: boolean | undefined
/** Whether the element will be auto focused. */
autoFocus?: boolean | undefined
/** Callback to be called when the focus visibility changes. */
onChange?: ((details: FocusVisibleChangeDetails) => void) | undefined
}
export function trackFocusVisible(props: FocusVisibleProps = {}): VoidFunction {
const { isTextInput, autoFocus, onChange, root } = props
setupGlobalFocusEvents(root)
onChange?.({ isFocusVisible: autoFocus || isFocusVisible(), modality: currentModality })
const handler = (modality: Modality, e: HandlerEvent) => {
if (!isKeyboardFocusEvent(!!isTextInput, modality, e)) return
onChange?.({ isFocusVisible: isFocusVisible(), modality })
}
changeHandlers.add(handler)
return () => {
changeHandlers.delete(handler)
}
}