shadcn-templ 2.0 beta. Switch to templUI v1
1.7k

Select

Displays a list of options for the user to pick from, triggered by a button.

package examples import selectcomp "github.com/axadrn/shadcn-templ/v2/components/select"

Installation

shadcn-templ add select

Usage

import selectcomp "github.com/axadrn/shadcn-templ/v2/components/select"
@selectcomp.Select() {	@selectcomp.Trigger(selectcomp.TriggerProps{Class: "w-[180px]"}) {		@selectcomp.Value(selectcomp.ValueProps{Placeholder: "Theme"})	}	@selectcomp.Content() {		@selectcomp.Group() {			@selectcomp.Item(selectcomp.ItemProps{Value: "light"}) {				Light			}			@selectcomp.Item(selectcomp.ItemProps{Value: "dark"}) {				Dark			}			@selectcomp.Item(selectcomp.ItemProps{Value: "system"}) {				System			}		}	}}

Composition

Use the following composition to build a Select:

selectcomp.Select├── selectcomp.Trigger│   └── selectcomp.Value└── selectcomp.Content    ├── selectcomp.Group    │   ├── selectcomp.Label    │   ├── selectcomp.Item    │   └── selectcomp.Item    ├── selectcomp.Separator    └── selectcomp.Group        ├── selectcomp.Label        ├── selectcomp.Item        └── selectcomp.Item

Align Item With Trigger

By default the popup positions so the selected item appears over the trigger (Base UI’s alignItemWithTrigger). Set DisableAlignItemWithTrigger on selectcomp.Content to open it below the trigger edge like a dropdown instead.

Toggle to align the item with the trigger.

package examples import (

Groups

Use selectcomp.Group, selectcomp.Label, and selectcomp.Separator to organize items.

package examples import selectcomp "github.com/axadrn/shadcn-templ/v2/components/select"

Scrollable

A select with many items that scrolls.

package examples import selectcomp "github.com/axadrn/shadcn-templ/v2/components/select"

Disabled

package examples import selectcomp "github.com/axadrn/shadcn-templ/v2/components/select"

Invalid

Set the Invalid prop on the field.Field component and aria-invalid on the selectcomp.Trigger component to show an error state.

@field.Field(field.Props{Attributes: templ.Attributes{"data-invalid": "true"}}) {	@field.Label() {		Fruit	}	@selectcomp.Trigger(selectcomp.TriggerProps{Attributes: templ.Attributes{"aria-invalid": "true"}}) {		@selectcomp.Value()	}}
package examples import (

API Reference

Select

The selectcomp.Select component is the root that carries the selection and the form value.

Prop Type Default
Name string -
Value string -
Disabled bool false

SelectTrigger

The selectcomp.Trigger component is the button that opens the listbox.

Prop Type Default
Size SizeDefault | SizeSm SizeDefault
Class string -

SelectValue

The selectcomp.Value component shows the selected label inside the trigger.

Prop Type Default
Placeholder string -
Class string -

SelectContent

The selectcomp.Content component is the listbox popup.

Prop Type Default
DisableAlignItemWithTrigger bool false
Align AlignStart | AlignCenter | AlignEnd AlignCenter
Class string -

SelectGroup

The selectcomp.Group component wraps related items.

Prop Type Default
Class string -

SelectLabel

The selectcomp.Label component titles a group.

Prop Type Default
Class string -

SelectItem

The selectcomp.Item component is a selectable option.

Prop Type Default
Value string -
Label string -
Disabled bool false
Class string -

SelectSeparator

The selectcomp.Separator component divides groups.

Prop Type Default
Class string -