Skip to content

Commit 1421077

Browse files
fix(deps): Update and pin Autocomplete to the latest version (#999)
* Update DocSearch to the latest Autocomplete version Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
1 parent e7797a5 commit 1421077

9 files changed

Lines changed: 2010 additions & 1869 deletions

File tree

bundlesize.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
},
77
{
88
"path": "packages/docsearch-react/dist/umd/index.js",
9-
"maxSize": "18 kB"
9+
"maxSize": "18.2 kB"
1010
},
1111
{
1212
"path": "packages/docsearch-js/dist/umd/index.js",
13-
"maxSize": "25.5 kB"
13+
"maxSize": "25.8 kB"
1414
}
1515
]
1616
}

packages/docsearch-react/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"watch": "watch \"yarn on:change\" --ignoreDirectoryPattern \"/dist/\""
3535
},
3636
"dependencies": {
37-
"@algolia/autocomplete-core": "^1.0.0-alpha.35",
38-
"@algolia/autocomplete-preset-algolia": "^1.0.0-alpha.35",
37+
"@algolia/autocomplete-core": "1.0.0-alpha.44",
38+
"@algolia/autocomplete-preset-algolia": "1.0.0-alpha.44",
3939
"@docsearch/css": "3.0.0-alpha.33",
4040
"algoliasearch": "^4.0.0"
4141
},

packages/docsearch-react/src/DocSearchModal.tsx

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ export function DocSearchModal({
5151
completion: null,
5252
context: {},
5353
isOpen: false,
54-
selectedItemId: null,
54+
activeItemId: null,
5555
status: 'idle',
5656
});
5757

5858
const containerRef = React.useRef<HTMLDivElement | null>(null);
5959
const modalRef = React.useRef<HTMLDivElement | null>(null);
60-
const searchBoxRef = React.useRef<HTMLDivElement | null>(null);
60+
const formElementRef = React.useRef<HTMLDivElement | null>(null);
6161
const dropdownRef = React.useRef<HTMLDivElement | null>(null);
6262
const inputRef = React.useRef<HTMLInputElement | null>(null);
6363
const snippetLength = React.useRef<number>(10);
@@ -117,7 +117,7 @@ export function DocSearchModal({
117117
React.KeyboardEvent
118118
>({
119119
id: 'docsearch',
120-
defaultSelectedItemId: 0,
120+
defaultActiveItemId: 0,
121121
placeholder,
122122
openOnFocus: true,
123123
initialState: {
@@ -139,6 +139,7 @@ export function DocSearchModal({
139139

140140
return [
141141
{
142+
sourceId: 'recentSearches',
142143
onSelect({ item, event }) {
143144
saveRecentSearch(item);
144145

@@ -154,6 +155,7 @@ export function DocSearchModal({
154155
},
155156
},
156157
{
158+
sourceId: 'favoriteSearches',
157159
onSelect({ item, event }) {
158160
saveRecentSearch(item);
159161

@@ -237,43 +239,46 @@ export function DocSearchModal({
237239

238240
setContext({ nbHits });
239241

240-
return Object.values<DocSearchHit[]>(sources).map((items) => {
241-
return {
242-
onSelect({ item, event }) {
243-
saveRecentSearch(item);
244-
245-
if (!event.shiftKey && !event.ctrlKey && !event.metaKey) {
246-
onClose();
247-
}
248-
},
249-
getItemUrl({ item }) {
250-
return item.url;
251-
},
252-
getItems() {
253-
return Object.values(
254-
groupBy(items, (item) => item.hierarchy.lvl1)
255-
)
256-
.map(transformItems)
257-
.map((hits) =>
258-
hits.map((item) => {
259-
return {
260-
...item,
261-
// eslint-disable-next-line @typescript-eslint/camelcase
262-
__docsearch_parent:
263-
item.type !== 'lvl1' &&
264-
hits.find(
265-
(siblingItem) =>
266-
siblingItem.type === 'lvl1' &&
267-
siblingItem.hierarchy.lvl1 ===
268-
item.hierarchy.lvl1
269-
),
270-
};
271-
})
242+
return Object.values<DocSearchHit[]>(sources).map(
243+
(items, index) => {
244+
return {
245+
sourceId: `hits${index}`,
246+
onSelect({ item, event }) {
247+
saveRecentSearch(item);
248+
249+
if (!event.shiftKey && !event.ctrlKey && !event.metaKey) {
250+
onClose();
251+
}
252+
},
253+
getItemUrl({ item }) {
254+
return item.url;
255+
},
256+
getItems() {
257+
return Object.values(
258+
groupBy(items, (item) => item.hierarchy.lvl1)
272259
)
273-
.flat();
274-
},
275-
};
276-
});
260+
.map(transformItems)
261+
.map((hits) =>
262+
hits.map((item) => {
263+
return {
264+
...item,
265+
// eslint-disable-next-line @typescript-eslint/camelcase
266+
__docsearch_parent:
267+
item.type !== 'lvl1' &&
268+
hits.find(
269+
(siblingItem) =>
270+
siblingItem.type === 'lvl1' &&
271+
siblingItem.hierarchy.lvl1 ===
272+
item.hierarchy.lvl1
273+
),
274+
};
275+
})
276+
)
277+
.flat();
278+
},
279+
};
280+
}
281+
);
277282
});
278283
},
279284
}),
@@ -298,7 +303,7 @@ export function DocSearchModal({
298303
useTouchEvents({
299304
getEnvironmentProps,
300305
panelElement: dropdownRef.current,
301-
searchBoxElement: searchBoxRef.current,
306+
formElement: formElementRef.current,
302307
inputElement: inputRef.current,
303308
});
304309
useTrapFocus({ container: containerRef.current });
@@ -387,7 +392,7 @@ export function DocSearchModal({
387392
}}
388393
>
389394
<div className="DocSearch-Modal" ref={modalRef}>
390-
<header className="DocSearch-SearchBar" ref={searchBoxRef}>
395+
<header className="DocSearch-SearchBar" ref={formElementRef}>
391396
<SearchBox
392397
{...autocomplete}
393398
state={state}

packages/docsearch-react/src/Results.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { AutocompleteApi, AutocompleteState } from '@algolia/autocomplete-core';
1+
import {
2+
AutocompleteApi,
3+
AutocompleteState,
4+
BaseItem,
5+
} from '@algolia/autocomplete-core';
26
import React from 'react';
37

48
import { DocSearchProps } from './DocSearch';
59
import { Snippet } from './Snippet';
610
import { InternalDocSearchHit, StoredDocSearchHit } from './types';
711

8-
interface ResultsProps<TItem>
12+
interface ResultsProps<TItem extends BaseItem>
913
extends AutocompleteApi<
1014
TItem,
1115
React.FormEvent,
@@ -51,7 +55,7 @@ export function Results<TItem extends StoredDocSearchHit>(
5155
);
5256
}
5357

54-
interface ResultProps<TItem> extends ResultsProps<TItem> {
58+
interface ResultProps<TItem extends BaseItem> extends ResultsProps<TItem> {
5559
item: TItem;
5660
index: number;
5761
}

packages/docsearch-react/src/ScreenState.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { AutocompleteApi, AutocompleteState } from '@algolia/autocomplete-core';
1+
import {
2+
AutocompleteApi,
3+
AutocompleteState,
4+
BaseItem,
5+
} from '@algolia/autocomplete-core';
26
import React from 'react';
37

48
import { DocSearchProps } from './DocSearch';
@@ -9,7 +13,7 @@ import { StartScreen } from './StartScreen';
913
import { StoredSearchPlugin } from './stored-searches';
1014
import { InternalDocSearchHit, StoredDocSearchHit } from './types';
1115

12-
export interface ScreenStateProps<TItem>
16+
export interface ScreenStateProps<TItem extends BaseItem>
1317
extends AutocompleteApi<
1418
TItem,
1519
React.FormEvent,

packages/docsearch-react/src/SearchBox.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export function SearchBox(props: SearchBoxProps) {
6262
inputElement: props.inputRef.current!,
6363
autoFocus: props.autoFocus,
6464
maxLength: MAX_QUERY_SIZE,
65-
enterKeyHint: 'go',
6665
})}
6766
/>
6867

packages/docsearch-react/src/types/DocSearchHit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ interface DocSearchHitSnippetResult {
4242
hierarchy_camel: DocSearchHitHighlightResultHierarchy[];
4343
}
4444

45-
export interface DocSearchHit {
45+
export declare type DocSearchHit = {
4646
objectID: string;
4747
content: string | null;
4848
url: string;
@@ -78,4 +78,4 @@ export interface DocSearchHit {
7878
};
7979
};
8080
_distinctSeqID?: number;
81-
}
81+
};

packages/docsearch-react/src/useTouchEvents.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ import React from 'react';
44
interface UseTouchEventsProps {
55
getEnvironmentProps: AutocompleteApi<any>['getEnvironmentProps'];
66
panelElement: HTMLDivElement | null;
7-
searchBoxElement: HTMLDivElement | null;
7+
formElement: HTMLDivElement | null;
88
inputElement: HTMLInputElement | null;
99
}
1010

1111
export function useTouchEvents({
1212
getEnvironmentProps,
1313
panelElement,
14-
searchBoxElement,
14+
formElement,
1515
inputElement,
1616
}: UseTouchEventsProps) {
1717
React.useEffect(() => {
18-
if (!(panelElement && searchBoxElement && inputElement)) {
18+
if (!(panelElement && formElement && inputElement)) {
1919
return undefined;
2020
}
2121

2222
const { onTouchStart, onTouchMove } = getEnvironmentProps({
2323
panelElement,
24-
searchBoxElement,
24+
formElement,
2525
inputElement,
2626
});
2727

@@ -32,5 +32,5 @@ export function useTouchEvents({
3232
window.removeEventListener('touchstart', onTouchStart);
3333
window.removeEventListener('touchmove', onTouchMove);
3434
};
35-
}, [getEnvironmentProps, panelElement, searchBoxElement, inputElement]);
35+
}, [getEnvironmentProps, panelElement, formElement, inputElement]);
3636
}

0 commit comments

Comments
 (0)