--- layout: layouts/page.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: HTML id: usage-html - label: HTML + Javascript id: usage-js - 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 "select.njk" import select %} {% set code_native %}{% endset %} {{ code_preview("select-native", code_native) }} {% set code_html %} {% call select( name="select-js", trigger_attrs={"class": "w-[180px]"}, register_on=["alpine:init", "htmx:afterSwap"] ) %}
Fruits
Apple
Banana
Blueberry
Grapes
Pineapple
{% endcall %} {% endset %} {% set code_script %} {% endset %} {{ code_preview("select", code_script | prettyHtml ~ code_html | prettyHtml) }}

Usage

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) }}

HTML + Javascript

{% lucide "circle-alert" %}

This component requires Javascript.

You will need to include the Javascript (Alpine.js) code provided below before the HTML code for this component. As with all interactive components, you will need to add some Alpine.js properties to certain elements (e.g. x-bind, x-data, @click).

If you need to do more than what <select> allows (e.g. HTML options), you can use this component. It is structured as such:

As with the Popover component, you can set up a few additional options on the <div data-popover> element:

{% from "tabs.njk" import tabs %} {% set tabsets = [ { tab: "HTML", panel: code_block(code_html | prettyHtml, class="") }, { tab: "Javascipt (Alpine.js)", panel: code_block(code_script | prettyHtml, class="") } ] %} {{ tabs(id="select", tabsets=tabsets) }}

Jinja or Nunjucks

If you use Jinja (or Nunjucks), you can use the dropdown_menu() macro directly. Read more about using Basecoat with Jinja and Nunjucks.

{% 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"}, register_on=["alpine:init", "htmx:afterSwap"] ) %} {% 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"}, register_on=["alpine:init", "htmx:afterSwap"] ) %}
Disabled
{% endcall %} {% endset %} {{ code_preview("select-disabled", code | prettyHtml) }}

With icon

{% set code %} {% call select( trigger_attrs={'class': 'w-[180px]'}, register_on=["alpine:init", "htmx:afterSwap"] ) %}
{% 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) }}