diff options
author | Christopher Ng <chrng8@gmail.com> | 2023-04-20 19:48:44 -0700 |
---|---|---|
committer | Christopher Ng <chrng8@gmail.com> | 2023-04-24 15:45:40 -0700 |
commit | 79310bd54533a9dfd21c41796cecabdbc8ba94ca (patch) | |
tree | 44a8ae6854fd140c100ead18586d3db72c0630b6 /core | |
parent | 87ae9bbc12c1cddb8ca4685c4c46d01d11025ae0 (diff) | |
download | nextcloud-server-79310bd54533a9dfd21c41796cecabdbc8ba94ca.tar.gz nextcloud-server-79310bd54533a9dfd21c41796cecabdbc8ba94ca.zip |
Trap focus in dialogs
Signed-off-by: Christopher Ng <chrng8@gmail.com>
Diffstat (limited to 'core')
-rw-r--r-- | core/src/jquery/ocdialog.js | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/core/src/jquery/ocdialog.js b/core/src/jquery/ocdialog.js index 63b8082da06..c0ef31fc67b 100644 --- a/core/src/jquery/ocdialog.js +++ b/core/src/jquery/ocdialog.js @@ -24,6 +24,7 @@ */ import $ from 'jquery' +import { createFocusTrap } from 'focus-trap' import { isA11yActivation } from '../Util/a11y.js' $.widget('oc.ocdialog', { @@ -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) { @@ -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 }, @@ -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. |