Skip to main content

What are you looking for?

Try searching for…

Components

Toggle group

Toggle group groups related options. The component consists of a group of buttons that are connected, where only one button can be selected at a time.

HTML

Press Enter to start editing
Unable to parse html
const PreviewEn = () => {
  return (
    <ToggleGroup data-toggle-group='Filter' defaultValue='inbox'>
      <ToggleGroup.Item value='inbox'>Inbox</ToggleGroup.Item>
      <ToggleGroup.Item value='drafts'>Drafts</ToggleGroup.Item>
      <ToggleGroup.Item value='archive'>Archive</ToggleGroup.Item>
      <ToggleGroup.Item value='sent'>Sent</ToggleGroup.Item>
    </ToggleGroup>
  );
};

render(<PreviewEn />)

Usage

If you are using @digdir/designsystemet-web, you can implement toggle group by using the data-toggle-group attribute.

Old way with <button>

With this approach, you need to handle keyboard navigation and ARIA roles yourself. You can read about what you need to do to implement this in the accessibility documentation.

The class name ds-toggle-group is placed on a <div> with role="radiogroup".

Each button in the group is a <button> with class name ds-button with role="radio" and aria-checked which indicates whether the button is selected or not. data-variant for selected buttons should match data-variant on toggle group. The remaining buttons should have data-variant="tertiary".

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.

CSS Variables
NameValue
--dsc-togglegroup-backgroundvar(--ds-color-surface-default)
--dsc-togglegroup-border-widthvar(--ds-border-width-default)
--dsc-togglegroup-border-stylesolid
--dsc-togglegroup-border-colorvar(--ds-color-border-default)
--dsc-togglegroup-colorvar(--ds-color-text-default)
--dsc-togglegroup-spacing0
--dsc-togglegroup-border-radiusvar(--ds-border-radius-default)
--dsc-togglegroup-button-sizevar(--ds-size-12)
--dsc-togglegroup-label""
--dsc-button-background--activevar(--ds-color-base-active)
--dsc-button-background--hovervar(--ds-color-base-hover)
--dsc-button-backgroundvar(--ds-color-base-default)
--dsc-button-color--activevar(--ds-color-base-contrast-default)
--dsc-button-color--hovervar(--ds-color-base-contrast-default)
--dsc-button-colorvar(--ds-color-base-contrast-default)
--dsc-button-border-colorvar(--ds-color-border-default)
Data Attributes
NameValue
data-icon
data-variantsecondary

React

Controlled example

variant
Description

Specify which variant to use

Type
"primary" | "secondary"
Default
primary
value
Description

Controlled state for `ToggleGroup` component.

Type
string
defaultValue
Description

Default value.

Type
string
onChange
Description

Callback with selected `ToggleGroupItem` `value`

Type
((value: string) => void)
name
Description

Form element name

Type
string
data-toggle-group
Description

Toggle group label for accessibility

Type
string
NameTypeDefaultDescription
variant"primary" | "secondary"primary

Specify which variant to use

valuestring-

Controlled state for `ToggleGroup` component.

defaultValuestring-

Default value.

onChange((value: string) => void)-

Callback with selected `ToggleGroupItem` `value`

namestring-

Form element name

data-toggle-groupstring-

Toggle group label for accessibility

ToggleGroupItem

value
Description

The value of the ToggleGroupItem. Generates a random value if not set.

Type
string | (string & readonly string[])
icon
Description

@deprecated Icon prop is deprecated

Type
boolean
NameTypeDefaultDescription
valuestring | (string & readonly string[])-

The value of the ToggleGroupItem. Generates a random value if not set.

iconboolean-

@deprecated Icon prop is deprecated

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