NoriginMedia / react-spatial-navigation

DEPRECATED. HOC-based Spatial Navigation. NEW Hooks version is available here: https://github.com/NoriginMedia/norigin-spatial-navigation
MIT License
226 stars 64 forks source link

Handle keypresses safely for focusables that become unmounted. #27

Closed shirakaba closed 5 years ago

shirakaba commented 5 years ago

This is a guard relevant to the use-case of UI fading out (specifically, when your last-focused component becomes unmounted upon fade-out, which leads to this.focusableComponents[this.focusKey] becoming undefined).

I prevent onEnterPress and onArrowPress from triggering if the focusKey no longer corresponds to a component. Otherwise, the app tries to access fields on the component variable, which evaluates to undefined, and thus results in a TypeError.

"Ideal behaviour" might arguably be to move focus to the next-closest focusable upon the user's current focus target unmounting (this would constitute a separate PR), but in my case, there were no enabled focusables anywhere in the app to move the target to, so we'd still need this guard either way.

asgvard commented 5 years ago

Thanks, I will merge it soon today. Didn’t had time last week :)