Headless Search
Search is the behaviour of the styled Search as unstyled React parts: a role="search" landmark named by its label, an <input type="search">, a clear button, and the magnifier button of an expandable search. The parts handle Esc, where focus goes after a clear, open or close, and when the clear button is in the Tab order. An uncontrolled field goes back to defaultValue when its form resets.
You own the markup: the icons, how the label is hidden, the button names and every style. The parts set data-* attributes for you to style on.
Installation
The headless parts come with the package (there is no shadcn item; the React + Tailwind items are built on them):
Use it
value,defaultValue,disabledandreadOnlygo onRoot, not onInput.- Clearing empties the field with a real
inputevent, so youronChangesees it;onClearruns after. - Always render
Label, even when you hide it: it names the landmark and the input.
Anatomy
Clear the text
Clear empties the field and keeps focus in it. Esc in a filled field does the same. On an empty field Esc does nothing, so a dialog around the search can still close on Esc. While there is nothing to clear (the field is empty or read-only), Clear is out of the Tab order and has data-hidden; hide it with your CSS.
Expandable search
expandable (Carbon's ExpandableSearch) collapses the search to its Trigger until someone clicks it or presses Enter or Space; then it opens and focus moves to the field. When focus leaves an empty open search for anywhere outside its Landmark, it collapses. Esc on an empty open search collapses it and focuses the Trigger.
expandedcontrols the state;defaultExpandedsets where it starts.- While collapsed,
LandmarkandInputhavedata-collapsedand the input is out of the Tab order. Hiding or narrowing the input is your CSS.
Style it
Parts
Search.Root
Renders no element; it holds the text and the open state.
| Prop | Type | Default | |
|---|---|---|---|
inputId |
string |
generated | The input's id. The label's id is ${inputId}-label. |
value |
string | number | readonly string[] |
— | The text (controlled; follow the input's onChange). |
defaultValue |
string | number | readonly string[] |
— | The starting text (uncontrolled). |
disabled |
boolean |
false |
Turns off the input and the buttons. |
readOnly |
boolean |
false |
Keeps the text: nothing to clear. |
onClear |
() => void |
— | After Clear or Esc emptied the field. |
expandable |
boolean |
false |
Collapsed to its Trigger until opened. |
expanded |
boolean |
— | Whether an expandable search is open (controlled). |
defaultExpanded |
boolean |
false |
Whether it starts open. |
onExpandedChange |
(expanded: boolean) => void |
— | Each open or close. |
Search.Landmark
Renders a <div role="search"> named by Label (aria-labelledby). It adds no props.
| Attribute | When |
|---|---|
data-empty |
The field has no text |
data-collapsed |
An expandable search is collapsed |
data-disabled |
Root is disabled |
data-readonly |
Root is read-only |
Search.Label
Renders a <label> for the input, with the id the landmark points to. You can't set its htmlFor or id.
Search.Input
Renders an <input type="search" autoComplete="off">. It takes every <input> prop except type, id, value, defaultValue, disabled and readOnly, which come from Root. It has the same data-* attributes as Landmark.
Search.Clear
Renders a <button type="button">. It adds no props, and takes no disabled (it follows Root). Its mousedown is cancelled, so focus stays in the field.
| Attribute | When |
|---|---|
data-hidden |
Nothing to clear: the field is empty or read-only |
Search.Trigger
Renders a <button type="button"> with aria-expanded and aria-controls pointing at the input. It adds no props, and takes no disabled (it follows Root). It is a Tab stop only while the search is collapsed.
| Attribute | When |
|---|---|
data-expanded |
The search is open (always, unless expandable) |
Search.useSearchContext()
Returns the state of the enclosing Root, such as empty, clearable, open, expandable, inputId and labelId.
Keyboard and accessibility
| Key | What it does |
|---|---|
| Esc | Clears the text and keeps focus in the field; on an empty open expandable search, collapses it and focuses the Trigger |
| Enter | Submits the form the search sits in, as a native input does |
Enter or Space on the Trigger |
Opens an expandable search; it doesn't submit the form |
| Tab | Moves to Clear only while there is text to clear |
Landmarkis arole="search"landmark named byLabel, so screen reader users can jump to it.- Carbon hides the label on screen. Hide it with a visually-hidden class, not
display: none. - You add names for
ClearandTrigger(aria-label) andaria-hidden="true"on their icons.