Skip to content

Commit 67a3667

Browse files
committed
move key detect to DocSearch
1 parent 16b2ef9 commit 67a3667

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

packages/docsearch-react/src/DocSearch.tsx

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,23 @@ import { createPortal } from 'react-dom';
88

99
import { DocSearchButton } from './DocSearchButton';
1010
import { DocSearchModal } from './DocSearchModal';
11+
import { ControlKeyIcon } from './icons/ControlKeyIcon';
1112
import {
1213
DocSearchHit,
1314
InternalDocSearchHit,
1415
StoredDocSearchHit,
1516
} from './types';
1617
import { useDocSearchKeyboardEvents } from './useDocSearchKeyboardEvents';
1718

19+
const { useState, useEffect, useCallback, useRef } = React;
20+
21+
const ACTION_KEY_APPLE = '⌘' as const;
22+
const ACTION_KEY_DEFAULT = 'Ctrl' as const;
23+
24+
function isAppleDevice() {
25+
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
26+
}
27+
1828
export interface DocSearchProps {
1929
appId?: string;
2030
apiKey: string;
@@ -36,21 +46,31 @@ export interface DocSearchProps {
3646
}
3747

3848
export function DocSearch(props: DocSearchProps) {
39-
const searchButtonRef = React.useRef<HTMLButtonElement>(null);
40-
const [isOpen, setIsOpen] = React.useState(false);
41-
const [initialQuery, setInitialQuery] = React.useState<string | undefined>(
49+
const searchButtonRef = useRef<HTMLButtonElement>(null);
50+
const [isOpen, setIsOpen] = useState(false);
51+
const [initialQuery, setInitialQuery] = useState<string | undefined>(
4252
undefined
4353
);
4454

45-
const onOpen = React.useCallback(() => {
55+
const [key, setKey] = useState<
56+
typeof ACTION_KEY_APPLE | typeof ACTION_KEY_DEFAULT | null
57+
>(null);
58+
59+
useEffect(() => {
60+
if (typeof navigator !== 'undefined') {
61+
setKey(isAppleDevice() ? ACTION_KEY_APPLE : ACTION_KEY_DEFAULT);
62+
}
63+
}, []);
64+
65+
const onOpen = useCallback(() => {
4666
setIsOpen(true);
4767
}, [setIsOpen]);
4868

49-
const onClose = React.useCallback(() => {
69+
const onClose = useCallback(() => {
5070
setIsOpen(false);
5171
}, [setIsOpen]);
5272

53-
const onInput = React.useCallback(
73+
const onInput = useCallback(
5474
(event: KeyboardEvent) => {
5575
setIsOpen(true);
5676
setInitialQuery(event.key);
@@ -68,7 +88,16 @@ export function DocSearch(props: DocSearchProps) {
6888

6989
return (
7090
<>
71-
<DocSearchButton onClick={onOpen} ref={searchButtonRef} />
91+
<DocSearchButton onClick={onOpen} ref={searchButtonRef}>
92+
{key !== null ? (
93+
<span className="DocSearch-Button-Keys">
94+
<span className="DocSearch-Button-Key">
95+
{key === ACTION_KEY_DEFAULT ? <ControlKeyIcon /> : key}
96+
</span>
97+
<span className="DocSearch-Button-Key">K</span>
98+
</span>
99+
) : undefined}
100+
</DocSearchButton>
72101

73102
{isOpen &&
74103
createPortal(
Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import React, { useEffect, useState } from 'react';
1+
import React from 'react';
22

3-
import { ControlKeyIcon } from './icons/ControlKeyIcon';
43
import { SearchIcon } from './icons/SearchIcon';
54

65
type Translations = Partial<{
@@ -12,29 +11,12 @@ export type DocSearchButtonProps = React.ComponentProps<'button'> & {
1211
translations?: Translations;
1312
};
1413

15-
const ACTION_KEY_DEFAULT = 'Ctrl' as const;
16-
const ACTION_KEY_APPLE = '⌘' as const;
17-
18-
function isAppleDevice() {
19-
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
20-
}
21-
2214
export const DocSearchButton = React.forwardRef<
2315
HTMLButtonElement,
2416
DocSearchButtonProps
2517
>(({ translations = {}, ...props }, ref) => {
2618
const { buttonText = 'Search', buttonAriaLabel = 'Search' } = translations;
2719

28-
const [key, setKey] = useState<
29-
typeof ACTION_KEY_APPLE | typeof ACTION_KEY_DEFAULT | null
30-
>(null);
31-
32-
useEffect(() => {
33-
if (typeof navigator !== 'undefined') {
34-
setKey(isAppleDevice() ? ACTION_KEY_APPLE : ACTION_KEY_DEFAULT);
35-
}
36-
}, []);
37-
3820
return (
3921
<button
4022
type="button"
@@ -47,15 +29,7 @@ export const DocSearchButton = React.forwardRef<
4729
<SearchIcon />
4830
<span className="DocSearch-Button-Placeholder">{buttonText}</span>
4931
</span>
50-
51-
{key !== null && (
52-
<span className="DocSearch-Button-Keys">
53-
<span className="DocSearch-Button-Key">
54-
{key === ACTION_KEY_DEFAULT ? <ControlKeyIcon /> : key}
55-
</span>
56-
<span className="DocSearch-Button-Key">K</span>
57-
</span>
58-
)}
32+
{props.children}
5933
</button>
6034
);
6135
});

0 commit comments

Comments
 (0)