Skip to content

Commit 4c82640

Browse files
feat: add keypress effect to DocSearchButton (#2087)
* feat: add keypress effect to DocSearchButton * Bump expected bundle sizes --------- Co-authored-by: Paul Jankowski <[email protected]> Co-authored-by: Paul Jankowski <[email protected]>
1 parent 5771deb commit 4c82640

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

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": "22.63 kB"
9+
"maxSize": "22.80 kB"
1010
},
1111
{
1212
"path": "packages/docsearch-js/dist/umd/index.js",
13-
"maxSize": "30.50 kB"
13+
"maxSize": "30.70 kB"
1414
}
1515
]
1616
}

packages/docsearch-css/src/_variables.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
);
3939
--docsearch-key-shadow: inset 0 -2px 0 0 rgb(205, 205, 230),
4040
inset 0 0 1px 1px #fff, 0 1px 2px 1px rgba(30, 35, 90, 0.4);
41-
41+
--docsearch-key-pressed-shadow: inset 0 -2px 0 0 #cdcde6,inset 0 0 1px 1px #fff,0 1px 1px 0 rgba(30,35,90,0.4);
4242
/* footer */
4343
--docsearch-footer-height: 44px;
4444
--docsearch-footer-background: #fff;
@@ -66,6 +66,7 @@ html[data-theme='dark'] {
6666
);
6767
--docsearch-key-shadow: inset 0 -2px 0 0 rgb(40, 45, 85),
6868
inset 0 0 1px 1px rgb(81, 87, 125), 0 2px 2px 0 rgba(3, 4, 9, 0.3);
69+
--docsearch-key-pressed-shadow: inset 0 -2px 0 0 #282d55, inset 0 0 1px 1px #51577d, 0 1px 1px 0 #0304094d;
6970
--docsearch-footer-background: rgb(30, 33, 54);
7071
--docsearch-footer-shadow: inset 0 1px 0 0 rgba(73, 76, 106, 0.5),
7172
0 -4px 8px 0 rgba(0, 0, 0, 0.2);

packages/docsearch-css/src/button.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
width: 20px;
6464
}
6565

66+
.DocSearch-Button-Key--pressed {
67+
transform: translate3d(0, 1px, 0);
68+
box-shadow: var(--docsearch-key-pressed-shadow);
69+
}
70+
6671
@media (max-width: 768px) {
6772
.DocSearch-Button-Keys,
6873
.DocSearch-Button-Placeholder {

packages/docsearch-react/src/DocSearchButton.tsx

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,72 @@ export const DocSearchButton = React.forwardRef<
5151
<span className="DocSearch-Button-Keys">
5252
{key !== null && (
5353
<>
54-
<kbd className="DocSearch-Button-Key">
54+
<DocSearchButtonKey
55+
reactsToKey={
56+
key === ACTION_KEY_DEFAULT ? ACTION_KEY_DEFAULT : 'Meta'
57+
}
58+
>
5559
{key === ACTION_KEY_DEFAULT ? <ControlKeyIcon /> : key}
56-
</kbd>
57-
<kbd className="DocSearch-Button-Key">K</kbd>
60+
</DocSearchButtonKey>
61+
<DocSearchButtonKey reactsToKey="k">K</DocSearchButtonKey>
5862
</>
5963
)}
6064
</span>
6165
</button>
6266
);
6367
});
68+
69+
type DocSearchButtonKeyProps = {
70+
reactsToKey?: string;
71+
};
72+
73+
function DocSearchButtonKey({
74+
reactsToKey,
75+
children,
76+
}: React.PropsWithChildren<DocSearchButtonKeyProps>) {
77+
const [isKeyDown, setIsKeyDown] = useState(false);
78+
79+
useEffect(() => {
80+
if (!reactsToKey) {
81+
return undefined;
82+
}
83+
84+
function handleKeyDown(e: KeyboardEvent) {
85+
if (e.key === reactsToKey) {
86+
setIsKeyDown(true);
87+
}
88+
}
89+
90+
function handleKeyUp(e: KeyboardEvent) {
91+
if (
92+
e.key === reactsToKey ||
93+
// keyup doesn't fire when Command is held down,
94+
// workaround is to mark key as also released when Command is released
95+
// See https://stackoverflow.com/a/73419500
96+
e.key === 'Meta'
97+
) {
98+
setIsKeyDown(false);
99+
}
100+
}
101+
102+
window.addEventListener('keydown', handleKeyDown);
103+
window.addEventListener('keyup', handleKeyUp);
104+
105+
return () => {
106+
window.removeEventListener('keydown', handleKeyDown);
107+
window.removeEventListener('keyup', handleKeyUp);
108+
};
109+
}, [reactsToKey]);
110+
111+
return (
112+
<kbd
113+
className={
114+
isKeyDown
115+
? 'DocSearch-Button-Key DocSearch-Button-Key--pressed'
116+
: 'DocSearch-Button-Key'
117+
}
118+
>
119+
{children}
120+
</kbd>
121+
);
122+
}

0 commit comments

Comments
 (0)