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,AutocompleteandMultiselectcomponents are not covering your use cases, you should usePopover.
- Before using this component, double-check your design requirements. We are providing Menu,Autocomplete, andMultiselectfor 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 onClosecallback prop. OtherwisecloseOnEscandcloseOnBlurwill not work properly.
Code examples
- Pass trigger component as a child for Popover.Trigger. NOTE: 🚨 Ensure that the component that you pass acceptsref. Consider usingforwardReffor 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 buttonforPopover.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 setautoFocustofalse, it will not move the focus.
- When the popover is open and focus is within the Popover.Content, click onEsckey will close the popover. If you setcloseOnEsctofalse, 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 setcloseOnBlurtofalse, it will not close.
- All the necessary a11y attributes for Popover.ContentandPopover.Triggerare provided.
Props
- Namechildren(required)ReactNode
- NameautoFocusDescriptionIf true, the popover will be focused after opening falsetrue
- NamecloseOnBlurDescriptionIf true, the popover will close when you blur out it by clicking outside or tabbing out falsetrue
- NamecloseOnEscDescriptionIf true, the popover will close when you hit the Esc key falsetrue
- NameidDescriptionPopover id. Will be used as an `id` attribute on popover and as `aria-controls` attribute on trigger string
- NameisAutoalignmentEnabledDescriptionBoolean 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
- NameisFullWidthDescriptionBoolean to determine if the Popover should be the same width as the trigger element falsetrue
- NameisOpenDescriptionBoolean to control whether or not the Popover is open falsetrue
- NameoffsetDescriptionThe `X-axis` and `Y-axis` offset to position popper element from its trigger element. `[X, Y]` [number, number]
- NameonCloseDescriptionCallback fired when the popover closes () => void
- NameplacementDescriptionDetermines 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"
- NameusePortalDescriptionBoolean 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