@orama/searchbox
v1.0.0-rc53
Published
## Installation
Downloads
1,713
Keywords
Readme
Searchbox
Installation
# npm
npm i @orama/searchbox
# yarn
yarn add @orama/searchbox
# pnpm
pnpm install @orama/searchbox
Usage
The Orama Searchbox is a web component and can be used in any JavaScript framework, and even in vanilla JS and HTML.
In Vanilla JS
The following is an example of using the Orama Searchbox with Docusaurus on the client-side, using just HTML and JavaScript.
<script type="module" async>
import {
create,
insertMultiple,
save,
} from "https://unpkg.com/@orama/orama@latest/dist/index.js";
import {
RegisterSearchBox,
presets,
} from "https://unpkg.com/@orama/searchbox@latest/dist/index.global.js";
import "@orama/searchbox/dist/index.css";
const docs = await fetch("/my-docusaurus-data.json").then((res) =>
res.json()
);
const oramaInstance = await create({
schema: preset.docs.schema,
});
await insertMultiple(db, docs);
RegisterSearchBox({
oramaInstance,
});
</script>
<orama-searchbox />
Usage in non React JSX
When using this way will get Typescript errors because the components are not recognized in the JSX namespace and in that case please add something like:
import {
RegisterSearchBoxProps,
RegisterSearchButtonProps,
} from "@orama/searchbox/dist/index.js";
declare module "your-module" {
namespace JSX {
interface IntrinsicElements {
"orama-searchbox": RegisterSearchBoxParams;
"orama-search-button": RegisterSearchButtonParams;
}
}
}
This will add them to the namespace and you are ready to go!
With React
import { SearchBox } from "@orama/searchbox";
import { oramaInstance } from "somewhere";
import "@orama/searchbox/dist/index.css";
export function MyPage() {
return (
<div>
<SearchBox oramaInstance={oramaInstance} />
</div>
);
}
Searchbox Params
All of these can either be passed into the RegisterSearchBox
function or as props to the SearchBox
component
| Prop | Type | Default Value |
| ------------------------ | ----------------------------- | -------------------------------------------------------------------------------------------------- | --- |
| oramaInstance | AnyOrama or OramaClient | null |
| showFullScreenOnMobile | boolean | false |
| colorScheme | "light", "dark" or "system" | system |
| themeConfig | ThemeConfig | undefined |
| searchInputPlaceholder | string | Search... |
| searchResultsPlaceholder | string | Search something... |
| noResultsTemplate | string | No results found for term
|
| show | boolean | false |
| keyboardNavigationHints | boolean | true |
| backdrop | boolean | false |
| showSearchLink | boolean | false |
| seeAllLink | SeeAllLinkProps | { url: '/search', label: 'See all results' }
|
| resultsMap | ReturningProps | { path: 'path', title: 'title', description: 'title', section: 'section', category: 'category' }
| |
| setResultTitle | (doc: DocsDocument) => string | null |
| setResultLink | (doc: DocsDocument) => string | null |
| onClose | () => void | |
| keepOpen | boolean | false |
The resultsMap
parameter
This prop tells the searchbox how to display your results, we expect a result like:
{
path: "https://example.com"
title: "My example"
section: "Example Sites"
}
But using the resultsMap
prop you can map your object to what the searchbox expects, let's say your hit looks something like:
{
name: "Sonic the Hedgehog 2",
description: "Sonic the Hedgehog 2 is a 1992 platform game developed by Sega Technical Institute for the Sega Genesis.",
link: "https://en.wikipedia.org/wiki/Sonic_the_Hedgehog_2",
console: "Sega Genesis/Megadrive",
genre: "Platform"
}
You could use a resultsMap
like so to have the searchbox return the correct values:
{
path: "link",
title: "name",
description: "description",
section: "genre",
category: "console"
}
The setResultTitle
parameter
The setResultTitle
parameter can be used to customize the title that is generated for each search result item. It's a function that receives the hit document and should return the string to be used. If not provided, the resultsMap.title
will be used as the item title.
The setResultLink
parameter
The setResultLink
parameter can be used to customize the link that is generated for each search result item. It's a function that receives the hit document and should return the URL to be used. If not provided, the resultsMap.path
will be used as the item link.
The facetProperty
parameter
If you want to group your results by a specific property, you can use the facetProperty
param. For example, if you want to group your results by category
you can use the facetProperty
param with the value category
. The property used as facetProperty
must be an enum.
The colorScheme
parameter
colorScheme
param can be one of light
, dark
, or system
. This param is used to set the initial color scheme of the search box. It's optional and defaults to light
.
If dark
is passed, the search box will be rendered in dark mode.
If system
is passed, the color scheme will be determined by the user's system preferences, if available, or it will default to light
.
light
and dark
color schemes include a set of predefined colors for the search box. You can choose to use the default colors or override them with your own. The themeConfig
param can be used to override the default colors.
The themeConfig
parameter
The themeConfig
prop is useful to customize the default colors of the Searchbox. You can pass an object of type ThemeConfig
to the themeConfig
prop to customize the colors of the Searchbox. The ThemeConfig
type is defined as follows:
type ThemeConfig = {
light: Record<string, string>;
dark: Record<string, string>;
};
The light
and dark
properties are objects that define the colors of the Searchbox in light and dark mode, respectively. The keys of these objects must be the CSS custom properties that we are using to style the Searchbox elements, and the values are the colors in hexadecimal format. The following example demonstrates how to customize the colors of the Searchbox:
const themeConfig = {
light: {
'--text-color-primary': '#54525B',
'--text-color-secondary': '#302F33',
'--text-color-tertiary': '#6D6B74',
'--background-color-primary': '#ffffff',
'--background-color-secondary': '#fafaff',
'--background-color-tertiary': '#f6f2ff',
'--border-color-primary': '#ccd5e0',
'--border-color-secondary': '#71717A',
'--border-color-accent': '#3451b2'
'--background-color-fourth': '#f6f6f7',
'--icon-color-primary': '#54525b',
'--icon-color-secondary': '#6d6b74',
'--backdrop-bg-color': 'rgba(0, 0, 0, 0.50)'
},
dark: {
'--text-color-primary': '#d4d4d8',
'--text-color-secondary': '#71717a',
'--text-color-tertiary': '#a1a1aa',
'--background-color-primary': '#18181b',
'--background-color-secondary': '#27272a',
'--background-color-tertiary': '#09090b',
'--border-color-primary': '#27272a',
'--border-color-secondary': '#71717A',
'--border-color-accent': '#3451b2',
'--icon-color-primary': '#d4d4d8',
'--icon-color-secondary': '#71717a',
'--backdrop-bg-color': 'rgba(0, 0, 0, 0.50)'
}
}
You don't need to define all the colors, only the ones you want to customize. The rest of the colors will be set to the default values.
The sellAllLink
parameter
The sellAllLink
parameter can be used to override the default url and label of the "See all results" link. The label can be a function that gets count
and term
as parameters.
{
url: '/my-all-results-page',
label: (count: string, term?: string | undefined) => `See all ${count} results for ${term}`
}
Usage with SearchButton Component
The SearchButton
component can be used in conjunction with the SearchBox
to trigger the search functionality. Here's an example of how to use it in vanilla JS and React.
In Vanilla JS
The following is an example of using the Orama Searchbox and SearchButton with Docusaurus on the client-side, using just HTML and JavaScript.
<script type="module" async>
import {
create,
insertMultiple,
save,
} from "https://unpkg.com/@orama/orama@latest/dist/index.js";
import {
RegisterSearchBox,
RegisterSearchButton,
presets,
} from "https://unpkg.com/@orama/searchbox@latest/dist/index.global.js";
import "@orama/searchbox/dist/index.css";
const docs = await fetch("/my-docusaurus-data.json").then((res) =>
res.json()
);
const oramaInstance = await create({
schema: preset.docs.schema,
});
await insertMultiple(db, docs);
RegisterSearchBox({
oramaInstance,
});
RegisterSearchButton();
</script>
<orama-searchbox />
<orama-search-button />
With React
import { SearchBox, SearchButton } from "@orama/searchbox";
import { oramaInstance } from "somewhere";
import "@orama/searchbox/dist/index.css";
export function MyPage() {
return (
<div>
<SearchBox oramaInstance={oramaInstance} />
<SearchButton />
</div>
);
}
SearchButton params
| Prop | Type | Default Value | | ----------- | ----------------- | ------------- | | fullWidth | boolean | false | | label | string | "Search" | | colorScheme | "light" or "dark" | "light" | | themeConfig | ThemeConfig | undefined | | onClick | void | undefined |
By default, clicking the SearchButton
triggers the opening of the SearchBox
. However, you can customize this behavior by providing your own function to the onClick
prop.
The onClick
prop accepts a function that is called when the button is clicked. This function should handle any additional logic you want to execute when the button is clicked.
Here's an example of how to use the onClick
prop:
<SearchButton
onClick={() => {
// Add your custom logic here
console.log("SearchButton was clicked!");
}}
/>
In this example, clicking the SearchButton will log 'SearchButton was clicked!' to the console, in addition to triggering the opening of the SearchBox. You will replace the console.log statement with your actual logic.
themeConfig
You can customize the colors of the SearchButton component by using the themeConfig
params. You don't need to pass all the colors, just the ones you want to change.
const themeConfig = {
light: {
"--search-btn-text-color": "#54525B",
"--search-btn-text-color-hover": "#54525B",
"--search-btn-text-color-focus": "#54525B",
"--search-btn-background-color": "#FFFFFF",
"--search-btn-background-color-hover": "#FFFFFF",
"--search-btn-background-color-focus": "#FFFFFF",
"--search-btn-border-color": "transparent",
"--search-btn-border-color-hover": "#a8b1ff",
"--search-btn-border-color-focus": "#a8b1ff",
"--search-btn-icon-color": "#71717A",
},
dark: {
"--search-btn-text-color": "#D4D4D8",
"--search-btn-text-color-hover": "#D4D4D8",
"--search-btn-text-color-focus": "#D4D4D8",
"--search-btn-background-color": "#09090B",
"--search-btn-background-color-hover": "#09090B",
"--search-btn-background-color-focus": "#09090B",
"--search-btn-border-color": "transparent",
"--search-btn-border-color-hover": "#a8b1ff",
"--search-btn-border-color-focus": "#a8b1ff",
"--search-btn-icon-color": "#71717A",
},
};
Usage with SearchAllResults component
You can use the SearchAllResults
component to display search results on a dedicated page, such as /search?q=term
.
It displays the search results and optionally facets and an AI generated search summary.
SearchAllResults params
| Prop | Type | Default Value |
| ------------- | ---------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| colorScheme | "light", "dark" or "system" | "light" |
| themeConfig | ThemeConfig | undefined |
| title | string | "Search results for" |
| oramaInstance | AnyOrama or OramaClient | null |
| resultsMap | ReturningProps | { path: 'path', title: 'title', description: 'title', section: 'section', category: 'category' }
|
| facetProperty | string | undefined |
| renderItem | Nullable<(doc: DocsDocument) => h.JSX.Element> | null |
facetProperty
You can use the facetProperty
parameter to specify the property to use for faceting. This is optional, and if not provided, the search results will not be faceted.
renderItem
You can use the renderItem
parameter to specify a custom rendering function for an additional item to display in each search result.
License
Copyright OramaSearch Inc. 2024. Licensed under the Apache 2.0 license.