@molecule/app-keyboard-shortcuts-svelte
Feature · keyboard-shortcuts · App (browser) · v1.1.2 · Apache-2.0
Svelte functions for @molecule/app-keyboard-shortcuts — register a shortcut for the life of a component
npm install @molecule/app-keyboard-shortcuts-svelteHow it works
@molecule/app-keyboard-shortcuts-svelte 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.
<script lang="ts">
import { useKeyboardShortcut } from '@molecule/app-keyboard-shortcuts-svelte'
import { useTranslation } from '@molecule/app-svelte'
const { t } = useTranslation()
let input: HTMLInputElement | undefined = $state()
useKeyboardShortcut('/', () => input?.focus(), {
description: t('catalogSearch.focus', undefined, { defaultValue: 'Focus search' }),
})
useKeyboardShortcut('mod+k', () => openPalette())
</script>
<input bind:this={input} />Works with: @molecule/app-keyboard-shortcuts, @molecule/app-svelte, @molecule/app-ui, @molecule/app-ui-svelte
Reference
Svelte functions for @molecule/app-keyboard-shortcuts.
useKeyboardShortcut registers a shortcut with the bonded provider
for the life of a component and always calls the latest handler, so a
component can bind / to focus its search box or mod+k to open a
palette in one line. It is the Svelte 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
<script lang="ts">
import { useKeyboardShortcut } from '@molecule/app-keyboard-shortcuts-svelte'
import { useTranslation } from '@molecule/app-svelte'
const { t } = useTranslation()
let input: HTMLInputElement | undefined = $state()
useKeyboardShortcut('/', () => input?.focus(), {
description: t('catalogSearch.focus', undefined, { defaultValue: 'Focus search' }),
})
useKeyboardShortcut('mod+k', () => openPalette())
</script>
<input bind:this={input} />
Type
feature
Installation
npm install @molecule/app-keyboard-shortcuts-svelte @molecule/app-keyboard-shortcuts @molecule/app-svelte @molecule/app-ui @molecule/app-ui-svelte svelte
API
Interfaces
UseKeyboardShortcutOptions
Options for useKeyboardShortcut in @molecule/app-keyboard-shortcuts-svelte.
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 (onDestroy), and unregisters on destroy or when
enabled turns false. The latest handler is always the one called: pass a
changing action as a store, which the registration reads at call time. With
no provider bonded the function does nothing.
Call it during component init (a component's <script> or a .svelte.ts
module's init), where Svelte's lifecycle works. Pass keys or options as
a readable store when any part of them can change: a changed combination,
enable flag, description, scope or preventDefault flag unregisters the
previous registration and registers the new one on the SAME mount, the way
React re-renders the hook with new arguments.
function useKeyboardShortcut(
keys: string | string[] | Readable<string | string[]>,
handler: ShortcutHandler | Readable<ShortcutHandler>,
options?: UseKeyboardShortcutOptions | Readable<UseKeyboardShortcutOptions>,
): void
keys— One combination or several ('mod+k'is expanded toctrl+kandcommand+k), or a store of either.handler— Called with the keyboard event; or a store of one, read at call time.options— Enable flag, description, scope, preventDefault; or a store of them.
Injection Notes
Requirements
Peer dependencies:
@molecule/app-keyboard-shortcuts^1.0.1@molecule/app-svelte^3.5.0 || ^4.0.0@molecule/app-ui^1.0.1@molecule/app-ui-svelte^1.1.0svelte^5.0.0
Runtime Dependencies
-
@molecule/app-keyboard-shortcuts -
@molecule/app-svelte -
@molecule/app-ui -
@molecule/app-ui-svelte -
svelte -
mod+means Ctrl on Windows and Linux, Command on macOS — the function 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 function 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 during component init (a component's
<script>block), where Svelte's lifecycle works. Passkeysoroptionsas a readable store when any part of them can change, and a changing handler as a store too; both are read at call time.