Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable25] Trap focus in dialogs #38668

Merged
merged 2 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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-common.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions dist/core-common.js.LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@
*/

/*!
* focus-trap 7.1.0
* focus-trap 7.2.0
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE
*/

/*!
* focus-trap 7.2.0
* focus-trap 7.4.3
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE
*/

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

/*!
* tabbable 6.1.2
* @license MIT, https://github.com/focus-trap/tabbable/blob/master/LICENSE
*/

/*! @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 */

/*! For license information please see NcActionSeparator.js.LICENSE.txt */
Expand Down
2 changes: 1 addition & 1 deletion dist/core-common.js.map

Large diffs are not rendered by default.

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.

29 changes: 15 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"debounce": "^1.2.1",
"dompurify": "^2.3.6",
"escape-html": "^1.0.3",
"focus-trap": "^7.4.0",
"focus-visible": "^5.2.0",
"handlebars": "^4.7.7",
"ical.js": "^1.4.0",
Expand Down