question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

💡 Proposal - useKeyboard hook in f36-utils

See original GitHub issue

The problem

I see in our code base we have many approaches devs takes to listen to keyboard events and detect keycode, …etc.

const handleKeyUp = (e: React.KeyboardEvent) => {
    if (KeyCodes.esc(e)) {
      hidePanel();
    }
 };
document.addEventListener('keyup', (handleKeyUp as unknown) as EventListener);
return () => document.removeEventListener('keyup', (handleKeyUp as unknown) as EventListener);

That I believe we can make simpler, and since it’s considered as interactions, I believe it would fit well in F36

The proposed solution

A simple hook that accepts a reference or a document, adds/remove the events on mount and unmount, and finally emits callback functions

Also maybe some predefined ones for what we think is the most keyboard buttons we need to listen to?

const { onEsc, onEnter, onKey } = useKeyboard(ref | document);

onEsc(() => alert('👹'))

onKey('h', () => alert('hello'))

Common A11y Keyboard interactions

See full list https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/Grid_Role#keyboard_interactions

Key Action
Moves focus to the right.
Moves focus to the left.
Moves focus down.
Moves focus up.
Page Down Moves focus down an author-determined number of rows.
Page Up Moves focus up an author-determined number of rows.
Home Moves focus to the first cell in the row that contains focus.
End Moves focus to the last cell in the row that contains focus.
ctrl + Home Moves focus to the first cell in the first row.
ctrl + End Moves focus to the last cell in the last row.

Or

Key combination Action
ctrl + Space Select the column that contains the focus.
shift + Space Selects the row that contains the focus.
ctrl + A Selects all cells.
shift + → Extends selection one cell to the right.
shift + ← Extends selection one cell to the left.
shift + ↓ Extends selection one cell down.
shift + ↑ Extends selection one cell up.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
suevalovcommented, Mar 19, 2021
1reaction
gui-santoscommented, Mar 19, 2021

I like it a lot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-native-community/hooks - GitHub
React Native APIs turned into React Hooks allowing you to access asynchronous APIs directly in your functional components. Note: You must use React...
Read more >
useKeyboard Hook Example - Expo Snack
useKeyboard hook listening to keyboard events.
Read more >
A Proposal for an Alternative Design for Hooks - paulgray.net
An Alternative API for hooks based on functional programming patterns.
Read more >
Introducing Hooks - React
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. import React,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found