Skip to content
GitHub11 stars

A common form component for choosing a predefined value in a dropdown menu.

<div class="select-field">
    <SelectRoot Items="Apples">
        <SelectLabel class="select-label">

Usage guidelines

  • Prefer Combobox for large lists: Select isn't filterable, apart from keyboard typeahead to find and highlight items. Prefer Combobox when there are enough items to warrant filtering.
  • Special positioning behavior: By default the popup overlaps its trigger, so the selected item's text lines up with the trigger's value. Turn it off with AlignItemWithTrigger.
  • Form controls must have an accessible name: Prefer SelectLabel, or give SelectTrigger an aria-label when no visible label is rendered.
  • Closing animations: The popup stays rendered until its closing animation finishes. See Animation.

Anatomy

Import the primitives and assemble the parts:

@using Blazix.Primitives

<SelectRoot>
    <SelectLabel />
    <SelectTrigger>
        <SelectValue />
        <SelectIcon />
    </SelectTrigger>

    <SelectPortal>
        <SelectBackdrop />
        <SelectPositioner>
            <SelectPopup>
                <SelectScrollUpArrow />
                <SelectArrow />
                <SelectList>
                    <SelectItem>
                        <SelectItemText />
                        <SelectItemIndicator />
                    </SelectItem>
                    <SelectSeparator />
                    <SelectGroup>
                        <SelectGroupLabel />
                    </SelectGroup>
                </SelectList>
                <SelectScrollDownArrow />
            </SelectPopup>
        </SelectPositioner>
    </SelectPortal>
</SelectRoot>

API Reference

SelectRoot

Doesn't render its own HTML element.

ParameterTypeDefault
Value string? -
ValueChanged

Called with the new value when Value changes, for @bind-Value.

EventCallback<string?> -
Values IReadOnlyList<string>? -
ValuesChanged

Called with the new value when Values changes, for @bind-Values.

EventCallback<IReadOnlyList<string>?> -
OnValueChange

Runs before the value changes, with the reason; call Cancel() to keep it.

EventCallback<ValueChangeEventArgs<IReadOnlyList<string>>> -
Multiple bool false
Items IEnumerable<SelectOption>? -
Disabled

Whether the component should ignore user interaction.

bool false
ReadOnly

Whether the user should be unable to change the value. It stays focusable.

bool false
Required

Whether the user must enter a value before submitting a form.

bool false
Name string? -
Modal bool true
AlignItemWithTrigger bool true
LoopFocus bool true
HighlightItemOnHover

Whether moving the pointer over an item highlights it.

bool true
Open bool false
OpenChanged

Called with the new value when Open changes, for @bind-Open.

EventCallback<bool> -
OnOpenChange EventCallback<OpenChangeEventArgs> -
OnOpenChangeComplete EventCallback<bool> -
Handle PopupHandle? -
ContentTemplate RenderFragment<object?>? -
ChildContent

The part's content.

RenderFragment? -

SelectTrigger

Renders a <button> element.

ParameterTypeDefault
Disabled

Whether the component should ignore user interaction.

bool false
Handle PopupHandle? -
Payload object? -
NativeButton bool? -
Render RenderFragment<PopupTriggerRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-popup-open

Present when the corresponding select is open.

-
data-pressed

Present when the trigger is pressed.

-
data-disabled

Present when the select is disabled.

-
data-readonly

Present when the select is readonly.

-
data-required

Present when the select is required.

-
data-valid

Present when the select is in a valid state (when wrapped in FieldRoot).

-
data-invalid

Present when the select is in an invalid state (when wrapped in FieldRoot).

-
data-dirty

Present when the select's value has changed (when wrapped in FieldRoot).

-
data-touched

Present when the select has been touched (when wrapped in FieldRoot).

-
data-filled

Present when the select has a value (when wrapped in FieldRoot).

-
data-focused

Present when the select trigger is focused (when wrapped in FieldRoot).

-
data-placeholder

Present when the select doesn't have a value.

-

SelectValue

Renders a <span> element.

ParameterTypeDefault
Placeholder string? -
Template RenderFragment<IReadOnlyList<string>>? -
Render RenderFragment<SelectRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-placeholder

Present when the select doesn't have a value.

-

SelectIcon

Renders a <span> element.

ParameterTypeDefault
Render RenderFragment<SelectRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-popup-open

Present when the corresponding popup is open.

-

SelectList

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<SelectRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

SelectPortal

Doesn't render its own HTML element.

ParameterTypeDefault
KeepMounted bool false
ChildContent

The part's content.

RenderFragment? -

SelectBackdrop

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<PopupRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-open

Present when the select is open.

-
data-closed

Present when the select is closed.

-
data-starting-style

Present when the select begins animating in.

-
data-ending-style

Present when the select is animating out.

-

SelectPositioner

Doesn't render its own HTML element.

ParameterTypeDefault
AlignItemWithTrigger bool? -
Side Side? -
Align Align? -
SideOffset int? -
AlignOffset int? -
CollisionPadding int? -
ArrowPadding int? -
Sticky bool? -
Anchor string? -
ChildContent

The part's content.

RenderFragment? -
AttributeType
data-open

Present when the select popup is open.

-
data-closed

Present when the select popup is closed.

-
data-anchor-hidden

Present when the anchor is hidden.

-
data-align

Indicates how the popup is aligned relative to specified side.

'start' | 'center' | 'end'
data-side

Indicates which side the popup is positioned relative to the trigger.

'none' | 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start'
CSS variableType
--anchor-height

The anchor's height.

number
--anchor-width

The anchor's width.

number
--available-height

The available height between the trigger and the edge of the viewport.

number
--available-width

The available width between the trigger and the edge of the viewport.

number
--transform-origin

The coordinates that this element is anchored to. Used for animations and transitions.

string

SelectPopup

Renders a <div> element.

ParameterTypeDefault
Side Side? -
Align Align? -
SideOffset int? -
AlignOffset int? -
CollisionPadding int? -
ArrowPadding int 5
Sticky bool false
Anchor string? -
FallbackAxis bool? -
KeepMounted bool false
InitialFocus string? -
FinalFocus string? -
PositionerAttributes IReadOnlyDictionary<string, object>? -
Render RenderFragment<SelectRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-open

Present when the select is open.

-
data-closed

Present when the select is closed.

-
data-align

Indicates how the popup is aligned relative to specified side.

'start' | 'center' | 'end'
data-side

Indicates which side the popup is positioned relative to the trigger.

'none' | 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start'
data-starting-style

Present when the select begins animating in.

-
data-ending-style

Present when the select is animating out.

-

SelectArrow

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<PopupRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-open

Present when the select popup is open.

-
data-closed

Present when the select popup is closed.

-
data-uncentered

Present when the select arrow is uncentered.

-
data-align

Indicates how the popup is aligned relative to specified side.

'start' | 'center' | 'end'
data-side

Indicates which side the popup is positioned relative to the trigger.

'none' | 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start'

SelectItem

Renders a <div> element.

ParameterTypeDefault
Valuerequired string -
Label string? -
Disabled

Whether the component should ignore user interaction.

bool false
Render RenderFragment<SelectItemRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-selected

Present when the select item is selected.

-
data-highlighted

Present when the select item is highlighted.

-
data-disabled

Present when the select item is disabled.

-

SelectGroup

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<SelectRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

SelectGroupLabel

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<PartRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

SelectSeparator

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<PartRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-orientation

Indicates the orientation of the separator.

'horizontal' | 'vertical'

SelectLabel

Renders a <label> element.

ParameterTypeDefault
Render RenderFragment<SelectRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

SelectItemText

Renders a <span> element.

ParameterTypeDefault
Render RenderFragment<SelectItemRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

SelectItemIndicator

Renders a <span> element.

ParameterTypeDefault
KeepMounted bool false
Render RenderFragment<SelectItemRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-starting-style

Present when the indicator begins animating in.

-
data-ending-style

Present when the indicator is animating out.

-

SelectScrollUpArrow

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<PartRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-direction

Indicates the direction of the scroll arrow.

'up'
data-side

Indicates which side the popup is positioned relative to the trigger.

'none' | 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start'
data-visible

Present when the scroll arrow is visible.

-
data-starting-style

Present when the scroll arrow begins animating in.

-
data-ending-style

Present when the scroll arrow is animating out.

-

SelectScrollDownArrow

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<PartRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-direction

Indicates the direction of the scroll arrow.

'down'
data-side

Indicates which side the popup is positioned relative to the trigger.

'none' | 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start'
data-visible

Present when the scroll arrow is visible.

-
data-starting-style

Present when the scroll arrow begins animating in.

-
data-ending-style

Present when the scroll arrow is animating out.

-