From c4efe749208e854dc9ced5bdd00933269d5b4382 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Sat, 5 Apr 2025 17:23:12 +0200 Subject: [PATCH] Improve TSDoc comments for `type UseKeyPressOptions` and `useKeyPress` hook --- .changeset/twelve-windows-search.md | 5 +++++ packages/react/src/hooks/useKeyPress.ts | 29 ++++++++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 .changeset/twelve-windows-search.md diff --git a/.changeset/twelve-windows-search.md b/.changeset/twelve-windows-search.md new file mode 100644 index 00000000..cc8b74c7 --- /dev/null +++ b/.changeset/twelve-windows-search.md @@ -0,0 +1,5 @@ +--- +'@xyflow/react': patch +--- + +Improve TSDoc comments for `type UseKeyPressOptions` and `useKeyPress` hook diff --git a/packages/react/src/hooks/useKeyPress.ts b/packages/react/src/hooks/useKeyPress.ts index ec12e562..d7e022fa 100644 --- a/packages/react/src/hooks/useKeyPress.ts +++ b/packages/react/src/hooks/useKeyPress.ts @@ -6,7 +6,16 @@ type PressedKeys = Set; type KeyOrCode = 'key' | 'code'; export type UseKeyPressOptions = { + /** + * You may want to listen to key presses on a specific element. This field lets you configure + * that! + * @default document + */ target?: Window | Document | HTMLElement | ShadowRoot | null; + /** + * You can use this flag to prevent triggering the key press hook when an input field is focused. + * @default true + */ actInsideInputWithModifier?: boolean; preventDefault?: boolean; }; @@ -18,9 +27,7 @@ const defaultDoc = typeof document !== 'undefined' ? document : null; * currently pressed or not. * * @public - * @param param.keyCode - The key code (string or array of strings) to use - * @param param.options - Options - * @returns boolean + * @param options - Options * * @example * ```tsx @@ -40,11 +47,17 @@ const defaultDoc = typeof document !== 'undefined' ? document : null; *``` */ export function useKeyPress( - /* - * the keycode can be a string 'a' or an array of strings ['a', 'a+d'] - * a string means a single key 'a' or a combination when '+' is used 'a+d' - * an array means different possibilites. Explainer: ['a', 'd+s'] here the - * user can use the single key 'a' or the combination 'd' + 's' + /** + * The key code (string or array of strings) specifies which key(s) should trigger + * an action. + * + * A **string** can represent: + * - A **single key**, e.g. `'a'` + * - A **key combination**, using `'+'` to separate keys, e.g. `'a+d'` + * + * An **array of strings** represents **multiple possible key inputs**. For example, `['a', 'd+s']` + * means the user can press either the single key `'a'` or the combination of `'d'` and `'s'`. + * @default null */ keyCode: KeyCode | null = null, options: UseKeyPressOptions = { target: defaultDoc, actInsideInputWithModifier: true }