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

Fix: Update documentation #1043

Merged
merged 1 commit into from
Jun 23, 2023
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
50 changes: 23 additions & 27 deletions documentation/docs/api/use-hotkeys.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -178,37 +178,14 @@ type Options = {

#### Properties

##### `filter`

```ts
filter: (event: KeyboardEvent) => boolean // default: undefined
```

Pass a function `filter` to determine if the callback should get triggered. Return `false` to __prevent__ the execution
of the callback and `true` to __allow__ the callback to be triggered.

##### `filterPreventDefault`
##### `enabled`

```ts
filterPreventDefault: boolean // default: true
enabled: boolean | ((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => boolean) // default: true
```

This flag determines if the default browser behavior should be prevented if the given `filter` function returns `false` and
therefore prevents the execution of the callback. `true` is the default value, so each time the filter blocks the callback
execution, the browser won't proceed with its default behavior. Setting this to `false` will allow the browser to proceed
with the default behavior.

A good example for this behavior is the override of `meta+s`, which normally triggers a save page dialog
inside the browser.

```js
useHotkeys('meta+s', someCallback, {
// This will allow the browser to show the save page dialog.
// Setting it to true will prevent that.
filterPreventDefault: false,
filter: () => false
});
```
Determines if the callback should get triggered. Return `false` to __prevent__ the execution
of the callback and `true` to __allow__ the callback to be triggered. You can also pass a function that returns a boolean.

##### `enableOnFormTags`

Expand Down Expand Up @@ -292,6 +269,25 @@ useHotkeys('a', () => someCallback, {

:::

##### `preventDefault`

```ts
preventDefault: boolean | ((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => boolean) // default: false
```

This flag determines if the default browser behavior should be prevented. `false` is the default value, so the browser
will proceed with its default behavior. Setting this to `true` will prevent __some__ of the default browser behavior.

A good example for this behavior is the override of `meta+s`, which normally triggers a save page dialog
inside the browser.

```js
useHotkeys('meta+s', someCallback, {
// This will prevent the browser from showing the save page dialog
preventDefault: true,
});
```

##### `document`

if our React app uses iframes, we can pass the `document` object of the iframe to the hook. This way the hook will bind
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Of course, we also want to leverage more complex keystrokes. `useHotkeys` suppor
* `alt`
* `ctrl`
* `meta`
* `mod` (which listens for `ctrl` on Windows/Linux and `cmd` on macOS)

:::info macOS and Windows/Linux compatability
Since version 4 `alt` and `option` are identical. The `meta` key is the same as `cmd` on macOS and `os` key on Windows.
Expand Down
2 changes: 1 addition & 1 deletion src/useHotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function useHotkeys<T extends HTMLElement>(
: !(dependencies instanceof Array)
? (dependencies as Options)
: undefined
const _keys: string = isReadonlyArray(keys ) ? keys.join(_options?.splitKey) : keys
const _keys: string = isReadonlyArray(keys) ? keys.join(_options?.splitKey) : keys
const _deps: DependencyList | undefined =
options instanceof Array ? options : dependencies instanceof Array ? dependencies : undefined

Expand Down
5 changes: 4 additions & 1 deletion src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export function isKeyboardEventTriggeredByInput(ev: KeyboardEvent): boolean {
return isHotkeyEnabledOnTag(ev, ['input', 'textarea', 'select'])
}

export function isHotkeyEnabledOnTag({ target }: KeyboardEvent, enabledOnTags: readonly FormTags[] | boolean = false): boolean {
export function isHotkeyEnabledOnTag(
{ target }: KeyboardEvent,
enabledOnTags: readonly FormTags[] | boolean = false
): boolean {
const targetTagName = target && (target as HTMLElement).tagName

if (isReadonlyArray(enabledOnTags)) {
Expand Down