Skip to content

Commit f2db4bd

Browse files
docs(autocomplete-js): rewrite docs (#482)
* docs(autocomplete-js): rewrite docs * fix: apply suggestions from code review Co-authored-by: François Chalifour <[email protected]> * docs(autocomplete): tweak docs Co-authored-by: François Chalifour <[email protected]>
1 parent ce07b48 commit f2db4bd

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

packages/website/docs/autocomplete-js.md

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,42 @@ id: autocomplete-js
33
title: autocomplete
44
---
55

6-
This function creates a JavaScript autocomplete experience.
6+
Autocomplete JS creates a virtual DOM-based autocomplete experience.
7+
8+
The `autocomplete` function creates an autocomplete experience and attaches it to an element of the DOM. By default, it uses [Preact 10](https://preactjs.com/guide/v10/whats-new/) to render templates.
9+
10+
## Installation
11+
12+
First, you need to install the package.
13+
14+
```bash
15+
yarn add @algolia/autocomplete-js@alpha
16+
# or
17+
npm install @algolia/autocomplete-js@alpha
18+
```
19+
20+
Then import it in your project:
21+
22+
```js
23+
import { autocomplete } from '@algolia/autocomplete-js';
24+
```
25+
26+
If you don't use a package manager, you can use a standalone endpoint:
27+
28+
```html
29+
<script src="https://cdn.jsdelivr.net/npm/@algolia/autocomplete-js@alpha"></script>
30+
```
731

832
## Example
933

34+
Make sure to define an empty container in your HTML where to inject your autocomplete.
35+
1036
```js title="HTML"
1137
<div id="autocomplete"></div>
1238
```
1339

40+
This example uses Autocomplete with an Algolia index, along with the [`algoliasearch`](https://www.npmjs.com/package/algoliasearch) API client. All Algolia utility functions to retrieve hits and parse results are available directly in the package.
41+
1442
```js title="JavaScript"
1543
import algoliasearch from 'algoliasearch/lite';
1644
import {
@@ -56,21 +84,15 @@ const autocompleteSearch = autocomplete({
5684
});
5785
```
5886

59-
## Import
87+
## Parameters
6088

61-
```ts
62-
import { autocomplete } from '@algolia/autocomplete-js';
63-
```
64-
65-
## Params
66-
67-
`autocomplete` accepts all the props that [`createAutocomplete`](/docs/createAutocomplete#reference) supports.
89+
The `autocomplete` function accepts all the props that [`createAutocomplete`](/docs/createAutocomplete#reference) supports.
6890

6991
### `container`
7092

7193
> `string | HTMLElement` | **required**
7294
73-
The container for the Autocomplete search box. You can either pass a [CSS selector](https://developer.mozilla.org/docs/Web/CSS/CSS_Selectors) or an [Element](https://developer.mozilla.org/docs/Web/API/HTMLElement). The first element matching the provided selector will be used as container.
95+
The container for the Autocomplete search box. You can either pass a [CSS selector](https://developer.mozilla.org/docs/Web/CSS/CSS_Selectors) or an [Element](https://developer.mozilla.org/docs/Web/API/HTMLElement). If there are several containers matching the selector, Autocomplete picks up the first one.
7496

7597
import CreateAutocompleteProps from './partials/createAutocomplete-props.md'
7698

@@ -80,19 +102,19 @@ import CreateAutocompleteProps from './partials/createAutocomplete-props.md'
80102

81103
> `string | HTMLElement`
82104
83-
The container for the Autocomplete panel. You can either pass a [CSS selector](https://developer.mozilla.org/docs/Web/CSS/CSS_Selectors) or an [Element](https://developer.mozilla.org/docs/Web/API/HTMLElement). The first element matching the provided selector will be used as container.
105+
The container for the Autocomplete panel. You can either pass a [CSS selector](https://developer.mozilla.org/docs/Web/CSS/CSS_Selectors) or an [Element](https://developer.mozilla.org/docs/Web/API/HTMLElement). If there are several containers matching the selector, Autocomplete picks up the first one.
84106

85107
### `panelPlacement`
86108

87109
> `"start" | "end" | "full-width" | "input-wrapper-width"` | defaults to `"input-wrapper-width"`
88110
89-
The panel horizontal position.
111+
The panel's horizontal position.
90112

91113
### `classNames`
92114

93115
> `ClassNames`
94116
95-
The class names to inject in each created DOM element. It it useful to design with external CSS frameworks.
117+
Class names to inject for each created DOM element. This is useful to style your autocomplete with external CSS frameworks.
96118

97119
```ts
98120
type ClassNames = Partial<{
@@ -127,9 +149,9 @@ type ClassNames = Partial<{
127149

128150
> `(params: { children: VNode, state: AutocompleteState<TItem>, sections: VNode[], createElement: Pragma, Fragment: PragmaFrag }) => void`
129151
130-
Function called to render the autocomplete panel. It is useful for rendering sections in different row or column layouts.
152+
The function that renders the autocomplete panel. This is useful to customize the rendering, for example, using multi-row or multi-column layouts.
131153

132-
Default implementation:
154+
This is the default implementation:
133155

134156
```js
135157
import { render } from 'preact';
@@ -146,9 +168,9 @@ autocomplete({
146168

147169
> `(params: { children: VNode, state: AutocompleteState<TItem>, sections: VNode[], createElement: Pragma, Fragment: PragmaFrag }) => void`
148170
149-
Function called to render a no results section when no hits are returned. It is useful for letting the user know that the query returned no results.
171+
The function that renders a no results section when there are no hits. This is useful to let the user know that the query returned no results.
150172

151-
There is no default implementation, which closes the panel when there's no results.
173+
There's no default implementation. By default, Autocomplete closes the panel when there's no results. Here's how you can customize this behavior:
152174

153175
```js
154176
import { render } from 'preact';
@@ -167,15 +189,23 @@ autocomplete({
167189

168190
> `(type: any, props: Record<string, any> | null, ...children: ComponentChildren[]) => VNode` | defaults to `preact.createElement`
169191
170-
Function used to create elements.
192+
The function that create virtual nodes.
193+
194+
It uses [Preact 10](https://preactjs.com/guide/v10/whats-new/)'s `createElement` by default, but you can provide your own implementation.
171195

172196
#### `Fragment`
173197

174198
> defaults to `preact.Fragment`
175199
176-
Component used for fragments.
200+
The component to use to create fragments.
177201

178-
## Returned props
202+
It uses [Preact 10](https://preactjs.com/guide/v10/whats-new/)'s `Fragment` by default, but you can provide your own implementation.
203+
204+
## Returns
205+
206+
The `autocomplete` function returns [state setters](state#setters) and a `refresh` method that updates the UI state.
207+
208+
These setters are useful to control the autocomplete experience from external events.
179209

180210
```js
181211
const {
@@ -188,7 +218,3 @@ const {
188218
refresh,
189219
} = autocomplete(options);
190220
```
191-
192-
`autocomplete` returns all the [state setters](state#setters) and `refresh` method that updates the UI state.
193-
194-
These setters are useful to control the autocomplete experience from external events.

0 commit comments

Comments
 (0)