Popover is used to display some content on top of another, and should be paired with a clickable trigger element.
It is a base for other more specific component, like Menu
, Autocomplete
and Multiselect
. Please, consider using these specific componenets to cover your needs.
How to use Popover
- Only if
Menu
,Autocomplete
andMultiselect
components are not covering your use cases, you should usePopover
. - Before using this component, double-check your design requirements. We are providing
Menu
,Autocomplete
, andMultiselect
for more specific use-cases and they can address your needs. - Keep in mind that you will have to implement everything related to accessibility for the popover content.
- Component is controllable, so don't forget to pass
onClose
callback prop. OtherwisecloseOnEsc
andcloseOnBlur
will not work properly.
Code examples
- Pass trigger component as a child for
Popover.Trigger
. NOTE: 🚨 Ensure that the component that you pass acceptsref
. Consider usingforwardRef
for functional components. - Pass popover content as a child for
Popover.Content
Trapping focus within Popover
If the popover contains interactive elements that user can navigate through with Tab
,
consider using react-focus-lock to trap the focus within Popover
// import { Popover } from '@contentful/f36-popover';// import { Button } from '@contentful/f36-button';// import { Stack } from '@contentful/f36-core';// import { Switch } from '@contentful/f36-forms';// import FocusLock from 'react-focus-lock';function PopoverExample() {const [isOpen, setIsOpen] = React.useState(false);return (<Popover isOpen={isOpen} onClose={() => setIsOpen(false)}><Popover.Trigger><Button onClick={() => setIsOpen(!isOpen)}>Toggle</Button></Popover.Trigger><Popover.Content>{/* Just wrap your content with FocusLock component */}<FocusLock><Stackpadding="spacingM"margin="none"spacing="spacingS"flexDirection="column"><Switch>Option 1</Switch><Switch>Option 2</Switch><Switch>Option 3</Switch></Stack></FocusLock></Popover.Content></Popover>);}
Content guidelines
- Use an interactive element such as
button
forPopover.Trigger
Accessibility
- If the popover contains interactive elements that user can navigate through with
Tab
, consider using react-focus-lock to trap the focus within Popover - When the popover is opened, focus is moved to the
Popover.Content
. If you setautoFocus
tofalse
, it will not move the focus. - When the popover is open and focus is within the
Popover.Content
, click onEsc
key will close the popover. If you setcloseOnEsc
tofalse
, it will not close. - When the popover is open and focus is within the
Popover.Content
, click outside popover or blurring out will close the it. If you setcloseOnBlur
tofalse
, it will not close. - All the necessary a11y attributes for
Popover.Content
andPopover.Trigger
are provided.
Props
- Name
children(required)
ReactNode - Name
autoFocus
DescriptionIf true, the popover will be focused after opening
falsetrue - Name
closeOnBlur
DescriptionIf true, the popover will close when you blur out it by clicking outside or tabbing out
falsetrue - Name
closeOnEsc
DescriptionIf true, the popover will close when you hit the Esc key
falsetrue - Name
id
DescriptionPopover id. Will be used as an `id` attribute on popover and as `aria-controls` attribute on trigger
string - Name
isAutoalignmentEnabled
DescriptionBoolean to control if popover is allowed to change its placement automatically based on available space in the viewport. For example: If you set placement prop to bottom, but there isn't enough space to position the popover in that direction, it will change the popper placement to top. As soon as enough space is detected, the placement will be reverted to the defined one. If you want the popover to strictly follow the placement prop you should set this prop to false.
falsetrue - Name
isFullWidth
DescriptionBoolean to determine if the Popover should be the same width as the trigger element
falsetrue - Name
isOpen
DescriptionBoolean to control whether or not the Popover is open
falsetrue - Name
offset
DescriptionThe `X-axis` and `Y-axis` offset to position popper element from its trigger element. `[X, Y]`
[number, number] - Name
onClose
DescriptionCallback fired when the popover closes
() => void - Name
placement
DescriptionDetermines the preferred position of the Popover. This position is not guaranteed, as the Popover might be moved to fit the viewport
"auto""auto-start""auto-end""top""bottom""right""left""top-start""top-end""bottom-start""bottom-end""right-start""right-end""left-start""left-end" - Name
usePortal
DescriptionBoolean to control whether or not to render the Popover in a React Portal. Rendering content inside a Portal allows the Popover to escape the bounds of its parent while still being positioned correctly. Using a Portal is necessary if an ancestor of the Popover hides overflow.
falsetrue