Skip to content
This repository was archived by the owner on Jun 11, 2021. It is now read-only.

Commit f2abfba

Browse files
refactor(docsearch): create touch events hook
1 parent a758ea0 commit f2abfba

2 files changed

Lines changed: 45 additions & 21 deletions

File tree

packages/docsearch-react/src/DocSearch.tsx

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { groupBy, noop } from './utils';
1616
import { createStoredSearches } from './stored-searches';
1717
import { useSearchClient } from './useSearchClient';
1818
import { useTrapFocus } from './useTrapFocus';
19+
import { useTouchEvents } from './useTouchEvents';
1920
import { Hit } from './Hit';
2021
import { SearchBox } from './SearchBox';
2122
import { ScreenState } from './ScreenState';
@@ -66,7 +67,6 @@ export function DocSearch({
6667
: ''
6768
).current;
6869

69-
useTrapFocus({ container: containerRef.current });
7070
const searchClient = useSearchClient(appId, apiKey);
7171
const favoriteSearches = React.useRef(
7272
createStoredSearches<StoredDocSearchHit>({
@@ -274,6 +274,14 @@ export function DocSearch({
274274

275275
const { getEnvironmentProps, getRootProps, refresh } = autocomplete;
276276

277+
useTouchEvents({
278+
getEnvironmentProps,
279+
dropdownElement: dropdownRef.current,
280+
searchBoxElement: searchBoxRef.current,
281+
inputElement: inputRef.current,
282+
});
283+
useTrapFocus({ container: containerRef.current });
284+
277285
React.useEffect(() => {
278286
const isMobileMediaQuery = window.matchMedia('(max-width: 750px)');
279287

@@ -299,26 +307,6 @@ export function DocSearch({
299307
}
300308
}, [initialQuery, refresh]);
301309

302-
React.useEffect(() => {
303-
if (!(searchBoxRef.current && dropdownRef.current && inputRef.current)) {
304-
return undefined;
305-
}
306-
307-
const { onTouchStart, onTouchMove } = getEnvironmentProps({
308-
searchBoxElement: searchBoxRef.current,
309-
dropdownElement: dropdownRef.current,
310-
inputElement: inputRef.current,
311-
});
312-
313-
window.addEventListener('touchstart', onTouchStart);
314-
window.addEventListener('touchmove', onTouchMove);
315-
316-
return () => {
317-
window.removeEventListener('touchstart', onTouchStart);
318-
window.removeEventListener('touchmove', onTouchMove);
319-
};
320-
}, [getEnvironmentProps, searchBoxRef, dropdownRef, inputRef]);
321-
322310
return (
323311
<div
324312
ref={containerRef}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React from 'react';
2+
import { AutocompleteApi } from '@francoischalifour/autocomplete-core';
3+
4+
interface UseTouchEventsProps {
5+
getEnvironmentProps: AutocompleteApi<any>['getEnvironmentProps'];
6+
dropdownElement: HTMLDivElement | null;
7+
searchBoxElement: HTMLDivElement | null;
8+
inputElement: HTMLInputElement | null;
9+
}
10+
11+
export function useTouchEvents({
12+
getEnvironmentProps,
13+
dropdownElement,
14+
searchBoxElement,
15+
inputElement,
16+
}: UseTouchEventsProps) {
17+
React.useEffect(() => {
18+
if (!(dropdownElement && searchBoxElement && inputElement)) {
19+
return undefined;
20+
}
21+
22+
const { onTouchStart, onTouchMove } = getEnvironmentProps({
23+
dropdownElement,
24+
searchBoxElement,
25+
inputElement,
26+
});
27+
28+
window.addEventListener('touchstart', onTouchStart);
29+
window.addEventListener('touchmove', onTouchMove);
30+
31+
return () => {
32+
window.removeEventListener('touchstart', onTouchStart);
33+
window.removeEventListener('touchmove', onTouchMove);
34+
};
35+
}, [getEnvironmentProps, dropdownElement, searchBoxElement, inputElement]);
36+
}

0 commit comments

Comments
 (0)