Installation
Install the@docsearch/js package using your preferred package manager:
CDN Installation
If you prefer not to use a package manager, you can load DocSearch directly from a CDN:Basic Setup
1
Import DocSearch and styles
Import the
docsearch function and CSS styles in your application:2
Add a container element
Add a container element to your HTML where DocSearch will be rendered. DocSearch generates a fully accessible search box for you, so use a container element (like a
div), not an input:3
Initialize DocSearch
Call the
docsearch() function with your configuration. You can pass either a CSS selector string or an HTML element:Don’t have your Algolia credentials yet? Apply to DocSearch to get started for free.
Complete Example
Here’s a complete working example:index.html
API Reference
docsearch(props)
The main function that initializes DocSearch and returns a control instance.Parameters
string | HTMLElement
required
The container for your DocSearch component. Can be a CSS selector string or an HTMLElement.
string
required
Your Algolia application ID.
string
required
Your Algolia Search API key (public, search-only key).
string
required
The name of your Algolia index to search.
string
Placeholder text for the search input. Defaults to “Search docs”.
object
Additional Algolia search parameters to apply to all queries.
function
Hook to transform search results before rendering.
function
Custom component to render individual search results. Supports JSX, HTML strings with the
html helper, or function-based templates.Custom component rendered at the bottom of the results panel.
function
Hook to wrap or modify the Algolia search client.
number
Maximum number of results to show per category. Defaults to 5.
boolean
Disable storage and display of recent and favorite searches. Defaults to
false.string
Query string to prefill when opening the search modal.
Return Value
Thedocsearch() function returns a DocSearchInstance object with the following methods:
boolean
Returns
true once the component is mounted and ready.boolean
Returns
true if the modal is currently open.function
Opens the search modal programmatically.
function
Closes the search modal programmatically.
function
Opens Ask AI mode in the modal.
function
Unmounts the DocSearch component and cleans up resources.
Programmatic Control
You can control DocSearch programmatically using the returned instance:Lifecycle Callbacks
DocSearch supports lifecycle callbacks to hook into important events:Styling
DocSearch comes with default styles via@docsearch/css. You can customize the appearance by:
- CSS Variables: Override DocSearch’s CSS custom properties
- Custom Classes: Target DocSearch’s class names with your own CSS
- Theme Object: Pass a theme configuration (see React documentation)
Next Steps
Configuration
Learn about all available configuration options
Styling
Customize the appearance of your search
Ask AI
Enable AI-powered search assistance
API Reference
Explore the complete API documentation
