import {Popover} from '@primer/react'
<Box position="relative"><Box justifyContent="center" display="flex"><Button variant="primary">Hello!</Button></Box><Popover relative open={true} caret="top"><Popover.Content sx={{mt: 2}}><Heading sx={{fontSize: 2}}>Popover heading</Heading><Text as="p">Message about this particular piece of UI.</Text><Button>Got it!</Button></Popover.Content></Popover></Box>
Two components make up a popover; the Popover
component controls the absolute positioning of the popover, and Popover.Content
renders the inner content of the popover as well as the caret.
By default, the popover renders with absolute positioning, meaning it should usually be wrapped in an element with a relative position in order to be positioned properly. To render the popover with relative positioning, use the relative
property.
It can be useful to give the Popover.Content
element a margin to help align the popover.
Popover
supports various caret positions, which you can specify via the caret
property. This demo shows all the valid values for the prop. The default is top
. Note that the top-left
, bottom-left
, top-right
, and bottom-right
values modify the horizontal alignment of the popover.
function PopoverDemo(props) {const [pos, setPos] = React.useState('top')const [open, setOpen] = React.useState(true)return (<Box><Heading as="h3" sx={{fontSize: 3}}>Caret Position</Heading><CaretSelector current={pos} onChange={setPos} /><Heading as="h3" sx={{fontSize: 3}}>Popover Visibility</Heading><Box my={2}><label><input type="checkbox" value={open} checked={open} onChange={() => setOpen(open => !open)} /> Open</label></Box><Box position="relative" pt={4}><Popover relative open={open} caret={pos}><Popover.Content><Heading sx={{fontSize: 2}}><code>{pos}</code> caret</Heading><Text as="p">Message about this particular piece of UI.</Text><Button onClick={() => setOpen(false)}>Got it!</Button></Popover.Content></Popover></Box></Box>)}function CaretSelector(props) {const choices = ['top','bottom','left','right','left-bottom','left-top','right-bottom','right-top','top-left','bottom-left','top-right','bottom-right'].map(dir => (<label><inputkey={dir}type="radio"name="caret"value={dir}checked={dir === props.current}onChange={() => props.onChange(dir)}/>{' '}{dir}</label>))return (<Box display="grid" gridTemplateColumns="repeat(4, auto)" gridGap={3} my={2}>{choices}</Box>)}render(<PopoverDemo />)
Name | Type | Default | Description |
---|---|---|---|
as | string | div | Sets the underlying HTML tag for the component |
caret | | 'top' | 'bottom' | 'left' | 'right' | 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right' | 'left-bottom' | 'left-top' | 'right-bottom' | 'right-top' | 'top' | Controls the position of the caret |
open | boolean | false | Controls the visibility of the popover. |
relative | boolean | false | Set to true to render the popover using relative positioning. |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. |
Name | Type | Default | Description |
---|---|---|---|
as | string | div | Sets the underlying HTML tag for the component |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. |