--- layout: layouts/component.njk title: Select description: Displays a list of options for the user to pick from—triggered by a button. toc: - label: Usage id: usage children: - label: Default id: usage-default - label: Jinja or Nunjucks id: usage-jinja - label: Examples id: examples children: - label: Scrollable id: example-scrollable - label: Disabled id: example-disabled - label: With icon id: example-with-icon --- {% from "macros/code_preview.njk" import code_preview %} {% from "macros/code_block.njk" import code_block %} {% from "components/select.njk" import select %} {% set code_native %}{% endset %} {{ code_preview("select-native", code_native) }} {% set code_alpine %} {% call select(name="select-js", trigger_attrs={"class": "w-[180px]"}) %}
Fruits
Apple
Banana
Blueberry
Grapes
Pineapple
{% endcall %} {% endset %} {{ code_preview("select-alpine", code_alpine | prettyHtml) }}

Usage

Pure HTML

If you use a <select> element, just add the select class to it or have a parent with the form class (read more about form).

{{ code_block(code_native) }}

With Javascript

If you need to do more than what <select> allows (e.g. HTML options), you can use the code below which uses the Popover component. Make sure to include the hidden input with the name you want to use for that field.

{{ code_block(code_alpine | prettyHtml) }}

Jinja or Nunjucks

If you use Jinja (or Nunjucks), you can use the select() macro directly.

{% set raw_code %}{% raw %}{% call select(name="select-js") %}
Fruits
Apple
Banana
Blueberry
Grapes
Pineapple
{% endcall %}{% endraw %}{% endset %} {{ code_block(raw_code, "jinja") }}

Examples

Scrollable

{% set code %} {% call select( listbox_attrs={"class": "scrollbar overflow-y-auto max-h-64"} ) %} {% for i in range(0, 99) %}
Item {{ i }}
{% endfor %} {% endcall %} {% endset %} {{ code_preview("select-scrollable", code | prettyHtml) }}

Disabled

{% set code %} {% call select(trigger_attrs={"disabled": "disabled"}) %}
Disabled
{% endcall %} {% endset %} {{ code_preview("select-disabled", code | prettyHtml) }}

With icon

{% set code %} {% call select(trigger_attrs={'class': 'w-[180px]'}) %}
{% lucide "chart-bar", {"class": "text-muted-foreground"} %} Bar
{% lucide "chart-line", {"class": "text-muted-foreground"} %} Line
{% lucide "chart-pie", {"class": "text-muted-foreground"} %} Pie
{% endcall %} {% endset %} {{ code_preview("select-scrollable", code | prettyHtml) }}