-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* [added] Focus trap when reentering document (#742)
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import portalOpenInstances from "./portalOpenInstances"; | ||
// Body focus trap see Issue #742 | ||
|
||
let before, | ||
after, | ||
instances = []; | ||
|
||
function focusContent() { | ||
if (instances.length === 0) { | ||
if (process.env.NODE_ENV !== "production") { | ||
// eslint-disable-next-line no-console | ||
console.warn(`React-Modal: Open instances > 0 expected`); | ||
} | ||
return; | ||
} | ||
instances[instances.length - 1].focusContent(); | ||
} | ||
|
||
function bodyTrap(eventType, openInstances) { | ||
if (!before || !after) { | ||
before = document.createElement("div"); | ||
before.setAttribute("data-react-modal-body-trap", ""); | ||
before.style.position = "absolute"; | ||
before.style.opacity = "0"; | ||
before.setAttribute("tabindex", "0"); | ||
before.addEventListener("focus", focusContent); | ||
after = before.cloneNode(); | ||
after.addEventListener("focus", focusContent); | ||
} | ||
|
||
instances = openInstances; | ||
|
||
if (instances.length > 0) { | ||
// Add focus trap | ||
if (document.body.firstChild !== before) { | ||
document.body.insertBefore(before, document.body.firstChild); | ||
} | ||
if (document.body.lastChild !== after) { | ||
document.body.appendChild(after); | ||
} | ||
} else { | ||
// Remove focus trap | ||
if (before.parentElement) { | ||
before.parentElement.removeChild(before); | ||
} | ||
if (after.parentElement) { | ||
after.parentElement.removeChild(after); | ||
} | ||
} | ||
} | ||
|
||
portalOpenInstances.subscribe(bodyTrap); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Tracks portals that are open and emits events to subscribers | ||
|
||
class PortalOpenInstances { | ||
constructor() { | ||
this.openInstances = []; | ||
this.subscribers = []; | ||
} | ||
|
||
register = openInstance => { | ||
if (this.openInstances.indexOf(openInstance) !== -1) { | ||
if (process.env.NODE_ENV !== "production") { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`React-Modal: Cannot register modal instance that's already open` | ||
); | ||
} | ||
return; | ||
} | ||
this.openInstances.push(openInstance); | ||
this.emit("register"); | ||
}; | ||
|
||
deregister = openInstance => { | ||
const index = this.openInstances.indexOf(openInstance); | ||
if (index === -1) { | ||
if (process.env.NODE_ENV !== "production") { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`React-Modal: Unable to deregister ${openInstance} as it was never registered` | ||
); | ||
} | ||
return; | ||
} | ||
this.openInstances.splice(index, 1); | ||
this.emit("deregister"); | ||
}; | ||
|
||
subscribe = callback => { | ||
this.subscribers.push(callback); | ||
}; | ||
|
||
emit = eventType => { | ||
this.subscribers.forEach(subscriber => | ||
subscriber( | ||
eventType, | ||
// shallow copy to avoid accidental mutation | ||
this.openInstances.slice() | ||
) | ||
); | ||
}; | ||
} | ||
|
||
const portalOpenInstances = new PortalOpenInstances(); | ||
|
||
export default portalOpenInstances; |