Comments (10)
@cpsubrian I've just release v0.4.0
on npm with a new attach
property which optionally takes a DOM Element which will listen to key events.
For your use case you'll need both of the new props.
<HotKeys focused={true} attach={window} {...otherProps} />
Let know if you have any issues with this and we can reopen! :)
from react-hotkeys.
One little edge case is if you have multiple <HotKeys>
attaching to window
, they always fire together.
An example: You bind Esc
to window
to navigate up a level in your app. A modal appears which also binds to Esc
to dismiss the modal. Hitting Esc
dismisses the modal and navigates you up the app at once. You’d expect the inner-most nested <HotKey>
would only fire; in this case, just the modal <HotKey>
.
from react-hotkeys.
Hey @cpsubrian thanks for bringing this up. It was something I have thought about myself and I have two solutions.
- I solution could simply implement now with few worries is to have a
focused
prop which overrides the internal__isFocused__
state which would enable you just to say your rootHotKeys
component is always focused and allows people to manually manage focus state if that prop is present. - The root
HotKeys
component could be made to always be an active handler. We'd just check to see if we have (known internally) a "hotkey parent" and if one doesn't exist your hotkeys can always be honoured.
from react-hotkeys.
Realised just as I hit "comment" that these could live side by side quite easily. So for now I'll implement soluton 1.
from react-hotkeys.
Great ideas. Please let me know if I can help with testing/coding either of those solutions. If you don't have much time right now I could dive into the source deeper and see if I can grok your #1 above enough to try my hand at a PR
from react-hotkeys.
Would be great if you could download and test out latest master. :)
You can control focus using the focused
prop as of 6e677b1
from react-hotkeys.
My build-chain (webpack, babel-loader) is having issues with master
. Will dive in more when I get a chance. It appears to be the export {default as blah} from 'thing'
that is the problem.
ERROR in ./~/react-hotkeys/lib/index.js
Module parse failed: /Users/cpsubrian/projects/personal/zelda/node_modules/react-hotkeys/lib/index.js Line 1: Unexpected reserved word
You may need an appropriate loader to handle this file type.
| export {default as HotKeys} from './HotKeys';
| export {default as FocusTrap} from './FocusTrap';
| export {default as HotKeyMapMixin} from './HotKeyMapMixin';
@ ./src/components/handlers/AppHandler.js 25:20-44
from react-hotkeys.
Interesting. Seems valid and babel should handle it: https://babeljs.io/repl/#?experimental=true&evaluate=true&loose=false&spec=false&code=export%20%7Bdefault%20as%20HotKeys%7D%20from%20'.%2FHotKeys'%3B
I've tested and realised that the focused
addition won't actually solve the problem. Basically because your root component (or rather, the rendered dom element) probably won't receive any keyboard events.
Solution should probably be a pretty special root
prop which when true
will listen to events on the window
(ie. catch all) rather than any element.
Will hopefully work on this tonight when I get chance.
from react-hotkeys.
Just confirming the fix worked like a charm.
from react-hotkeys.
Just checking since this was closed long ago, is there now support for the case mentioned by @pistachiomatt? I have the same use case.
from react-hotkeys.
Related Issues (20)
- Dynamic hotkeys for multiple components.
- Handler function doesn't reference the latest state HOT 2
- [BUG] - released key is still considered pressed HOT 3
- Add "mod" as a key binding like mousetrap
- cmd+c triggers action bound to c even when allowCombinationSubmatches is false [BUG] HOT 3
- custom key codes shift+num1[BUG]
- [BUG] Shift+Space combination doesn't work and breaks other bindings HOT 1
- [BUG] Pressing the spacebar with no element focused breaks the event listeners HOT 2
- Get sequence which triggered the handler
- Hotkeys is wrapping my components in a div that is breaking my styling HOT 1
- [BUG] Key released still pressed if the focus is lost HOT 1
- [BUG] hitting space twice breaks all events HOT 1
- [BUG] Component does not update keymap HOT 2
- [BUG] Pressing Ctrl+ALT+[A number] keep the key in the combination until focus out.
- [BUG] Escape not working when used in GlobalHotKeys HOT 6
- [BUG] hotkeys not working after pressing tab HOT 2
- React-hotkeys not working with lazy import
- [BUG] Unable to preventDefault on "meta + enter" sequences, so browser treats enter as click
- [BUG] Slow unmount with many usages / larger component tree HOT 1
- New configure property `log` or `onMatched`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-hotkeys.