Skip to content
Focsd homeFocsd homeFocsd
Forms

Listbox

A list that stays on screen, for choosing one value or several.

  • Tag: focsd-listbox
  • Pattern: Listbox (aria-activedescendant)
  • Category: Forms

Example

Standard (5–7 days) Express (2 days) Overnight Mozzarella Feta Olives Peppers Small Medium Large
HTML
<div class="stack">
<focsd-listbox label="Shipping speed" name="ship" value="standard">
<focsd-option value="standard">Standard (5–7 days)</focsd-option>
<focsd-option value="express">Express (2 days)</focsd-option>
<focsd-option value="overnight" disabled>Overnight</focsd-option>
</focsd-listbox>
<focsd-listbox
label="Toppings"
name="toppings"
multiple
hint="Pick as many as you like.">
<focsd-option-group label="Cheese">
<focsd-option value="mozzarella">Mozzarella</focsd-option>
<focsd-option value="feta">Feta</focsd-option>
</focsd-option-group>
<focsd-option-group label="Vegetables">
<focsd-option value="olives">Olives</focsd-option>
<focsd-option value="peppers">Peppers</focsd-option>
</focsd-option-group>
</focsd-listbox>
<focsd-listbox label="Size" name="size" orientation="horizontal" value="m">
<focsd-option value="s">Small</focsd-option>
<focsd-option value="m">Medium</focsd-option>
<focsd-option value="l">Large</focsd-option>
</focsd-listbox>
</div>

Usage

Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.

<script type="module">
import '@focsd-dev/components/focsd-listbox.js';
</script>
<focsd-listbox label="Shipping speed" name="ship" value="standard">…</focsd-listbox>

API

Attributes

AttributePropertyTypeDefault
namenamestring—
disableddisabledbooleanfalse
requiredrequiredbooleanfalse
readonlyreadOnlybooleanfalse
aria-labelhostAriaLabelstring | nullnull
aria-descriptionhostAriaDescriptionstring | nullnull
aria-labelledbyhostAriaLabelledBystring | nullnull
aria-describedbyhostAriaDescribedBystring | nullnull
label-positionlabelPosition'top' | 'start' | 'end' | undefined—
hinthintstring—
errorerror——
warningwarningstring | undefined—
successsuccessstring | undefined—
error-messageerrorMessagestring | undefined—
warning-messagewarningMessagestring | undefined—
warning-patternwarningPatternstring | undefined—
labellabelstring—
valuevalue——
multiplemultiplebooleanfalse
orientationorientation'vertical' | 'horizontal'vertical

Methods

MethodDescription
describedByIds()IDs to add to the inner control's aria-describedby: the hint, then the shown message.
checkValidity()True when the value is valid; otherwise fires invalid and returns false, like native.
reportValidity()Like checkValidity(), and also shows the browser's validation message when invalid.
setCustomValidity(message)Set (non-empty) or clear (empty string) a custom validation error, like native controls.
setValidityState(flags, message, anchor)For subclasses: set the validity flags and message. anchor is the element the browser points its message at.
validate()Show (or clear) the error from the control's own checks: required, type, pattern, min, max and the like. Uses error-message, else the browser's localized message. An error the author set (a server error, say) is left as it is.
clearValidation()Clear the error and warning that validate() and warning-pattern showed (a form reset does).
emitValue(commit)For subclasses: fire the value events, as a native control does on a user edit: input (bubbles, composed), then with commit also change (bubbles, stays in its tree).
idrefText(ids)For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces.

Events

EventDescription
inputFired on every user change of the selection.
changeFired with input when the user changes the selection.

Slots

SlotDescription
(default)The focsd-option and focsd-option-group children (data; not rendered themselves).

CSS parts

Style the inside of the component with ::part(), for example focsd-listbox::part(layout).

PartDescription
layoutThe wrapper holding the label and the body; its `data-position` is the label position.
bodyThe listbox, then the hint and the message.
labelThe label element.
listboxThe `role="listbox"` box that takes focus.
optionOne option row; also `active` for the active option and `selected` when chosen.
activeThe active option row.
selectedA selected option row.
groupA group of options.
group-labelA group's visible label (the group is named by `aria-label`).
checkThe check mark of a selected option (with `multiple`).
hintThe author's hint.
messageThe shown error, warning or success message (also named by its kind).
message-iconThe icon in the message.
announcerThe live region that announces a changed message.

Accessibility

  • A labeled role="listbox" that takes focus itself; the active option is pointed to with aria-activedescendant, so screen readers read each option as the arrows move. Groups are named role="group"s.
  • With one value, the selection follows the active option, as in a native list box. With multiple, the list is aria-multiselectable and moving never changes the selection on its own: Space does.
  • Disabled options are announced as unavailable and skipped by the arrows and by typing. The arrows stop at the first and last option instead of wrapping.
  • Listbox or list? Use focsd-listbox for a form value that should always be on screen: it is form-associated and submits one entry per selected value. Use focsd-list with selectable for navigation-style choice, such as picking which message to read.
  • Options are data: each row shows the option's label (or its text), not rich content such as icons or markup inside the focsd-option. Set the selection with the listbox's value or values; writing option.selected from code does not change it (the selected attribute sets the default). Give each option its value as an attribute: a value set only as a property on a focsd-option is not picked up until the options change again.
  • Form-associated: participates in FormData, required validation, reset (back to the value attribute or the options marked selected), fieldset disabled and back/forward restore.

Keyboard

KeyAction
Arrow Down / UpNext / previous option (Right / Left with orientation="horizontal")
Home / EndFirst / last option
LettersNext option starting with the letters typed; type quickly to search several
SpaceSelect or clear the active option (multiple)
Shift + ArrowSelect from the last chosen option to the next one; moving back deselects (multiple)
Shift + SpaceSelect every option from the last chosen one to the active one (multiple)
Ctrl / ⌘ + ASelect every option, or none when all are selected (multiple)