-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
initial autofocus, e.g. in modal overlay #25
Comments
Interesting one. Thanks for the report! I think this may be down to the browser not thinking the element is truly in focus and so not firing the keyboard events on the correct element. I would've thought that manually calling I don't have the time to investigate this right now but I will give it a shot either on the train home this evening or tomorrow! |
Just to make sure there's nothing odd about the <HotKeys ref={el => el.focus()} /> |
I am experiencing the same sort of issue. My initial app is contained in a Trying to focus the element on EDIT Actually, if the ref callback is changed a little bit, it works. const autofocus = (el) => {
const found = ReactDOM.findDOMNode(el);
if (found) {
found.focus();
}
};
return (
<HotKeys
keyMap={keyMap}
handlers={this.handlers}
ref={autofocus}>
... |
@mako-taco Do you have the examples of both working & non-working focus on mounts? Seems odd that focus could be called somewhere but hotkeys not be aware of it. |
Sorry, I have pushed passed it, and no longer have those examples off hand. I don't consider this to be an issue anymore. |
Hey, sorry I didn't reply at all... I consider the callback solution proposed by @mako-taco totally fine. I think it's not even a workaround, just proper usage :) Thanks Works fine for me too |
👍 Thanks for the update :) |
Would it be feasible to add an |
This is how I implemented this, based on @mako-taco's solution: const Autofocus = (WrappedComponent) => {
return class extends Component {
setFocused(el){
let found = findDOMNode(el);
if (found) found.focus()
}
render() {
return <WrappedComponent {...this.props} ref={this.setFocused} />
}
}
}
const AutofocusedHotKeys = Autofocus(HotKeys); |
You can shorten the ref callback to be a one liner with: const autofocus = el => el && findDOMNode(el).focus();
// ...
<HotKeys ref={autofocus} ... /> |
Hi.
I need to use hotkeys inside a modal overlay.
After the overlay is rendered, currently, the user must click into the container element once to give it focus, after that the hotkeys work as expected.
However, what I need is to have the hotkeys ready right away after the modal has opened up, without further mouse interaction.
This is kinda what I have, and I also tried manually focussing an element inside HotKeys, but it doesn't work. How can this be achieved?
I guess i could make it work by using HotKeys further up the component tree, but I guess I would have to define some part of the logic outside of the target component as well? (keyMap, handlers), should be encapsulated inside the 'app' running inside the modal overlay, shouldn't be known outside of it.
I tried the
focused
prop too, but it didn't help.The text was updated successfully, but these errors were encountered: