Skip to content

Commit 9bc826e

Browse files
francoischalifoursarahdayanMaria Elisabeth Schreiber
authored
docs: merge docs-1 into next (#433)
Co-authored-by: François Chalifour <[email protected]> Co-authored-by: Sarah Dayan <[email protected]> Co-authored-by: Maria Elisabeth Schreiber <[email protected]>
2 parents 687a98a + 47d876f commit 9bc826e

30 files changed

+1780
-457
lines changed

packages/autocomplete-js/src/autocomplete.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export function autocomplete<TItem extends BaseItem>(
143143
propGetters,
144144
state: lastStateRef.current,
145145
};
146+
146147
const render =
147148
(!getItemsCount(state) &&
148149
!hasEmptySourceTemplateRef.current &&

packages/website/docs/api.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
---
22
id: api
3-
title: API
3+
title: Introduction
44
---
5+
6+
import Draft from './partials/draft.md'
7+
8+
<Draft />
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
id: basic-options
3+
title: Basic configuration options
4+
---
5+
6+
Everything you need to create fantastic autocomplete experiences.
7+
8+
We've built Autocomplete to give you unlimited flexibility while freeing you from having to think about things like keyboard navigation, accessibility, or UI state. **The library offers a wide variety of APIs to let you fully customize the behavior and rendering of your autocomplete.**
9+
10+
Yet, only two parameters are required to create an autocomplete:
11+
- The **container** you want your autocomplete to go in.
12+
- The **sources** from which to get the items to display (see more in [**Sources**](sources)).
13+
14+
```js title="JavaScript"
15+
import { autocomplete } from '@algolia/autocomplete-js';
16+
17+
autocomplete({
18+
container: '#autocomplete',
19+
getSources() {
20+
return [
21+
{
22+
getItems({ query }) {
23+
return [
24+
{ label: 'Twitter', url: 'https://twitter.com' },
25+
{ label: 'GitHub', url: 'https://github.com' },
26+
].filter(({ label }) =>
27+
label.toLowerCase().includes(query.toLowerCase())
28+
);
29+
},
30+
getItemUrl({ item }) {
31+
return item.url;
32+
},
33+
templates: {
34+
item({ item }) {
35+
return item.label;
36+
},
37+
},
38+
},
39+
];
40+
},
41+
});
42+
```
43+
44+
The `container` options refers to where to inject the autocomplete in your HTML. It can be a [CSS selector](https://developer.mozilla.org/docs/Web/CSS/CSS_Selectors) or an [Element](https://developer.mozilla.org/docs/Web/API/HTMLElement). Make sure to provide a container (e.g., a `div`), not an `input`. Autocomplete generates a fully accessible search box for you.
45+
46+
```html title="HTML"
47+
<div id="autocomplete"></div>
48+
```
49+
50+
This is all you need to build a [fully functional, accessible, keyboard-navigable autocomplete](https://codesandbox.io/s/vigilant-dew-g2ezl).
51+
52+
Now, this is a great start, but **you can go much further**. Here are some of the options you'll probably want to use next:
53+
- Use [`placeholder`](autocomplete-js#placeholder) to define the text that appears in the input before the user types anything.
54+
- Use [`autoFocus`](autocomplete-js#autofocus) to focus on the search box on page load, and [`openOnFocus`](autocomplete-js#openonfocus) to display items as soon as a user selects the autocomplete, even without typing.
55+
- Use the [`onStateChange`](autocomplete-js#onstatechange) lifecycle hook to execute code whenever the state changes.
56+
- Use [`debug: true`](autocomplete-js#debug) to keep the autocomplete panel open even when the blur event occurs (see [**Debugging**](debugging)).
57+
58+
For a full list of all available parameters, check out the [API reference](autocomplete-js).

packages/website/docs/context.md

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
---
22
id: context
3-
title: Context
3+
title: Accessing data with Context
44
---
55

6-
The autocomplete context allows to store data in the state to use in different lifecycle hooks.
6+
The Context lets you store data and access it in different lifecycle hooks.
77

8-
You can use this API to access data in the templates that you would otherwise have access only in `getSources` for instance. The `setContext` setters expects an object that will be merged with the previous context. You can then read the context in `state.context`.
8+
Sometimes you need to store arbitrary data so you can access it later in your autocomplete. For example, when retrieving hits from Algolia, you may want to reuse the total number of retrieved hits in a template.
99

10-
The following example stores the number of hits from an Algolia response to display it in the templates.
10+
Autocomplete lets you store data using its Context API and access it anywhere from the [state](/docs/state).
11+
12+
## Usage
13+
14+
Context exposes a `setContext` function, which takes an object and merges it with the existing context. You can then access the context in `state.context`.
15+
16+
The following example stores the number of hits from an Algolia response, making it accessible everywhere in your autocomplete.
1117

1218
```js
13-
const autocomplete = createAutocomplete({
19+
autocomplete({
1420
// ...
1521
getSources({ query, setContext }) {
1622
return getAlgoliaResults({
@@ -21,15 +27,13 @@ const autocomplete = createAutocomplete({
2127
query,
2228
},
2329
],
24-
}).then((results) => {
25-
const productsResults = results[0];
26-
30+
}).then(([products]) => {
2731
setContext({
28-
nbProducts: productsResults.nbHits,
32+
nbProducts: products.nbHits,
2933
});
3034

31-
// You can now use `state.context.nbProducts` anywhere you have access to
32-
// the state.
35+
// You can now use `state.context.nbProducts`
36+
// anywhere where you have access to `state`.
3337

3438
return [
3539
// ...
@@ -38,3 +42,48 @@ const autocomplete = createAutocomplete({
3842
},
3943
});
4044
```
45+
46+
Context can be handy when developing [Autocomplete plugins](/docs/plugins). It avoids polluting the global namespace while still being able to pass data around across different lifecycle hooks.
47+
48+
```js
49+
function createAutocompletePlugin() {
50+
return {
51+
// ...
52+
subscribe({ setContext }) {
53+
setContext({
54+
autocompletePlugin: {
55+
// ...
56+
},
57+
});
58+
},
59+
};
60+
}
61+
```
62+
63+
## Reference
64+
65+
The `setContext` function is accessible on your `autocomplete` instance.
66+
67+
It's also provided in:
68+
- [`getSources`](createAutocomplete#getsources)
69+
- [`onInput`](createAutocomplete#oninput)
70+
- [`onSubmit`](createAutocomplete#onsubmit)
71+
- [`onReset`](createAutocomplete#onreset)
72+
- [`source.onActive`](sources#onactive)
73+
- [`source.onSelect`](sources#onselect)
74+
- [`source.getItems`](sources#getitems)
75+
- `plugin.subscribe`
76+
77+
The `context` object is available on the [`state`](/docs/state) object.
78+
79+
### `setContext`
80+
81+
> `(value: Record<string, unknown>) => void`
82+
83+
The function to pass data to to store it in the context.
84+
85+
### `context`
86+
87+
> `Record<string, unknown>`
88+
89+
The context to read data from.

packages/website/docs/controlled-mode.md

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
id: createAlgoliaInsightsPlugin
3+
---
4+
5+
## Example
6+
7+
```ts
8+
import algoliasearch from 'algoliasearch/lite';
9+
import { autocomplete } from '@algolia/autocomplete-js';
10+
import { createAlgoliaInsightsPlugin } from '@algolia/autocomplete-plugin-algolia-insights';
11+
import insightsClient from 'search-insights';
12+
13+
const appId = 'latency';
14+
const apiKey = '6be0576ff61c053d5f9a3225e2a90f76';
15+
const searchClient = algoliasearch(appId, apiKey);
16+
insightsClient('init', { appId, apiKey });
17+
18+
const algoliaInsightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });
19+
20+
autocomplete({
21+
container: '#autocomplete',
22+
plugins: [algoliaInsightsPlugin],
23+
});
24+
```
25+
26+
## Import
27+
28+
```ts
29+
import { createAlgoliaInsightsPlugin } from '@algolia/autocomplete-plugin-algolia-insights';
30+
```
31+
32+
## Params
33+
34+
### `insightsClient`
35+
36+
> `InsightsClient` | required
37+
38+
The initialized Search Insights client.
39+
40+
### `onItemsChange`
41+
42+
> `(params: OnItemsChangeParams) => void`
43+
44+
Hook to send an Insights event when the items change.
45+
46+
This hook is debounced every 400ms to better reflect when items are acknowledged by the user.
47+
48+
```ts
49+
type OnItemsChangeParams = {
50+
insights: InsightsApi;
51+
insightsEvents: ViewedObjectIDsParams[];
52+
state: AutocompleteState<any>;
53+
};
54+
```
55+
56+
### `onSelect`
57+
58+
> `(params: OnSelectParams) => void`
59+
60+
Hook to send an Insights event when an item is selected.
61+
62+
```ts
63+
type OnSelectParams = {
64+
insights: InsightsApi;
65+
insightsEvents: ClickedObjectIDsAfterSearchParams[];
66+
item: AlgoliaInsightsHit;
67+
state: AutocompleteState<any>;
68+
event: any;
69+
};
70+
```
71+
72+
### `onActive`
73+
74+
> `(params: OnActiveParams) => void`
75+
76+
Hook to send an Insights event when an item is active.
77+
78+
```ts
79+
type OnActiveParams = {
80+
insights: InsightsApi;
81+
insightsEvents: ClickedObjectIDsAfterSearchParams[];
82+
item: AlgoliaInsightsHit;
83+
state: AutocompleteState<any>;
84+
event: any;
85+
};
86+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
id: creating-multi-source-autocompletes
3+
title: Creating multi-source autocompletes
4+
---

0 commit comments

Comments
 (0)