Add phantom overlay to Selects to prevent background touches

https://github.com/radix-ui/primitives/issues/1658
This commit is contained in:
Justin Edmund 2022-12-28 21:47:28 -08:00
parent 2c2a9dd65d
commit 84aa355ad8
6 changed files with 86 additions and 23 deletions

View file

@ -11,6 +11,7 @@
margin: 0;
padding: 0;
max-width: 761px;
isolation: isolate;
@include breakpoint(tablet) {
justify-content: space-between;

View file

@ -45,6 +45,7 @@
box-sizing: border-box;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.2);
display: block;
isolation: isolate;
flex-grow: 2;
flex-shrink: 0;
height: $height;

View file

@ -0,0 +1,13 @@
.Overlay {
isolation: isolate;
position: fixed;
z-index: 30;
top: 0;
right: 0;
bottom: 0;
left: 0;
&.Visible {
background: rgba(0, 0, 0, 0.6);
}
}

View file

@ -0,0 +1,52 @@
import React, { useEffect, useState } from 'react'
import classNames from 'classnames'
import './index.scss'
interface Props {
visible: boolean
open: boolean
}
const defaultProps = {
visible: true,
}
const Overlay = ({
visible: displayed,
open,
}: {
visible: boolean
open: boolean
}) => {
const [visible, setVisible] = useState(open)
const classes = classNames({
Overlay: true,
Visible: displayed,
})
useEffect(() => {
if (!open) {
console.log('No longer open, setting timeout...')
const timer = setTimeout(() => {
console.log('Timeout cleared!')
setVisible(false)
}, 200)
return () => {
clearTimeout(timer)
}
}
setVisible(true)
return () => {}
}, [open])
function handleClick(event: React.MouseEvent<HTMLDivElement, MouseEvent>) {
event.stopPropagation()
}
return visible ? <div className={classes} onClick={handleClick} /> : null
}
Overlay.defaultProps = defaultProps
export default Overlay

View file

@ -5,6 +5,7 @@ import classNames from 'classnames'
import ArrowIcon from '~public/icons/Arrow.svg'
import './index.scss'
import Overlay from '~components/Overlay'
// Props
interface Props
@ -76,19 +77,24 @@ const Select = React.forwardRef<HTMLButtonElement, Props>(function Select(
</RadixSelect.Trigger>
<RadixSelect.Portal className="Select">
<RadixSelect.Content
onCloseAutoFocus={onCloseAutoFocus}
onEscapeKeyDown={onEscapeKeyDown}
onPointerDownOutside={onPointerDownOutside}
>
<RadixSelect.ScrollUpButton className="Scroll Up">
<ArrowIcon />
</RadixSelect.ScrollUpButton>
<RadixSelect.Viewport>{props.children}</RadixSelect.Viewport>
<RadixSelect.ScrollDownButton className="Scroll Down">
<ArrowIcon />
</RadixSelect.ScrollDownButton>
</RadixSelect.Content>
<>
<Overlay open={open} visible={false} />
<RadixSelect.Content
className="Select"
onCloseAutoFocus={onCloseAutoFocus}
onEscapeKeyDown={onEscapeKeyDown}
onPointerDownOutside={onPointerDownOutside}
>
<RadixSelect.ScrollUpButton className="Scroll Up">
<ArrowIcon />
</RadixSelect.ScrollUpButton>
<RadixSelect.Viewport>{props.children}</RadixSelect.Viewport>
<RadixSelect.ScrollDownButton className="Scroll Down">
<ArrowIcon />
</RadixSelect.ScrollDownButton>
</RadixSelect.Content>
</>
</RadixSelect.Portal>
</RadixSelect.Root>
)

View file

@ -119,16 +119,6 @@ select {
}
}
.Overlay {
background: rgba(0, 0, 0, 0.6);
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 20;
}
.Hovercard {
background: #222;
border-radius: $unit;