Skip to content
Home Theme Gallery

Search field

The custom element is <hui-search-field>.

Overview

A text input with a search icon and a clear control, form-associated so it submits like any other field. It owns no suggestions; a host drives an hx-get from its input event.

Example

Search field
Source
<hui-search-field name="q" placeholder="Search things" aria-label="Search"></hui-search-field>

The Go template that renders it:

<hui-search-field name="q" placeholder="{{ .Placeholder }}" aria-label="{{ .Label }}"></hui-search-field>

API

Attributes4
Attributes
NameTypeDefaultDescription
namestring""The form field name. Without one the control submits nothing.
valuestring""The current query. Reflects to the value attribute.
placeholderstring"Search"Placeholder shown in the input while it is empty.
disabledbooleanfalseDisables the input and the clear button.
Properties4
Properties
NameTypeDefaultDescription
namestring''Reflects to the name attribute.
valuestring''Reflects to the value attribute.
placeholderstring'Search'Reflects to the placeholder attribute.
disabledbooleanfalseReflects to the disabled attribute.
Events2
Events
NameDescription
inputEmitted on the host as the user types and when the clear button is used. It bubbles and is composed; the internal input's own event is stopped at the boundary.
changeEmitted alongside input when the value is committed and on clear. It bubbles and is composed.
CSS custom properties5
CSS custom properties
NameDefaultDescription
--hui-ringoklch(0.62 0.019 323.02)Colour of the ring drawn on [part="field"] while the input is focused.
--hui-focus-ring-width3pxWidth of that ring.
--hui-inputoklch(0.62 0.019 323.02)Border of the field, and of the invalid field.
--hui-muted-foregroundoklch(0.542 0.034 322.5)Colour of the search icon and the clear control.
--hui-destructiveoklch(0.56 0.245 27.325)Border of the field while aria-invalid="true".
::part() hooks4
::part() hooks
NameDescription
fieldThe bordered container that draws the focus ring.
search-iconThe leading search glyph.
inputThe internal native type="search" input.
clearThe trailing clear button; hidden while the value is empty.

States

With a value

With text in it the clear control appears; using it empties the field and emits input, so a filter driven by the field updates.

With a value
Source
<hui-search-field name="q" value="hello" aria-label="Search with value"></hui-search-field>

Disabled

Disabled: the field and its clear control are both out of reach.

Disabled
Source
<hui-search-field name="q" value="locked" disabled aria-label="Disabled search"></hui-search-field>

Server-side mechanics

The server renders a plain <hui-search-field>; it has no children, because the control owns no suggestions. It is form-associated: give it a name and the current query submits under that name, and without one it submits nothing. A host drives an hx-get from the control's synthetic input event to fetch results into a separate region. An hx-swap that replaces the field while it is focused destroys the input and its caret, so swap the results region rather than the field itself.

Accessibility

  • The clear button has the accessible name "Clear search".
  • The focus ring is drawn on the whole [part="field"], not on the borderless input inside it.
  • An aria-invalid="true" host colours the field border with --hui-destructive.

Keyboard

Keyboard
KeysAction
Printable charactersUpdate the value and emit input.
TabMoves focus from the input to the clear button while it is visible.
Enter, SpaceActivate the clear button, which empties the field and emits input and change.

Gotchas

None recorded.