Skip to content

Commit

Permalink
some updates on focustrap (based on #35165)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot committed Jan 30, 2022
1 parent 0c0c6cc commit 21ae2fb
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions js/src/util/focustrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import EventHandler from '../dom/event-handler'
import SelectorEngine from '../dom/selector-engine'
import { getDocument } from './index'
import Config from './config'

/**
Expand Down Expand Up @@ -43,6 +44,7 @@ class FocusTrap extends Config {
this._config = this._getConfig(config)
this._isActive = false
this._lastTabNavDirection = null
this._document = getDocument()
}

// Getters
Expand All @@ -68,9 +70,9 @@ class FocusTrap extends Config {
this._config.trapElement.focus()
}

EventHandler.off(document, EVENT_KEY) // guard against infinite focus loop
EventHandler.on(document, EVENT_FOCUSIN, event => this._handleFocusin(event))
EventHandler.on(document, EVENT_KEYDOWN_TAB, event => this._handleKeydown(event))
EventHandler.off(this._document, EVENT_KEY) // guard against infinite focus loop
EventHandler.on(this._document, EVENT_FOCUSIN, event => this._handleFocusin(event))
EventHandler.on(this._document, EVENT_KEYDOWN_TAB, event => this._handleKeydown(event))

this._isActive = true
}
Expand All @@ -81,14 +83,14 @@ class FocusTrap extends Config {
}

this._isActive = false
EventHandler.off(document, EVENT_KEY)
EventHandler.off(this._document, EVENT_KEY)
}

// Private
_handleFocusin(event) {
const { trapElement } = this._config

if (event.target === document || event.target === trapElement || trapElement.contains(event.target)) {
if (event.target === this._document || event.target === trapElement || trapElement.contains(event.target)) {
return
}

Expand Down

0 comments on commit 21ae2fb

Please sign in to comment.