Skip to content

Commit 41dca37

Browse files
committed
feat: add keypress effect to DocSearchButton
1 parent d00f5c4 commit 41dca37

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

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: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,55 @@ export const DocSearchButton = React.forwardRef<
5151
<span className="DocSearch-Button-Keys">
5252
{key !== null && (
5353
<>
54-
<kbd className="DocSearch-Button-Key">
54+
<DocSearchButtonKey reactsToKey={key === ACTION_KEY_DEFAULT ? ACTION_KEY_DEFAULT : 'Meta'}>
5555
{key === ACTION_KEY_DEFAULT ? <ControlKeyIcon /> : key}
56-
</kbd>
57-
<kbd className="DocSearch-Button-Key">K</kbd>
56+
</DocSearchButtonKey>
57+
<DocSearchButtonKey reactsToKey='k'>K</DocSearchButtonKey>
5858
</>
5959
)}
6060
</span>
6161
</button>
6262
);
6363
});
64+
65+
type DocSearchButtonKeyProps = {
66+
reactsToKey?: string;
67+
}
68+
69+
function DocSearchButtonKey({ reactsToKey, children}: React.PropsWithChildren<DocSearchButtonKeyProps>) {
70+
const [isKeyDown, setIsKeyDown] = useState(false)
71+
72+
useEffect(() => {
73+
if (!reactsToKey) {
74+
return undefined;
75+
}
76+
77+
function handleKeyDown(e: KeyboardEvent) {
78+
if (e.key === reactsToKey) {
79+
setIsKeyDown(true)
80+
}
81+
}
82+
83+
function handleKeyUp(e: KeyboardEvent) {
84+
if (e.key === reactsToKey ||
85+
// keyup doesn't fire when Command is held down,
86+
// workaround is to mark key as also released when Command is released
87+
// See https://stackoverflow.com/a/73419500
88+
e.key === 'Meta') {
89+
setIsKeyDown(false)
90+
}
91+
}
92+
93+
window.addEventListener('keydown', handleKeyDown)
94+
window.addEventListener('keyup', handleKeyUp)
95+
96+
return () => {
97+
window.removeEventListener('keydown', handleKeyDown)
98+
window.removeEventListener('keyup', handleKeyUp)
99+
}
100+
}, [reactsToKey])
101+
102+
return (
103+
<kbd className={isKeyDown ? "DocSearch-Button-Key DocSearch-Button-Key--pressed" : "DocSearch-Button-Key"}>{children}</kbd>
104+
)
105+
}

0 commit comments

Comments
 (0)