diff options
Diffstat (limited to 'js/src/util/focustrap.js')
| -rw-r--r-- | js/src/util/focustrap.js | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/js/src/util/focustrap.js b/js/src/util/focustrap.js index a1975f489..158f3d184 100644 --- a/js/src/util/focustrap.js +++ b/js/src/util/focustrap.js @@ -1,13 +1,13 @@ /** * -------------------------------------------------------------------------- - * Bootstrap (v5.1.3): util/focustrap.js + * Bootstrap util/focustrap.js * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * -------------------------------------------------------------------------- */ -import EventHandler from '../dom/event-handler' -import SelectorEngine from '../dom/selector-engine' -import { typeCheckConfig } from './index' +import EventHandler from '../dom/event-handler.js' +import SelectorEngine from '../dom/selector-engine.js' +import Config from './config.js' /** * Constants @@ -24,36 +24,48 @@ const TAB_NAV_FORWARD = 'forward' const TAB_NAV_BACKWARD = 'backward' const Default = { - trapElement: null, // The element to trap focus inside of - autofocus: true + autofocus: true, + trapElement: null // The element to trap focus inside of } const DefaultType = { - trapElement: 'element', - autofocus: 'boolean' + autofocus: 'boolean', + trapElement: 'element' } /** * Class definition */ -class FocusTrap { +class FocusTrap extends Config { constructor(config) { + super() this._config = this._getConfig(config) this._isActive = false this._lastTabNavDirection = null } + // Getters + static get Default() { + return Default + } + + static get DefaultType() { + return DefaultType + } + + static get NAME() { + return NAME + } + // Public activate() { - const { trapElement, autofocus } = this._config - if (this._isActive) { return } - if (autofocus) { - trapElement.focus() + if (this._config.autofocus) { + this._config.trapElement.focus() } EventHandler.off(document, EVENT_KEY) // guard against infinite focus loop @@ -74,10 +86,9 @@ class FocusTrap { // Private _handleFocusin(event) { - const { target } = event const { trapElement } = this._config - if (target === document || target === trapElement || trapElement.contains(target)) { + if (event.target === document || event.target === trapElement || trapElement.contains(event.target)) { return } @@ -99,15 +110,6 @@ class FocusTrap { this._lastTabNavDirection = event.shiftKey ? TAB_NAV_BACKWARD : TAB_NAV_FORWARD } - - _getConfig(config) { - config = { - ...Default, - ...(typeof config === 'object' ? config : {}) - } - typeCheckConfig(NAME, config, DefaultType) - return config - } } export default FocusTrap |
