Select
A common form component for choosing a predefined value in a dropdown menu.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Select;
use function Phem\Ui\{SelectIcon, SelectItem, SelectItemIndicator, SelectItemText, SelectLabel, SelectList, SelectPopup, SelectScrollDownArrow, SelectScrollUpArrow, SelectTrigger, SelectValue};
#[Component]
function SelectHeroDemo(): Node
{
$apples = [
'gala' => 'Gala',
'fuji' => 'Fuji',
'honeycrisp' => 'Honeycrisp',
'granny-smith' => 'Granny Smith',
'pink-lady' => 'Pink Lady',
];
return (
<Select id="apple" name="apple" sideOffset={{ 4 }}>
<SelectLabel>Apple</SelectLabel>
<SelectTrigger>
<SelectValue placeholder="Select apple" />
<SelectIcon />
</SelectTrigger>
<SelectPopup>
<SelectScrollUpArrow />
<SelectList>
{% for value, name in apples %}
<SelectItem key={{ value }} value={{ value }}>
<SelectItemIndicator />
<SelectItemText>{{ name }}</SelectItemText>
</SelectItem>
{% endfor %}
</SelectList>
<SelectScrollDownArrow />
</SelectPopup>
</Select>
);
}
Examples#
Formatting the value#
SelectValue renders the label of the selected item: its SelectItemText, not its raw value.
To show more than a text, see the object values.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Select;
use function Phem\Ui\{SelectIcon, SelectItem, SelectItemIndicator, SelectItemText, SelectLabel, SelectList, SelectPopup, SelectTrigger, SelectValue};
#[Component]
function SelectFormattedDemo(): Node
{
$themes = ['system' => 'System default', 'light' => 'Light', 'dark' => 'Dark'];
return (
<Select id="formatted" defaultValue="system" sideOffset={{ 4 }}>
<SelectLabel>Theme</SelectLabel>
<SelectTrigger>
<SelectValue />
<SelectIcon />
</SelectTrigger>
<SelectPopup>
<SelectList>
{% for value, name in themes %}
<SelectItem key={{ value }} value={{ value }}>
<SelectItemIndicator />
<SelectItemText>{{ name }}</SelectItemText>
</SelectItem>
{% endfor %}
</SelectList>
</SelectPopup>
</Select>
);
}
Labeling a select#
Use SelectLabel to provide a visible label for the select trigger. SelectLabel renders a <div>, so clicking it focuses the select trigger without opening the popup.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Select;
use function Phem\Ui\{SelectIcon, SelectItem, SelectItemIndicator, SelectItemText, SelectLabel, SelectList, SelectPopup, SelectTrigger, SelectValue};
#[Component]
function SelectLabeledDemo(): Node
{
$themes = ['system' => 'System default', 'light' => 'Light', 'dark' => 'Dark'];
return (
<Select id="labeled" defaultValue="light" sideOffset={{ 4 }}>
<SelectLabel>Theme</SelectLabel>
<SelectTrigger>
<SelectValue />
<SelectIcon />
</SelectTrigger>
<SelectPopup>
<SelectList>
{% for value, name in themes %}
<SelectItem key={{ value }} value={{ value }}>
<SelectItemIndicator />
<SelectItemText>{{ name }}</SelectItemText>
</SelectItem>
{% endfor %}
</SelectList>
</SelectPopup>
</Select>
);
}
Placeholder values#
To show a placeholder value, use the placeholder prop on SelectValue.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Select;
use function Phem\Ui\{SelectIcon, SelectItem, SelectItemIndicator, SelectItemText, SelectLabel, SelectList, SelectPopup, SelectTrigger, SelectValue};
#[Component]
function SelectPlaceholderDemo(): Node
{
$themes = ['system' => 'System default', 'light' => 'Light', 'dark' => 'Dark'];
return (
<Select id="placeholder" sideOffset={{ 4 }}>
<SelectLabel>Theme</SelectLabel>
<SelectTrigger>
<SelectValue placeholder="Select theme" />
<SelectIcon />
</SelectTrigger>
<SelectPopup>
<SelectList>
{% for value, name in themes %}
<SelectItem key={{ value }} value={{ value }}>
<SelectItemIndicator />
<SelectItemText>{{ name }}</SelectItemText>
</SelectItem>
{% endfor %}
</SelectList>
</SelectPopup>
</Select>
);
}
Multiple selection#
Add the multiple prop to the Select component to allow multiple selections. The template of SelectValue gives the text of two values or more.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Select;
use function Phem\Ui\{SelectIcon, SelectItem, SelectItemIndicator, SelectItemText, SelectLabel, SelectList, SelectPopup, SelectTrigger, SelectValue};
#[Component]
function SelectMultipleDemo(): Node
{
$languages = [
'javascript' => 'JavaScript',
'typescript' => 'TypeScript',
'python' => 'Python',
'java' => 'Java',
'csharp' => 'C#',
'php' => 'PHP',
'cpp' => 'C++',
'rust' => 'Rust',
'go' => 'Go',
'swift' => 'Swift',
];
return (
<Select id="languages" multiple defaultValue={{ ['javascript', 'typescript'] }} alignItemWithTrigger={{ false }} sideOffset={{ 4 }}>
<SelectLabel>Languages</SelectLabel>
<SelectTrigger className="min-w-56">
<SelectValue placeholder="Select languages" template="{first} (+{others} more)" />
<SelectIcon />
</SelectTrigger>
<SelectPopup>
<SelectList>
{% for value, name in languages %}
<SelectItem key={{ value }} value={{ value }}>
<SelectItemIndicator />
<SelectItemText>{{ name }}</SelectItemText>
</SelectItem>
{% endfor %}
</SelectList>
</SelectPopup>
</Select>
);
}
Object values#
Select items can show more than a text. With markup, SelectValue shows a copy of the markup of the selected item text.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Select;
use function Phem\Ui\{SelectIcon, SelectItem, SelectItemIndicator, SelectItemText, SelectLabel, SelectList, SelectPopup, SelectScrollDownArrow, SelectScrollUpArrow, SelectTrigger, SelectValue};
#[Component]
function SelectObjectValuesDemo(): Node
{
$methods = [
[
'id' => 'standard',
'name' => 'Standard',
'duration' => 'Delivers in 4-6 business days',
'price' => '$4.99',
],
[
'id' => 'express',
'name' => 'Express',
'duration' => 'Delivers in 2-3 business days',
'price' => '$9.99',
],
[
'id' => 'overnight',
'name' => 'Overnight',
'duration' => 'Delivers next business day',
'price' => '$19.99',
],
];
return (
<Select id="shipping" defaultValue="standard" sideOffset={{ 4 }}>
<SelectLabel>Shipping method</SelectLabel>
<SelectTrigger className="h-auto min-h-control min-w-[16rem] py-1.5">
<SelectValue markup />
<SelectIcon />
</SelectTrigger>
<SelectPopup>
<SelectScrollUpArrow />
<SelectList>
{% for method in methods %}
<SelectItem key={{ method.id }} value={{ method.id }} className="items-start">
<SelectItemIndicator className="relative top-[0.4em] self-start" />
<SelectItemText>
<span className="flex flex-col items-start gap-0.5">
<span className="text-sm">{{ method.name }}</span>
<span className="text-xs opacity-70">{{ method.duration }} ({{ method.price }})</span>
</span>
</SelectItemText>
</SelectItem>
{% endfor %}
</SelectList>
<SelectScrollDownArrow />
</SelectPopup>
</Select>
);
}
Grouped#
Organize related options with SelectGroup and SelectGroupLabel to add section headings inside the popup.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Select;
use function Phem\Ui\{SelectGroup, SelectGroupLabel, SelectIcon, SelectItem, SelectItemIndicator, SelectItemText, SelectLabel, SelectList, SelectPopup, SelectScrollDownArrow, SelectScrollUpArrow, SelectSeparator, SelectTrigger, SelectValue};
#[Component]
function SelectGroupedDemo(): Node
{
$groups = [
'Fruits' => [
'apple' => 'Apple',
'banana' => 'Banana',
'mango' => 'Mango',
'kiwi' => 'Kiwi',
'grape' => 'Grape',
'orange' => 'Orange',
'strawberry' => 'Strawberry',
'watermelon' => 'Watermelon',
],
'Vegetables' => [
'broccoli' => 'Broccoli',
'carrot' => 'Carrot',
'cauliflower' => 'Cauliflower',
'cucumber' => 'Cucumber',
'kale' => 'Kale',
'bell-pepper' => 'Bell pepper',
'spinach' => 'Spinach',
'zucchini' => 'Zucchini',
],
];
return (
<Select id="produce" sideOffset={{ 4 }}>
<SelectLabel>Produce</SelectLabel>
<SelectTrigger>
<SelectValue placeholder="Select produce" />
<SelectIcon />
</SelectTrigger>
<SelectPopup>
<SelectScrollUpArrow />
<SelectList>
{% for name, items in groups %}
<SelectGroup key={{ name }}>
<SelectGroupLabel>{{ name }}</SelectGroupLabel>
{% for value, label in items %}
<SelectItem key={{ value }} value={{ value }}>
<SelectItemIndicator />
<SelectItemText>{{ label }}</SelectItemText>
</SelectItem>
{% endfor %}
</SelectGroup>
{% if not loop.last %}<SelectSeparator />{% endif %}
{% endfor %}
</SelectList>
<SelectScrollDownArrow />
</SelectPopup>
</Select>
);
}
API reference#
Import the components of the family:
use Phem\Ui\Select;
use function Phem\Ui\{SelectLabel, SelectTrigger, SelectValue, SelectIcon, SelectPopup, SelectList, SelectScrollUpArrow, SelectScrollDownArrow, SelectItem, SelectItemIndicator, SelectItemText, SelectGroup, SelectGroupLabel, SelectSeparator};Select#
A button that opens a list of options: Base UI Select.Root, on the phem-select controller.
defaultValue is the value of the selected item (a list with multiple). With a name (or phem:bind), a hidden input keeps the value for the form. By default the popup goes over the trigger (alignItemWithTrigger). Give an id to label the trigger with its SelectLabel.
| Prop | Type | Default |
|---|---|---|
defaultValue | string|array|null | null |
multiple | bool | false |
name | ?string | null |
id | ?string | null |
alignItemWithTrigger | bool | true |
modal | bool | true |
disabled | bool | false |
readOnly | bool | false |
required | bool | false |
loopFocus | bool | false |
side | 'top' | 'bottom' | 'left' | 'right' | 'inline-start' | 'inline-end' | Side::Bottom |
align | 'start' | 'center' | 'end' | Align::Start |
sideOffset | int | 0 |
value | ?Bound | null |
children | ?Html | null |
...attrs | array | [] |
SelectLabel#
The label of the select. It names the trigger (aria-labelledby).
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SelectTrigger#
The button that opens the popup. In a Toolbar, it is an item of the toolbar.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SelectValue#
The label of the selected item, or the placeholder. The controller writes its text. template gives the text of two values or more ({first} (+{others} more)), and markup shows a copy of the markup of the item text.
| Prop | Type | Default |
|---|---|---|
placeholder | ?string | null |
template | ?string | null |
markup | bool | false |
...rest | mixed | — |
SelectItem#
An option. value is its value.
| Prop | Type | Default |
|---|---|---|
value (required) | string | — |
disabled | bool | false |
children | ?Html | null |
...rest | mixed | — |
SelectIcon#
The icon of the trigger, a caret by default.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SelectPopup#
The box of the select, with the list of the items. The controller places it next to the trigger.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SelectList#
The list of the items in the popup (listbox). It scrolls when the items are higher than the screen.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SelectScrollUpArrow#
An arrow at the top of the popup: a hover scrolls the list up.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SelectScrollDownArrow#
An arrow at the bottom of the popup: a hover scrolls the list down.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SelectItemIndicator#
The check of the selected item.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SelectItemText#
The text of an item: the label that the value part shows.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SelectGroup#
A group of items of the list, with a SelectGroupLabel.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SelectGroupLabel#
The label of a SelectGroup.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SelectSeparator#
A line between two items or two groups of the list.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
The source of the examples: examples/ui/Select.phem.