DocSearch provides a native React component that integrates seamlessly with React applications. The component is built with React hooks and supports React 16.8+.
Installation
Install the @docsearch/react package using your preferred package manager:
npm install @docsearch/react
The package requires React >= 16.8.0 and React DOM >= 16.8.0 as peer dependencies.
CDN Installation
For quick prototyping, you can load DocSearch from a CDN:
< link rel = "stylesheet" href = "https://cdn.jsdelivr.net/npm/@docsearch/css@4" />
< script src = "https://cdn.jsdelivr.net/npm/@docsearch/react@4" ></ script >
Basic Setup
Import the DocSearch component
Import the DocSearch component and CSS styles: import { DocSearch } from '@docsearch/react' ;
import '@docsearch/css' ;
Add DocSearch to your component
Render the DocSearch component with your Algolia credentials: function App () {
return (
< DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
/>
);
}
Complete Example
Here’s a complete example with typical configuration:
import React from 'react' ;
import { DocSearch } from '@docsearch/react' ;
import '@docsearch/css' ;
function App () {
return (
< div className = "App" >
< header >
< nav >
< div className = "logo" > My Docs </ div >
< DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
placeholder = "Search documentation"
/>
</ nav >
</ header >
< main >
{ /* Your content */ }
</ main >
</ div >
);
}
export default App ;
API Reference
DocSearch Component
The main React component for rendering DocSearch.
Props
Your Algolia application ID.
Your Algolia Search API key (public, search-only key).
The name of your Algolia index to search. The indexName prop is deprecated. Use the indices prop instead for multi-index support.
indices
Array<DocSearchIndex | string>
List of indices to search. Each item can be a string (index name) or an object with name and optional searchParameters. < DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indices = { [
{ name: 'docs_v1' , searchParameters: { facetFilters: [ 'version:1.0' ] } },
{ name: 'docs_v2' , searchParameters: { facetFilters: [ 'version:2.0' ] } },
'blog'
] }
/>
Placeholder text for the search input. Defaults to “Search docs”.
Additional Algolia search parameters to apply to all queries. The searchParameters prop is deprecated. Use the indices prop with per-index parameters instead.
Maximum number of results to show per category. Defaults to 5.
transformItems
(items: DocSearchHit[]) => DocSearchHit[]
Hook to transform search results before rendering. < DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
transformItems = { ( items ) => {
return items . map ( item => ({
... item ,
url: item . url . replace ( 'https://example.com' , '' )
}));
} }
/>
Custom component to render individual search results. < DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
hitComponent = { ({ hit , children }) => (
< a href = { hit . url } className = "custom-hit" >
{ children }
</ a >
) }
/>
Custom component rendered at the bottom of the results panel. < DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
resultsFooterComponent = { ({ state }) => (
< div className = "results-footer" >
Found { state . collections . length } results
</ div >
) }
/>
transformSearchClient
(searchClient: DocSearchTransformClient) => DocSearchTransformClient
Hook to wrap or modify the Algolia search client. < DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
transformSearchClient = { ( searchClient ) => {
searchClient . addAlgoliaAgent ( 'my-app' , '1.0.0' );
return searchClient ;
} }
/>
Custom navigator for controlling link navigation. Useful for client-side routing. // React Router example
import { useNavigate } from 'react-router-dom' ;
function SearchComponent () {
const navigate = useNavigate ();
return (
< DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
navigator = { {
navigate ({ itemUrl }) {
navigate ( itemUrl );
}
} }
/>
);
}
disableUserPersonalization
Disable storage and display of recent and favorite searches. Defaults to false.
Query string to prefill when opening the search modal.
Localized strings for the button and modal UI. < DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
translations = { {
button: {
buttonText: 'Buscar' ,
buttonAriaLabel: 'Buscar documentación'
},
modal: {
searchBox: {
resetButtonTitle: 'Borrar búsqueda' ,
cancelButtonText: 'Cancelar'
}
}
} }
/>
getMissingResultsUrl
({ query: string }) => string
Returns a URL to report missing results for a given query. < DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
getMissingResultsUrl = { ({ query }) =>
`https://github.com/myorg/docs/issues/new?title=Missing results for " ${ query } "`
}
/>
Algolia Insights client integration for analytics. import aa from 'search-insights' ;
aa ( 'init' , {
appId: 'YOUR_APP_ID' ,
apiKey: 'YOUR_SEARCH_API_KEY'
});
< DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
insights = { true }
/>
Theme overrides for customizing the appearance. < DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
theme = { {
primaryColor: '#5468ff' ,
backgroundColor: '#f5f6f7' ,
textColor: '#1c1e21'
} }
/>
portalContainer
DocumentFragment | Element
The container element where the modal should be portaled. Defaults to document.body.
Configuration for keyboard shortcuts. Allows enabling/disabling specific shortcuts. < DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
keyboardShortcuts = { {
'Ctrl/Cmd+K' : true ,
'/' : false // Disable forward slash shortcut
} }
/>
Configuration for Ask AI mode. Pass an assistant ID string or a full configuration object. < DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
askAi = { {
assistantId: 'YOUR_ASSISTANT_ID' ,
suggestedQuestions: true ,
searchParameters: {
facetFilters: [ 'version:2.0' ]
}
} }
/>
Using Refs
You can access DocSearch methods using a ref:
import React , { useRef } from 'react' ;
import { DocSearch } from '@docsearch/react' ;
import '@docsearch/css' ;
function App () {
const searchRef = useRef ( null );
const openSearch = () => {
searchRef . current ?. open ();
};
const openAskAi = () => {
searchRef . current ?. openAskAi ({ query: 'How do I get started?' });
};
return (
< div >
< button onClick = { openSearch } > Open Search </ button >
< button onClick = { openAskAi } > Ask AI </ button >
< DocSearch
ref = { searchRef }
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
/>
</ div >
);
}
Ref Methods
Opens the search modal programmatically.
Closes the search modal programmatically.
openAskAi
(initialMessage?: InitialAskAiMessage) => void
Opens Ask AI mode with an optional pre-filled message. searchRef . current ?. openAskAi ({
query: 'How do I configure search?' ,
messageId: 'msg_123'
});
Returns true if the modal is currently open.
Modular Components
DocSearch also exports individual components for advanced use cases:
The search button component without the modal:
import { DocSearchButton } from '@docsearch/react/button' ;
function CustomSearch () {
return (
< DocSearchButton
translations = { {
buttonText: 'Search docs' ,
buttonAriaLabel: 'Search documentation'
} }
onClick = { () => {
console . log ( 'Search button clicked' );
} }
/>
);
}
DocSearchModal
The search modal component without the button:
import { useState } from 'react' ;
import { DocSearchModal } from '@docsearch/react/modal' ;
import '@docsearch/react/style' ;
function CustomSearch () {
const [ isOpen , setIsOpen ] = useState ( false );
return (
<>
< button onClick = { () => setIsOpen ( true ) } > Search </ button >
{ isOpen && (
< DocSearchModal
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
initialScrollY = { window . scrollY }
onClose = { () => setIsOpen ( false ) }
/>
) }
</>
);
}
useDocSearchKeyboardEvents
Hook for handling keyboard shortcuts:
import { useRef , useState } from 'react' ;
import { useDocSearchKeyboardEvents } from '@docsearch/react/useDocSearchKeyboardEvents' ;
function CustomSearch () {
const [ isOpen , setIsOpen ] = useState ( false );
const buttonRef = useRef ( null );
useDocSearchKeyboardEvents ({
isOpen ,
onOpen : () => setIsOpen ( true ),
onClose : () => setIsOpen ( false ),
searchButtonRef: buttonRef
});
return (
< button ref = { buttonRef } onClick = { () => setIsOpen ( true ) } >
Search
</ button >
);
}
TypeScript Support
DocSearch is written in TypeScript and includes full type definitions:
import type { DocSearchProps , DocSearchHit } from '@docsearch/react' ;
import { DocSearch } from '@docsearch/react' ;
import '@docsearch/css' ;
const searchConfig : DocSearchProps = {
appId: 'YOUR_APP_ID' ,
apiKey: 'YOUR_SEARCH_API_KEY' ,
indexName: 'YOUR_INDEX_NAME' ,
transformItems : ( items : DocSearchHit []) => {
return items . map ( item => ({
... item ,
url: new URL ( item . url , window . location . origin ). href
}));
}
};
function App () {
return < DocSearch { ... searchConfig } /> ;
}
Styling
DocSearch comes with default styles. You can customize the appearance using:
CSS Variables
:root {
--docsearch-primary-color : #5468ff ;
--docsearch-text-color : #1c1e21 ;
--docsearch-spacing : 12 px ;
--docsearch-icon-stroke-width : 1.4 ;
--docsearch-highlight-color : var ( --docsearch-primary-color );
--docsearch-muted-color : #969faf ;
--docsearch-container-background : rgba ( 101 , 108 , 133 , 0.8 );
--docsearch-modal-background : #f5f6f7 ;
}
Theme Prop
< DocSearch
appId = "YOUR_APP_ID"
apiKey = "YOUR_SEARCH_API_KEY"
indexName = "YOUR_INDEX_NAME"
theme = { {
primaryColor: '#5468ff' ,
backgroundColor: '#ffffff' ,
textColor: '#1c1e21' ,
spacing: 12
} }
/>
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