react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

react-aria-components Button interferes with global keyboard shortcut handlers

Open Talor-A opened this issue 1 year ago โ€ข 4 comments

Provide a general summary of the issue here

I'm attaching a global cmd+enter hotkey on our webapp, but switching over to react-aria-components is interfering with it.

๐Ÿค” Expected Behavior?

see the live repro with code below. I'd expect that pressing cmd+enter while focus is on <Button /> will act the same as when focus is on <button />.

๐Ÿ˜ฏ Current Behavior

focus on <Button />:

  1. hold cmd: nothing
  2. press down enter: global keybind triggers, counter is reset
  3. release enter: nothing
  4. release cmd: onPress is triggered, counter is incremented

focus on native <button />:

  1. hold cmd: nothing
  2. press down enter: global keybind triggers, counter is reset
  3. release enter: nothing
  4. release cmd: nothing

๐Ÿ’ Possible Solution

No response

๐Ÿ”ฆ Context

we're using react-aria-components on replit.com, and our main "run" action is bound to cmd+enter. clicking a <Button /> and then trying to run your code doesn't work as expected now that we've migrated to react-aria-components.

๐Ÿ–ฅ๏ธ Steps to Reproduce

hosted example: https://react-aria-keyboard-handler-repro-TalorAnderson.replit.app see the code / fork: https://replit.com/@TalorAnderson/react-aria-keyboard-handler-repro?v=1#src/App.tsx

Version

1.0.0-rc.0

What browsers are you seeing the problem on?

Chrome

If other, please specify.

No response

What operating system are you using?

MacOS Sonoma 14.1.1

๐Ÿงข Your Company/Team

replit.com

๐Ÿ•ท Tracking Issue

No response

Talor-A avatar Jan 30 '24 20:01 Talor-A

Does the issue persist if you include this in your press handler?

if (e.metaKey) {
  e.continuePropagation();
}

reidbarber avatar Jan 30 '24 20:01 reidbarber

Does the issue persist if you include this in your press handler?

if (e.metaKey) {
  e.continuePropagation();
}

yes, I still see the same behavior with this code:

<Button onPress={(e) => {
  if (e.metaKey) {

    e.continuePropagation()
  }
  increment()
}}>
  Increment
</Button>

Talor-A avatar Jan 30 '24 20:01 Talor-A

I got a 404 on the replit link so can't tell if you're already doing this, but you could try using a capturing event listener for your global events. That would be the recommended approach since any component in the tree might stop propagation.

devongovett avatar Feb 01 '24 05:02 devongovett

I got a 404 on the replit link so can't tell if you're already doing this

updated it to be public, my mistake.

but you could try using a capturing event listener for your global events. That would be the recommended approach since any component in the tree might stop propagation.

can you explain a bit more why this would be recommended? I haven't really seen this done before

Talor-A avatar Feb 01 '24 15:02 Talor-A