Forms

Search Field

A text input specialised for search, with a clear action.

Rust
1use herogpui::components::input::SearchField;

Usage

Rust
1use herogpui::prelude::SearchField;
2use gpui::prelude::*;
3use gpui::px;
4
5gpui::div()
6    .flex()
7    .flex_col()
8    .items_start()
9    .gap(px(12.))
10    .children(vec![
11        gpui::div()
12            .w(px(256.))
13            .flex()
14            .flex_col()
15            .child(SearchField::new(self.search_state.clone())
16                        .label("Search docs")
17                        .placeholder("Search components")
18                        .on_change(cx.listener(|this, text: &str, _, cx| {
19                            this.search_query = text.to_owned();
20                            cx.notify();
21                        }))),
22        para(
23            &if query.is_empty() {
24                "Type to search".to_owned()
25            } else {
26                format!("Query: {query}")
27            },
28            cx,
29        ),
30    ])

Live HeroGPUI compiled to WebAssembly. Select any example without loading another WASM instance.

Anatomy

Rust types

SearchFieldInput

Search Field is assembled from these builders. The API reference lists each one.

Customization

Appearance builders and theme tokens Search Field uses.

Styling

Search Field styling
StyleDescription
Input wrapper flex_col + gap(4px)Root field stack and invalid description replacement; labels use 14px/20px and helper/error text use 12px/16px.
FIELD_HEIGHT + field_radius + apply_field_chromeUnified 36px field shell, radius, colours and shadow match.
direct apply_field_chrome state coloursState colours and focus shadow switch directly rather than interpolating.
Input row + FIELD_TEXTText inset and desktop type match; browser search decorations do not exist in GPUI.
FIELD_ICON + Input px(12px)Leading icon size and inset match.
20px clear div + 12px svg + end padding20px clear target with 12px glyph and 8px end inset.
FieldVariant::Secondary in apply_field_chromeLower-emphasis surface treatment.
full_width()Full-width root and group modifier.

API reference

Builders

Search Field builders
BuilderTypeDefaultDescription
content(render)AnyElement | Fn(values: SearchFieldRenderProps) -> AnyElementReplacement content receives disabled, invalid, read-only, required, focus, value and empty state.
full_width()boolfalseStretches the root and field to the available width.
new(state)stateThe caller-owned InputState entity supplies stable GPUI identity instead of a element id.
variant(FieldVariant)FieldVariantFieldVariant::PrimaryPrimary field chrome or the lower-emphasis surface variant.
default_value(text)textSeeds the uncontrolled InputState once.
on_change(callback)Fn(value: string) -> ()Reports each accepted query edit.
is_required(bool)boolfalseMarks the field required for native form validation.
is_invalid(bool)boolForces invalid field chrome and error state.
validate(callback)Fn(value: string) -> ValidationError | true | nullA callback returns one optional validation message.
validation_behavior(ValidationBehavior)ValidationBehaviorValidationBehavior::NativeSelects blocking native validation or non-blocking ARIA-style validation.
validation_errors(errors)errorsServer messages take precedence over custom validation.
is_disabled(bool)boolfalseRemoves focus, editing and clear actions and applies disabled opacity.
is_read_only(bool)boolfalseKeeps focus and selection while suppressing edits and clear actions.
name(text)textSubmission name stored with the current query.
auto_focus(bool)boolfalseFocuses the input on its first render.
on_submit(callback)Fn(value: string) -> ()Enter reports the current query.
on_clear(callback)Fn() -> ()The clear button and Escape report an explicit clear; deleting the final character does not.
placeholder(text)textPlaceholder displayed while the query is empty and unfocused.
search_icon(element)AnyElementAnyElement::SearchIconReplaces the leading magnifier.
clear_icon(element)AnyElementAnyElement::CloseIconReplaces the glyph while preserving the built-in clear action and geometry.

Parts

Search Field parts
PartDescription
SearchFieldRoot field state, label, description and error composition.
SearchFieldUnified field shell containing icon, editable query and clear affordance.
InputInputState-backed editable search query.
SearchFieldBuilt-in or caller-supplied leading icon.
InputClear action with built-in or caller-supplied glyph, shown only for an editable non-empty query.

States

Search Field states
BuilderStateDescription
resolved Input validityInvalidHides description and applies invalid group chrome.
is_disabled(bool)DisabledApplies disabled opacity and removes focus, editing and clear actions.
Input focus + apply_field_chromeFocus withinDraws focused field chrome while the input owns focus.
SearchFieldRenderState::is_focus_visibleFocus visibleReplacement content receives keyboard-visible focus state; built-in chrome uses focus-within.
Input hover + apply_field_chromeHoveredUses hover fill and border while focus is outside the group.
InputState::is_emptyEmptyRemoves the clear affordance from hit testing and painting.