aboutsummaryrefslogtreecommitdiff
path: root/js/src/util/focustrap.js
diff options
context:
space:
mode:
authorBobby <[email protected]>2024-08-16 20:47:33 -0400
committerGitHub <[email protected]>2024-08-16 20:47:33 -0400
commit6b28433d9cfde435be8ec2bd6cf91e6324d08865 (patch)
tree8343c27b8b95ff5639233e81cf157f92e5688466 /js/src/util/focustrap.js
parentd53094ec16ba385faae2973ddee648698b32ab24 (diff)
parent048f56f51460df75e92a2f7b472e1c56baeb68f7 (diff)
downloadbootstrap-6b28433d9cfde435be8ec2bd6cf91e6324d08865.tar.xz
bootstrap-6b28433d9cfde435be8ec2bd6cf91e6324d08865.zip
Merge branch 'twbs:main' into mainHEADmain
Diffstat (limited to 'js/src/util/focustrap.js')
-rw-r--r--js/src/util/focustrap.js50
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