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

Allow keyboard shortcuts #10713

Merged
merged 33 commits into from Oct 22, 2020
Merged

Allow keyboard shortcuts #10713

merged 33 commits into from Oct 22, 2020

Conversation

Link2Twenty
Copy link
Contributor

@Link2Twenty Link2Twenty commented Oct 7, 2020

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

Add handler for keyboard shortcuts. This allows custom shortcuts to be set up on a page by page (or even component by component) basis.

React demo

Usage

In this example we're going to run a function when the user presses CTRL+SHIFT+P

const shortcuts = {
  "ctrl+shift+KeyP":  (e) => {
    e.preventDefault();
    someFunction();
  }
}

<KeyboardShortcuts shortcuts={shortcuts} />

Related Tickets & Documents

related: #5023 #596

QA Instructions, Screenshots, Recordings

This facilitates changes in the future

Added tests?

  • yes
  • no, because they aren't needed
  • no, because I need help

Added to documentation?

  • docs.forem.com
  • readme
  • no documentation needed

[optional] Are there any post deployment tasks we need to perform?

Once this is merged I'd like to modify this current shortcut to use this method before thinking about what other shortcuts could/should be added.

[optional] What gif best describes this PR or how it makes you feel?

seeing the code

Add handler for keyboard shortcuts
@pr-triage pr-triage bot added the PR: unreviewed bot applied label for PR's with no review label Oct 7, 2020
@rhymes
Copy link
Contributor

rhymes commented Oct 7, 2020

Hi @Link2Twenty, interesting! What if we integrate https://github.com/github/hotkey ?

It'd be great if we'd eventually work towards a keyboard shortcuts modal populated by the registered shortcuts on each page, something like this:

Screenshot_2020-10-07 Allow keyboard shortcuts by Link2Twenty · Pull Request #10713 · forem forem

Makes objects clearer.
@Link2Twenty
Copy link
Contributor Author

Link2Twenty commented Oct 7, 2020

It looks like hotkeys is only for interacting with specific elements. Whereas this way you can call any function you like including, but not limited to, dom interactions.

I liked how they registered their shortcuts, was much cleaner than my way so I've borrowed that.

@reobin
Copy link
Contributor

reobin commented Oct 7, 2020

Super clean @Link2Twenty !

I've implemented something similar in #10468 that is called KeyEventListener.

It would be nice to merge our efforts to go in the right direction with forem native shortcuts. My component doesn't handle key modifiers like ctrl and shift, which I think is a really nice touch.

I've already implemented KeyEventListener in a couple of places in the PR like the / and 0 shortcuts for the header, as well as j and k navigation for the article feed if you want to take a look.

Tell me what you think, feedback is appreciated! 🌞

Then, as @rhymes said, we could create an issue to work on a store that is fed by the calls to that very component. With that store, build a modal that lists all the available shortcuts for a given page.

update to allow single character shortcuts/hotkeys
@Link2Twenty
Copy link
Contributor Author

Thanks @reobin 😊 I've updated my code to allow it to cope with single key shortcuts now, similar to hotkeys.

@nickytonline is this the sort of direction you'd like to see this go? If so we could get this merged then Reobin and I could look at using this in #10468

@citizen428
Copy link
Contributor

@Link2Twenty I really like the work you and @reobin are doing on this! 😄

@ludwiczakpawel ludwiczakpawel requested review from a team and removed request for ludwiczakpawel October 8, 2020 07:01
Add inline documentation for the component.
@nickytonline
Copy link
Contributor

Just got out of the stream. Taking a peek now @Link2Twenty.

Copy link
Contributor

@nickytonline nickytonline left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Link2Twenty, thanks for the PR! This is looking good. Just some questions.

app/javascript/shared/components/keyboardShortcuts.jsx Outdated Show resolved Hide resolved
app/javascript/shared/components/keyboardShortcuts.jsx Outdated Show resolved Hide resolved
@nickytonline
Copy link
Contributor

Thanks @reobin 😊 I've updated my code to allow it to cope with single key shortcuts now, similar to hotkeys.

@nickytonline is this the sort of direction you'd like to see this go? If so we could get this merged then Reobin and I could look at using this in #10468

@Link2Twenty I think going with use effect makes sense. I made a suggestion to consider making what you're proposing a custom hook. See PR review comments.

Update 10 minutes after writing first paragraph.😄 I saw @reobin's PR is using a custom hook, https://github.com/forem/forem/pull/10468/files#diff-976b57823cb7a305a55921032b2708aeR45, so I think a custom hook with useEffect is the direction to go.

@Link2Twenty
Copy link
Contributor Author

@nickytonline any further thought or are we good to go?

Copy link
Contributor

@nickytonline nickytonline left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good @Link2Twenty. I have some suggestions, but they do not block the PR.

One blocker though is we need some tests. If you need to see an example of testing hooks, take a peek at https://github.com/forem/forem/blob/master/app/javascript/utilities/__tests__/dragAndDrop.test.js and if you have any questions, let me know.

@pr-triage pr-triage bot removed the PR: unreviewed bot applied label for PR's with no review label Oct 13, 2020
Copy link
Contributor

@nickytonline nickytonline left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for being patient as there was a lot of back and forth and thanks so much for the PR @Link2Twenty! 🚀

@pr-triage pr-triage bot added PR: partially-approved bot applied label for PR's where a single reviewer approves changes and removed PR: unreviewed bot applied label for PR's with no review labels Oct 15, 2020
@nickytonline nickytonline requested a review from a team October 15, 2020 21:02
@reobin
Copy link
Contributor

reobin commented Oct 15, 2020

Good job @Link2Twenty !

It seems stable enough. I'll start integrating this in #10468 some time tonight

@Link2Twenty
Copy link
Contributor Author

@nickytonline thanks for the help hopefully I'll have a better understanding of tests now 😅

@reobin thank you, sorry for the hold up 😬

@reobin
Copy link
Contributor

reobin commented Oct 15, 2020

Quick question @nickytonline @Link2Twenty : is shared/components/ the right place for this hook? Asking because I'll put my list navigation component in the same place.

Edit: Was thinking more about utilities/ at first

@nickytonline
Copy link
Contributor

Quick question @nickytonline @Link2Twenty : is shared/components the right place for this hook? Asking because I'll put my list navigation component in the same place.

@reobin, for common components shared/components is good place. We might revise this in the future, but for now, pop it there.

@Link2Twenty
Copy link
Contributor Author

Just realised a problem with using event.code rather than event.key.toLowerCase()
In the uk the @ symbol would appear at shift+Quote but in the US it would be shift+Digit2.
I'm going to update the code so it can use either, because we may want crtl+Digit2 to do something but ctrl+Numpad2 to not. @reobin you shouldn't need to update what you've done I don't think.

@pr-triage pr-triage bot added PR: unreviewed bot applied label for PR's with no review and removed PR: partially-approved bot applied label for PR's where a single reviewer approves changes labels Oct 16, 2020
@reobin
Copy link
Contributor

reobin commented Oct 16, 2020

Pulled the changes and all is well 👍

@Link2Twenty
Copy link
Contributor Author

Hey @Ridhwana any progress on this? 😊

(no rush, of course, just curious)

Copy link
Contributor

@Ridhwana Ridhwana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me 🚀

@pr-triage pr-triage bot added PR: partially-approved bot applied label for PR's where a single reviewer approves changes and removed PR: unreviewed bot applied label for PR's with no review labels Oct 22, 2020
@rhymes rhymes merged commit b7dfb24 into forem:master Oct 22, 2020
@pr-triage pr-triage bot added PR: merged bot applied label for PR's that are merged and removed PR: partially-approved bot applied label for PR's where a single reviewer approves changes labels Oct 22, 2020
@Link2Twenty Link2Twenty deleted the patch-2 branch October 22, 2020 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: merged bot applied label for PR's that are merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants