-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move shortcut helpers to its own separate module
- Loading branch information
1 parent
2aeb7a1
commit 5110ec8
Showing
5 changed files
with
67 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { KeyboardEventHandler } from "react"; | ||
|
||
type ShorcutConfiguration = { | ||
modifiers?: Partial< | ||
Pick<KeyboardEvent, "altKey" | "metaKey" | "ctrlKey" | "shiftKey"> | ||
>; | ||
key: string; | ||
}; | ||
|
||
export function createShortcutHandler(options: ShorcutConfiguration) { | ||
return function handler( | ||
callback: KeyboardEventHandler, | ||
): KeyboardEventHandler { | ||
return (e) => { | ||
if (e.repeat || isEditableElement(e.target)) { | ||
// Either This event was already handled (user is holding the keys) | ||
// Or the user is editing some text, so we do nothing. | ||
return; | ||
} | ||
if (!options.modifiers && options.key === e.code) { | ||
callback(e); | ||
return; | ||
} | ||
|
||
if (!options.modifiers) { | ||
return; | ||
} | ||
|
||
const modifierKeys = Object.keys(options.modifiers) as Array< | ||
keyof typeof options.modifiers | ||
>; | ||
for (const key of modifierKeys) { | ||
if (e[key] !== options.modifiers[key]) { | ||
return; | ||
} | ||
} | ||
if (options.key === e.code) { | ||
callback(e); | ||
} | ||
}; | ||
}; | ||
} | ||
|
||
function isEditableElement(target: EventTarget): boolean { | ||
if (!(target instanceof HTMLElement)) { | ||
return false; | ||
} | ||
return ( | ||
target.isContentEditable || | ||
target.tagName === "INPUT" || | ||
target.tagName === "TEXTAREA" | ||
); | ||
} | ||
|
||
export function registerShortcuts( | ||
...handlers: KeyboardEventHandler[] | ||
): KeyboardEventHandler { | ||
return (e) => { | ||
for (const handler of handlers) { | ||
handler(e); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters