GlassSearchField
Search with suggestions, cancellation and a cancellable debounce for asynchronous queries.
- System control
- iOS 26 · iPadOS 26 · macOS 26
- Version 1.1.0
glass-search-field
Drawn by the system. GlassKit configures it and leaves the glass to the system.
CaptureInstallation
spglass add glass-search-fieldCopies the sources and their dependencies into your project. Set up the CLI.
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")
import SPGlassKit| Identifier | glass-search-field |
|---|---|
| Module | SPGlassForms |
| Kind | native-wrapper — the .glassSearchable modifier wraps .searchable; the inline GlassSearchField view is a system TextField on an opaque surface |
| Platforms | iOS 26, iPadOS 26, macOS 26 |
| Example | Examples/Gallery/Forms/GlassSearchFieldExample.swift |
Minimal usage
@State private var query = ""
NavigationStack {
ResultList(query: query)
.glassSearchable(text: $query)
}Variants
| Variant | Use |
|---|---|
.glassSearchable(text:…) | The system search field, placed and drawn by the system. Use it wherever there is a navigation bar or a toolbar. |
suggestions: | Suggestions shown under the system field; choosing one puts its completion in the field. |
isPresented: | Activates or dismisses the system search from code. |
GlassSearchField(text:…) | An inline field for a panel, a sheet or a filter row, where no navigation bar exists. |
GlassSearchDebouncer | Waits for a pause in typing before an asynchronous search, and cancels the searches that were overtaken. |
Parameters
glassSearchable
| Parameter | Type | Default | Meaning |
|---|---|---|---|
text | Binding<String> | — | Search text. |
isPresented | Binding<Bool>? | nil | Whether search is active. |
prompt | LocalizedStringKey? | "Search" | Placeholder. |
suggestions | [GlassSearchSuggestion] | [] | Suggestions. |
onSubmit | @MainActor () -> Void | {} | Called when the search is submitted. |
GlassSearchField
| Parameter | Type | Default | Meaning |
|---|---|---|---|
text | Binding<String> | — | Search text. |
prompt | LocalizedStringKey? | "Search" | Placeholder. |
showsCancel | Bool? | iOS: true, macOS: false | Cancel button while focused. |
onSubmit | @MainActor () -> Void | {} | Called on submit. |
onCancel | @MainActor () -> Void | {} | Called after Cancel (or Escape on macOS) emptied the field. |
GlassSearchDebouncer(delay:sleep:): submit(_:operation:), submit(_:search:deliver:failure:), cancel(), isPending.
States
Empty, with text (clear button), focused (outline, Cancel button on iOS), searching (debouncer.isPending). GlassSearchSuggestion.matching(_:in:limit:) filters a list of suggestions without case, accents or width.
Advanced example
@State private var query = ""
@State private var results: [Product] = []
@State private var debouncer = GlassSearchDebouncer(delay: .milliseconds(300))
List(results) { ProductRow($0) }
.overlay { if debouncer.isPending { ProgressView() } }
.glassSearchable(text: $query, suggestions: GlassSearchSuggestion.matching(query, in: recentSearches))
.onChange(of: query) { _, newValue in
debouncer.submit(newValue, search: { try await catalogue.search($0) }, deliver: { results = $0 })
}
.onDisappear { debouncer.cancel() }Typing "s", "sw", "swift" quickly runs one search, for "swift". If a search for an older query is still running when a newer one is submitted, its task is cancelled and, with the search:deliver: form, its late result is dropped even if the search ignores cancellation.
Accessibility
- The system field brings its own label, clear and cancel controls.
- The inline field is labelled "Search", has the search-field trait, and its clear and cancel buttons are labelled and reachable with the keyboard.
- Suggestions are system rows with text; symbols are decorative.
- The Cancel button fades in with the theme's fast animation; with Reduce Motion it is a short cross-fade.
Limits
.glassSearchableneeds aNavigationStackorNavigationSplitViewto have somewhere to put the field.- Suggestions exist for the system field only; the inline field has none.
- The debouncer runs on the main actor; the search itself can be any
asyncfunction. submit(_:operation:)relies on the operation noticing its cancellation; prefersubmit(_:search:deliver:)when the result is assigned to state.- The debouncer is covered by tests; the placement of the system field and of suggestions was not checked on a running device here.
Example
import SwiftUI
import SPGlassKit
public struct GlassSearchFieldExample: GlassExample {
public static let componentID = "glass-search-field"
private static let catalogue = ["Café crème", "Cafetière italienne", "Thé vert", "Théière en fonte", "Chocolat chaud", "Citronnade", "Eau pétillante", "Jus d’orange"]
private static let recent = [
GlassSearchSuggestion(title: "café", systemImage: "clock"),
GlassSearchSuggestion(title: "thé", systemImage: "clock"),
GlassSearchSuggestion(title: "Hot drinks", systemImage: "cup.and.saucer", completion: "chaud"),
]
@State private var query = ""
@State private var inlineQuery = ""
@State private var results = GlassSearchFieldExample.catalogue
@State private var debouncer = GlassSearchDebouncer(delay: .milliseconds(250))
public init() {}
/// A stand-in for a remote search: it only filters a local list after a short pause.
private static func search(_ query: String) async throws -> [String] {
try await Task.sleep(for: .milliseconds(150))
let needle = query.folding(options: [.caseInsensitive, .diacriticInsensitive], locale: nil)
return needle.isEmpty ? catalogue : catalogue.filter { $0.folding(options: [.caseInsensitive, .diacriticInsensitive], locale: nil).contains(needle) }
}
public var body: some View {
NavigationStack {
List {
Section("Inline field") {
GlassSearchField(text: $inlineQuery, prompt: "Filter this section")
.listRowSeparator(.hidden)
ForEach(Self.catalogue.filter { inlineQuery.isEmpty || $0.localizedStandardContains(inlineQuery) }, id: \.self) { item in
Text(verbatim: item)
}
}
Section {
ForEach(results, id: \.self) { item in
Text(verbatim: item)
}
if results.isEmpty {
Text("No results").foregroundStyle(.secondary)
}
} header: {
HStack {
Text("Debounced results")
if debouncer.isPending { ProgressView().controlSize(.small) }
}
}
}
.navigationTitle("Drinks")
.glassSearchable(text: $query, prompt: "Search drinks", suggestions: query.isEmpty ? Self.recent : [])
.onChange(of: query) { _, newValue in
debouncer.submit(newValue, search: Self.search) { results = $0 }
}
.onDisappear { debouncer.cancel() }
}
}
}
#Preview("GlassSearchField") { GlassSearchFieldExample() }
Registry entry
| Identifier | glass-search-field |
|---|---|
| Version | 1.1.0 |
| Depends on | glass-forms-support, glass-foundation |
| Frameworks | SwiftUI |
| Public symbols | GlassSearchSuggestion, GlassSearchField |
| Licence | MIT |
| Manifest | manifest.json |
Files
Forms/Forms/GlassSearchField.swift9211 bytes · sha256 f1ef28d1629f…