Skip to content

Commit 53f50e4

Browse files
Pytalartonge
authored andcommitted
Trap focus in dialogs
Signed-off-by: Christopher Ng <chrng8@gmail.com> Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent 2bc7426 commit 53f50e4

7 files changed

Lines changed: 47 additions & 23 deletions

File tree

core/src/jquery/ocdialog.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ $.widget('oc.ocdialog', {
114114

115115
this._setOptions(this.options)
116116
this._createOverlay()
117+
this._useFocusTrap()
117118
},
118119
_init() {
119-
this.$dialog.focus()
120120
this._trigger('open')
121121
},
122122
_setOption(key, value) {
@@ -252,6 +252,23 @@ $.widget('oc.ocdialog', {
252252
this.overlay = null
253253
}
254254
},
255+
_useFocusTrap() {
256+
// Create global stack if undefined
257+
Object.assign(window, { _nc_focus_trap: window._nc_focus_trap || [] })
258+
259+
const dialogElement = this.$dialog[0]
260+
this.focusTrap = createFocusTrap(dialogElement, {
261+
allowOutsideClick: true,
262+
trapStack: window._nc_focus_trap,
263+
fallbackFocus: dialogElement,
264+
})
265+
266+
this.focusTrap.activate()
267+
},
268+
_clearFocusTrap() {
269+
this.focusTrap?.deactivate()
270+
this.focusTrap = null
271+
},
255272
widget() {
256273
return this.$dialog
257274
},
@@ -262,6 +279,7 @@ $.widget('oc.ocdialog', {
262279
this.enterCallback = null
263280
},
264281
close() {
282+
this._clearFocusTrap()
265283
this._destroyOverlay()
266284
const self = this
267285
// 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.

package-lock.json

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

0 commit comments

Comments
 (0)