Lemmmy / KanjiSchool

Alternative web client for WaniKani
https://kanji.school
GNU Affero General Public License v3.0
16 stars 2 forks source link

Prevent space from scrolling when getting an incorrect answer, since it moves onto the next item #11

Closed slice closed 1 year ago

slice commented 1 year ago

This is a major nitpick, but I'd figure to open this issue anyways.

When getting an incorrect answer during reviews, I use the space hotkey to move to the next item. However, this triggers a brief scroll before moving on, which is quite disjointing to my eyes.

I investigated and found this code in SessionQuestionsPage.tsx:

  const onSpace = useCallback((e?: KeyboardEvent) => {
    debug("space pressed");
    if (incorrectAnswer !== undefined) {
      e?.stopPropagation();
      onIncorrectNext();
    }
  }, [incorrectAnswer, onIncorrectNext]);

It makes sense that this only stops propagation instead of dropping the event completely, because I'm assuming that would make pressing space inside of the search box or inside of a user-added synonym or note impossible (?)

What could potentially be done is to check if an <input> or similar element is currently focused, and allow the event to pass if so—otherwise, drop it. Another approach would be passing a ref to the containing page element that actually does the scrolling, and check if it's currently the target of focus.

Lemmmy commented 1 year ago

Thanks for the report! Deploying an update now that should fix it.

It ended up being more involved than expected - the event was triggering on the body element before the GlobalHotkeys managed to get hold of it, so stopping propagation/preventing default inside onSpace didn't make a difference. Instead now I catch all space presses in the session page and cancel it unless it's on an input element.

This solved the problem on my end but the behavior was a little flakey before so let me know if you still run into it/anything similar!

slice commented 1 year ago

Thank you for bothering to fix this (and so quickly too); I hesitated opening this issue in the first place since it seemed like such a frivolous thing, but this genuinely improves my experience a ton! I appreciate it.