diff --git a/bundlesize.config.json b/bundlesize.config.json index ded1b0685..bd079c40b 100644 --- a/bundlesize.config.json +++ b/bundlesize.config.json @@ -6,11 +6,11 @@ }, { "path": "packages/docsearch-react/dist/umd/index.js", - "maxSize": "22.63 kB" + "maxSize": "22.80 kB" }, { "path": "packages/docsearch-js/dist/umd/index.js", - "maxSize": "30.50 kB" + "maxSize": "30.70 kB" } ] } diff --git a/packages/docsearch-css/src/_variables.css b/packages/docsearch-css/src/_variables.css index d59766d95..83b97e901 100644 --- a/packages/docsearch-css/src/_variables.css +++ b/packages/docsearch-css/src/_variables.css @@ -38,7 +38,7 @@ ); --docsearch-key-shadow: inset 0 -2px 0 0 rgb(205, 205, 230), inset 0 0 1px 1px #fff, 0 1px 2px 1px rgba(30, 35, 90, 0.4); - + --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); /* footer */ --docsearch-footer-height: 44px; --docsearch-footer-background: #fff; @@ -66,6 +66,7 @@ html[data-theme='dark'] { ); --docsearch-key-shadow: inset 0 -2px 0 0 rgb(40, 45, 85), inset 0 0 1px 1px rgb(81, 87, 125), 0 2px 2px 0 rgba(3, 4, 9, 0.3); + --docsearch-key-pressed-shadow: inset 0 -2px 0 0 #282d55, inset 0 0 1px 1px #51577d, 0 1px 1px 0 #0304094d; --docsearch-footer-background: rgb(30, 33, 54); --docsearch-footer-shadow: inset 0 1px 0 0 rgba(73, 76, 106, 0.5), 0 -4px 8px 0 rgba(0, 0, 0, 0.2); diff --git a/packages/docsearch-css/src/button.css b/packages/docsearch-css/src/button.css index d8577aed7..54da831f0 100644 --- a/packages/docsearch-css/src/button.css +++ b/packages/docsearch-css/src/button.css @@ -63,6 +63,11 @@ width: 20px; } +.DocSearch-Button-Key--pressed { + transform: translate3d(0, 1px, 0); + box-shadow: var(--docsearch-key-pressed-shadow); +} + @media (max-width: 768px) { .DocSearch-Button-Keys, .DocSearch-Button-Placeholder { diff --git a/packages/docsearch-react/src/DocSearchButton.tsx b/packages/docsearch-react/src/DocSearchButton.tsx index 57d62347f..0c8eaebbd 100644 --- a/packages/docsearch-react/src/DocSearchButton.tsx +++ b/packages/docsearch-react/src/DocSearchButton.tsx @@ -51,13 +51,72 @@ export const DocSearchButton = React.forwardRef< {key !== null && ( <> - + {key === ACTION_KEY_DEFAULT ? : key} - - K + + K )} ); }); + +type DocSearchButtonKeyProps = { + reactsToKey?: string; +}; + +function DocSearchButtonKey({ + reactsToKey, + children, +}: React.PropsWithChildren) { + const [isKeyDown, setIsKeyDown] = useState(false); + + useEffect(() => { + if (!reactsToKey) { + return undefined; + } + + function handleKeyDown(e: KeyboardEvent) { + if (e.key === reactsToKey) { + setIsKeyDown(true); + } + } + + function handleKeyUp(e: KeyboardEvent) { + if ( + e.key === reactsToKey || + // keyup doesn't fire when Command is held down, + // workaround is to mark key as also released when Command is released + // See https://stackoverflow.com/a/73419500 + e.key === 'Meta' + ) { + setIsKeyDown(false); + } + } + + window.addEventListener('keydown', handleKeyDown); + window.addEventListener('keyup', handleKeyUp); + + return () => { + window.removeEventListener('keydown', handleKeyDown); + window.removeEventListener('keyup', handleKeyUp); + }; + }, [reactsToKey]); + + return ( + + {children} + + ); +}