-
Notifications
You must be signed in to change notification settings - Fork 118
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
Enable hotkey on INPUT/SELECT/TEXTAREA elements #146
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,29 @@ | ||
import hotkeys, {HotkeysEvent} from 'hotkeys-js'; | ||
import {useCallback, useEffect} from "react"; | ||
import hotkeys, { HotkeysEvent } from 'hotkeys-js'; | ||
import { useCallback, useEffect } from 'react'; | ||
|
||
type CallbackFn = (event: KeyboardEvent, handler: HotkeysEvent) => void; | ||
type TagNames = 'INPUT' | 'TEXTAREA' | 'SELECT'; | ||
|
||
export function useHotkeys(keys: string, callback: CallbackFn, deps: any[] = []) { | ||
export function useHotkeys( | ||
keys: string, | ||
callback: CallbackFn, | ||
deps: any[] = [], | ||
tagNames?: TagNames[], | ||
) { | ||
const memoisedCallback = useCallback(callback, deps); | ||
|
||
useEffect(() => { | ||
// Enable hotkeys for INPUT/SELECT/TEXTAREA elements | ||
// https://github.com/jaywcjlove/hotkeys#filter | ||
hotkeys.filter = ({ target, srcElement }) => { | ||
if (tagNames) { | ||
const targetTagName = (target && target.tagName) || (srcElement && srcElement.tagName); | ||
return Boolean(targetTagName && tagNames.includes(targetTagName as TagNames)); | ||
} | ||
return false; | ||
}; | ||
hotkeys(keys, memoisedCallback); | ||
|
||
return () => hotkeys.unbind(keys); | ||
return () => hotkeys.unbind(keys, memoisedCallback); | ||
}, [memoisedCallback]); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure it makes sense to enforce this specific filter implementation for only specific tag names - it's not very extendable. Imo, it would be better to allow the user to pass in their own filter implementation, so they had more customization.
If we don't want an options object passed in yet, you could have the user pass in the filter function themselves and set the hotkeys.filter if it's passed in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the filter docs it is only used on these three tags.
I'm not sure what other custom filter option you would pass in instead of those tags
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm usually a fan of keeping things more extensible and letting end users implement how they want. In this case, if the internal filter implementation of hotkeys changes, or they add other tags that are filterable in the future, then this code would need to be maintained. But you're right, currently this would work.
I'll leave it up to @JohannesKlauss on what he thinks is best!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think allowing to pass a complete config to the actual hotkeys implementation makes a lot of sense, because it gives the end users full flexibility over how they want the hotkeys package to work.
Regarding on what this package should be I am not real sure. It started out as a tiny wrapper but people are using it extensively and I think it makes sense to give users the options to have a flexible usage if they want to.
What I am thinking about is whether we should move on with this simple implementation for a 1.5.4 release and think about a better and hotkeys complete param API for a version 2 or do a complete feature set for hotkeys manipulation in a 1.6 release without changing the params api of the hook.
What I would have in mind is for a v2 using just a typed out object as first parameter and the
deps
array as second parameter. This first object would also include all the hotkeys options.Or for a 1.6 release we could keep all those params and just replace the new
tagNames
param with anoptions
object for hotkeys.I like the v2 approach more but it would be a breaking change. What do you guys think about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally like both of these suggestions. Adding the options now would also allow for consumers to get used to the options concept before making the breaking change, while still being able to flesh it out more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this fell off all our radars, but I just remembered it today while working on some hotkeys stuff :)
@JohannesKlauss, I sent out another PR switching to the 1.6 options implementation discussed above: #219