Giter Club home page Giter Club logo

Comments (10)

chrisui avatar chrisui commented on July 23, 2024 20

@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.

pistachiomatt avatar pistachiomatt commented on July 23, 2024 2

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.

chrisui avatar chrisui commented on July 23, 2024

Hey @cpsubrian thanks for bringing this up. It was something I have thought about myself and I have two solutions.

  1. 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 root HotKeys component is always focused and allows people to manually manage focus state if that prop is present.
  2. 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.

chrisui avatar chrisui commented on July 23, 2024

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.

cpsubrian avatar cpsubrian commented on July 23, 2024

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.

chrisui avatar chrisui commented on July 23, 2024

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.

cpsubrian avatar cpsubrian commented on July 23, 2024

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.

chrisui avatar chrisui commented on July 23, 2024

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.

cpsubrian avatar cpsubrian commented on July 23, 2024

Just confirming the fix worked like a charm.

from react-hotkeys.

jordantomax avatar jordantomax commented on July 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.