Skip to content

Commit

Permalink
fix: revert to wrapping select browser inside a custom portal to prev…
Browse files Browse the repository at this point in the history
…ent z-index clashing
  • Loading branch information
robinpyon committed Feb 26, 2021
1 parent a66d558 commit 14462ab
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {ThemeProvider as LegacyThemeProvider} from 'theme-ui'
import {Z_INDEX_APP, Z_INDEX_TOAST_PROVIDER} from './constants'
import {AssetBrowserDispatchProvider} from './contexts/AssetSourceDispatchContext'
import Browser from './components/Browser'
import Portal from './components/Portal'
import ReduxProvider from './components/ReduxProvider'
import useKeyPress from './hooks/useKeyPress'
import GlobalStyle from './styled/GlobalStyles'
Expand Down Expand Up @@ -39,20 +40,22 @@ const AssetBrowser: FC<Props> = (props: Props) => {
<Browser onClose={onClose} />
</Box>
) : (
<Box
onMouseUp={handleStopPropagation}
style={{
bottom: 0,
height: 'auto',
left: 0,
position: 'fixed',
top: 0,
width: '100%',
zIndex: Z_INDEX_APP
}}
>
<Browser onClose={onClose} />
</Box>
<Portal>
<Box
onMouseUp={handleStopPropagation}
style={{
bottom: 0,
height: 'auto',
left: 0,
position: 'fixed',
top: 0,
width: '100%',
zIndex: Z_INDEX_APP
}}
>
<Browser onClose={onClose} />
</Box>
</Portal>
)}
</AssetBrowserDispatchProvider>
</ToastProvider>
Expand Down
47 changes: 47 additions & 0 deletions src/components/Portal/index.tsx
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

0 comments on commit 14462ab

Please sign in to comment.