Displays a list of options for the user to pick from—triggered by a button.
Can be controlled or uncontrolled.
Offers 2 positioning modes.
Supports items, labels, groups of items.
Focus is fully managed.
Full keyboard navigation.
Supports custom placeholder.
Typeahead support.
Supports Right to Left direction.
Install the component from your command line.
npm install @radix-ui/react-select
Import all parts and piece them together.
import * as Select from '@radix-ui/react-select';
export default () => (
<Select.Root>
<Select.Trigger>
<Select.Value />
<Select.Icon />
</Select.Trigger>
<Select.Portal>
<Select.Content>
<Select.ScrollUpButton />
<Select.Viewport>
<Select.Item>
<Select.ItemText />
<Select.ItemIndicator />
</Select.Item>
<Select.Group>
<Select.Label />
<Select.Item>
<Select.ItemText />
<Select.ItemIndicator />
</Select.Item>
</Select.Group>
<Select.Separator />
</Select.Viewport>
<Select.ScrollDownButton />
<Select.Arrow />
</Select.Content>
</Select.Portal>
</Select.Root>
);
Contains all the parts of a select.
The button that toggles the select. The Select.Content
will position itself by aligning over the trigger.
The part that reflects the selected value. By default the selected item's text will be rendered. if you require more control, you can instead control the select and pass your own children
. It should not be styled to ensure correct positioning. An optional placeholder
prop is also available for when the select has no value.
A small icon often displayed next to the value as a visual affordance for the fact it can be open. By default renders ▼ but you can use your own icon via asChild
or use children
.
When used, portals the content part into the body
.
The component that pops out when the select is open.
The scrolling viewport that contains all of the items.
The component that contains the select items.
The textual part of the item. It should only contain the text you want to see in the trigger when that item is selected. It should not be styled to ensure correct positioning.
Renders when the item is selected. You can style this element directly, or you can use it as a wrapper to put an icon into, or both.
An optional button used as an affordance to show the viewport overflow as well as functionaly enable scrolling upwards.
An optional button used as an affordance to show the viewport overflow as well as functionaly enable scrolling downwards.
Used to group multiple items. use in conjunction with Select.Label
to ensure good accessibility via automatic labelling.
Used to render the label of a group. It won't be focusable using arrow keys.
Used to visually separate items in the select.
An optional arrow element to render alongside the content. This can be used to help visually link the trigger with the Select.Content
. Must be rendered inside Select.Content
. Only available when position
is set to popper
.
By default, Select
will behave similarly to a native MacOS menu by positioning Select.Content
relative to the active item. If you would prefer an alternative positioning approach similar to Popover
or DropdownMenu
then you can set position
to popper
and make use of additional alignment options such as side
, sideOffset
and more.
// index.jsx
import * as Select from '@radix-ui/react-select';
export default () => (
<Select.Root>
<Select.Trigger>…</Select.Trigger>
<Select.Portal>
<Select.Content position="popper" sideOffset={5}>
…
</Select.Content>
</Select.Portal>
</Select.Root>
);
When using position="popper"
on Select.Content
, you may want to constrain the width of the content so that it matches the trigger width. You may also want to constrain its height to not exceed the viewport.
We expose several CSS custom properties such as --radix-select-trigger-width
and --radix-select-content-available-height
to support this. Use them to constrain the content dimensions.
// index.jsx
import * as Select from '@radix-ui/react-select';
import './styles.css';
export default () => (
<Select.Root>
<Select.Trigger>…</Select.Trigger>
<Select.Portal>
<Select.Content
className="SelectContent"
position="popper"
sideOffset={5}
>
…
</Select.Content>
</Select.Portal>
</Select.Root>
);
/* styles.css */
.SelectContent {
width: var(--radix-select-trigger-width);
max-height: var(--radix-select-content-available-height);
}
You can add special styles to disabled items via the data-disabled
attribute.
// index.jsx
import * as Select from '@radix-ui/react-select';
import './styles.css';
export default () => (
<Select.Root>
<Select.Trigger>…</Select.Trigger>
<Select.Portal>
<Select.Content>
<Select.Viewport>
<Select.Item className="SelectItem" disabled>
…
</Select.Item>
<Select.Item>…</Select.Item>
<Select.Item>…</Select.Item>
</Select.Viewport>
</Select.Content>
</Select.Portal>
</Select.Root>
);
/* styles.css */
.SelectItem[data-disabled] {
color: 'gainsboro';
}
You can use the placeholder
prop on Value
for when the select has no value. There's also a data-placeholder
attribute on Trigger
to help with styling.
// index.jsx
import * as Select from '@radix-ui/react-select';
import './styles.css';
export default () => (
<Select.Root>
<Select.Trigger className="SelectTrigger">
<Select.Value placeholder="Pick an option" />
<Select.Icon />
</Select.Trigger>
<Select.Portal>
<Select.Content>…</Select.Content>
</Select.Portal>
</Select.Root>
);
/* styles.css */
.SelectTrigger[data-placeholder] {
color: 'gainsboro';
}
Use the Separator
part to add a separator between items.
<Select.Root>
<Select.Trigger>…</Select.Trigger>
<Select.Portal>
<Select.Content>
<Select.Viewport>
<Select.Item>…</Select.Item>
<Select.Item>…</Select.Item>
<Select.Item>…</Select.Item>
<Select.Separator />
<Select.Item>…</Select.Item>
<Select.Item>…</Select.Item>
</Select.Viewport>
</Select.Content>
</Select.Portal>
</Select.Root>
Use the Group
and Label
parts to group items in a section.
<Select.Root>
<Select.Trigger>…</Select.Trigger>
<Select.Portal>
<Select.Content>
<Select.Viewport>
<Select.Group>
<Select.Label>Label</Select.Label>
<Select.Item>…</Select.Item>
<Select.Item>…</Select.Item>
<Select.Item>…</Select.Item>
</Select.Group>
</Select.Viewport>
</Select.Content>
</Select.Portal>
</Select.Root>
You can use custom content in your items.
import * as Select from '@radix-ui/react-select';
export default () => (
<Select.Root>
<Select.Trigger>…</Select.Trigger>
<Select.Portal>
<Select.Content>
<Select.Viewport>
<Select.Item>
<Select.ItemText>
<img src="…" />
Adolfo Hess
</Select.ItemText>
<Select.ItemIndicator>…</Select.ItemIndicator>
</Select.Item>
<Select.Item>…</Select.Item>
<Select.Item>…</Select.Item>
</Select.Viewport>
</Select.Content>
</Select.Portal>
</Select.Root>
);
By default the trigger will automatically display the selected item ItemText
's content. You can control what appears by chosing to put things inside/outside the ItemText
part.
If you need more flexibility, you can control the component using value
/onValueChange
props and passing children
to SelectValue
. Remember to make sure what you put in there is accessible.
const countries = { france: '🇫🇷', 'united-kingdom': '🇬🇧', spain: '🇪🇸' };
export default () => {
const [value, setValue] = React.useState('france');
return (
<Select.Root value={value} onValueChange={setValue}>
<Select.Trigger>
<Select.Value aria-label={value}>
{countries[value]}
</Select.Value>
<Select.Icon />
</Select.Trigger>
<Select.Portal>
<Select.Content>
<Select.Viewport>
<Select.Item value="france">
<Select.ItemText>France</Select.ItemText>
<Select.ItemIndicator>…</Select.ItemIndicator>
</Select.Item>
<Select.Item value="united-kingdom">
<Select.ItemText>United Kingdom</Select.ItemText>
<Select.ItemIndicator>…</Select.ItemIndicator>
</Select.Item>
<Select.Item value="spain">
<Select.ItemText>Spain</Select.ItemText>
<Select.ItemIndicator>…</Select.ItemIndicator>
</Select.Item>
</Select.Viewport>
</Select.Content>
</Select.Portal>
</Select.Root>
);
};
The native scrollbar is hidden by default as we recommend using the ScrollUpButton
and ScrollDownButton
parts for the best UX. If you do not want to use these parts, compose your select with our Scroll Area primitive.
// index.jsx
import * as Select from '@radix-ui/react-select';
import * as ScrollArea from '@radix-ui/react-scroll-area';
import './styles.css';
export default () => (
<Select.Root>
<Select.Trigger>…</Select.Trigger>
<Select.Portal>
<Select.Content>
<ScrollArea.Root className="ScrollAreaRoot" type="auto">
<Select.Viewport asChild>
<ScrollArea.Viewport className="ScrollAreaViewport">
<StyledItem>…</StyledItem>
<StyledItem>…</StyledItem>
<StyledItem>…</StyledItem>
</ScrollArea.Viewport>
</Select.Viewport>
<ScrollArea.Scrollbar
className="ScrollAreaScrollbar"
orientation="vertical"
>
<ScrollArea.Thumb className="ScrollAreaThumb" />
</ScrollArea.Scrollbar>
</ScrollArea.Root>
</Select.Content>
</Select.Portal>
</Select.Root>
);
/* styles.css */
.ScrollAreaRoot {
width: 100%;
height: 100%;
}
.ScrollAreaViewport {
width: 100%;
height: 100%;
}
.ScrollAreaScrollbar {
width: 4px;
padding: 5px 2px;
}
.ScrollAreaThumb {
background: rgba(0, 0, 0, 0.3);
border-radius: 3px;
}
Adheres to the ListBox WAI-ARIA design pattern.
See the W3C Select-Only Combobox example for more information.
Use our Label component in order to offer a visual and accessible label for the select.
import * as Select from '@radix-ui/react-select';
import { Label } from '@radix-ui/react-label';
export default () => (
<>
<Label>
Country
<Select.Root>…</Select.Root>
</Label>
{/* or */}
<Label htmlFor="country">Country</Label>
<Select.Root>
<Select.Trigger id="country">…</Select.Trigger>
<Select.Portal>
<Select.Content>…</Select.Content>
</Select.Portal>
</Select.Root>
</>
);
Create your own API by abstracting the primitive parts into your own component.
Select
and SelectItem
This example abstracts most of the parts.
import { Select, SelectItem } from './your-select';
export default () => (
<Select defaultValue="2">
<SelectItem value="1">Item 1</SelectItem>
<SelectItem value="2">Item 2</SelectItem>
<SelectItem value="3">Item 3</SelectItem>
</Select>
);
// your-select.jsx
import React from 'react';
import * as SelectPrimitive from '@radix-ui/react-select';
import {
CheckIcon,
ChevronDownIcon,
ChevronUpIcon,
} from '@radix-ui/react-icons';
export const Select = React.forwardRef(
({ children, ...props }, forwardedRef) => {
return (
<SelectPrimitive.Root {...props}>
<SelectPrimitive.Trigger ref={forwardedRef}>
<SelectPrimitive.Value />
<SelectPrimitive.Icon>
<ChevronDownIcon />
</SelectPrimitive.Icon>
</SelectPrimitive.Trigger>
<SelectPrimitive.Portal>
<SelectPrimitive.Content>
<SelectPrimitive.ScrollUpButton>
<ChevronUpIcon />
</SelectPrimitive.ScrollUpButton>
<SelectPrimitive.Viewport>{children}</SelectPrimitive.Viewport>
<SelectPrimitive.ScrollDownButton>
<ChevronDownIcon />
</SelectPrimitive.ScrollDownButton>
</SelectPrimitive.Content>
</SelectPrimitive.Portal>
</SelectPrimitive.Root>
);
}
);
export const SelectItem = React.forwardRef(
({ children, ...props }, forwardedRef) => {
return (
<SelectPrimitive.Item {...props} ref={forwardedRef}>
<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
<SelectPrimitive.ItemIndicator>
<CheckIcon />
</SelectPrimitive.ItemIndicator>
</SelectPrimitive.Item>
);
}
);