<Search>
ComponentA search box and results based on Algolia. Used primarily on documentation websites and learn.
<SearchProvider><SearchrenderHitContent={({ hit, Highlight }) => <div><p>{hit.description}</p></div>}resolveHitLink={(hit) => ({ href: { pathname: '/' + hit.objectID, query: { id: hit.__queryID} } })}placeholder="Search documentation"/></SearchProvider>
Name | Description |
---|---|
onSubmit function | Handle search form submission |
placeholder string | Placeholder content within the search box |
renderCalloutCta function | Render function for displaying a small CTA beneath the search results |
renderHitContent* function | Render function for displaying search results. Function returns hit data and a Highlight component in an object as arguments. |
resolveHitLink function | Optional function to transform the results of any given hit. Primarily used to remove /index from resuling urls |
showSearchLegend boolean | Enable the search keyboard legend |
function
|({ hit: object, Highlight: React.Node }) => React.Component
| Required
A render function whose result is used to display each query "hit"
Params
Property | Type | Description |
---|---|---|
hit | object | https://www.algolia.com/doc/api-reference/widgets/highlight/react/#widget-param-hit |
Highlight | React.Node | https://www.algolia.com/doc/api-reference/widgets/highlight/react/ |
function
| |( hit: object ) => NextLinkProps
| Optional | Default: (hit) => ({ href:`/${hit.objectID}` })
A function whose return value is spread as props to next/link
.
For more information about the available props, reference the next/link documentation: https://nextjs.org/docs/api-reference/next/link
Params
Property | Type | Description |
---|---|---|
hit | object | https://www.algolia.com/doc/api-reference/widgets/highlight/react/#widget-param-hit |
This component relies on the presence of the following environment variables to be available client side:
To use the primary <Search />
component, ensure it exists as a child of the <SearchProvider />
component. For example:
App.jsx
Any child component of <SearchProvider />
can utilize the provided useSearch()
hook and access search-specific information. For example:
useSearch()
exposes the following values:
client
(object
) - Initialized Algolia clientindexName
(string
) - The name of the Algolia index that search is performed uponinitAlgoliaInsights
(function
) - Required to initialize AlgoliaisCancelled
(boolean
) - Indicates if search is currently cancelled or notlogClick
(function
) - Fires an analytics event via the search-insights
packagequery
(string
) - Current search querysetIsCancelled
(function
) - Setter function that updates the search cancel statesetQuery
(function
) - Setter function that updates the search queryThis package includes a tools.js
file that includes Algolia-related Node.js scripts
function
|(config: object)
This specific helper function is designed specifically for perfoming search indexing on our various product sites' documentation pages.
Algolia-related configuration
Default:
Path to directory that contains the content to be indexed by Algolia
Default: path.join(__dirname, 'pages')
minimatch-style string to be performed within config.contentDir
. The results of this pattern match will determine which files to index.
Default: '**/*.mdx'
Additional options to include to the glob match. Available options here
Default: { ignore: path.join(config.contentDir, 'partials/**/*') }
Assuming your search-indexed content includes frontmatter, the keys included in this array
will be included as search criteria.
They will also be set as searchableAttributes
in the index configuration.
Default: ['page_title', 'description']
function
|(config: object)
This generic helper function allows for custom Algolia indexing
Algolia-related configuration
Default:
This function should return an array of objects that will get passed to Algolia's saveObjects
function. Each object must have an objectID
attribute.
Optional settings
object that can be used to configure the given index's settings.
It's preferable to configure the index this way rather with the web UI because it keeps our settings in source control.
Ex: