-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathfocus-management.ts
319 lines (266 loc) · 9.54 KB
/
focus-management.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import type { MutableRefObject } from 'react'
import { disposables } from './disposables'
import { match } from './match'
import { getOwnerDocument } from './owner'
// Credit:
// - https://stackoverflow.com/a/30753870
export let focusableSelector = [
'[contentEditable=true]',
'[tabindex]',
'a[href]',
'area[href]',
'button:not([disabled])',
'iframe',
'input:not([disabled])',
'select:not([disabled])',
'summary',
'textarea:not([disabled])',
]
.map(
process.env.NODE_ENV === 'test'
? // TODO: Remove this once JSDOM fixes the issue where an element that is
// "hidden" can be the document.activeElement, because this is not possible
// in real browsers.
(selector) => `${selector}:not([tabindex='-1']):not([style*='display: none'])`
: (selector) => `${selector}:not([tabindex='-1'])`
)
.join(',')
let autoFocusableSelector = [
// In a perfect world this was just `autofocus`, but React doesn't pass `autofocus` to the DOM...
'[data-autofocus]',
]
.map(
process.env.NODE_ENV === 'test'
? // TODO: Remove this once JSDOM fixes the issue where an element that is
// "hidden" can be the document.activeElement, because this is not possible
// in real browsers.
(selector) => `${selector}:not([tabindex='-1']):not([style*='display: none'])`
: (selector) => `${selector}:not([tabindex='-1'])`
)
.join(',')
export enum Focus {
/** Focus the first non-disabled element */
First = 1 << 0,
/** Focus the previous non-disabled element */
Previous = 1 << 1,
/** Focus the next non-disabled element */
Next = 1 << 2,
/** Focus the last non-disabled element */
Last = 1 << 3,
/** Wrap tab around */
WrapAround = 1 << 4,
/** Prevent scrolling the focusable elements into view */
NoScroll = 1 << 5,
/** Focus the first focusable element with the `data-autofocus` attribute. */
AutoFocus = 1 << 6,
}
export enum FocusResult {
/** Something went wrong while trying to focus. */
Error,
/** When `Focus.WrapAround` is enabled, going from position `N` to `N+1` where `N` is the last index in the array, then we overflow. */
Overflow,
/** Focus was successful. */
Success,
/** When `Focus.WrapAround` is enabled, going from position `N` to `N-1` where `N` is the first index in the array, then we underflow. */
Underflow,
}
enum Direction {
Previous = -1,
Next = 1,
}
export function getFocusableElements(container: HTMLElement | null = document.body) {
if (container == null) return []
return Array.from(container.querySelectorAll<HTMLElement>(focusableSelector)).sort(
// We want to move `tabIndex={0}` to the end of the list, this is what the browser does as well.
(a, z) =>
Math.sign((a.tabIndex || Number.MAX_SAFE_INTEGER) - (z.tabIndex || Number.MAX_SAFE_INTEGER))
)
}
export function getAutoFocusableElements(container: HTMLElement | null = document.body) {
if (container == null) return []
return Array.from(container.querySelectorAll<HTMLElement>(autoFocusableSelector)).sort(
// We want to move `tabIndex={0}` to the end of the list, this is what the browser does as well.
(a, z) =>
Math.sign((a.tabIndex || Number.MAX_SAFE_INTEGER) - (z.tabIndex || Number.MAX_SAFE_INTEGER))
)
}
export enum FocusableMode {
/** The element itself must be focusable. */
Strict,
/** The element should be inside of a focusable element. */
Loose,
}
export function isFocusableElement(
element: HTMLElement,
mode: FocusableMode = FocusableMode.Strict
) {
if (element === getOwnerDocument(element)?.body) return false
return match(mode, {
[FocusableMode.Strict]() {
return element.matches(focusableSelector)
},
[FocusableMode.Loose]() {
let next: HTMLElement | null = element
while (next !== null) {
if (next.matches(focusableSelector)) return true
next = next.parentElement
}
return false
},
})
}
export function restoreFocusIfNecessary(element: HTMLElement | null) {
let ownerDocument = getOwnerDocument(element)
disposables().nextFrame(() => {
if (
ownerDocument &&
!isFocusableElement(ownerDocument.activeElement as HTMLElement, FocusableMode.Strict)
) {
focusElement(element)
}
})
}
// The method of triggering an action, this is used to determine how we should
// restore focus after an action has been performed.
enum ActivationMethod {
/* If the action was triggered by a keyboard event. */
Keyboard = 0,
/* If the action was triggered by a mouse / pointer / ... event.*/
Mouse = 1,
}
// We want to be able to set and remove the `data-headlessui-mouse` attribute on the `html` element.
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
document.addEventListener(
'keydown',
(event) => {
if (event.metaKey || event.altKey || event.ctrlKey) {
return
}
document.documentElement.dataset.headlessuiFocusVisible = ''
},
true
)
document.addEventListener(
'click',
(event) => {
// Event originated from an actual mouse click
if (event.detail === ActivationMethod.Mouse) {
delete document.documentElement.dataset.headlessuiFocusVisible
}
// Event originated from a keyboard event that triggered the `click` event
else if (event.detail === ActivationMethod.Keyboard) {
document.documentElement.dataset.headlessuiFocusVisible = ''
}
},
true
)
}
export function focusElement(element: HTMLElement | null) {
element?.focus({ preventScroll: true })
}
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select
let selectableSelector = ['textarea', 'input'].join(',')
function isSelectableElement(
element: Element | null
): element is HTMLInputElement | HTMLTextAreaElement {
return element?.matches?.(selectableSelector) ?? false
}
export function sortByDomNode<T>(
nodes: T[],
resolveKey: (item: T) => HTMLElement | null = (i) => i as HTMLElement | null
): T[] {
return nodes.slice().sort((aItem, zItem) => {
let a = resolveKey(aItem)
let z = resolveKey(zItem)
if (a === null || z === null) return 0
let position = a.compareDocumentPosition(z)
if (position & Node.DOCUMENT_POSITION_FOLLOWING) return -1
if (position & Node.DOCUMENT_POSITION_PRECEDING) return 1
return 0
})
}
export function focusFrom(current: HTMLElement | null, focus: Focus) {
return focusIn(getFocusableElements(), focus, { relativeTo: current })
}
export function focusIn(
container: HTMLElement | HTMLElement[],
focus: Focus,
{
sorted = true,
relativeTo = null,
skipElements = [],
}: Partial<{
sorted: boolean
relativeTo: HTMLElement | null
skipElements: (HTMLElement | MutableRefObject<HTMLElement | null>)[]
}> = {}
) {
let ownerDocument = Array.isArray(container)
? container.length > 0
? container[0].ownerDocument
: document
: container.ownerDocument
let elements = Array.isArray(container)
? sorted
? sortByDomNode(container)
: container
: focus & Focus.AutoFocus
? getAutoFocusableElements(container)
: getFocusableElements(container)
if (skipElements.length > 0 && elements.length > 1) {
elements = elements.filter(
(element) =>
!skipElements.some(
(skipElement) =>
skipElement != null && 'current' in skipElement
? skipElement?.current === element // Handle MutableRefObject
: skipElement === element // Handle HTMLElement directly
)
)
}
relativeTo = relativeTo ?? (ownerDocument.activeElement as HTMLElement)
let direction = (() => {
if (focus & (Focus.First | Focus.Next)) return Direction.Next
if (focus & (Focus.Previous | Focus.Last)) return Direction.Previous
throw new Error('Missing Focus.First, Focus.Previous, Focus.Next or Focus.Last')
})()
let startIndex = (() => {
if (focus & Focus.First) return 0
if (focus & Focus.Previous) return Math.max(0, elements.indexOf(relativeTo)) - 1
if (focus & Focus.Next) return Math.max(0, elements.indexOf(relativeTo)) + 1
if (focus & Focus.Last) return elements.length - 1
throw new Error('Missing Focus.First, Focus.Previous, Focus.Next or Focus.Last')
})()
let focusOptions = focus & Focus.NoScroll ? { preventScroll: true } : {}
let offset = 0
let total = elements.length
let next = undefined
do {
// Guard against infinite loops
if (offset >= total || offset + total <= 0) return FocusResult.Error
let nextIdx = startIndex + offset
if (focus & Focus.WrapAround) {
nextIdx = (nextIdx + total) % total
} else {
if (nextIdx < 0) return FocusResult.Underflow
if (nextIdx >= total) return FocusResult.Overflow
}
next = elements[nextIdx]
// Try the focus the next element, might not work if it is "hidden" to the user.
next?.focus(focusOptions)
// Try the next one in line
offset += direction
} while (next !== ownerDocument.activeElement)
// By default if you <Tab> to a text input or a textarea, the browser will
// select all the text once the focus is inside these DOM Nodes. However,
// since we are manually moving focus this behavior is not happening. This
// code will make sure that the text gets selected as-if you did it manually.
// Note: We only do this when going forward / backward. Not for the
// Focus.First or Focus.Last actions. This is similar to the `autoFocus`
// behavior on an input where the input will get focus but won't be
// selected.
if (focus & (Focus.Next | Focus.Previous) && isSelectableElement(next)) {
next.select()
}
return FocusResult.Success
}