Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Trap focus in dialogs
Signed-off-by: Christopher Ng <[email protected]>
(cherry picked from commit 79310bd)
  • Loading branch information
Pytal authored and artonge committed Apr 25, 2023
commit c02a34fe7f615d8cc4bd495a588ae367c22cc03c
23 changes: 21 additions & 2 deletions core/src/jquery/ocdialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
*/

import $ from 'jquery'
import { isA11yActivation } from '../Util/a11y'
import { createFocusTrap } from 'focus-trap'
import { isA11yActivation } from '../Util/a11y.js'

$.widget('oc.ocdialog', {
options: {
Expand Down Expand Up @@ -114,9 +115,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 +253,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 +280,7 @@ $.widget('oc.ocdialog', {
this.enterCallback = null
},
close() {
this._clearFocusTrap()
this._destroyOverlay()
const self = this
// Ugly hack to catch remaining keyup events.
Expand Down
4 changes: 2 additions & 2 deletions dist/core-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-main.js.map

Large diffs are not rendered by default.