Multi-Purpose Search: Three Implementation Patterns
The search partial is designed to be multi-purpose, supporting three different usage patterns with the same underlying code:
1. Inline Search (Page Sections)
Embed search functionality directly into any page using the search-only section:
sections:
- sectionType: search-only
placeholder: 'Search components...'
settings:
maxResults: 20
minCharacters: 2
Results display inline on the same page - perfect for dedicated search pages or content-specific search areas.
2. Header Search Bar → Dedicated Results Page
This library implements a global search bar in the site header that redirects to a dedicated search results page. This provides a professional search experience users expect from modern sites.
How it works:
- Search input in header (
header.njk) - Form submission redirects to
/search/?q=query - Search page reads URL parameter and auto-executes search
- Results display on dedicated full-page layout
Implementation:
The header search form is simple - it just redirects:
<form class="header-search-form" action="/search/" method="get">
<input type="search" name="q" placeholder="Search..." />
<button type="submit">Search</button>
</form>
The search.js automatically detects the ?q= parameter and executes the search:
function autoExecuteFromURL() {
const urlParams = new URLSearchParams(window.location.search);
const query = urlParams.get('q');
if (query && query.trim().length > 0) {
const searchInstance = searchInstances.values().next().value;
if (searchInstance) {
searchInstance.searchInput.value = query;
handleSearch(searchInstance);
}
}
}
Benefits:
- Global search access from any page
- Shareable search URLs (
/search/?q=maps) - Browser history support (back button works)
- Keyboard shortcut support (Cmd/Ctrl + K to focus)
3. Direct URL Access
Users can visit search URLs directly or share them:
/search/?q=maps - Pre-filled search for "maps"/search/?q=accordion - Pre-filled search for "accordion"
The search page auto-executes and displays results immediately.
All three patterns use the same search partial - it adapts to different contexts automatically. This is the power of multi-purpose partials in component-based architecture.