-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: revert to wrapping select browser inside a custom portal to prev…
…ent z-index clashing
- Loading branch information
Showing
2 changed files
with
64 additions
and
14 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,47 @@ | ||
import React from 'react' | ||
import {createPortal} from 'react-dom' | ||
|
||
const canUseDOM = !!( | ||
typeof window !== 'undefined' && | ||
window.document && | ||
window.document.createElement | ||
) | ||
|
||
type Props = { | ||
children: React.ReactNode | ||
} | ||
|
||
class Portal extends React.Component<Props> { | ||
node!: HTMLDivElement | null | ||
|
||
componentWillUnmount() { | ||
if (this.node) { | ||
document.body.removeChild(this.node) | ||
} | ||
this.node = null | ||
} | ||
|
||
render() { | ||
if (!canUseDOM) { | ||
return null | ||
} | ||
if (!this.node) { | ||
this.node = document.createElement('div') | ||
document.body.appendChild(this.node) | ||
} | ||
return createPortal( | ||
<React.Fragment> | ||
{this.props.children} | ||
{/* | ||
the following element is needed to prevent tab key from navigating out of window context. Since the | ||
portal content is appended to the DOM, hitting the tab key while having focus on on the last element | ||
will navigate *out* of the document, causing focus to move to a browser UI control. | ||
*/} | ||
<span tabIndex={0} /> | ||
</React.Fragment>, | ||
this.node | ||
) | ||
} | ||
} | ||
|
||
export default Portal |