1 | /*!
|
---|
2 | * Bootstrap focustrap.js v5.3.3 (https://getbootstrap.com/)
|
---|
3 | * Copyright 2011-2024 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
|
---|
4 | * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
---|
5 | */
|
---|
6 | (function (global, factory) {
|
---|
7 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('../dom/event-handler.js'), require('../dom/selector-engine.js'), require('./config.js')) :
|
---|
8 | typeof define === 'function' && define.amd ? define(['../dom/event-handler', '../dom/selector-engine', './config'], factory) :
|
---|
9 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Focustrap = factory(global.EventHandler, global.SelectorEngine, global.Config));
|
---|
10 | })(this, (function (EventHandler, SelectorEngine, Config) { 'use strict';
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * --------------------------------------------------------------------------
|
---|
14 | * Bootstrap util/focustrap.js
|
---|
15 | * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
---|
16 | * --------------------------------------------------------------------------
|
---|
17 | */
|
---|
18 |
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Constants
|
---|
22 | */
|
---|
23 |
|
---|
24 | const NAME = 'focustrap';
|
---|
25 | const DATA_KEY = 'bs.focustrap';
|
---|
26 | const EVENT_KEY = `.${DATA_KEY}`;
|
---|
27 | const EVENT_FOCUSIN = `focusin${EVENT_KEY}`;
|
---|
28 | const EVENT_KEYDOWN_TAB = `keydown.tab${EVENT_KEY}`;
|
---|
29 | const TAB_KEY = 'Tab';
|
---|
30 | const TAB_NAV_FORWARD = 'forward';
|
---|
31 | const TAB_NAV_BACKWARD = 'backward';
|
---|
32 | const Default = {
|
---|
33 | autofocus: true,
|
---|
34 | trapElement: null // The element to trap focus inside of
|
---|
35 | };
|
---|
36 | const DefaultType = {
|
---|
37 | autofocus: 'boolean',
|
---|
38 | trapElement: 'element'
|
---|
39 | };
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Class definition
|
---|
43 | */
|
---|
44 |
|
---|
45 | class FocusTrap extends Config {
|
---|
46 | constructor(config) {
|
---|
47 | super();
|
---|
48 | this._config = this._getConfig(config);
|
---|
49 | this._isActive = false;
|
---|
50 | this._lastTabNavDirection = null;
|
---|
51 | }
|
---|
52 |
|
---|
53 | // Getters
|
---|
54 | static get Default() {
|
---|
55 | return Default;
|
---|
56 | }
|
---|
57 | static get DefaultType() {
|
---|
58 | return DefaultType;
|
---|
59 | }
|
---|
60 | static get NAME() {
|
---|
61 | return NAME;
|
---|
62 | }
|
---|
63 |
|
---|
64 | // Public
|
---|
65 | activate() {
|
---|
66 | if (this._isActive) {
|
---|
67 | return;
|
---|
68 | }
|
---|
69 | if (this._config.autofocus) {
|
---|
70 | this._config.trapElement.focus();
|
---|
71 | }
|
---|
72 | EventHandler.off(document, EVENT_KEY); // guard against infinite focus loop
|
---|
73 | EventHandler.on(document, EVENT_FOCUSIN, event => this._handleFocusin(event));
|
---|
74 | EventHandler.on(document, EVENT_KEYDOWN_TAB, event => this._handleKeydown(event));
|
---|
75 | this._isActive = true;
|
---|
76 | }
|
---|
77 | deactivate() {
|
---|
78 | if (!this._isActive) {
|
---|
79 | return;
|
---|
80 | }
|
---|
81 | this._isActive = false;
|
---|
82 | EventHandler.off(document, EVENT_KEY);
|
---|
83 | }
|
---|
84 |
|
---|
85 | // Private
|
---|
86 | _handleFocusin(event) {
|
---|
87 | const {
|
---|
88 | trapElement
|
---|
89 | } = this._config;
|
---|
90 | if (event.target === document || event.target === trapElement || trapElement.contains(event.target)) {
|
---|
91 | return;
|
---|
92 | }
|
---|
93 | const elements = SelectorEngine.focusableChildren(trapElement);
|
---|
94 | if (elements.length === 0) {
|
---|
95 | trapElement.focus();
|
---|
96 | } else if (this._lastTabNavDirection === TAB_NAV_BACKWARD) {
|
---|
97 | elements[elements.length - 1].focus();
|
---|
98 | } else {
|
---|
99 | elements[0].focus();
|
---|
100 | }
|
---|
101 | }
|
---|
102 | _handleKeydown(event) {
|
---|
103 | if (event.key !== TAB_KEY) {
|
---|
104 | return;
|
---|
105 | }
|
---|
106 | this._lastTabNavDirection = event.shiftKey ? TAB_NAV_BACKWARD : TAB_NAV_FORWARD;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | return FocusTrap;
|
---|
111 |
|
---|
112 | }));
|
---|
113 | //# sourceMappingURL=focustrap.js.map
|
---|