-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathFocusRegion.ts
More file actions
247 lines (222 loc) · 7.92 KB
/
FocusRegion.ts
File metadata and controls
247 lines (222 loc) · 7.92 KB
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import keycode from 'keycode'
import {
contains,
addEventListener,
ownerDocument,
findTabbable,
canUseDOM
} from '@instructure/ui-dom-utils'
import { uid } from '@instructure/uid'
import { logError as error } from '@instructure/console'
import { ScreenReaderFocusRegion } from './ScreenReaderFocusRegion'
import { KeyboardFocusRegion } from './KeyboardFocusRegion'
import { FocusRegionOptions } from './FocusRegionOptions'
class FocusRegion {
private _contextElement: Node | Element | null = null
private _options: FocusRegionOptions
private readonly _screenReaderFocusRegion: ScreenReaderFocusRegion
private readonly _keyboardFocusRegion: KeyboardFocusRegion
private readonly _id: string
private _listeners: ReturnType<typeof addEventListener>[] = []
private _active = false
private _documentClickTarget: Node | null = null
private _contextContainsTarget = false
constructor(element: Element | Node | null, options: FocusRegionOptions) {
this._options = options || {
shouldCloseOnDocumentClick: true,
shouldCloseOnEscape: true,
isTooltip: false
}
this._contextElement = element
this._screenReaderFocusRegion = new ScreenReaderFocusRegion(
element,
options
)
this._keyboardFocusRegion = new KeyboardFocusRegion(element, options)
this._id = uid()
}
updateElement(element: Element | Node, options?: FocusRegionOptions) {
this._contextElement = element
if (options) {
this._options = options
}
if (this._keyboardFocusRegion) {
this._keyboardFocusRegion.updateElement(element)
}
if (this._screenReaderFocusRegion) {
this._screenReaderFocusRegion.updateElement(element)
}
}
handleDismiss = (
event: React.MouseEvent | React.KeyboardEvent,
documentClick?: boolean
) => {
this._options.onDismiss?.(event, documentClick)
}
captureDocumentMousedown = (event: React.MouseEvent) => {
this._documentClickTarget = event.target as Node
this._contextContainsTarget = contains(
this._contextElement,
this._documentClickTarget
)
}
handleDocumentClick = (event: React.PointerEvent) => {
if (
this._options.shouldCloseOnDocumentClick &&
event.button === 0 &&
event.detail > 0 && // if event.detail is 0 then this is a keyboard and not a mouse press
!this._contextContainsTarget &&
//this prevents clicking on Tooltip from closing the parent dialog
!(
canUseDOM &&
this._documentClickTarget instanceof HTMLElement &&
this._documentClickTarget.closest('[role="tooltip"]')
)
) {
this.handleDismiss(event, true)
}
}
handleFrameClick = (event: React.MouseEvent, frame: HTMLIFrameElement) => {
if (!contains(this._contextElement, frame)) {
// dismiss if frame is not within the region
this.handleDismiss(event, true)
}
}
handleKeyUp = (event: React.KeyboardEvent) => {
if (
this._options.shouldCloseOnEscape &&
event.keyCode === keycode.codes.esc &&
!event.defaultPrevented
) {
// If a dialog contains an <input type="file"/> element and the user opens the file picker and closes it with the
// escape key then Firefox passes through that event which could close the parent dialog. This code prevents that
// from happening (listening for a `cancel` event doesn't seem to work in firefox)
const activeElement = ownerDocument(this._contextElement)?.activeElement
const fileInputFocused =
activeElement?.tagName === 'INPUT' &&
(<HTMLInputElement>activeElement).type === 'file'
if (fileInputFocused) {
;(<HTMLInputElement>activeElement).blur()
} else {
//This should prevent a Tooltip from closing when inside of a Modal
if (this._options.isTooltip) {
event.stopPropagation()
}
this.handleDismiss(event)
}
}
}
get id() {
return this._id
}
// Focused returns when the focus region is active. Checking focused with the active element
// is inconsistent across browsers (Safari/Firefox do not focus elements on click)
get focused() {
return this._active
}
get keyboardFocusable() {
return (findTabbable(this._contextElement) || []).length > 0
}
activate() {
if (!this._active) {
const doc = ownerDocument(this._contextElement)!
this._keyboardFocusRegion.activate()
this._screenReaderFocusRegion.activate()
if (this._options.shouldCloseOnDocumentClick) {
this._listeners.push(
addEventListener(doc, 'mousedown', this.captureDocumentMousedown)
)
this._listeners.push(
addEventListener(doc, 'click', this.handleDocumentClick)
)
Array.from(doc.getElementsByTagName('iframe')).forEach((el) => {
// listen for mouseup events on any iframes in the document
const frameDoc = el.contentDocument
if (frameDoc) {
this._listeners.push(
addEventListener(frameDoc, 'mouseup', (event) => {
this.handleFrameClick(event as React.MouseEvent, el)
})
)
}
})
}
//This will ensure that the Tooltip's Escape event listener is executed first
//because listeners in the capturing phase are called before other event listeners (like that of the Modal's Escape listener)
//so a Modal with a Tooltip will not get closed when closing the Tooltip with Escape
const useCapture = this._options.isTooltip
if (this._options.shouldCloseOnEscape) {
this._listeners.push(
addEventListener(doc, 'keyup', this.handleKeyUp, useCapture)
)
}
this._active = true
}
}
deactivate({ keyboard = true }: { keyboard?: boolean } = {}) {
if (this._active) {
this._listeners.forEach((listener) => {
listener.remove()
})
this._listeners = []
if (keyboard) {
this._keyboardFocusRegion.deactivate()
}
this._screenReaderFocusRegion.deactivate()
this._active = false
}
}
focus() {
error(
this._active,
`[FocusRegion] Cannot call '.focus()' on a region that is not currently active.`
)
this._keyboardFocusRegion.focus()
}
blur() {
error(
!this._active,
`[FocusRegion] Cannot call '.blur()' on a region that is currently active.`
)
this._keyboardFocusRegion.blur()
}
}
export default FocusRegion
export {
/**
* ---
* category: utilities/a11y
* ---
*
* Class for focus operations, manages [ScreenReaderFocusRegion](#ScreenReaderFocusRegion)
* and [KeyboardFocusRegion](#KeyboardFocusRegion) for the given DOM element.
* - Scoping focus within a given context (DOM node),
* - Mark active element for focus later
* - Return focus to the marked element
* @module FocusRegion
*/
FocusRegion
}