Skip to main content
196

Search Lumen

Find components, APIs, guides, and recipes.

GitHub
Web docs
Selection and preferences

useSelect

Manages a fully custom select with native <select> fallback, ARIA listbox, typeahead character search, keyboard navigation, and external form integration.

Live component preview · Astro

Explore Select

Try the component this hook connects to. The preview uses the Astro adapter; the React example below uses useSelect.

React usage

Import from @santi020k/lumen-react and spread the controller props onto the matching components.

tsxuseSelect example
import { Select } from '@santi020k/lumen-react'
import { useSelect } from '@santi020k/lumen-react'

export function CountrySelect() {
  const select = useSelect({
    options: ['United States', 'Canada', 'Mexico'],
    placeholder: 'Choose a country',
    onValueChange: (value) => console.log('Selected:', value)
  })

  return (
    <div {...select.rootProps}>
      <button {...select.triggerProps}>{select.triggerText}</button>
      <select {...select.nativeSelectProps} />
      <div {...select.listProps}>
        {select.options.map(option => (
          <button key={option.value} {...select.getOptionProps(option)}>
            {option.label}
          </button>
        ))}
      </div>
    </div>
  )
}
import { Select } from '@santi020k/lumen-react'
import { useSelect } from '@santi020k/lumen-react'

export function CountrySelect() {
  const select = useSelect({
    options: ['United States', 'Canada', 'Mexico'],
    placeholder: 'Choose a country',
    onValueChange: (value) => console.log('Selected:', value)
  })

  return (
    <div {...select.rootProps}>
      <button {...select.triggerProps}>{select.triggerText}</button>
      <select {...select.nativeSelectProps} />
      <div {...select.listProps}>
        {select.options.map(option => (
          <button key={option.value} {...select.getOptionProps(option)}>
            {option.label}
          </button>
        ))}
      </div>
    </div>
  )
}

Load @santi020k/lumen-react/styles.css once in your application. With React Server Components, put 'use client' at the top of the module that calls the hook. Follow the React setup guide for the framework boundary.

Options

PropertyTypeDefaultDescription
optionsArray<string | SelectOption>[]Available options. Strings are converted to { label, value } objects.
defaultValuestring""Initial selected value.
valuestring-Controlled selected value.
onValueChange(value: string) => void-Callback fired when the selected value changes.
placeholderstring-Placeholder text shown when no option is selected.
disabledbooleanfalseDisable the select trigger.
requiredbooleanfalseMark the hidden native select as required for form validation.
namestring-Name attribute for the hidden native select.
idstringautoExplicit id for the listbox.

Controller return values

PropertyTypeDescription
rootPropsLumenProps<"div">Spread onto the select root wrapper.
triggerPropsLumenProps<"button">Spread onto the trigger button. Includes keyboard navigation.
listPropsLumenProps<"div">Spread onto the listbox container.
controlPropsLumenProps<"div">Spread onto the visual control wrapper.
nativeSelectPropsLumenProps<"select">Spread onto a hidden native <select> for form compatibility.
getOptionProps(option)(option, props?) => LumenProps<"button">Returns props for an option button.
selectedOptionSelectOption | undefinedCurrently selected option object.
triggerTextstringDisplay text for the trigger (selected label or placeholder).
valuestringCurrently selected value.
optionsSelectOption[]Normalized options array.
openbooleanWhether the listbox is open.
close() => voidClose the listbox.
selectOption(value: string) => voidProgrammatically select an option by value.
Web docsFull catalog