Skip to main content

Components

Dropdown

Dropdown is a generic dropdown list. It lays the foundation for building menus and lists.

See Popover for props.

Usage

Dropdown uses Popover internally.

React and popovertarget

When you use popover without Dropdown.TriggerContext, you link yourself trigger and popover. Then popovertarget is used in lowercase, so that all versions of React correctly render the attribute. When using @digdir/designsystemet-react we extend @types/react-dom to accept this.

Polyfill

Dropdown uses the Popover API (mozilla.org). This API is classified as Baseline: Newly available (mozilla.org) as of April 2024, with Firefox as 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 Dropdown works for everyone.

Examples

Controlled

If you submit open, then you use Dropdown controlled. You can use onClose to get notified when Dropdown wants to close.

Without TriggerContext

Dropdown uses the popover API, so you can use Dropdown without Dropdown.Trigger.

You must then add popovertarget={id} to Dropdown, and id to Dropdown.

HTML

You can use the native popover API directly in HTML. This can be used without JavaScript, if you don't want to polyfill.

Note that the element with popover="manual" uses two classes, ds-popover and ds-dropdown.

You choose whether you want to use popover="manual" or just popover to open the dropdown.

Popover API (mozilla.org)

CSS variables and data attributes

CSS Variables
NameValue
--dsc-dropdown-paddingvar(--ds-size-3) var(--ds-size-2)
--dsc-dropdown-item-paddingvar(--ds-size-2) var(--ds-size-4)
--dsc-dropdown-item-sizevar(--ds-size-12)
--dsc-dropdown-backgroundvar(--ds-color-neutral-surface-default)
--dsc-dropdown-border-widthvar(--ds-border-width-default)
--dsc-dropdown-border-stylesolid
--dsc-dropdown-border-colorvar(--ds-color-neutral-border-subtle)
--dsc-popover-arrow-sizevar(--ds-size-2)

No relevant data attributes found.

References

We use ul and li tags in the dropdown, the choice is based on these:

Edit this page on github.com (opens in a new tab)