@molecule/app-keyboard-shortcuts-solid
Feature · keyboard-shortcuts · App (browser) · v1.1.1 · Apache-2.0
Solid hooks for @molecule/app-keyboard-shortcuts — register a shortcut for the life of a component
npm install @molecule/app-keyboard-shortcuts-solidHow it works
@molecule/app-keyboard-shortcuts-solid is a ready-made keyboard-shortcuts feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.
import { createSignal, onCleanup } from 'solid-js'
import { render } from 'solid-js/web'
import { useKeyboardShortcut } from '@molecule/app-keyboard-shortcuts-solid'
import { useTranslation } from '@molecule/app-solid'
function Search() {
const { t } = useTranslation()
let input!: HTMLInputElement
useKeyboardShortcut('/', () => input.focus(), {
description: t('catalogSearch.focus', undefined, { defaultValue: 'Focus search' }),
})
useKeyboardShortcut('mod+k', () => openPalette())
return <input ref={input} />
}
render(() => <Search />, document.getElementById('root')!)Works with: @molecule/app-keyboard-shortcuts, @molecule/app-solid, @molecule/app-ui, @molecule/app-ui-solid
Reference
Solid primitives for @molecule/app-keyboard-shortcuts.
useKeyboardShortcut registers a shortcut with the bonded provider
for the life of a component and fires the handler the call gave it, so a
component can bind / to focus its search box or mod+k to open a
palette in one line. It is the Solid counterpart of
@molecule/app-keyboard-shortcuts-react: same names, same options, same
provider calls.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { createSignal, onCleanup } from 'solid-js'
import { render } from 'solid-js/web'
import { useKeyboardShortcut } from '@molecule/app-keyboard-shortcuts-solid'
import { useTranslation } from '@molecule/app-solid'
function Search() {
const { t } = useTranslation()
let input!: HTMLInputElement
useKeyboardShortcut('/', () => input.focus(), {
description: t('catalogSearch.focus', undefined, { defaultValue: 'Focus search' }),
})
useKeyboardShortcut('mod+k', () => openPalette())
return <input ref={input} />
}
render(() => <Search />, document.getElementById('root')!)
Type
feature
Installation
npm install @molecule/app-keyboard-shortcuts-solid @molecule/app-keyboard-shortcuts @molecule/app-solid @molecule/app-ui @molecule/app-ui-solid solid-js
API
Interfaces
UseKeyboardShortcutOptions
Options for useKeyboardShortcut in @molecule/app-keyboard-shortcuts-solid.
interface UseKeyboardShortcutOptions {
/** Register the shortcut at all. `false` unregisters it. Defaults to `true`. */
enabled?: boolean
/** What the shortcut does, already translated, for a shortcuts panel. */
description?: string
/** The provider scope to register under. */
scope?: string
/** Call `preventDefault()` on the event. Defaults to `true`. */
preventDefault?: boolean
}
Functions
expandKeys(keys)
Normalizes a key spec: 'mod+k' becomes both ctrl+k and command+k, so
one declaration serves every platform.
function expandKeys(keys: string | string[]): string[]
keys— One combination or several.
Returns: The combinations to register.
useKeyboardShortcut(keys, handler, options)
Registers keys with the bonded keyboard-shortcuts provider for the life
of the component (Solid's onCleanup), and unregisters on dispose or when
enabled turns false. The handler given at the call is the one fired — pass
a stable wrapper that reads your accessor ((event) => handler()(event))
where the action can change, the Solid counterpart of React always calling
the latest closure. With no provider bonded the primitive does nothing.
A component body runs once in Solid, so pass keys or options as an
accessor when any part of them can change (() => props.keys): a changed
combination, enable flag, description, scope or preventDefault flag
unregisters the previous registration and registers the new one on the SAME
instance, the way React re-renders the hook with new arguments.
function useKeyboardShortcut(
keys: string | string[] | Accessor<string | string[]>,
handler: ShortcutHandler,
options?: UseKeyboardShortcutOptions | Accessor<UseKeyboardShortcutOptions>,
): void
keys— One combination or several ('mod+k'is expanded toctrl+kandcommand+k), or an accessor of either.handler— Called with the keyboard event (see above for a changing action).options— Enable flag, description, scope, preventDefault; or an accessor of them.
Injection Notes
Requirements
Peer dependencies:
@molecule/app-keyboard-shortcuts^1.0.1@molecule/app-solid^3.5.0 || ^4.0.0@molecule/app-ui^1.0.1@molecule/app-ui-solid^1.1.0solid-js^1.8.0
Runtime Dependencies
-
@molecule/app-keyboard-shortcuts -
@molecule/app-solid -
@molecule/app-ui -
@molecule/app-ui-solid -
solid-js -
mod+means Ctrl on Windows and Linux, Command on macOS — the primitive registers both, so never writectrl+kfor a shortcut people expect as⌘Kon a Mac. -
Whether a shortcut fires while an input has focus is the bond's decision (the hotkeys bond suppresses it, which is what
/needs). Keys the input itself should handle — Escape to clear, arrows to move — belong on the input's own keydown handling, not here. -
With no provider bonded the primitive is a no-op rather than an error, so a shared component still renders in an app that never wired shortcuts.
-
The
descriptionis shown by shortcut panels: pass it throught(). -
Call it in a component body (Solid's lifecycle only works there). A component body runs once, so pass
keysoroptionsas an accessor when any part of them can change; keep the handler a stable function that reads your own accessor when the action can change.