Skip to content

Commit 1a9e7f6

Browse files
Pytalartonge
authored andcommitted
Trap focus in dialogs
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent 56b1306 commit 1a9e7f6

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

core/src/jquery/ocdialog.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
*/
2525

2626
import $ from 'jquery'
27-
import { isA11yActivation } from '../Util/a11y'
27+
import { createFocusTrap } from 'focus-trap'
28+
import { isA11yActivation } from '../Util/a11y.js'
2829

2930
$.widget('oc.ocdialog', {
3031
options: {
@@ -114,9 +115,9 @@ $.widget('oc.ocdialog', {
114115

115116
this._setOptions(this.options)
116117
this._createOverlay()
118+
this._useFocusTrap()
117119
},
118120
_init() {
119-
this.$dialog.focus()
120121
this._trigger('open')
121122
},
122123
_setOption(key, value) {
@@ -252,6 +253,23 @@ $.widget('oc.ocdialog', {
252253
this.overlay = null
253254
}
254255
},
256+
_useFocusTrap() {
257+
// Create global stack if undefined
258+
Object.assign(window, { _nc_focus_trap: window._nc_focus_trap || [] })
259+
260+
const dialogElement = this.$dialog[0]
261+
this.focusTrap = createFocusTrap(dialogElement, {
262+
allowOutsideClick: true,
263+
trapStack: window._nc_focus_trap,
264+
fallbackFocus: dialogElement,
265+
})
266+
267+
this.focusTrap.activate()
268+
},
269+
_clearFocusTrap() {
270+
this.focusTrap?.deactivate()
271+
this.focusTrap = null
272+
},
255273
widget() {
256274
return this.$dialog
257275
},
@@ -262,6 +280,7 @@ $.widget('oc.ocdialog', {
262280
this.enterCallback = null
263281
},
264282
close() {
283+
this._clearFocusTrap()
265284
this._destroyOverlay()
266285
const self = this
267286
// Ugly hack to catch remaining keyup events.

dist/core-common.js

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

dist/core-common.js.LICENSE.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,12 @@
316316
*/
317317

318318
/*!
319-
* focus-trap 7.1.0
319+
* focus-trap 7.2.0
320320
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE
321321
*/
322322

323323
/*!
324-
* focus-trap 7.2.0
324+
* focus-trap 7.4.3
325325
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE
326326
*/
327327

@@ -330,6 +330,11 @@
330330
* @license MIT, https://github.com/focus-trap/tabbable/blob/master/LICENSE
331331
*/
332332

333+
/*!
334+
* tabbable 6.1.2
335+
* @license MIT, https://github.com/focus-trap/tabbable/blob/master/LICENSE
336+
*/
337+
333338
/*! @license DOMPurify 2.4.4 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/2.4.4/LICENSE */
334339

335340
/*! For license information please see NcActionSeparator.js.LICENSE.txt */

dist/core-common.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)