Skip to content

Commit

Permalink
Trap focus in dialogs
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <chrng8@gmail.com>
  • Loading branch information
Pytal authored and backportbot-nextcloud[bot] committed Jun 6, 2023
1 parent 2bc7426 commit 91e4224
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion core/src/jquery/ocdialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ $.widget('oc.ocdialog', {

this._setOptions(this.options)
this._createOverlay()
this._useFocusTrap()
},
_init() {
this.$dialog.focus()
this._trigger('open')
},
_setOption(key, value) {
Expand Down Expand Up @@ -252,6 +252,23 @@ $.widget('oc.ocdialog', {
this.overlay = null
}
},
_useFocusTrap() {
// Create global stack if undefined
Object.assign(window, { _nc_focus_trap: window._nc_focus_trap || [] })

const dialogElement = this.$dialog[0]
this.focusTrap = createFocusTrap(dialogElement, {
allowOutsideClick: true,
trapStack: window._nc_focus_trap,
fallbackFocus: dialogElement,
})

this.focusTrap.activate()
},
_clearFocusTrap() {
this.focusTrap?.deactivate()
this.focusTrap = null
},
widget() {
return this.$dialog
},
Expand All @@ -262,6 +279,7 @@ $.widget('oc.ocdialog', {
this.enterCallback = null
},
close() {
this._clearFocusTrap()
this._destroyOverlay()
const self = this
// Ugly hack to catch remaining keyup events.
Expand Down

0 comments on commit 91e4224

Please sign in to comment.