Skip to content

Dropdown

The Dropdown component allows you to create a dropdown menu with selectable options. It works seamlessly with the Dropdown.Options and Dropdown.Option slots.

For detailed information about the Dropdown.Options and Dropdown.Option slots, refer to the Dropdown.Options documentation.

To implement a dropdown in your UI, wrap the Dropdown.Options and Dropdown.Option slots within the Dropdown component:

import Dropdown from '@components/Basic/Dropdown/Dropdown';
const App = () => {
return (
<Dropdown>
<Dropdown.Options>
<Dropdown.Option selected value="red">red</Dropdown.Option>
<Dropdown.Option value="green">green</Dropdown.Option>
<Dropdown.Option value="blue">blue</Dropdown.Option>
</Dropdown.Options>
</Dropdown>
);
};
export default App;
Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the component’s root element.
classstring""Additional CSS classes to apply to the component.
refDropdownRef | undefinedundefinedA reference to the component, providing access to its methods and underlying HTML element for programmatic control.
disabledbooleanfalseDisables the dropdown when set to true.
class-disabledstring""Additional CSS classes to apply when the dropdown is disabled.
onChange(value: string) => voidundefinedCallback function triggered whenever the selected option changes, providing the selected option’s value.
onActionRecord<string, (scope?: string, ...args: any[]) => void>undefinedExtends or overrides the component’s default navigation action handlers. See Implemented Navigation Actions for details.
anchorstring | HTMLElementundefinedLinks navigation to another element. When the anchor element is focused, the dropdown’s actions will execute. Can be a CSS selector or HTMLElement.

To interact with the Dropdown programmatically, you can use the DropdownRef interface. This interface provides properties and methods to access and manipulate the component’s state.

PropertyTypeDescription
selectedAccessor<string>Read the currently selected option: ref.selected() returns the value.
elementHTMLDivElementA reference to the dropdown’s root HTML element, useful for DOM access or styling.
MethodParametersReturn ValueDescription
selectOptionvalue: stringvoidSets a new selected option programmatically.

The Dropdown.Trigger slot allows you to customize the dropdown’s trigger button, which toggles the dropdown’s open or closed state when clicked. Use this slot to modify the trigger’s appearance by applying custom styles or CSS classes.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the trigger element.
classstring""Additional CSS classes to apply to the trigger element.
import Dropdown from '@components/Basic/Dropdown/Dropdown';
const App = () => {
return (
<Dropdown>
<Dropdown.Options>
<Dropdown.Option selected value="red">red</Dropdown.Option>
<Dropdown.Option value="green">green</Dropdown.Option>
<Dropdown.Option value="blue">blue</Dropdown.Option>
</Dropdown.Options>
<Dropdown.Trigger style={{ background: '#cccccc' }}/>
</Dropdown>
);
};
export default App;

The Dropdown.Placeholder slot allows you to display a placeholder when no option is selected in the dropdown. If this slot is not used, no placeholder will be shown by default.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the placeholder element.
classstring""Additional CSS classes to apply to the placeholder element.
childrenJSX.Element""Content to display when no option is selected, such as text or JSX.
import Dropdown from '@components/Basic/Dropdown/Dropdown';
const App = () => {
return (
<Dropdown>
<Dropdown.Options>
<Dropdown.Option value="red">red</Dropdown.Option>
<Dropdown.Option value="green">green</Dropdown.Option>
<Dropdown.Option value="blue">blue</Dropdown.Option>
</Dropdown.Options>
<Dropdown.Placeholder style={{ color: '#cccccc' }}>Select any option</Dropdown.Placeholder>
</Dropdown>
);
};
export default App;

The Dropdown.Icon slot allows you to add or customize the icon displayed inside the dropdown’s trigger, typically on the right side.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the dropdown trigger’s icon element.
classstring""Additional CSS classes to apply to the dropdown trigger’s icon element.
childrenJSX.Element""Custom content, such as text, HTML, or JSX, to render as the dropdown icon.
import Dropdown from '@components/Basic/Dropdown/Dropdown';
const App = () => {
return (
<Dropdown>
<Dropdown.Options>
<Dropdown.Option selected value="red">red</Dropdown.Option>
<Dropdown.Option value="green">green</Dropdown.Option>
<Dropdown.Option value="blue">blue</Dropdown.Option>
</Dropdown.Options>
<Dropdown.Icon style={{ background: '#cccccc' }}>
<img src="my-custom-icon.png" />
</Dropdown.Icon>
</Dropdown>
);
};
export default App;

The Dropdown.Track slot enables you to customize the scroll track of the dropdown when the options overflow the container.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the dropdown’s track element.
classstring""Additional CSS classes to apply to the dropdown’s track element.
childrenJSX.Element""Custom content, such as text, HTML, or JSX, to render inside the track.
import Dropdown from '@components/Basic/Dropdown/Dropdown';
const App = () => {
return (
<Dropdown>
<Dropdown.Options>
<Dropdown.Option selected value="red">red</Dropdown.Option>
<Dropdown.Option value="green">green</Dropdown.Option>
<Dropdown.Option value="blue">blue</Dropdown.Option>
</Dropdown.Options>
<Dropdown.Track style={{ background: '#cccccc' }}/>
</Dropdown>
);
};
export default App;

The Dropdown.Handle slot lets you customize the scroll handle for the dropdown when the options overflow the container.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the handle element.
classstring""Additional CSS classes to apply to the handle element.
childrenJSX.Element""Custom content, such as text, HTML, or JSX, to render inside the handle.
import Dropdown from '@components/Basic/Dropdown/Dropdown';
const App = () => {
return (
<Dropdown>
<Dropdown.Options>
<Dropdown.Option selected value="red">red</Dropdown.Option>
<Dropdown.Option value="green">green</Dropdown.Option>
<Dropdown.Option value="blue">blue</Dropdown.Option>
</Dropdown.Options>
<Dropdown.Handle style={{ background: '#cccccc' }}/>
</Dropdown>
);
};
export default App;

The Dropdown component implements the following navigation actions by default:

Action NameBehavior
selectOpens the dropdown when closed. When open, collapses the dropdown (options handle selection)
backCloses the dropdown and returns focus to the trigger or anchor element

You can extend the Dropdown with additional navigation actions or override the default behavior using the onAction prop:

import Dropdown from '@components/Basic/Dropdown/Dropdown';
<Dropdown
onAction={{
'select': () => console.log('Custom select behavior!'),
'back': () => console.log('Custom back behavior!')
}}
>
<Dropdown.Options>
<Dropdown.Option value="red">red</Dropdown.Option>
<Dropdown.Option value="green">green</Dropdown.Option>
<Dropdown.Option value="blue">blue</Dropdown.Option>
</Dropdown.Options>
</Dropdown>

For more information about navigation actions, check the Navigation component documentation.

Retrieve the selected option’s value on change

Section titled “Retrieve the selected option’s value on change”

To capture the value of the selected option:

  • Use the onChange prop to handle changes in the selected option.
  1. Pass a callback function to the onChange prop to handle state updates.
  2. Access the selected option’s value through the selected parameter in the callback.
import Dropdown from '@components/Basic/Dropdown/Dropdown';
const App = () => {
const handleSelectionChange = (selected: string) => {
console.log(selected);
};
return (
<Dropdown onChange={handleSelectionChange}>
<Dropdown.Options>
<Dropdown.Option selected value="red">red</Dropdown.Option>
<Dropdown.Option value="green">green</Dropdown.Option>
<Dropdown.Option value="blue">blue</Dropdown.Option>
</Dropdown.Options>
</Dropdown>
);
};
export default App;

The Dropdown component allows you to programmatically update the selected option.

  1. Create a ref variable of type DropdownRef and assign it to the Dropdown component.
  2. Use the ref.selectOption() method to programmatically set a new selected option, such as in response to a button click or keyboard event.
import Dropdown, { DropdownRef } from '@components/Basic/Dropdown/Dropdown';
const App = () => {
let dropdownRef!: DropdownRef;
const handleKeyPress = (e: KeyboardEvent) => {
if (e.keyCode !== 13) return
dropdownRef.selectOption('green');
};
return (
<Dropdown ref={dropdownRef} keypress={handleKeyPress}>
<Dropdown.Options>
<Dropdown.Option selected value="red">red</Dropdown.Option>
<Dropdown.Option value="green">green</Dropdown.Option>
<Dropdown.Option value="blue">blue</Dropdown.Option>
</Dropdown.Options>
</Dropdown>
);
};
export default App;