SPB Git

Repository Public MIT

doc-glass-search-field.md 4.1 KB

# GlassSearchField

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

swift
@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

swift
@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

  • .glassSearchable needs a NavigationStack or NavigationSplitView to 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 async function.
  • submit(_:operation:) relies on the operation noticing its cancellation; prefer submit(_: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.
Files main
Menu

Theme