Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/combobox.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Combobox } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    /** What the `view` closure of the kit's `Combobox` receives. */
    export type ComboboxParts = Component.Headless.Parts<typeof Combobox>
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    const icon = <Msg>(h: Html.H<Msg>, path: string) =>
      h.el("svg")
        .attrs({
          viewBox: "0 0 24 24",
          fill: "none",
          stroke: "currentColor",
          "stroke-width": "2",
          "stroke-linecap": "round",
          "stroke-linejoin": "round",
          "aria-hidden": "true"
        })
        .children(h.el("path").attr("d", path))
    
    const iconButton =
      "grid size-6 shrink-0 place-items-center rounded-sm text-muted-foreground transition-colors duration-150 " +
      "hover:bg-accent hover:text-foreground data-[disabled]:pointer-events-none [&_svg]:size-3.5"
    
    /** The text above the input, or above the trigger with `inputInPopup`. */
    export const ComboboxLabel = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.label(h.div.class(cn("mb-1.5 cursor-default text-[13px] leading-snug font-medium", options.class)))
    
    /** The bordered box around the input and its buttons. The popup is placed against it. */
    export const ComboboxInputGroup = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.inputGroup(
        h.div.class(cn(
          "flex min-h-8.5 w-60 items-center rounded-md border border-input bg-background pr-1 shadow-sm",
          "transition-[border-color,box-shadow] duration-150",
          "focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/15",
          "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 data-[readonly]:bg-muted",
          options.class
        ))
      )
    
    /** The text input. Chain `.attr("placeholder", ...)` and an `aria-label` when there is no label. */
    export const ComboboxInput = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.input(
        h.input.class(cn(
          "h-8 min-w-0 flex-1 bg-transparent px-3 text-[13px] text-foreground outline-none",
          "placeholder:text-muted-foreground disabled:cursor-not-allowed",
          "group-data-[slot=combobox-chips]/chips:h-6 group-data-[slot=combobox-chips]/chips:min-w-16",
          "group-data-[slot=combobox-chips]/chips:px-1",
          options.class
        ))
      )
    
    /** The button that clears the text and the value. `undefined` while there is nothing to clear. */
    export const ComboboxClear = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.clear({ element: h.button.class(cn(iconButton, options.class)) })?.children(
        icon(h, "M18 6L6 18M6 6l12 12")
      )
    
    /** The chevron button at the end of the input group. */
    export const ComboboxTrigger = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.trigger(h.button.class(cn(iconButton, options.class))).children(
        combobox.icon(h.span.class("grid place-items-center")).children(icon(h, "M6 9l6 6 6-6"))
      )
    
    /** With `inputInPopup`: the button that is the combobox. It shows the selected label and a chevron. */
    export const ComboboxButton = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.trigger(
        h.button.class(cn(
          "inline-flex h-8.5 w-56 items-center justify-between gap-2 rounded-md border border-input bg-background",
          "pr-2 pl-3 text-start text-[13px] whitespace-nowrap text-foreground shadow-sm select-none",
          "transition-colors duration-150 hover:bg-accent data-[popup-open]:bg-accent",
          "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 [&_svg]:size-3.5",
          options.class
        ))
      ).children(
        combobox.value(h.span.class("min-w-0 truncate data-[placeholder]:text-muted-foreground")),
        combobox.icon(h.span.class("grid shrink-0 place-items-center text-muted-foreground")).children(
          icon(h, "M7 15l5 5 5-5M7 9l5-5 5 5")
        )
      )
    
    /** With `multiple`: the row of chips, with the input as its last child. Goes inside the input group. */
    export const ComboboxChips = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.chips(
        h.div.attr("data-slot", "combobox-chips").class(
          cn("group/chips flex min-w-0 flex-1 flex-wrap items-center gap-1 py-1 pl-1", options.class)
        )
      )
    
    /** One selected value. Put its label and a `ComboboxChipRemove` inside. */
    export const ComboboxChip = <Msg>(
      h: Html.H<Msg>,
      combobox: ComboboxParts,
      options: ClassOption & { readonly value: string }
    ) =>
      combobox.chip({
        element: h.div.class(cn(
          "inline-flex h-6 cursor-default items-center gap-0.5 rounded-sm bg-secondary pr-0.5 pl-2 text-xs font-medium",
          "text-secondary-foreground outline-none select-none focus:bg-brand focus:text-brand-foreground",
          "data-[disabled]:opacity-60",
          options.class
        )),
        value: options.value
      })
    
    export const ComboboxChipRemove = <Msg>(
      h: Html.H<Msg>,
      combobox: ComboboxParts,
      options: ClassOption & { readonly value: string; readonly label?: string | undefined }
    ) =>
      combobox.chipRemove({
        element: h.button
          .attr("aria-label", options.label)
          .class(cn(
            "grid size-5 place-items-center rounded-xs text-current opacity-60 transition-opacity duration-150",
            "hover:opacity-100 [&_svg]:size-3",
            options.class
          )),
        value: options.value
      }).children(icon(h, "M18 6L6 18M6 6l12 12"))
    
    /** The element the kit places, in the top layer. Put `ComboboxPopup` inside. */
    export const ComboboxPositioner = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.positioner(h.div.class(cn("outline-none", options.class)))
    
    /** The box under the input: as wide as the input group, at most 20rem tall. */
    export const ComboboxPopup = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.popup(
        h.div.class(cn(
          "box-border flex max-h-[min(var(--available-height),20rem)] w-[var(--anchor-width)] min-w-48 flex-col",
          "max-w-[var(--available-width)] overflow-hidden rounded-lg border bg-popover text-[13px] text-foreground",
          "shadow-float outline-none",
          "origin-[var(--transform-origin)] transition-[opacity,scale] duration-150",
          "data-[starting-style]:scale-95 data-[starting-style]:opacity-0",
          "data-[ending-style]:scale-95 data-[ending-style]:opacity-0",
          options.class
        ))
      )
    
    /** With `inputInPopup`: the search row at the top of the popup, holding the input. */
    export const ComboboxSearchInput = <Msg>(
      h: Html.H<Msg>,
      combobox: ComboboxParts,
      options: ClassOption & { readonly placeholder?: string | undefined } = {}
    ) =>
      h.div.class(cn("flex shrink-0 items-center border-b pl-3 text-muted-foreground [&_svg]:size-3.5", options.class))
        .children(
          icon(h, "M21 21l-4.3-4.3M11 18a7 7 0 100-14 7 7 0 000 14z"),
          combobox.input(
            h.input
              .attrs({ placeholder: options.placeholder, "aria-label": options.placeholder })
              .class(
                "h-9 min-w-0 flex-1 bg-transparent px-2 text-[13px] text-foreground outline-none " +
                  "placeholder:text-muted-foreground"
              )
          )
        )
    
    /** The scrolling list of options. */
    export const ComboboxList = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.list(
        h.div.class(cn("min-h-0 flex-1 scroll-py-1 overflow-y-auto p-1 outline-none data-[empty]:p-0", options.class))
      )
    
    /** One option. Put its label and a `ComboboxItemIndicator` inside. */
    export const ComboboxItem = <Msg>(
      h: Html.H<Msg>,
      combobox: ComboboxParts,
      options: ClassOption & { readonly value: string }
    ) =>
      combobox.item({
        element: h.div.class(cn(
          "flex min-h-7.5 cursor-default items-center justify-between gap-2 rounded-sm px-2 py-1 leading-snug",
          "select-none data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground",
          "data-[selected]:font-medium data-[disabled]:text-muted-foreground data-[disabled]:opacity-60",
          options.class
        )),
        value: options.value
      })
    
    /** The tick of a selected option. `undefined` for the others. */
    export const ComboboxItemIndicator = <Msg>(
      h: Html.H<Msg>,
      combobox: ComboboxParts,
      options: ClassOption & { readonly value: string }
    ) =>
      combobox.itemIndicator({
        element: h.span.class(cn(
          "mr-1.5 mb-0.5 ml-2.5 h-[9px] w-[5px] shrink-0 rotate-45 border-r-[1.5px] border-b-[1.5px] border-brand",
          options.class
        )),
        value: options.value
      })
    
    /** A run of options under a `ComboboxGroupLabel`. `group` is the `group` of the items inside. */
    export const ComboboxGroup = <Msg>(
      h: Html.H<Msg>,
      combobox: ComboboxParts,
      options: ClassOption & { readonly group: string }
    ) => combobox.group({ element: h.div.key(options.group).class(cn(options.class)), group: options.group })
    
    export const ComboboxGroupLabel = <Msg>(
      h: Html.H<Msg>,
      combobox: ComboboxParts,
      options: ClassOption & { readonly group: string }
    ) =>
      combobox.groupLabel({
        element: h.div.class(cn("px-2 pt-1.5 pb-1 text-xs font-medium text-muted-foreground", options.class)),
        group: options.group
      })
    
    export const ComboboxSeparator = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.separator(h.div.class(cn("-mx-1 my-1 h-px bg-border", options.class)))
    
    /** The message shown while no option matches. Its children are dropped while there are options. */
    export const ComboboxEmpty = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.empty(h.div.class(cn("px-3 py-5 text-center text-muted-foreground empty:hidden", options.class)))
    
    /** A live region above the list, for a message such as "Searching…". */
    export const ComboboxStatus = <Msg>(h: Html.H<Msg>, combobox: ComboboxParts, options: ClassOption = {}) =>
      combobox.status(h.div.class(cn("px-3 py-2.5 text-muted-foreground empty:hidden", options.class)))
    
    /** A spinner for the input group while results load. */
    export const ComboboxSpinner = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) =>
      h.span.attr("aria-hidden", "true").class(cn(
        "mx-1.5 size-3.5 shrink-0 animate-spin rounded-full border-2 border-muted-foreground border-r-transparent",
        options.class
      ))
    
  3. Import it

    import { ComboboxLabel, ComboboxInputGroup, ComboboxInput, ComboboxClear, ComboboxTrigger, ComboboxButton, ComboboxChips, ComboboxChip, ComboboxChipRemove, ComboboxPositioner, ComboboxPopup, ComboboxSearchInput, ComboboxList, ComboboxItem, ComboboxItemIndicator, ComboboxGroup, ComboboxGroupLabel, ComboboxSeparator, ComboboxEmpty, ComboboxStatus, ComboboxSpinner } from "#ui/combobox"
    

Usage

Basic.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Combobox } from "@effect/ui-kit"
import { Schema } from "effect"
import {
  ComboboxClear,
  ComboboxEmpty,
  ComboboxInput,
  ComboboxInputGroup,
  ComboboxItem,
  ComboboxItemIndicator,
  ComboboxList,
  ComboboxPopup,
  ComboboxPositioner,
  ComboboxTrigger
} from "#ui/combobox"

const frameworks = [
  { value: "next", label: "Next.js" },
  { value: "sveltekit", label: "SvelteKit" },
  { value: "nuxt", label: "Nuxt" },
  { value: "remix", label: "Remix" },
  { value: "astro", label: "Astro" },
  { value: "solidstart", label: "SolidStart" }
]

const ComboboxBasicMessage = UiSchema.TaggedEnum({
  RequestedOpenFramework: {},
  RequestedCloseFramework: {},
  PickedFramework: { framework: Schema.NullOr(Schema.String) },
  RequestedInputValueFramework: { text: Schema.String }
})

export const ComboboxBasic = Html.component({
  name: "ComboboxBasic",
  model: Schema.Struct({
    open: Schema.Boolean,
    framework: Schema.NullOr(Schema.String),
    text: Schema.String
  }),
  message: ComboboxBasicMessage
}).make({
  init: () => ({ open: false, framework: null, text: "" }),
  update: {
    RequestedOpenFramework: ($) => $.evolve({ open: true }),
    RequestedCloseFramework: ($) => $.evolve({ open: false }),
    PickedFramework: ($, { framework }) => $.evolve({ framework }),
    RequestedInputValueFramework: ($, { text }) => $.evolve({ text })
  },
  view: (h, model) =>
    Combobox(h, {
      props: {
        open: model.open,
        value: model.framework,
        inputValue: model.text,
        items: frameworks,
        autoHighlight: true,
        sideOffset: 6
      },
      onEmit: {
        RequestedOpen: () => ComboboxBasicMessage.RequestedOpenFramework(),
        RequestedClose: () => ComboboxBasicMessage.RequestedCloseFramework(),
        Picked: ({ value }) =>
          ComboboxBasicMessage.PickedFramework({ framework: typeof value === "string" ? value : null }),
        RequestedInputValue: ({ inputValue }) =>
          ComboboxBasicMessage.RequestedInputValueFramework({ text: inputValue }),
        Highlighted: () => undefined,
        Settled: () => undefined
      },
      view: (combobox) =>
        h.div.children(
          ComboboxInputGroup(h, combobox).children(
            ComboboxInput(h, combobox).attrs({ placeholder: "Select a framework", "aria-label": "Framework" }),
            ComboboxClear(h, combobox),
            ComboboxTrigger(h, combobox)
          ),
          ComboboxPositioner(h, combobox).children(
            ComboboxPopup(h, combobox).children(
              ComboboxEmpty(h, combobox).children("No framework found."),
              ComboboxList(h, combobox).children(
                combobox.items.map(({ label, value }) =>
                  ComboboxItem(h, combobox, { value }).children(
                    label,
                    ComboboxItemIndicator(h, combobox, { value })
                  )
                )
              )
            )
          )
        )
    })
})

Examples

Multiple with chips

Each value is a chip in front of the input. Backspace removes the last one, and ArrowLeft moves onto the chips.

Labels

Groups

combobox.groups splits the filtered items by their group and drops the empty ones.

With inputInPopup a button shows the value, and the search input opens with the list.

Async results

With filter "none" the consumer supplies the items: here a Command answers after a delay, and the status part reports progress.

Destination

Disabled and read-only

A disabled option can be highlighted but not picked. A read-only combobox opens and can be browsed, not changed.

Disabled option
Disabled
Read-only

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
inputGroupA <div> around the input and its buttons. The popup is placed against it.
inputThe <input>, which is the combobox. Focus stays here while the list is used.
triggerA <button> that toggles the popup. With inputInPopup it is the combobox.
iconA <span> in the trigger.
clear({ element })A <button> that clears the text and the value. undefined while there is nothing to clear.
chipsA <div> around the chips and the input, with multiple.
chip({ element, value })A <div> per selected value. Draw combobox.selectedItems.
chipRemove({ element, value })A <button> in a chip that removes its value.
labelA <div> that names the input, the list and the popup.
valueA <span> in the trigger with the selected label, with inputInPopup.
positionerA <div> the combobox places, a manual popover in the top layer.
popupA <div> in the positioner: the box to style.
listA <div> around the items: the listbox.
item({ element, value })A <div> per drawn option. Draw combobox.items, the filtered list.
itemIndicator({ element, value })A <span> in an item, undefined unless it is selected.
group / groupLabel({ element, group })A <div> around the items of one of combobox.groups, and its label.
separatorA <div> between items.
emptyA <div> whose children show only while no item matches.
statusA <div> live region, for a message such as a count or "Searching".

API reference

Props

PropTypeDescription
openbooleanWhether the popup shows. Required.
inputValuestringThe input's text. Required.
itemsReadonlyArray<{ value, label, disabled?, group? }>Every option, in drawing order.
valuestring | ReadonlyArray<string> | nullThe selected value, or the values with multiple.
multiplebooleanMore than one option can be selected. Default false.
filter"contains" | "startsWith" | "endsWith" | "none"How the text matches labels. Default "contains".
filteredItemsReadonlyArray<ComboboxItem>The items to draw, from your own filter.
limitnumberThe most items drawn.
autoHighlightbooleanTyping highlights the first match, so Enter picks it. Default false.
inputInPopupbooleanThe input is drawn inside the popup and the trigger is the combobox.
inlinebooleanThe list is drawn without a popup and is always expanded.
openOnInputClickbooleanA press on the input opens the popup. Default true.
disabled / readOnly / requiredbooleanAs on a native <input>.
name / formstringAdds a hidden input, so a form submits the value.
side / align / sideOffsetPositioningWhere the popup goes. side defaults to "bottom".

Events

EventDescription
RequestedOpen({ reason })Asks for open to be true.
RequestedClose({ reason })Asks for open to be false.
Picked({ value, reason })Asks for value to be the whole new selection.
RequestedInputValue({ inputValue, reason })Asks for inputValue: what was typed, the picked label, or the selected label put back on close.
Highlighted({ value, index, reason })The highlighted item changed. For a virtualized list to scroll to it.
Settled({ open })The popup's transitions ended.

Data attributes

AttributeDescription
data-popup-openOn the input, the input group, the trigger, the icon and the clear button while open.
data-list-emptyOn the input, the input group and the trigger while no item matches.
data-disabled / data-readonlyOn the input, the input group and the trigger.
data-placeholderOn the trigger and the value while nothing is selected.
data-open / data-closedOn the positioner and the popup.
data-starting-style / data-ending-styleOn the popup for the first frame after it opens, and while it closes.
data-side / data-alignOn the positioner and the popup: where it ended up.
data-emptyOn the positioner, the popup and the list while no item matches.
data-selected / data-highlighted / data-disabledOn an item.
--anchor-width / --available-heightCSS variables on the positioner, for sizing the popup.
--transform-originCSS variable on the popup: the point it grows from.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.