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.

[Select] Inability to stop Escape keyDown propagation

See original GitHub issue

Bug report

If there’s a keydown handler defined above select component, there’s no way to prevent event propagation when Escape is pressed but keep closing the select as expected.

See reproducible CodeSandbox example

Current Behavior

The only way to prevent event propagation is to define

<Box onKeyDown={(e) => {
   if (e.key === "Escape") {
      console.log("this should not be called");
   }
}}>
  <Select>
    ...
    <SelectContent
       onKeyDown={(e) => {
          if (e.key === "Escape") {
            // propagation stopped and Box onKeyDown is not called
            // but Select is not closed either
            e.stopPropagation();
          }
        }}
    />
    ...
  </Select>
</Box>

but it breaks “close select when Esc is pressed” fuctionality.

Expected behavior

I can prevent Box onKeyDown event from being called, but still keep closing Select when Esc is pressed.

Reproducible example

CodeSandbox Example

Your environment

Software Name(s) Version
Radix Package(s) react-select 0.1.1
React n/a 17.x
Browser Chrome

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
suevalovcommented, Apr 5, 2022

So, I solved the problem with a generic attribute that I added to SelectContent and SelectOption components, this an event is targeted from these elements it gets skipped in a global shortcut handler.

So, the new Select is coming to Linear. We already use Tabs component and more to come. Thanks for an amazing library.

1reaction
suevalovcommented, Apr 4, 2022

@benoitgrelard these are good ideas, thanks for sharing. I’ll explore these possibilities

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to prevent propagation of the same event in different ...
You have different event which are being called in both the component, I am unable to understand what causes the issue if you...
Read more >
When to actually use preventDefault(), stopPropagation(), and ...
Calling preventDefault() in a 'keydown' event handler tells the browser you don't want the browser to do the default action. The browser is...
Read more >
Element: keydown event - Web APIs | MDN
Unlike the deprecated keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value.
Read more >
Working with Events - StimulusReflex
Similar to keydown , keypress returns the previous value when you access event.target.value . However, it only fires for keys that product a...
Read more >
Keyboard: keydown and keyup - The Modern JavaScript Tutorial
So, event.code may match a wrong character for unexpected layout. Same letters in different layouts may map to different physical keys, leading ...
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