Skip to main content

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.

id
Description

id to connect the trigger with the popover - required when not using Popover.Context.

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
NameTypeDefaultDescription
idstring-

id to connect the trigger with the popover - required when not using Popover.Context.

placementPlacementtop

Placement of the popover on the trigger.

openbooleanundefined

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.

autoPlacementbooleantrue

Whether to enable auto placement.

asChildbooleanfalse

Change the default rendered element for the one passed as a child, merging their props and behavior.

Use

React and popovertarget

When you use Popover without Popover.TriggerContext, you connect the trigger and Popover yourself. In this case, 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.

Polyfill

Popover uses popover (mozilla.org). This api is classified as Baseline: Newly available (mozilla.org) as of April 2024, when Firefox was the last browser to add it. In some cases, you may find that users are locked to older browser versions for various reasons, and then it may be appropriate to add a Popover-Polyfill (github.com) to ensure that Popover works for everyone.

Examples

Standard

Inline trigger

Controlled

You can control when the popover is opened or closed:

Without TriggerContext

You can also use Popover without TriggerContext by using popovertarget:

HTML

You can use popover in plain HTML by using the popover attribute on an element and connecting it to a trigger with popovertarget.

The class name ds-popover is applied to the element that is the popover.

In React, we use floating-ui (floating-ui.com) for positioning, and this is a library that can also be used in pure JavaScript applications.

We recommend reading about popover (mozilla.org).

data-placement is used to position the arrow correctly. This attribute is related to a plugin we have created for floating-ui. It is portable to others who use floating-ui, and you can find the code on github.com.

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.

CSS variables and data attributes

CSS Variables
NameValue
--dsc-popover-backgroundvar(--ds-color-surface-default)
--dsc-popover-border-widthvar(--ds-border-width-default)
--dsc-popover-border-stylesolid
--dsc-popover-border-colorvar(--ds-color-border-default)
--dsc-popover-colorvar(--ds-color-text-default)
--dsc-popover-arrow-sizevar(--ds-size-3)
--dsc-popover-border-radiusvar(--ds-border-radius-md)
--dsc-popover-max-width300px
--dsc-popover-paddingvar(--ds-size-3) var(--ds-size-4)
--dsc-popover-box-shadowvar(--ds-shadow-md)
Data Attributes
NameValue
data-placementtop, left, right, bottom
data-variantdefault, tinted
data-popoverinline
Edit this page on github.com (opens in a new tab)