Components
Popover
Popover appears above other elements in the interface and is linked to a specific element. It is used to display additional information, interactive elements, or brief explanations without navigating away from the page.
<button
class="ds-button"
type="button"
popoverTarget="popover-preview-en"
>Open popover</button>
<div
class="ds-popover"
id="popover-preview-en"
popover="manual"
data-placement="top"
>
The popover provides a quick message. You can use it to show the user
information that is relevant to the context.
</div>const PreviewEn = () => { return ( <> <Button popovertarget='popover-preview-en'>Open popover</Button> <Popover id='popover-preview-en' placement='top'> The popover provides a quick message. You can use it to show the user information that is relevant to the context. </Popover> </> ); }; render(<PreviewEn />)
Use
You can use Popover in plain HTML by using the popover attribute on an element with the class ds-popover, and connecting it to a trigger with popovertarget="popover-id".
Designsystemet automatically adds popover-polyfill (npmjs.com) to support the native Popover API (mozilla.org) in older browsers if needed.
You can control positioning with attributes on the popover itself:
| attribute | type | default |
|---|---|---|
| data-placement | 'top' | 'top-start' | 'top-end' | 'right' | 'right-start' | 'right-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end' | 'none' | 'top' |
| data-overscroll | 'contain' | undefined | undefined |
| data-autoplacement | boolean | true |
If you don't use the class ds-popover you need to add the attribute data-placement to the popover element.
<button popovertarget="my-popover">Trigger</button>
<div id="my-popover" popover class="ds-popover">Content</div>
Polyfill
Popover uses popover (mozilla.org). We automatically add popover-polyfill (npmjs.com) to support older browsers if needed.
Examples
Inline popover
If you want the trigger inline in text, you still have to use <button>, but you can add data-popover="inline" to get the correct styling.
<p>
We use
<button popovertarget="my-popover" data-popover="inline">
design tokens
</button>
to ensure that we have a consistent design.
</p>
<div class="ds-popover" id="my-popover" popover="manual">
<p>
Design tokens are a collection of variables that define the design in a
design system.
</p>
</div>
CSS variables and data attributes
Sizes are controlled with data-size and colors with data-color. The component will inherit from the closest parent where these are set.
| Name | Value |
|---|---|
| --dsc-popover-arrow-size | var(--ds-size-3) |
| --dsc-popover-background | var(--ds-color-surface-default) |
| --dsc-popover-border-color | var(--ds-color-border-default) |
| --dsc-popover-border-radius | var(--ds-border-radius-md) |
| --dsc-popover-border-style | solid |
| --dsc-popover-border-width | var(--ds-border-width-default) |
| --dsc-popover-box-shadow | var(--ds-shadow-md) |
| --dsc-popover-color | var(--ds-color-text-default) |
| --dsc-popover-max-width | 300px |
| --dsc-popover-padding | var(--ds-size-3) var(--ds-size-4) |
| --dsc-popover-placement | top |
| Name | Value |
|---|---|
| data-overscroll | contain |
| data-popover | inline |
| data-variant | default, tinted |
React
import { Button, Popover } from '@digdir/designsystemet-react';
<Button popovertarget="my-popover">Trigger</Button>
<Popover id="my-popover">Content</Popover>
React and popovertarget
You connect the trigger and popover yourself with id and popovertarget.
popovertarget is used in lowercase, so that all versions of React correctly render the attribute.
When you use @digdir/designsystemet-react we extend @types/react-dom to accept this.
Controlled example
You can use React state to control when the popover opens and closes. Use togglePopover() to open and close it, and onToggle to update the state when the popover opens or closes in other ways, such as with the button, Esc or a click outside.
In React 18, onToggle does not fire for popovers. Listen for the toggle event with addEventListener instead.
<button
class="ds-button"
type="button"
popoverTarget="popover-controlled-en"
>Delete</button>
<div
class="ds-popover"
id="popover-controlled-en"
popover="manual"
data-placement="top"
data-color="danger"
>
<p class="ds-paragraph">Are you sure you want to delete this?</p>
<div
style="display:flex;gap:var(--ds-size-2);margin-top:var(--ds-size-2)"
>
<button
class="ds-button"
type="button"
data-size="sm"
data-color="danger"
>Delete</button>
<button
class="ds-button"
data-variant="tertiary"
type="button"
data-size="sm"
>Cancel</button>
</div>
</div>const ControlledEn = () => { const popoverRef = useRef<HTMLDivElement>(null); const [open, setOpen] = useState(false); useEffect(() => { popoverRef.current?.togglePopover(open); }, [open]); return ( <> <Button popovertarget='popover-controlled-en'>Delete</Button> <Popover ref={popoverRef} id='popover-controlled-en' onToggle={(event) => setOpen(event.newState === 'open')} data-color='danger' > <Paragraph>Are you sure you want to delete this?</Paragraph> <div style={{ display: 'flex', gap: 'var(--ds-size-2)', marginTop: 'var(--ds-size-2)', }} > <Button onClick={() => setOpen(false)} data-size='sm' data-color='danger' > Delete </Button> <Button data-variant='tertiary' onClick={() => setOpen(false)} data-size='sm' > Cancel </Button> </div> </Popover> </> ); }; render(<ControlledEn />)
Popover
- id
- Description
id to connect the trigger with the popover, using `popovertarget` on the trigger.
- Type
string
- placement
- Description
Placement of the popover on the trigger.
- Type
Placement- Default
top
- open
- Description
When a boolean is provided, the popover will be controlled.
- Type
boolean- Default
undefined
- variant
- Description
Change the background color of the popover.
- Type
"default" | "tinted"- Default
'default'
- onOpen
- Description
Callback when the popover wants to open.
- Type
(() => void)
- onClose
- Description
Callback when the popover wants to close.
- Type
(() => void)
- autoPlacement
- Description
Whether to enable auto placement.
- Type
boolean- Default
true
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior.
- Type
boolean- Default
false
| Name | Type | Default | Description |
|---|---|---|---|
| id | string | - | id to connect the trigger with the popover, using `popovertarget` on the trigger. |
| placement | Placement | top | Placement of the popover on the trigger. |
| open | boolean | undefined | When a boolean is provided, the popover will be controlled. |
| variant | "default" | "tinted" | 'default' | Change the background color of the popover. |
| onOpen | (() => void) | - | Callback when the popover wants to open. |
| onClose | (() => void) | - | Callback when the popover wants to close. |
| autoPlacement | boolean | true | Whether to enable auto placement. |
| asChild | boolean | false | Change the default rendered element for the one passed as a child, merging their props and behavior. |
PopoverTrigger
- inline
- Description
Will render the trigger as inline text.
- Type
boolean- Default
false false
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior.
- Type
boolean- Default
false false
- variant
- Description
Specify which variant to use
- Type
"primary" | "secondary" | "tertiary"- Default
'primary'
- icon
- Description
Toggle icon only styling, pass icon as children When combined with loading, the loading-icon will be shown instead of the icon.
- Type
boolean- Default
false
- loading
- Description
Toggle loading state. Pass an element if you want to display a custom loader.
- Type
ReactNode- Default
false
| Name | Type | Default | Description |
|---|---|---|---|
| inline | boolean | false false | Will render the trigger as inline text. |
| asChild | boolean | false false | Change the default rendered element for the one passed as a child, merging their props and behavior. |
| variant | "primary" | "secondary" | "tertiary" | 'primary' | Specify which variant to use |
| icon | boolean | false | Toggle icon only styling, pass icon as children When combined with loading, the loading-icon will be shown instead of the icon. |
| loading | ReactNode | false | Toggle loading state. Pass an element if you want to display a custom loader. |