SPB Git

Repository Public MIT

doc-glass-search-field.md 4.1 KB 92 lines markdown

1# GlassSearchField23| | |4|---|---|5| Identifier | `glass-search-field` |6| Module | `SPGlassForms` |7| Kind | `native-wrapper` — the `.glassSearchable` modifier wraps `.searchable`; the inline `GlassSearchField` view is a system `TextField` on an opaque surface |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/Forms/GlassSearchFieldExample.swift` |1011## Minimal usage1213```swift14@State private var query = ""1516NavigationStack {17    ResultList(query: query)18        .glassSearchable(text: $query)19}20```2122## Variants2324| Variant | Use |25|---|---|26| `.glassSearchable(text:…)` | The system search field, placed and drawn by the system. Use it wherever there is a navigation bar or a toolbar. |27| `suggestions:` | Suggestions shown under the system field; choosing one puts its `completion` in the field. |28| `isPresented:` | Activates or dismisses the system search from code. |29| `GlassSearchField(text:…)` | An inline field for a panel, a sheet or a filter row, where no navigation bar exists. |30| `GlassSearchDebouncer` | Waits for a pause in typing before an asynchronous search, and cancels the searches that were overtaken. |3132## Parameters3334`glassSearchable`3536| Parameter | Type | Default | Meaning |37|---|---|---|---|38| `text` | `Binding<String>` | — | Search text. |39| `isPresented` | `Binding<Bool>?` | `nil` | Whether search is active. |40| `prompt` | `LocalizedStringKey?` | "Search" | Placeholder. |41| `suggestions` | `[GlassSearchSuggestion]` | `[]` | Suggestions. |42| `onSubmit` | `@MainActor () -> Void` | `{}` | Called when the search is submitted. |4344`GlassSearchField`4546| Parameter | Type | Default | Meaning |47|---|---|---|---|48| `text` | `Binding<String>` | — | Search text. |49| `prompt` | `LocalizedStringKey?` | "Search" | Placeholder. |50| `showsCancel` | `Bool?` | iOS: `true`, macOS: `false` | Cancel button while focused. |51| `onSubmit` | `@MainActor () -> Void` | `{}` | Called on submit. |52| `onCancel` | `@MainActor () -> Void` | `{}` | Called after Cancel (or Escape on macOS) emptied the field. |5354`GlassSearchDebouncer(delay:sleep:)`: `submit(_:operation:)`, `submit(_:search:deliver:failure:)`, `cancel()`, `isPending`.5556## States5758Empty, 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.5960## Advanced example6162```swift63@State private var query = ""64@State private var results: [Product] = []65@State private var debouncer = GlassSearchDebouncer(delay: .milliseconds(300))6667List(results) { ProductRow($0) }68    .overlay { if debouncer.isPending { ProgressView() } }69    .glassSearchable(text: $query, suggestions: GlassSearchSuggestion.matching(query, in: recentSearches))70    .onChange(of: query) { _, newValue in71        debouncer.submit(newValue, search: { try await catalogue.search($0) }, deliver: { results = $0 })72    }73    .onDisappear { debouncer.cancel() }74```7576Typing "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.7778## Accessibility7980- The system field brings its own label, clear and cancel controls.81- The inline field is labelled "Search", has the search-field trait, and its clear and cancel buttons are labelled and reachable with the keyboard.82- Suggestions are system rows with text; symbols are decorative.83- The Cancel button fades in with the theme's fast animation; with Reduce Motion it is a short cross-fade.8485## Limits8687- `.glassSearchable` needs a `NavigationStack` or `NavigationSplitView` to have somewhere to put the field.88- Suggestions exist for the system field only; the inline field has none.89- The debouncer runs on the main actor; the search itself can be any `async` function.90- `submit(_:operation:)` relies on the operation noticing its cancellation; prefer `submit(_:search:deliver:)` when the result is assigned to state.91- The debouncer is covered by tests; the placement of the system field and of suggestions was not checked on a running device here.92
Files main
Menu

Theme