Components
Dropdown
Dropdown is a generic dropdown list. It lays the foundation for building menus and lists.
<button
class="ds-button"
type="button"
popoverTarget="dropdown"
>Dropdown</button>
<div
class="ds-dropdown"
id="dropdown"
popover="manual"
data-placement="bottom-end"
>
<ul>
<li>
<button
class="ds-button"
data-variant="tertiary"
type="button"
>Item 1</button>
</li>
<li>
<button
class="ds-button"
data-variant="tertiary"
type="button"
>Item 2</button>
</li>
</ul>
</div>const Preview = () => { return ( <> <Button popovertarget='dropdown'>Dropdown</Button> <Dropdown id='dropdown'> <Dropdown.List> <Dropdown.Item> <Dropdown.Button>Item 1</Dropdown.Button> </Dropdown.Item> <Dropdown.Item> <Dropdown.Button>Item 2</Dropdown.Button> </Dropdown.Item> </Dropdown.List> </Dropdown> </> ); }; render(<Preview />)
Use dropdown when
- you want to offer multiple options without taking up much space in the interface
- the options are secondary but should still be easily accessible
Avoid dropdown when
- an action is primary or frequently used; use a visible button instead
- there is only one option, as a dropdown adds unnecessary complexity
Example
With icons
You can use icons together with text in the dropdown items:
<button
class="ds-button"
type="button"
popoverTarget="dropdown-icons"
>Dropdown</button>
<div
class="ds-dropdown"
id="dropdown-icons"
popover="manual"
data-placement="bottom-end"
>
<ul>
<li>
<a
href="https://github.com/digdir/designsystemet"
target="_blank"
rel="noreferrer"
class="ds-button"
data-variant="tertiary"
>
<svg aria-hidden="true" role="img"></svg>
Github
</a>
</li>
<li>
<a
href="https://designsystemet.no"
target="_blank"
rel="noreferrer"
class="ds-button"
data-variant="tertiary"
>
<svg aria-hidden="true" role="img"></svg>
Designsystemet.no
</a>
</li>
</ul>
</div>const Icons = () => { return ( <> <Button popovertarget='dropdown-icons'>Dropdown</Button> <Dropdown id='dropdown-icons'> <Dropdown.List> <Dropdown.Item> <Dropdown.Button asChild> <a href='https://github.com/digdir/designsystemet' target='_blank' rel='noreferrer' > <LinkIcon aria-hidden /> Github </a> </Dropdown.Button> </Dropdown.Item> <Dropdown.Item> <Dropdown.Button asChild> <a href='https://designsystemet.no' target='_blank' rel='noreferrer' > <LinkIcon aria-hidden /> Designsystemet.no </a> </Dropdown.Button> </Dropdown.Item> </Dropdown.List> </Dropdown> </> ); }; render(<Icons />)
With groupings
You can use headings to group related items in the dropdown:
<button
class="ds-button"
type="button"
popoverTarget="dropdown-headings"
>Dropdown</button>
<div
class="ds-dropdown"
id="dropdown-headings"
popover="manual"
data-placement="bottom-end"
>
<h2 class="ds-heading">First heading</h2>
<ul>
<li>
<button
class="ds-button"
data-variant="tertiary"
type="button"
>Button 1.1</button>
</li>
<li>
<button
class="ds-button"
data-variant="tertiary"
type="button"
>Button 1.2</button>
</li>
</ul>
<h2 class="ds-heading">Second heading</h2>
<ul>
<li>
<button
class="ds-button"
data-variant="tertiary"
type="button"
>Button 2.1</button>
</li>
<li>
<button
class="ds-button"
data-variant="tertiary"
type="button"
>Button 2.2</button>
</li>
</ul>
</div>const Headings = () => { return ( <> <Button popovertarget='dropdown-headings'>Dropdown</Button> <Dropdown id='dropdown-headings'> <Dropdown.Heading>First heading</Dropdown.Heading> <Dropdown.List> <Dropdown.Item> <Dropdown.Button>Button 1.1</Dropdown.Button> </Dropdown.Item> <Dropdown.Item> <Dropdown.Button>Button 1.2</Dropdown.Button> </Dropdown.Item> </Dropdown.List> <Dropdown.Heading>Second heading</Dropdown.Heading> <Dropdown.List> <Dropdown.Item> <Dropdown.Button>Button 2.1</Dropdown.Button> </Dropdown.Item> <Dropdown.Item> <Dropdown.Button>Button 2.2</Dropdown.Button> </Dropdown.Item> </Dropdown.List> </Dropdown> </> ); }; render(<Headings />)
Guidelines
Dropdown does not create a semantic menu, but provides the foundation for building one yourself. Read the "Menu and Menubar Pattern" on w3.org for more information on how to create accessible menus.
- Use Dropdown for actions or navigation that do not need to be visible at all times
- Keep the number of items manageable
- Use headings to group related items
- Avoid multiple levels of dropdowns (nested menus)