Skip to content

Dropdown

The dropdown component provides a flexible and accessible way to present a list of options to users. It supports various styles, states, and even includes a search functionality.

Basic Usage

To use the basic dropdown, simply include the following HTML structure:

The open state of the search dropdown can be triggered by adding the dropdown-open to the main wrapper.

Adding Search Functionality

To add search functionality to your dropdown, use the search dropdown variant:

The open variant is applied in the same way as before, by adding the dropdown-open class to the main wrapper.

States

Error State

To indicate an error state, add the dropdown-error class to the main wrapper:

Inactive State

For an inactive dropdown, add the disabled property to the dropdown-wrapper div. Ensure that the aria labels are also changed to reflect this.

CSS Variables

The dropdown system can be customized using CSS variables:

Layout and Sizing

VariableDefault ValueDescription
--dropdown-width16remWidth of the dropdown
--dropdown-gap0.25remGap between dropdown elements
--dropdown-padding-y0.5remVertical padding of the dropdown
--dropdown-padding-x1remHorizontal padding of the dropdown
--dropdown-menu-margin-top0.25remTop margin of the dropdown menu
--dropdown-max-items5Maximum number of visible items
--dropdown-item-height2.5remHeight of each dropdown item

Borders and Styling

VariableDefault ValueDescription
--dropdown-border-width1pxDefault border width
--dropdown-border-width-active2pxBorder width when active
--dropdown-radiusvar(--radius-site)Border radius of the dropdown
--dropdown-error-border-colorvar(--color-error)Border color for error state
--dropdown-search-divider-width1pxWidth of the divider below the search input

Scrollbar Customization

VariableDefault ValueDescription
--dropdown-scroll-padding0.25emPadding around the scrollbar
--dropdown-scrollbar-radiusvar(--radius-site)Border radius of the scrollbar
--dropdown-scrollbar-width8pxWidth of the scrollbar

Scrolling Behavior

The dropdown uses a dynamic max-height calculation based on the following formula:

max-height: calc((var(--dropdown-item-height) * var(--dropdown-max-items)));

This ensures consistent height across all dropdown variants. When content exceeds this height, a scrollbar appears automatically. For the search dropdown, the formula is adjusted to account for the search input:

max-height: calc(
(var(--dropdown-item-height) * var(--dropdown-max-items)) +
var(--dropdown-search-height)
);

Accessibility

The dropdown components are built with accessibility in mind, using semantic HTML and ARIA attributes to ensure they work well with screen readers and keyboard navigation.

Current Implementation Features

Structure and Labeling

  • Every dropdown has a proper label that’s connected to the component using aria-labelledby
  • The dropdown uses a button element for the trigger and a ul element for the list of options
  • The dropdown wrapper uses the listbox role with appropriate ARIA attributes
  • Inactive state is managed using the disabled attribute on the dropdown wrapper

Keyboard Support

The current HTML structure supports:

  • Tab navigation to move focus to the dropdown button
  • Focus indicators on interactive elements

Screen Reader Support

Each variant includes specific features for screen readers:

  • Use of label and the for attribute
  • Options marked with role="option" and aria-selected states
  • Expanded state indicated with aria-expanded on the button and listbox wrapper
  • List of options connected to the button with aria-controls
  • Helper text that uses an aria-live="polite" region for dynamic updates

Search Functionality (for search dropdowns)

  • Search input is properly labeled with a visually hidden label
  • Search input has aria-controls attribute linking it to the dropdown list
  • Search icon is marked as aria-hidden="true" to prevent redundant announcements

States

  • Inactive dropdowns use the disabled attribute on the wrapper to prevent interaction
  • The disabled attribute is properly reflected in the ARIA states of child elements

While the current implementation provides a good foundation for accessibility, there are still some JavaScript-dependent features that need to be implemented to ensure full accessibility support.

JavaScript Functionality

To make the dropdown components fully functional and accessible, several JavaScript features need to be implemented. Here’s an overview of the key areas to address:

Event Handling

  • Toggle dropdown visibility on button click
  • Close dropdown when clicking outside or pressing the Escape key
  • Handle option selection and update the selected value

Keyboard Navigation

  • Implement up/down arrow key navigation through options
  • Add support for Home/End keys to jump to first/last options
  • Implement character search for quick option selection

ARIA Attribute Management

  • Dynamically update aria-expanded on the dropdown button
  • Manage aria-selected states on options
  • Update aria-activedescendant to reflect the currently focused option

Search Functionality

  • Filter options based on search input
  • Update the displayed options list in real-time
  • Manage focus within search results

Focus Management

  • Control focus when opening and closing the dropdown
  • Implement a focus trap within the open dropdown
  • Restore focus appropriately when closing the dropdown

State Management

  • Keep track of the selected option
  • Maintain the open/closed state of the dropdown
  • Handle disabled/error states and their interactions
  • Manage the disabled attribute on the dropdown wrapper and ensure it’s reflected in ARIA states of child elements

Performance Considerations

  • Use event delegation for option list interactions
  • Implement debouncing for search input to optimize performance
  • Consider virtualizing long option lists for improved rendering performance