Searchbar
Search input with dropdown results, keyboard navigation, and global shortcuts. Collapsible toggle button reveals search form with results.
Interactive Search Component
The Searchbar component provides a collapsible search input with dropdown results, built-in keyboard navigation (arrow keys, Enter, Escape), configurable global shortcuts (Cmd/Ctrl+K), and automatic platform-specific badge display.
Quick Start
Initialize a searchbar with full keyboard support in one line.
// Initialize with Cmd/Ctrl+K shortcut and badge
Searchbar.init('#mySearchbar', {
globalShortcut: true,
showShortcutBadge: true
})
Basic HTML Structure
Toggle button that reveals a search form with input, close button, and results dropdown.
<div class="focus-searchbar" id="mySearchbar">
<button class="focus-searchbar-toggle" type="button">
<i class="fa fa-search"></i><span> Search</span>
</button>
<form class="focus-searchbar-form" role="search">
<input type="search" class="focus-searchbar-input" placeholder="Search..." autocomplete="off">
<button type="button" class="focus-searchbar-close">
<i class="fa fa-times"></i>
</button>
<div class="focus-searchbar-results"></div>
</form>
</div>
JavaScript API
Searchbar.init(element, options)
Initialize a searchbar with keyboard shortcuts, event handlers, and optional shortcut badge.
// Default: Cmd/Ctrl+K with badge
Searchbar.init('#mySearchbar')
// Custom shortcut: Cmd/Ctrl+F
Searchbar.init('#mySearchbar', {
shortcutKey: 'f'
})
// Slash key with no modifier (like GitHub)
Searchbar.init('#mySearchbar', {
shortcutKey: '/',
shortcutModifier: 'none'
})
// Alt+S shortcut
Searchbar.init('#mySearchbar', {
shortcutKey: 's',
shortcutModifier: 'alt'
})
// No global shortcut (toggle/close buttons only)
Searchbar.init('#mySearchbar', {
globalShortcut: false
})
Options
| Option | Type | Default | Description |
|---|---|---|---|
globalShortcut |
boolean | true |
Enable global keyboard shortcut |
shortcutKey |
string | 'k' |
Key for shortcut (e.g., 'k', 'f', '/') |
shortcutModifier |
string | 'cmd' |
Modifier: 'cmd' (Cmd/Ctrl), 'alt', 'shift', or 'none' |
showShortcutBadge |
boolean | true |
Show shortcut badge in toggle button |
escapeToClose |
boolean | true |
Escape key closes searchbar |
resultNavigation |
boolean | true |
Arrow keys navigate results |
focusOnOpen |
boolean | true |
Focus input when opened |
Searchbar.destroy(element)
Remove all event listeners and clean up a searchbar instance.
Searchbar.destroy('#mySearchbar')
Searchbar.open(element)
Open the searchbar and focus the input.
Searchbar.open('#mySearchbar')
Searchbar.close(element, clearValue)
Close the searchbar and optionally clear the input value.
Searchbar.close('#mySearchbar') // Clears input
Searchbar.close('#mySearchbar', false) // Keeps input value
Searchbar.toggle(element)
Toggle the searchbar between open and closed states.
Searchbar.toggle('#mySearchbar')
Methods Summary
| Method | Parameters | Description |
|---|---|---|
Searchbar.init(el, opts) |
element, options | Initialize with full functionality |
Searchbar.destroy(el) |
element | Clean up listeners and badge |
Searchbar.open(el) |
element | Open and focus input |
Searchbar.close(el, clear) |
element, boolean | Close, optionally clear input |
Searchbar.toggle(el) |
element | Toggle open/closed state |
Keyboard Navigation
When initialized with Searchbar.init(), the following keyboard shortcuts are automatically enabled:
Built-in Keyboard Shortcuts
- Cmd/Ctrl + K: Open search (configurable via
shortcutKeyandshortcutModifier) - Arrow Down/Up: Navigate through results
- Enter: Select highlighted result
- Escape: Close search dropdown
Shortcut Badge
The shortcut badge is automatically added when showShortcutBadge: true and displays the platform-appropriate shortcut.
// Badge shows '⌘K' on Mac, 'Ctrl+K' on Windows
Searchbar.init('#shortcutExample', {
globalShortcut: true,
showShortcutBadge: true
})
Search Results Dropdown
Results dropdown displays automatically populated results with icons, titles, and preview text. Arrow key navigation is built-in.
<div class="focus-searchbar-results">
<a href="/components/buttons" class="focus-searchbar-result-item">
<div class="focus-searchbar-result-icon">
<i class="fa fa-square focus-text-primary"></i>
</div>
<div class="focus-searchbar-result-content">
<div class="focus-searchbar-result-title">Buttons</div>
<div class="focus-searchbar-result-preview">Interactive action triggers</div>
</div>
</a>
<!-- More results... -->
</div>
No Results State
Display a message when search returns no matches.
<div class="focus-searchbar-results">
<div class="focus-searchbar-no-results">
<i class="fa fa-search"></i>
<p>No results found for "xyz123"</p>
</div>
</div>
Platform Utilities
The searchbar uses shared utilities from the JavaScript Utilities API for platform detection and shortcut formatting.
import { isMac, getShortcutText, matchesShortcut } from 'focus-ui'
// Platform detection
isMac() // true on macOS
// Format shortcuts for display
getShortcutText('k') // '⌘K' on Mac, 'Ctrl+K' on Windows
getShortcutText('/', 'none') // '/'
// Check keyboard events
document.addEventListener('keydown', (e) => {
if (matchesShortcut(e, 'k', 'cmd')) {
// Cmd+K on Mac, Ctrl+K on Windows
}
})
CSS Classes Reference
| Class | Description |
|---|---|
.focus-searchbar |
Main container for searchbar component |
.focus-searchbar-active |
Shows the search form (add to .focus-searchbar) |
.focus-searchbar-toggle |
Toggle button that opens the search form |
.focus-searchbar-form |
Search form container (hidden by default) |
.focus-searchbar-input |
Search input field |
.focus-searchbar-close |
Close button to hide the search form |
.focus-searchbar-results |
Results dropdown container |
.focus-searchbar-result-item |
Individual result item (clickable link) |
.focus-searchbar-result-active |
Highlighted result (keyboard navigation state) |
.focus-searchbar-result-icon |
Icon container in result item |
.focus-searchbar-result-content |
Content wrapper (title + preview) |
.focus-searchbar-result-title |
Result title text |
.focus-searchbar-result-preview |
Result preview/description text |
.focus-searchbar-no-results |
Empty state message when no results |
.focus-searchbar-shortcut |
Keyboard shortcut badge (e.g., "⌘K") |
Accessibility
ARIA Attributes
- Use
role="search"on the form element - Add
aria-labelto toggle and close buttons - Use
autocomplete="off"on the input to prevent browser autocomplete interfering with custom results - Keyboard navigation is built-in with
Searchbar.init()