SP GlassKit1.1.0

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.

GlassSearchField running in SP Glass Studio.Capture
Capture of the component running in SP Glass Studio on the iOS 26.5 simulator. Liquid Glass is rendered by the system; this is an image, not a live preview.

Installation

spglass add glass-search-field

Copies the sources and their dependencies into your project. Set up the CLI.

Swift
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")

import SPGlassKit
Identifierglass-search-field
ModuleSPGlassForms
Kindnative-wrapper — the .glassSearchable modifier wraps .searchable; the inline GlassSearchField view is a system TextField on an opaque surface
PlatformsiOS 26, iPadOS 26, macOS 26
ExampleExamples/Gallery/Forms/GlassSearchFieldExample.swift

Minimal usage

Swift
@State private var query = ""

NavigationStack {
    ResultList(query: query)
        .glassSearchable(text: $query)
}

Variants

VariantUse
.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.
GlassSearchDebouncerWaits for a pause in typing before an asynchronous search, and cancels the searches that were overtaken.

Parameters

glassSearchable

ParameterTypeDefaultMeaning
textBinding<String>—Search text.
isPresentedBinding<Bool>?nilWhether search is active.
promptLocalizedStringKey?"Search"Placeholder.
suggestions[GlassSearchSuggestion][]Suggestions.
onSubmit@MainActor () -> Void{}Called when the search is submitted.

GlassSearchField

ParameterTypeDefaultMeaning
textBinding<String>—Search text.
promptLocalizedStringKey?"Search"Placeholder.
showsCancelBool?iOS: true, macOS: falseCancel 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.

Example

Complete example (compiled with the package)
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

Identifierglass-search-field
Version1.1.0
Depends onglass-forms-support, glass-foundation
FrameworksSwiftUI
Public symbolsGlassSearchSuggestion, GlassSearchField
LicenceMIT
Manifestmanifest.json

Files