Combobox

A fully accessible searchable input with intelligent positioning, keyboard navigation, and flexible data handling. Perfect for autocomplete scenarios, large option sets, and user-generated content.

The combobox combines text input with dropdown suggestions, supporting both frontend filtering and server-side async search. Features full keyboard navigation, smart positioning with Floating UI, and seamless Phoenix form integration.

Quick Start

The most basic combobox requires three components: .combobox, .combobox_input, and .combobox_options with .combobox_option elements.

Start typing to filter through options. The component automatically handles frontend filtering and keyboard navigation. Use arrow keys to navigate and Enter to select.

Advanced Usage

Selection

Set selections on the combobox to supply a selected item. Use a map with value and an optional display label. Pass the text supplied by :let to the input's value to display it immediately. Choose another country, then click Reset to restore the initial selection from the server.

Server-side Search

Set on_search on the input to enable debounced server-side search.

Handle the event with %{"query" => query} and return matching options. Return default options when the query is empty. No enclosing form is required.

Create Custom Values

Allow users to create values that don't exist in the predefined list using the .creatable_option component. Perfect for tags, categories, or any user-generated content scenarios.

Type a custom tag name and press Enter to create a new one.

The creatable option automatically appears when the user types something that doesn't match existing options. It shows "Create '[user input]'" and submits the typed value when selected.

Multiple Selection

Enable multiple selection by adding the multiple={true} attribute to the combobox. Use the .combobox_selections component to display selected items as removable pills.

Select multiple skills. Click the × button or press Backspace to remove selections.

The .combobox_selections component uses a template-based approach for rendering pills. Customize the appearance using the :selection slot - it supports surrounding markup including icons and badges. Use .combobox_selection_label to display the label and .combobox_selection_remove for a button bound to the selected value. Selected items are visually highlighted with data-selected="true".

For a react-select style appearance, wrap the selections and input in a flex container styled to look like an input field. The pills and input appear together in the same visual container, creating a seamless multi-select experience.

Smart Positioning

Powered by Floating UI, the combobox intelligently positions itself using the placement and offset attributes. It automatically flips to the opposite side when there's insufficient space.

Use placement="top-start", placement="right-start", or other positioning options. The component automatically repositions on scroll and window resize to stay visible.

Popup width

Width is controlled by CSS. --reference-width tracks the width of the search input, or the element selected by reference, while open. Apply width classes to .combobox_options.

Use min-w-[var(--reference-width)] for a minimum width, or w-[var(--reference-width)] for an exact match.

Flexible Markup

Combobox options support rich, custom markup including icons, descriptions, badges, and complex layouts. The component is completely unstyled, giving you full control over the visual presentation.

Build complex option layouts with multiple text elements, icons, images, or any HTML content. Use group-data-focus:text-white and similar selectors to style elements based on focus state.

Styling States

Combobox options expose two data attributes for styling different interaction states:

  • data-focus - Set to true when an option is focused via keyboard navigation or mouse hover. Perfect for highlighting the currently focused item.
  • data-selected - Set to true on the option that matches the current selection. Use this to visually indicate which option is selected, even when reopening the dropdown.

Use Tailwind's data-focus: and data-selected: modifiers to style these states.

Keyboard Interaction

The combobox component provides full keyboard accessibility with comprehensive navigation support. All interactions follow standard ARIA patterns for combobox components.

Key Description
When the input is focused:
↓ / ↑ Opens the combobox and focuses the first option
When the combobox is open:
Esc Closes the combobox and restores the previously selected value
↑ / ↓ Focuses the previous/next option (wraps around)
Home / PageUp Focuses the first option
End / PageDown Focuses the last option
Enter Selects the currently focused option and closes the combobox
A-Z / a-z Filters available options (frontend mode) or triggers search event (async mode)
Backspace Removes the last selected item (multi-select mode, when input is empty)