Modes
A mode is a named namespace for key bindings. A binding carrying mode only fires in that mode; one without fires in all modes (including no-mode). The common use is replicating vim's normal / insert.
Prerequisites
| Chapter | Why you need it |
|---|---|
| Basic Binding | mode is one of the binding options |
The binding mode option
mode{string} Omitted, the binding fires in all modes (including no-mode); given, it fires only whilegetCurrentMode()equals it.
boundKeyboard, boundSequence, globalKeys, globalSequence, penetration, stop, allowModal, and composition / mapping entries all accept this option.
addMode(mode)
Registers a mode name.
mode{string} The mode name.- Returns: {boolean}
truewhen added;falsewhen the mode already exists (idempotent).
removeMode(mode)
Unregisters a mode name.
mode{string} The mode name.- Returns: {boolean}
truewhen it existed and was removed. - Note: this does not exit the mode. If the removed mode is active,
getCurrentMode()still returns that name — callsetMode(null)yourself.
setMode(mode)
Switches the current mode.
mode{string | null} The target mode;nullenters no-mode.- Returns: {boolean}
trueand switches when the mode is registered (ornull);falseand leaves the current mode unchanged when it is not registered.
nextMode()
Cycles to the next mode. From no-mode it enters the first registered mode.
- Returns: nothing.
prevMode()
Cycles to the previous mode. From no-mode the current index matches nothing, so it lands on the second-to-last registered mode rather than the last. To enter the mode list deterministically from no-mode, use nextMode().
- Returns: nothing.
getCurrentMode()
- Returns: {string | null} The current mode name;
nullin no-mode.
Example
tsx
const { addMode, removeMode, setMode, nextMode, boundKeyboard } = useKeyboard()
useEffect(() => {
addMode("insert")
const unbind = boundKeyboard("*", handleChar, { mode: "insert" }) // insert only
return () => { unbind(); removeMode("insert") }
}, [addMode, removeMode, boundKeyboard])
// One shortcut cycles the mode
useEffect(() => boundKeyboard("escape", nextMode), [boundKeyboard, nextMode])Boundaries
- A
defaultModepassed at construction is not validated against the registered modes. - Outside React, the
KeyboardEngineconstructor'sEnginePropsacceptsmodes?: string[]anddefaultMode?: string— the initial registrations and initial mode; the other methods keep the same names and meanings.
