Autocomplete
Suggest options while entering a query.
Try it
Interact with the control below. This preview runs the exact code sample shown on this page.
Basic use
use xgpui_luma_look_radix as radix;
use gpui::{Context};
use xgpui_luma::controls::autocomplete::{SelectionItem};
struct Example {
control: xgpui_luma::controls::autocomplete::Autocomplete,
query: String,
}
impl Example {
fn new(cx: &mut Context<Self>) -> Self {
let look = cx.try_global::<radix::Look>().cloned().unwrap_or_else(radix::Look::built_in);
let control = radix::autocomplete_builder(&look, "search", [SelectionItem::new("rust", "Rust")])
.on_value_changed(cx, |app, query, cx| {
app.query = query.to_string();
cx.notify();
})
.spawn(cx);
Self { control, query: String::new() }
}
}Integration snippet, checked against the current SDK and Radix crates. cx is the initialized owner context. The control retains its callbacks; callback owners are held weakly. Render self.control.clone() in the owner’s view.
Radix styling
autocomplete_builder(&look, id, items) returns the SDK builder with Radix text-field, suggestion-panel, status, and scrollbar styling. SDK completion and query callbacks remain available.
Pass an explicit Look, including forks. Theme adapters retain that binding; notify affected controls and their owners after palette, mode, or stylesheet edits. Resolved look values are snapshots and must be resolved again during render.
See the Radix control support table for adapter and utility status.
SDK behavior & customization
The SDK owns query and selection behavior. Supply application options and handle query changes.
Relevant contracts: AutocompleteTemplateModifier, AutocompleteTemplateHandlers, AutocompleteRenderModel, AutocompleteTemplate.
Use the module’s template contract for structural changes. Builder modifiers and item templates vary by control.
Events
Callbacks follow the existing SDK event stream. Registrations append, hold the owner weakly, and live until the control is released. cx.notify() requests an owner redraw. Compound payloads are borrowed; scalar values are passed by value.
| Event / payload | Callback |
|---|---|
Change { query: SharedString } | .on_value_changed(cx, |app, query, cx| { … }) |
Select { item_id: SharedString, label: SharedString } | .on_select(cx, |app, item_id, label, cx| { … }) |
Complete { item_id: SharedString, label: SharedString } | .on_complete(cx, |app, item_id, label, cx| { … }) |
Clear | .on_clear(cx, |app, cx| { … }) |
FocusChanged { focused: bool } | .on_focus_changed(cx, |app, focused, cx| { … }) |
OpenChanged { open: bool } | .on_open_changed(cx, |app, open, cx| { … }) |
Dismiss | .on_dismiss(cx, |app, cx| { … }) |
Use .on_event(cx, |app, event, cx| { … }) for all events. Factory-created controls support the same callbacks through AutocompleteEventExt. Existing cx.subscribe observers remain supported.