• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

twbs / bootstrap / 9492305618

13 Jun 2024 01:41AM UTC coverage: 95.999% (-0.07%) from 96.07%
9492305618

push

github

mdo
Docs: drop unused `.bi-exclamation-triangle-fill` class

665 of 726 branches covered (91.6%)

Branch coverage included in aggregate %.

2022 of 2073 relevant lines covered (97.54%)

262.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.91
/js/src/util/focustrap.js
1
/**
2
 * --------------------------------------------------------------------------
3
 * Bootstrap util/focustrap.js
4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
 * --------------------------------------------------------------------------
6
 */
7

8
import EventHandler from '../dom/event-handler.js'
9
import SelectorEngine from '../dom/selector-engine.js'
10
import Config from './config.js'
11

12
/**
13
 * Constants
14
 */
15

16
const NAME = 'focustrap'
3✔
17
const DATA_KEY = 'bs.focustrap'
3✔
18
const EVENT_KEY = `.${DATA_KEY}`
3✔
19
const EVENT_FOCUSIN = `focusin${EVENT_KEY}`
3✔
20
const EVENT_KEYDOWN_TAB = `keydown.tab${EVENT_KEY}`
3✔
21

22
const TAB_KEY = 'Tab'
3✔
23
const TAB_NAV_FORWARD = 'forward'
3✔
24
const TAB_NAV_BACKWARD = 'backward'
3✔
25

26
const Default = {
3✔
27
  autofocus: true,
28
  trapElement: null // The element to trap focus inside of
29
}
30

31
const DefaultType = {
3✔
32
  autofocus: 'boolean',
33
  trapElement: 'element'
34
}
35

36
/**
37
 * Class definition
38
 */
39

40
class FocusTrap extends Config {
41
  constructor(config) {
42
    super()
113✔
43
    this._config = this._getConfig(config)
113✔
44
    this._isActive = false
113✔
45
    this._lastTabNavDirection = null
113✔
46
  }
47

48
  // Getters
49
  static get Default() {
50
    return Default
113✔
51
  }
52

53
  static get DefaultType() {
54
    return DefaultType
113✔
55
  }
56

57
  static get NAME() {
58
    return NAME
×
59
  }
60

61
  // Public
62
  activate() {
63
    if (this._isActive) {
67!
64
      return
×
65
    }
66

67
    if (this._config.autofocus) {
67✔
68
      this._config.trapElement.focus()
66✔
69
    }
70

71
    EventHandler.off(document, EVENT_KEY) // guard against infinite focus loop
67✔
72
    EventHandler.on(document, EVENT_FOCUSIN, event => this._handleFocusin(event))
67✔
73
    EventHandler.on(document, EVENT_KEYDOWN_TAB, event => this._handleKeydown(event))
67✔
74

75
    this._isActive = true
67✔
76
  }
77

78
  deactivate() {
79
    if (!this._isActive) {
28✔
80
      return
5✔
81
    }
82

83
    this._isActive = false
23✔
84
    EventHandler.off(document, EVENT_KEY)
23✔
85
  }
86

87
  // Private
88
  _handleFocusin(event) {
89
    const { trapElement } = this._config
46✔
90

91
    if (event.target === document || event.target === trapElement || trapElement.contains(event.target)) {
46✔
92
      return
2✔
93
    }
94

95
    const elements = SelectorEngine.focusableChildren(trapElement)
44✔
96

97
    if (elements.length === 0) {
44✔
98
      trapElement.focus()
41✔
99
    } else if (this._lastTabNavDirection === TAB_NAV_BACKWARD) {
3✔
100
      elements[elements.length - 1].focus()
1✔
101
    } else {
102
      elements[0].focus()
2✔
103
    }
104
  }
105

106
  _handleKeydown(event) {
107
    if (event.key !== TAB_KEY) {
2!
108
      return
×
109
    }
110

111
    this._lastTabNavDirection = event.shiftKey ? TAB_NAV_BACKWARD : TAB_NAV_FORWARD
2✔
112
  }
113
}
114

115
export default FocusTrap
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc