@justeattakeaway/pie-dropdown
v0.1.2
Published
PIE Design System Dropdown built using Web Components
Maintainers
Keywords
Readme
@justeattakeaway/pie-dropdown
Source Code | Design Documentation | NPM
@justeattakeaway/pie-dropdown is a Web Component built using the Lit library. It offers a simple and accessible dropdown component for web applications, supporting single and multi selection, and an optional search field for filtering the options list.
Table of Contents
Installation
To install any of our web components in your application, we would suggest following the getting started guide to set up your project.
Ideally, you should install the component using the @justeattakeaway/pie-webc package, which includes all of the components. Or you can install the individual component package.
Documentation
Properties
| Prop | Options | Description | Default |
|-------------------|---------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|
| options | array | The options to choose from, in display order. See Using the options prop. | [] |
| value | string, array | The current selection: a single option value, or — in multi-select variants — an array of option values. | "" |
| isMultiselect | boolean | When true, several options can be selected at once. The field shows a count chip (its text comes from selectedCountText) and the popover stays open while selections are made. | false |
| isSearchable | boolean | When true, the field becomes a text input that filters the options list as the user types, matching each option's primary or secondary text (substring match, case-insensitive). | false |
| isLoading | boolean | When true, the popover shows a loading state (a spinner plus loadingText) while options are fetched asynchronously. | false |
| label | string | A visible label for the field, rendered above it. When omitted, provide aria.label instead so the field keeps its accessible name. | undefined |
| placeholder | string | The text shown when no selection has been made. It disappears once a selection is made or the user starts typing. | undefined |
| name | string | The name the component's value is submitted under when it participates in a form. In multi-select variants one entry is submitted per selected option. | undefined |
| assistiveText | string | Helper text rendered below the field, also used to convey error or success messages. | undefined |
| status | "default", "error", "success" | The status of the field, used for validation styling. | default |
| size | "small", "medium", "large" | The vertical size of the field. | medium |
| disabled | boolean | When true the field cannot be interacted with. | false |
| required | boolean | When true the field must have a selection before its owning form can be submitted. | false |
| loadingText | string | The text shown in the popover while isLoading is true (and announced by the live region). No text is shown or announced when unset — the spinner still renders. | undefined |
| noResultsText | string | The text shown in the popover when a search returns no matching options (and announced by the live region). No message is shown when unset. | undefined |
| selectedCountText | string | The text shown in the count chip in multi-select variants when options are selected. Use a {count} placeholder where the number of selected options should appear, e.g. "{count} picked" renders as "3 picked". When unset the chip shows the bare count. | undefined |
| optionsAvailableText | string | The message announced to assistive technology when a search returns matching options. Use a {count} placeholder where the number of matching options should appear, e.g. "Options available: {count}" renders as "Options available: 6" (phrasing that avoids plurals keeps the message correct for any count). Nothing is announced when unset. | undefined |
| noSelectionText | string | The validation message used when the dropdown is required and submitted without a selection (the valueMissing validity state) — submission is blocked with an empty message when unset. This is separate from assistiveText, which is the helper text rendered below the field. | undefined |
| aria | object | Additional ARIA properties. See Using the aria prop. | undefined |
Using the options prop
The options prop accepts an array of option objects:
| Prop | Options | Description | Default |
|----------------|-----------|------------------------------------------------------------------------------------------------------|-------------|
| value | string | Required: The value the option represents, used for the component's value and form submission. | undefined |
| primaryText | string | Required: The option's primary text, used as its accessible name. | undefined |
| secondaryText| string | Supporting text rendered beneath the primary text, used as the option's accessible description and matched by the searchable variant's filter. | undefined |
| icon | string | The tag name of a registered pie-icons-webc element rendered as the option's leading icon, e.g. 'icon-food'. See Option icons. | undefined |
| disabled | boolean | When true the option cannot be selected, and keyboard focus skips it. | false |
const options = [
{ value: 'pizza', primaryText: 'Pizza', secondaryText: 'Wood-fired, with a crisp base' },
{ value: 'burger', primaryText: 'Burger' },
{ value: 'salad', primaryText: 'Salad', disabled: true },
];In multi-select variants, the selected options are sorted to the top of the list (in alphanumeric order) whenever the popover is opened.
Option icons
Each option accepts an icon — the tag name of a pie-icons-webc element. The dropdown renders that element as the option's leading icon in its row and, while the option is selected in a single-select variant, in the field alongside the selection text.
Your app imports the icons it uses — the dropdown never bundles them. Import the individual icons (recommended, so your bundler tree-shakes the rest) or the full pie-icons-webc package:
// individual icons (recommended)
import '@justeattakeaway/pie-icons-webc/dist/IconFlagGb.js';
import '@justeattakeaway/pie-icons-webc/dist/IconFlagIe.js';
// or every icon at once
// import '@justeattakeaway/pie-icons-webc';<script type="module">
import '@justeattakeaway/pie-icons-webc/dist/IconFlagGb.js';
import '@justeattakeaway/pie-icons-webc/dist/IconFlagIe.js';
import '@justeattakeaway/pie-webc/components/dropdown.js';
const dropdown = document.querySelector('pie-dropdown');
dropdown.options = [
{ value: 'gb', primaryText: 'United Kingdom', icon: 'icon-flag-gb' },
{ value: 'ie', primaryText: 'Ireland', icon: 'icon-flag-ie' },
];
</script>
<pie-dropdown
label="Country"
value="gb">
</pie-dropdown>In React, pass the tag name in the options array — no slotted children are involved:
import { PieDropdown } from '@justeattakeaway/pie-webc/react/dropdown.js';
import '@justeattakeaway/pie-icons-webc/dist/IconFlagGb.js';
import '@justeattakeaway/pie-icons-webc/dist/IconFlagIe.js';
<PieDropdown
label="Country"
options={[
{ value: 'gb', primaryText: 'United Kingdom', icon: 'icon-flag-gb' },
{ value: 'ie', primaryText: 'Ireland', icon: 'icon-flag-ie' },
]}
value="gb"
/>Notes:
- The tag must be a registered custom element by the time the dropdown renders — an unregistered or invalid tag name simply renders no icon.
- Option icons are decorative: the option's accessible name comes from its
primaryText. - Icons render at 20px in the subdued content colour, regardless of their
sizeattribute. - The field can also show a static, selection-independent icon via the
leadingIconslot — a separate mechanism from option icons. - Multi-select variants do not show option icons: the rows host checkboxes and the field shows the count chip. Option icons render in single-select rows and the field only.
Opening the dropdown
The popover opens when the field is clicked; non-searchable fields also open on keyboard focus. Focus-open never moves focus — it stays on the field in every variant; the arrow keys are what move focus into the option list. Tab reaches the field's other controls (the count chip's close button first in multi-select variants with a selection, then the single-select clear button) and then moves past the dropdown.
- Non-searchable single select — focus stays on the field;
ArrowDown/ArrowUpmove focus into the option list (the selected option, or the first one if nothing is selected), from both a closed and an open field. - Non-searchable multi select — with a selection, the first tab stop is the count chip's close button: the popover opens while focus stays on the chip, so keyboard users can clear every selection with
Enter. Tabbing from the chip moves straight to the first option — the trigger is not a tab stop while the chip is shown. Without a selection, focus stays on the trigger. - Searchable — the popover does not open on focus (the
aria-expandedstate change would chase the field's focus announcement and interrupt screen readers mid-announcement); it opens on the first keystroke, the arrow keys, a click or the chevron, with the caret staying in the search field so typing filters immediately. Filtering matches an option whose primary or secondary text contains the typed text, case-insensitively. Selecting from filtered results shows the selection in the field and clears the search: the popover's filtering is never committed, so the next open starts from the full list.
Focus that is a side effect of another interaction (such as the focus returning to the field when the popover closes) never reopens it.
Keyboard interaction
The option list is never part of the page tab sequence in single-select variants: the field is the single tab stop, Tab moves past the popover entirely, and the options are navigated with the arrow keys. Pressing Tab or clicking outside the dropdown closes the popover. In multi-select variants the options are checkboxes, which are genuine tab stops (the checkbox-group pattern).
Single select (non-searchable)
| Key | Popover closed | Popover open |
|---|---|---|
| Tab | Focuses the field and opens the popover; focus stays on the field | — |
| Tab (from the field) | — | The clear button (when a selection exists), then out of the dropdown |
| ArrowDown / ArrowUp | Opens the popover and moves focus into the option list (the selected option, or the first one if nothing is selected) | Moves focus into the option list from the field; moves between options within the list (skipping disabled options) |
| Home / End (in the list) | — | Jump to the first / last option |
| Printable characters | — | Type-ahead: jump to the first option whose text starts with the typed characters (like a native <select>) |
| Enter / Space (on an option) | — | Selects the option and closes the popover; focus returns to the field |
| Escape | — | Closes the popover; focus returns to the field |
Single select (searchable)
| Key | Behaviour |
|---|---|
| Tab | Focuses the search field (the popover does not open on focus) |
| Click | Opens the popover |
| Printable characters | Open the popover when closed and filter the options as you type; typing while an option is focused returns focus to the search field first and appends to any search text already typed (Space still toggles the focused row's control) |
| ArrowDown / ArrowUp | Closed: open the popover and move focus into the option list. Field-focused with the popover open: move focus into the list. Within the list: move between options (skipping disabled options) |
| Enter (on an option) | Selects the option and closes the popover; the field shows the selection (already present when focus returns, so screen readers announce it) |
| Escape | Closes the popover; any typed search is discarded and the field shows the selection again |
Multi select (non-searchable)
| Key | With a selection (chip shown) | Without a selection |
|---|---|---|
| Tab | Focuses the chip's close button and opens the popover; focus stays on the chip | Focuses the trigger and opens the popover; focus stays on the trigger |
| Tab again | The first checkbox (the trigger is not a tab stop while the chip is shown) | The first checkbox |
| Tab / Shift+Tab (in the list) | Move between the checkboxes, then out of the dropdown | Same |
| Space (on a checkbox) | Toggles the option; the popover stays open | Same |
| ArrowDown / ArrowUp (on the field) | Move focus into the option list (the first checkbox) | Same |
| Enter (on the chip's close button) | Clears every selection | — |
| Escape | Closes the popover; focus returns to the field | Same |
Multi select (searchable) follows the same model as non-searchable multi select, with the filtering input sitting between the chip and the list: typing always searches (and returns to the field from the list first), and ArrowDown from the input moves focus into the option list.
Popover positioning
The popover is positioned with CSS directly below the field — it never flips above the dropdown. It is clipped by an ancestor with overflow: hidden, auto, scroll or clip (e.g. a modal's scrollable body), so consumers should give the field enough room below it inside such containers.
Using the aria prop
The aria prop accepts an object used to provide accessible names for the parts of the component that cannot be derived from its other props:
| Prop | Options | Description | Default |
|-----------------------|-----------|----------------------------------------------------------------------------------------------------|-------------|
| label | string | The accessible name for the field when no visible label is provided. | undefined |
| clearSelection | string | The accessible name for the field's clear button in single-select variants without search, which removes the current selection. The field's accessible name (label, aria.label or placeholder) is appended for screen reader context, e.g. "Clear selection, Meal". The button has no accessible name when unset — always provide it. | undefined |
| clearAllSelections | string | The accessible name for the "N selected" chip's close button (multi select), which removes every selection at once. A {count} placeholder is replaced with the number of selections, e.g. "Clear all {count} selections", and the field's accessible name (label, aria.label or placeholder) is appended for screen reader context, e.g. "Clear all 2 selections, Meal". The button has no accessible name when unset — always provide it. | undefined |
The clear button
Which clear affordance is shown depends on the variant:
| Variant | Clear button | What it clears | |----------------------------|-------------------------------------------------------|-----------------------------------| | Single select | Shown while a selection exists | The selection | | Single select (searchable) | Never shown | — (typed search text is cleared by backspacing) | | Multi select | Never shown | — (the chip's close icon clears every selection) | | Multi select (searchable) | Never shown | — (the chip's close icon clears every selection) |
Slots
| Slot | Description |
|-----------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
| leadingIcon | An icon shown at the start of the field, visually supporting it. Slot a pie-icons-webc icon, e.g. <icon-placeholder slot="leadingIcon">. Slotted icons render at 20px regardless of their size attribute. |
CSS Variables
The component defines these CSS variables, which can be overridden:
| Variable | Description | Default |
|---------------------------------|---------------------------------------------------|------------------------------------|
| --dropdown-height | The height of the field. | 48px (40px small, 56px large) |
| --dropdown-border-color | The colour of the field's border. | var(--dt-color-border-form) |
| --dropdown-background-color | The background colour of the field. | var(--dt-color-container-default) |
| --dropdown-text-color | The text colour of the field. | var(--dt-color-content-default) |
| --dropdown-placeholder-color | The placeholder text colour. | var(--dt-color-content-subdued) |
| --dropdown-leading-icon-color | The colour of the field's leading icons (the static leadingIcon and the selected option's icon). | var(--dt-color-content-subdued) |
Events
| Event | Description |
|------------------------|----------------------------------------------------------------------------------------------------------------------------------|
| change | Fired when the selection is changed by the user, and when the owning form is reset (custom behaviour — native controls do not fire change on reset). Like a native form control, it bubbles and crosses the shadow boundary; read the new value from event.target.value. |
| pie-dropdown-open | Fired when the popover is opened. detail: { source: "keyboard" \| "pointer" } |
| pie-dropdown-close | Fired when the popover is closed. detail: { source: "keyboard" \| "pointer" \| "outside" \| "selection" } |
| pie-dropdown-search | Fired when the user types in a searchable dropdown. Use it to load options asynchronously: detail: { searchText: string } |
Controlled and uncontrolled usage
The component works in both modes. Uncontrolled (the default): the component owns its value and participates in its owning form's submission, reset and validation. Controlled (e.g. React state as the source of truth): pass the value prop and listen for change — in React the generated wrapper exposes it as onChange, which receives the native event (not React's synthetic ChangeEvent), so the new value is read from event.target.value:
const [meal, setMeal] = useState('');
<PieDropdown
label="Meal"
options={options}
value={meal}
onChange={(event) => setMeal(event.target.value)} />Because a change event also fires when the owning form is reset, controlled state stays in sync after resets.
Usage Examples
Single select
For HTML:
<script type="module">
import '@justeattakeaway/pie-webc/components/dropdown.js';
const dropdown = document.querySelector('pie-dropdown');
dropdown.options = [
{ value: 'pizza', primaryText: 'Pizza' },
{ value: 'burger', primaryText: 'Burger' },
{ value: 'salad', primaryText: 'Salad', disabled: true },
];
</script>
<pie-dropdown
label="Meal"
placeholder="Select a meal"
name="meal"
value="pizza">
</pie-dropdown>For React Applications:
import { PieDropdown } from '@justeattakeaway/pie-webc/react/dropdown.js';
const options = [
{ value: 'pizza', primaryText: 'Pizza' },
{ value: 'burger', primaryText: 'Burger' },
{ value: 'salad', primaryText: 'Salad', disabled: true },
];
<PieDropdown
label="Meal"
placeholder="Select a meal"
name="meal"
options={options}
value="pizza" />Multi select
For HTML:
<script type="module">
import '@justeattakeaway/pie-webc/components/dropdown.js';
const dropdown = document.querySelector('pie-dropdown');
dropdown.options = [
{ value: 'cheese', primaryText: 'Cheese', secondaryText: 'Extra mature' },
{ value: 'pepperoni', primaryText: 'Pepperoni' },
];
dropdown.value = ['cheese'];
</script>
<pie-dropdown
label="Toppings"
name="toppings"
isMultiselect>
</pie-dropdown>For React Applications:
import { PieDropdown } from '@justeattakeaway/pie-webc/react/dropdown.js';
const options = [
{ value: 'cheese', primaryText: 'Cheese', secondaryText: 'Extra mature' },
{ value: 'pepperoni', primaryText: 'Pepperoni' },
];
<PieDropdown
label="Toppings"
name="toppings"
isMultiselect
options={options} />Multi select, searchable
For HTML:
<script type="module">
import '@justeattakeaway/pie-webc/components/dropdown.js';
const dropdown = document.querySelector('pie-dropdown');
dropdown.options = [
{ value: 'cheese', primaryText: 'Cheese', secondaryText: 'Extra mature' },
{ value: 'pepperoni', primaryText: 'Pepperoni' },
{ value: 'mushroom', primaryText: 'Mushroom' },
];
dropdown.value = ['cheese', 'pepperoni'];
</script>
<pie-dropdown
label="Toppings"
name="toppings"
isMultiselect
isSearchable
placeholder="Search toppings"
selectedCountText="{count} picked">
</pie-dropdown>For React Applications:
import { useState } from 'react';
import { PieDropdown } from '@justeattakeaway/pie-webc/react/dropdown.js';
const options = [
{ value: 'cheese', primaryText: 'Cheese', secondaryText: 'Extra mature' },
{ value: 'pepperoni', primaryText: 'Pepperoni' },
{ value: 'mushroom', primaryText: 'Mushroom' },
];
const [toppings, setToppings] = useState(['cheese', 'pepperoni']);
<PieDropdown
label="Toppings"
name="toppings"
isMultiselect
isSearchable
placeholder="Search toppings"
selectedCountText="{count} picked"
options={options}
value={toppings}
onChange={(event) => setToppings(event.target.value)} />Searchable, with asynchronous options
For HTML:
<script type="module">
import '@justeattakeaway/pie-webc/components/dropdown.js';
const dropdown = document.querySelector('pie-dropdown');
dropdown.addEventListener('pie-dropdown-search', async (event) => {
dropdown.isLoading = true;
const results = await fetchOptions(event.detail.searchText);
dropdown.options = results.map((result) => ({
value: result.id,
primaryText: result.name,
}));
dropdown.isLoading = false;
});
</script>
<pie-dropdown
label="Restaurant location"
placeholder="Search countries"
isSearchable>
</pie-dropdown>For React Applications:
import { useState } from 'react';
import { PieDropdown } from '@justeattakeaway/pie-webc/react/dropdown.js';
const [options, setOptions] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const handleSearch = async (event) => {
setIsLoading(true);
const results = await fetchOptions(event.detail.searchText);
setOptions(results.map((result) => ({
value: result.id,
primaryText: result.name,
})));
setIsLoading(false);
};
<PieDropdown
label="Restaurant location"
placeholder="Search countries"
isSearchable
options={options}
isLoading={isLoading}
onPieDropdownSearch={handleSearch} />With option icons
See Option icons — each option's icon names the pie-icons-webc element the dropdown renders:
<script type="module">
import '@justeattakeaway/pie-icons-webc/dist/IconFlagGb.js';
import '@justeattakeaway/pie-icons-webc/dist/IconFlagIe.js';
import '@justeattakeaway/pie-webc/components/dropdown.js';
const dropdown = document.querySelector('pie-dropdown');
dropdown.options = [
{ value: 'gb', primaryText: 'United Kingdom', icon: 'icon-flag-gb' },
{ value: 'ie', primaryText: 'Ireland', icon: 'icon-flag-ie' },
];
</script>
<pie-dropdown
label="Country"
value="gb">
</pie-dropdown>Questions and Support
If you have any questions or need help using the PIE dropdown component, please visit the PIE Documentation or contact the PIE team via our support channels.
Contributing
Check out our contributing guide for more information about how you can contribute to the PIE Design System.
