Hierarchical keyboard shortcut management for ClojureScript applications.
This library provides a tree-based shortcut system with context-aware key bindings that bubble up from child to parent nodes. Supports platform-specific modifiers (Mac cmd vs Windows ctrl) and flexible handler functions or callback vectors.
Hierarchical keyboard shortcut management for ClojureScript applications. This library provides a tree-based shortcut system with context-aware key bindings that bubble up from child to parent nodes. Supports platform-specific modifiers (Mac cmd vs Windows ctrl) and flexible handler functions or callback vectors.
(get-context)Get the currently active node identifier.
Returns the keyword identifying the current active node, or nil if none is set.
Example:
(get-context) ; => :editor
Get the currently active node identifier. Returns the keyword identifying the current active node, or nil if none is set. Example: ```clojure (get-context) ; => :editor ```
(init! shortcuts-tree)(init! shortcuts-tree options)Initialize the keyboard shortcuts system with a shortcuts tree.
The shortcuts tree is a nested map structure:
Handlers can be:
Options:
Example:
(init! {:context :root
:bindings {"cmd-s" #(js/console.log "Save!")
"escape" [:ui/close-modal]}
:children [{:context :editor
:bindings {"cmd-b" #(js/console.log "Bold!")}}]}
{:callback (fn [event-vec event] (dispatch event-vec))})
Initialize the keyboard shortcuts system with a shortcuts tree.
The shortcuts tree is a nested map structure:
- :context - The node identifier (keyword)
- :bindings - Map of key-string to handler (function or callback vector)
- :children - Optional vector of child nodes
Handlers can be:
- Functions: Called directly with the keyboard event
- Vectors: Passed to the callback function (set via options)
Options:
- :callback - Function called with [handler-vector event] when handler is a vector
Example:
```clojure
(init! {:context :root
:bindings {"cmd-s" #(js/console.log "Save!")
"escape" [:ui/close-modal]}
:children [{:context :editor
:bindings {"cmd-b" #(js/console.log "Bold!")}}]}
{:callback (fn [event-vec event] (dispatch event-vec))})
```(kbd keystring)(kbd mac-string win-string)Helper for defining cross-platform keyboard shortcuts.
Single arity: Takes a keystring with 'defmod' as a placeholder for the platform-specific modifier (cmd on Mac, ctrl on Windows).
Two arity: Takes separate Mac and Windows keystrings for full control.
Examples:
(kbd "defmod-b") ; => "cmd-b" on Mac, "ctrl-b" on Windows
(kbd "shift-defmod-z") ; => "shift-cmd-z" on Mac, "shift-ctrl-z" on Windows
(kbd "cmd-k" "ctrl-k") ; => "cmd-k" on Mac, "ctrl-k" on Windows
Helper for defining cross-platform keyboard shortcuts. Single arity: Takes a keystring with 'defmod' as a placeholder for the platform-specific modifier (cmd on Mac, ctrl on Windows). Two arity: Takes separate Mac and Windows keystrings for full control. Examples: ```clojure (kbd "defmod-b") ; => "cmd-b" on Mac, "ctrl-b" on Windows (kbd "shift-defmod-z") ; => "shift-cmd-z" on Mac, "shift-ctrl-z" on Windows (kbd "cmd-k" "ctrl-k") ; => "cmd-k" on Mac, "ctrl-k" on Windows ```
(set-context! node-id)Set the currently active node in the shortcuts tree.
The active node determines which shortcuts are currently active. When a key is pressed, the system looks for a handler in the active node, and if not found, bubbles up to parent nodes.
Example:
(set-context! :editor) ; Enable editor shortcuts
(set-context! :sidebar) ; Switch to sidebar shortcuts
Set the currently active node in the shortcuts tree. The active node determines which shortcuts are currently active. When a key is pressed, the system looks for a handler in the active node, and if not found, bubbles up to parent nodes. Example: ```clojure (set-context! :editor) ; Enable editor shortcuts (set-context! :sidebar) ; Switch to sidebar shortcuts ```
(shutdown!)Clean up the keyboard shortcuts system.
Removes the event listener and resets all state. Should be called when tearing down the application or switching to a different shortcuts system.
Example:
(shutdown!)
Clean up the keyboard shortcuts system. Removes the event listener and resets all state. Should be called when tearing down the application or switching to a different shortcuts system. Example: ```clojure (shutdown!) ```
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |