Easy key bindings for your Reagent application.
You might have a sweet menus, but power users will want keyboard shortcuts!
Add this to your project dependencies:
keyboard-listener component in your document:
(defn main-page  [:div [:h1 "Page title"] [:span "Some content"] [kb/keyboard-listener]]) ; <-------
Now you can add key bindings as easily as adding a new component :
[kb/kb-action "ctrl-a" #(prn "Print this cool message!")]
You can also automatically overwrite a key binding by mounting another component :
[kb/kb-action "ctrl-a" #(prn "initial shortcut overwritten!")]
When the component is unmounted, any previous key binding is restored. This allows you to temporarily give the users new shortcuts without all the hassle.
Propagation and Default Prevention
All key bindings will automatically prevent the default action
.preventDefault and stop the event propagation
Because key bindings often implies a more 'serious' web application, we disable the following browser shortcuts automatically (if allowed by the browser) :
ctrl-rWe don't want the user to reload by accident;
ctrl-sDon't save the HTML file.
You can override this behavior by clearing the
Key Bindings Deactivation
It's possible to completely deactivate all the key bindings by
A typical use is with a modal in which the user needs to type text in a field.
You can access the keyboard state with the Reagent ratom
keyboard-state. The map contains the 3 main modifiers state
alt) and the character keycode for the normal
Copyright © 2018 Christian Fortin
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.