--- 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"] ) %}
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).
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:
<div class="popover"> which wraps around the entire component and holds it state (e.g. open/close).<button> that acts as the trigger to open or close the popover.<div data-popover> that holds the content of the popover.<div data-popover> is a <nav role="listbox"> that contains a combination of:
<div role="option"> for the options with a data-value attribute.<hr role="separator"> to display a horizontal separator.<div role="group"> to group options.<span role="heading"> for group headings.<input type="hidden"> to hold the value of the field (if needed).As with the Popover component, you can set up a few additional options on the <div data-popover> element:
data-side can be set to top, bottom, left, or right to change the side of the popover.data-align can be set to start, center, or end to change the alignment of the popover.If you use Jinja (or Nunjucks), you can use the dropdown_menu() macro directly. Read more about using Basecoat with Jinja and Nunjucks.