SearchBar
Verified@SzpakKamil
npx machina-cli add skill @SzpakKamil/searchbar --openclawSearchBar Skill
Overview
This skill provides expert guidance on SearchBar, a powerful and highly customizable SwiftUI component for creating native-feeling search experiences across iOS, iPadOS, macOS, and visionOS. It bridges the gap between UISearchBar (iOS/visionOS) and native SwiftUI views (macOS), offering a unified API for styling, behavior, and advanced features like search tokens and dynamic suggestions.
Agent Behavior (Follow These Rules)
- Identify Platform Targets: SearchBar behaves slightly differently on iOS/visionOS (wraps
UISearchBar) vs. macOS (custom SwiftUI). Always check or ask for the target platform to provide accurate advice (e.g., specific material effects or token behaviors). - Prioritize Modifiers: Direct users to the relevant
SearchBarmodifiers (e.g.,.searchBarStyle,.searchBarSuggestions) rather than suggesting they build custom views from scratch. - Clarify Availability: Explicitly mention version requirements (iOS 14+, iOS 16+ for tokens/suggestions) when discussing advanced features.
- Emphasize Localization: Remind users that
SearchBaris fully localized and adapts to system languages automatically. - Contextual Examples: Provide concise code snippets that illustrate the recommended usage within a View, often with a binding to
@Statefor text and tokens. - Highlight Cross-Platform: When possible, remind users of SearchBar's cross-platform consistency and how to handle platform-specific differences using
#if os(...)directives if necessary (though the library handles most internally).
Project Settings
- Deployment Targets: iOS 14.0+, iPadOS 14.0+, macOS 11.0+, visionOS 1.0+.
- Advanced Features: Tokens and Suggestions require iOS 16.0+, iPadOS 16.0+, visionOS 1.0+. (Suggestions also on macOS 15.0+).
- Swift Version: Swift 5.9+.
Quick Decision Tree
-
Setting up a basic search bar?
- Basic init and setup →
references/SearchBar.md
- Basic init and setup →
-
Customizing appearance?
- Changing colors, shape (capsule/rounded) →
references/SearchBarStyle.md - Using "Glass" or "Solid" materials →
references/SearchBarStyle.md - Changing the size/scale →
references/SearchBarStyle.md - Custom icon →
references/SearchBarModifiers.md(.searchBarIconView)
- Changing colors, shape (capsule/rounded) →
-
Configuring behavior?
- Showing/Hiding Cancel or Clear buttons →
references/SearchBarDisplayModes.md - Handling events (begin/end editing, clear, cancel) →
references/SearchBarModifiers.md - Focus management →
references/SearchBarModifiers.md(.searchBarIsFocused)
- Showing/Hiding Cancel or Clear buttons →
-
Using advanced search features (iOS 16+/visionOS)?
- Adding filter tokens (capsules) →
references/SearchBarData.md - Showing search suggestions →
references/SearchBarData.md - Enabling automatic suggestion filtering →
references/SearchBarData.md
- Adding filter tokens (capsules) →
Triage-First Playbook
- "My search bar looks different on macOS."
- Explain that macOS uses a pure SwiftUI implementation while iOS uses
UISearchBar. Styling is consistent but underlying implementation differs.
- Explain that macOS uses a pure SwiftUI implementation while iOS uses
- "Tokens/Suggestions are not showing up."
- Verify the deployment target is iOS 16.0+ or visionOS 1.0+.
- Ensure the binding to tokens/suggestions is active and populated.
- "How do I change the background color?"
- Use
.searchBarStyle(..., backgroundColor: .red). Seereferences/SearchBarStyle.md.
- Use
- "I want to hide the cancel button."
- Use
.searchBarCancelButtonDisplayMode(.never). Seereferences/SearchBarDisplayModes.md.
- Use
- "How do I make the search bar glass/transparent?"
- Use
.searchBarMaterial(.glass). Note platform/version restrictions (iOS 26+). Seereferences/SearchBarStyle.md.
- Use
Core Patterns Reference
Basic Setup
SearchBar(text: $text, prompt: "Search...")
.searchBarStyle(.rounded)
Advanced Styling
SearchBar(text: $text)
.searchBarStyle(.capsule, textColor: .white, tint: .blue, backgroundColor: .black.opacity(0.8))
.searchBarMaterial(.glass) // iOS 26+ (Experimental/Future)
Tokens & Suggestions
SearchBar(text: $text)
.searchBarCurrentTokens($tokens)
.searchBarSuggestions($suggestions)
.searchBarEnableAutomaticSuggestionsFiltering(true)
Event Handling
SearchBar(text: $text)
.searchBarBeginEditingAction { print("Started") }
.searchBarEndEditingAction { print("Ended") }
.searchBarCancelButtonAction { print("Cancelled") }
Integration Quick Guide
SearchBar is integrated via Swift Package Manager.
- Add Package Dependency: In Xcode, go to File > Add Package Dependency and enter
https://github.com/SzpakKamil/SearchBar.git. - Import:
import SearchBarin your Swift files. - Deployment Targets: Ensure your project targets iOS 14.0+, macOS 11.0+, visionOS 1.0+.
For detailed setup, see references/SearchBar.md.
Reference Files
SearchBar.md- General overview, setup, and initialization.SearchBarModifiers.md- Comprehensive list of all modifiers.SearchBarStyle.md- Styling, materials, corner styles, and scale.SearchBarDisplayModes.md- Cancel and Clear button behaviors.SearchBarData.md- Search Tokens and Suggestions._index.md- Index of all topics.
Overview
SearchBar is a powerful, highly customizable SwiftUI component for native-feeling search across iOS, iPadOS, macOS, and visionOS. It bridges UISearchBar on iOS/visionOS with native SwiftUI views on macOS, delivering consistent styling and advanced features like search tokens and dynamic suggestions.
How This Skill Works
The skill emphasizes identifying the target platform and using SearchBar modifiers such as .searchBarStyle and .searchBarSuggestions to configure appearance and behavior. It demonstrates binding text and tokens via @State and provides cross-platform examples, including conditional code for platform differences when needed.
When to Use It
- Building a custom SearchBar in SwiftUI and want native feel and cross‑platform consistency
- Implementing search tokens or suggestions for iOS 16+/visionOS 1.0+ (and macOS 15.0+ for suggestions)
- Styling search bars with glass or capsule appearances and customizing size
- Ensuring cross‑platform behavior with a unified API across iOS, macOS, and visionOS
- Using specific modifiers like .searchBarStyle or .searchBarSuggestions to control behavior
Quick Start
- Step 1: Create @State vars for query and tokens, e.g., @State private var query = "" and @State private var tokens: [String] = []
- Step 2: Attach a SearchBar to your view, binding to the text and tokens, then apply a style like .searchBarStyle and .searchBarSuggestions
- Step 3: Provide a suggestions data source and handle events (onSubmit, onCancel) to complete setup
Best Practices
- Prioritize SearchBar modifiers over building a custom view when possible
- Verify deployment targets and version requirements for tokens/suggestions
- Keep localization in mind; SearchBar is fully localized and adapts to system languages
- Provide a binding for text and tokens and supply a clear data model for suggestions
- Use platform checks (#if os(...)) only for edge cases while relying on the library for core behavior
Example Use Cases
- SearchBar wired to a List to filter results with dynamic suggestions
- Cross‑platform example: UISearchBar on iOS/visionOS and native SwiftUI SearchBar on macOS
- Token capsules appear as the user types to refine search
- Glass or capsule style SearchBar applied to a toolbar for a polished look
- Suggestions driven by a bound data source with automatic filtering