Dropdown
Pick a single value from a list of options.
Installation
Terminal
pnpm dlx shadcn@latest add \
https://react-toolbox.io/r/dropdown.json \
https://react-toolbox.io/r/button.jsonNew here? Set up your project first. Styles, fonts, and required components are included. The command includes the components used by the examples below.
Usage
Options use value and label; adapt other data with valueKey and labelKey. For custom template content, supply plain text with textValue or textKey so typeahead can match it.
Composition
Compose the trigger, selection, groups, and options. Set name on DropdownRoot to include its value in the native form.
Show composition code
tsx
'use client';
import { useId, useState } from 'react';
import { Button } from '@/components/ui/material/button';
import { FontIcon } from '@/components/ui/material/font-icon';
import {
DropdownRoot,
DropdownTrigger,
DropdownSelection,
DropdownIcon,
DropdownPortal,
DropdownContent,
DropdownViewport,
DropdownGroup,
DropdownLabel,
DropdownOption,
DropdownOptionText,
DropdownOptionIndicator,
} from '@/components/ui/material/dropdown';
export default function DropdownComposition() {
const labelId = useId();
const [saved, setSaved] = useState('');
return (
<form
className="max-w-sm space-y-4"
onSubmit={(event) => {
event.preventDefault();
setSaved(String(new FormData(event.currentTarget).get('destination')));
}}
>
<span id={labelId} className="text-toolbox-caption text-toolbox-text-secondary">
Destination
</span>
<DropdownRoot name="destination" defaultValue="madrid" required>
<DropdownTrigger aria-labelledby={labelId}>
<DropdownSelection placeholder="Choose a city" />
<DropdownIcon asChild>
<FontIcon value="arrow_drop_down" aria-hidden />
</DropdownIcon>
</DropdownTrigger>
<DropdownPortal>
<DropdownContent>
<DropdownViewport>
<DropdownGroup>
<DropdownLabel>Europe</DropdownLabel>
<DropdownOption value="madrid">
<DropdownOptionText>Madrid</DropdownOptionText>
<DropdownOptionIndicator className="ml-auto">
<FontIcon value="check" aria-hidden />
</DropdownOptionIndicator>
</DropdownOption>
<DropdownOption value="lisbon">
<DropdownOptionText>Lisbon</DropdownOptionText>
<DropdownOptionIndicator className="ml-auto">
<FontIcon value="check" aria-hidden />
</DropdownOptionIndicator>
</DropdownOption>
</DropdownGroup>
<DropdownGroup>
<DropdownLabel>Asia</DropdownLabel>
<DropdownOption value="tokyo" disabled>
<DropdownOptionText>Tokyo — unavailable</DropdownOptionText>
</DropdownOption>
</DropdownGroup>
</DropdownViewport>
</DropdownContent>
</DropdownPortal>
</DropdownRoot>
<Button primary type="submit">
Save destination
</Button>
<output className="block text-toolbox-body">{saved && `Destination: ${saved}`}</output>
</form>
);
}
State and callbacks
- Use
value/onChange, ordefaultValuefor internal state and form reset.name,form, andrequiredsupport native forms. Visibility acceptsopen/onOpenChangeordefaultOpen. - With
allowBlank={false}, an unmatched value displays the first enabled option. This fallback does not callonChangeor update your value.
API reference
Key props and defaults. Full types and inline documentation are included with the source in material/dropdown.
Dropdown
| Property | Type | Default |
|---|---|---|
allowBlank | boolean | true |
defaultValue | DropdownValue | — |
groupKey | keyof T | 'group' as keyof T |
onChange | (value: DropdownValue, event: | React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement> | React.ChangeEvent<HTMLSelectElement>) => void | — |
readOnly | booleanKeep the selected value focusable and submitted while preventing edits and validation. | false |
sourceRequired | readonly (T | DropdownValue)[] | — |
template | (item: T) => React.ReactNode | — |
textKey | keyof T | 'textValue' as keyof T |
value | DropdownValue | — |
Material guidelines
- Use a labeled dropdown to choose one value from a defined set. Keep the selected value visible when the options close.
- Keep options concise. If a choice needs lengthy explanation or a separate confirmation, consider a selection dialog instead.