Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compose: Add types to useKeyboardShortcut #32168

Merged
merged 2 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,14 @@ throws a warning when using useLayoutEffect in that environment.

Attach a keyboard shortcut handler.

_Related_

- <https://craig.is/killing/mice#api.bind> for information about the `callback` parameter.

_Parameters_

- _shortcuts_ `string[]|string`: Keyboard Shortcuts.
- _callback_ `Function`: Shortcut callback.
- _callback_ `(e: import('mousetrap').ExtendedKeyboardEvent, combo: string) => void`: Shortcut callback.
- _options_ `WPKeyboardShortcutConfig`: Shortcut options.

<a name="useMediaQuery" href="#useMediaQuery">#</a> **useMediaQuery**
Expand Down
1 change: 1 addition & 0 deletions packages/compose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.13.10",
"@types/mousetrap": "^1.6.8",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
"@wordpress/element": "file:../element",
Expand Down
31 changes: 24 additions & 7 deletions packages/compose/src/hooks/use-keyboard-shortcut/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import { useEffect, useRef } from '@wordpress/element';
* @property {boolean} [bindGlobal] Handle keyboard events anywhere including inside textarea/input fields.
* @property {string} [eventName] Event name used to trigger the handler, defaults to keydown.
* @property {boolean} [isDisabled] Disables the keyboard handler if the value is true.
* @property {Object} [target] React reference to the DOM element used to catch the keyboard event.
* @property {import('react').RefObject<HTMLElement>} [target] React reference to the DOM element used to catch the keyboard event.
*/

/**
* Return true if platform is MacOS.
*
* @param {Object} _window window object by default; used for DI testing.
* @param {Window} [_window] window object by default; used for DI testing.
*
* @return {boolean} True if MacOS; false otherwise.
*/
Expand All @@ -37,14 +37,18 @@ function isAppleOS( _window = window ) {
);
}

/* eslint-disable jsdoc/valid-types */
/**
* Attach a keyboard shortcut handler.
*
* @param {string[]|string} shortcuts Keyboard Shortcuts.
* @param {Function} callback Shortcut callback.
* @param {WPKeyboardShortcutConfig} options Shortcut options.
* @see https://craig.is/killing/mice#api.bind for information about the `callback` parameter.
*
* @param {string[]|string} shortcuts Keyboard Shortcuts.
* @param {(e: import('mousetrap').ExtendedKeyboardEvent, combo: string) => void} callback Shortcut callback.
* @param {WPKeyboardShortcutConfig} options Shortcut options.
*/
function useKeyboardShortcut(
/* eslint-enable jsdoc/valid-types */
shortcuts,
callback,
{
Expand All @@ -63,7 +67,14 @@ function useKeyboardShortcut(
if ( isDisabled ) {
return;
}
const mousetrap = new Mousetrap( target ? target.current : document );
const mousetrap = new Mousetrap(
target && target.current
? target.current
: // We were passing `document` here previously, so to successfully cast it to Element we must cast it first to `unknown`.
// Not sure if this is a mistake but it was the behavior previous to the addition of types so we're just doing what's
// necessary to maintain the existing behavior
/** @type {Element} */ (/** @type {unknown} */ ( document ))
);
castArray( shortcuts ).forEach( ( shortcut ) => {
const keys = shortcut.split( '+' );
// Determines whether a key is a modifier by the length of the string.
Expand All @@ -87,9 +98,15 @@ function useKeyboardShortcut(
}

const bindFn = bindGlobal ? 'bindGlobal' : 'bind';
// @ts-ignore `bindGlobal` is an undocumented property
mousetrap[ bindFn ](
shortcut,
( ...args ) => currentCallback.current( ...args ),
(
/* eslint-disable jsdoc/valid-types */
/** @type {[e: import('mousetrap').ExtendedKeyboardEvent, combo: string]} */ ...args
) =>
/* eslint-enable jsdoc/valid-types */
currentCallback.current( ...args ),
eventName
);
} );
Expand Down
3 changes: 2 additions & 1 deletion packages/compose/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
"src/hooks/use-async-list/**/*",
"src/hooks/use-constrained-tabbing/**/*",
"src/hooks/use-debounce/**/*",
"src/hooks/use-focus-return/**/*",
"src/hooks/use-instance-id/**/*",
"src/hooks/use-isomorphic-layout-effect/**/*",
"src/hooks/use-focus-return/**/*",
"src/hooks/use-keyboard-shortcut/**/*",
"src/hooks/use-previous/**/*",
"src/hooks/use-media-query/**/*",
"src/hooks/use-reduced-motion/**/*",
Expand Down