Skip to content

Commit 9e4c69d

Browse files
authored
feat: push keyboard buttons till client (#983)
This fixes a UI flash issue on Windows where the <kbd>⌘</kbd> key shows up right before <kbd>Ctrl</kbd> when using SSG (like Docusaurus). This happened because in these scenarii, the site is generated on a server, and we can't know yet what will the user's OS be. Closes facebook/docusaurus#3682.
1 parent b3ee1cc commit 9e4c69d

5 files changed

Lines changed: 196 additions & 1244 deletions

File tree

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"build:types": "lerna run build:types --scope @docsearch/*",
1111
"build:umd": "lerna run build:umd --scope @docsearch/*",
1212
"build": "lerna run build --scope @docsearch/*",
13-
"cy:clean": "rm -rf cypress/screenshots",
13+
"cy:clean": "rimraf cypress/screenshots",
1414
"cy:info": "cypress info",
1515
"cy:record": "percy exec -- cypress run --spec 'cypress/integration/**/*' --headless --record",
1616
"cy:run:chrome": "yarn run cy:run --browser chrome",
@@ -51,6 +51,7 @@
5151
"babel-plugin-module-resolver": "4.0.0",
5252
"bundlesize": "0.18.0",
5353
"concurrently": "5.3.0",
54+
"cross-env": "7.0.2",
5455
"cssnano": "4.1.10",
5556
"cypress": "4.8.0",
5657
"dotenv": "8.2.0",
@@ -71,6 +72,7 @@
7172
"prettier": "2.1.1",
7273
"react": "16.13.1",
7374
"react-dom": "16.13.1",
75+
"rimraf": "3.0.2",
7476
"rollup": "1.31.0",
7577
"rollup-plugin-babel": "4.4.0",
7678
"rollup-plugin-commonjs": "10.1.0",

packages/docsearch-css/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"unpkg": "dist/style.css",
1818
"jsdelivr": "dist/style.css",
1919
"scripts": {
20-
"build:clean": "rm -rf ./dist",
20+
"build:clean": "rimraf ./dist",
2121
"build:css": "node build-css.js",
2222
"build": "yarn build:clean && mkdir dist && yarn build:css",
2323
"prepare": "yarn build",

packages/docsearch-js/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
"unpkg": "dist/umd/index.js",
2222
"jsdelivr": "dist/umd/index.js",
2323
"scripts": {
24-
"build:clean": "rm -rf ./dist",
25-
"build:esm": "BUILD=esm rollup --config",
24+
"build:clean": "rimraf ./dist",
25+
"build:esm": "cross-env BUILD=esm rollup --config",
2626
"build:types": "tsc -p ./tsconfig.declaration.json --outFile ./dist/esm/index.d.ts",
27-
"build:umd": "BUILD=umd rollup --config",
27+
"build:umd": "cross-env BUILD=umd rollup --config",
2828
"build": "yarn build:clean && yarn build:umd && yarn build:esm && yarn build:types",
2929
"on:change": "concurrently \"yarn build:esm\" \"yarn build:types\"",
3030
"prepare": "yarn build:esm",

packages/docsearch-react/src/DocSearchButton.tsx

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,28 @@ export type DocSearchButtonProps = React.DetailedHTMLProps<
88
HTMLButtonElement
99
>;
1010

11-
const ACTION_KEY_DEFAULT = 'Ctrl';
12-
const ACTION_KEY_APPLE = '⌘';
11+
const ACTION_KEY_DEFAULT = 'Ctrl' as const;
12+
const ACTION_KEY_APPLE = '⌘' as const;
1313

14-
function isAppleDevice() {
15-
if (typeof navigator === 'undefined') {
16-
return ACTION_KEY_DEFAULT;
17-
}
14+
function hasNavigator() {
15+
return typeof navigator === 'undefined';
16+
}
1817

18+
function isAppleDevice() {
1919
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
2020
}
2121

2222
export const DocSearchButton = React.forwardRef<
2323
HTMLButtonElement,
2424
DocSearchButtonProps
2525
>((props, ref) => {
26-
const [key, setKey] = useState(() =>
27-
isAppleDevice() ? ACTION_KEY_APPLE : ACTION_KEY_DEFAULT
28-
);
26+
const [key, setKey] = useState<
27+
typeof ACTION_KEY_APPLE | typeof ACTION_KEY_DEFAULT | null
28+
>(null);
2929

3030
useEffect(() => {
31-
if (isAppleDevice()) {
32-
setKey(ACTION_KEY_APPLE);
31+
if (hasNavigator()) {
32+
setKey(isAppleDevice() ? ACTION_KEY_APPLE : ACTION_KEY_DEFAULT);
3333
}
3434
}, []);
3535

@@ -46,12 +46,14 @@ export const DocSearchButton = React.forwardRef<
4646
<span className="DocSearch-Button-Placeholder">Search</span>
4747
</div>
4848

49-
<div className="DocSearch-Button-Keys">
50-
<span className="DocSearch-Button-Key">
51-
{key === ACTION_KEY_DEFAULT ? <ControlKeyIcon /> : key}
52-
</span>
53-
<span className="DocSearch-Button-Key">K</span>
54-
</div>
49+
{key !== null ? (
50+
<div className="DocSearch-Button-Keys">
51+
<span className="DocSearch-Button-Key">
52+
{key === ACTION_KEY_DEFAULT ? <ControlKeyIcon /> : key}
53+
</span>
54+
<span className="DocSearch-Button-Key">K</span>
55+
</div>
56+
) : null}
5557
</button>
5658
);
5759
});

0 commit comments

Comments
 (0)