@molecule/app-keyboard-shortcuts-vue
Feature · keyboard-shortcuts · App (browser) · v1.1.1 · Apache-2.0
Vue hooks for @molecule/app-keyboard-shortcuts — register a shortcut for the life of a component
npm install @molecule/app-keyboard-shortcuts-vueHow it works
@molecule/app-keyboard-shortcuts-vue 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 setup lang="ts">
import { ref } from 'vue'
import { useKeyboardShortcut } from '@molecule/app-keyboard-shortcuts-vue'
import { useTranslation } from '@molecule/app-vue'
const { t } = useTranslation()
const input = ref<HTMLInputElement | null>(null)
useKeyboardShortcut('/', () => input.value?.focus(), {
description: t('catalogSearch.focus', undefined, { defaultValue: 'Focus search' }),
})
useKeyboardShortcut('mod+k', () => openPalette())
</script>
<template>
<input ref="input" />
</template>Works with: @molecule/app-keyboard-shortcuts, @molecule/app-ui, @molecule/app-ui-vue, @molecule/app-vue
Reference
Vue hooks 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 Vue 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 setup lang="ts">
import { ref } from 'vue'
import { useKeyboardShortcut } from '@molecule/app-keyboard-shortcuts-vue'
import { useTranslation } from '@molecule/app-vue'
const { t } = useTranslation()
const input = ref<HTMLInputElement | null>(null)
useKeyboardShortcut('/', () => input.value?.focus(), {
description: t('catalogSearch.focus', undefined, { defaultValue: 'Focus search' }),
})
useKeyboardShortcut('mod+k', () => openPalette())
</script>
<template>
<input ref="input" />
</template>
Type
feature
Installation
npm install @molecule/app-keyboard-shortcuts-vue @molecule/app-keyboard-shortcuts @molecule/app-ui @molecule/app-ui-vue @molecule/app-vue vue
API
Interfaces
UseKeyboardShortcutOptions
Options for useKeyboardShortcut in @molecule/app-keyboard-shortcuts-vue.
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's setup scope, and unregisters when the scope is disposed
or enabled turns false. The latest handler is always the one called, so a
handler can be swapped through a ref without re-registering. With no
provider bonded the composable does nothing.
Pass keys or options as a ref or getter to follow a change on the SAME
instance — the Vue counterpart of React re-rendering the hook with new
arguments. A changed key combination, enable flag, description, scope or
preventDefault flag unregisters the previous registration and registers
the new one; a handler change does not.
function useKeyboardShortcut(
keys: string | string[] | MaybeRefOrGetter<string | string[]>,
handler: ShortcutHandler | Ref<ShortcutHandler>,
options?: UseKeyboardShortcutOptions | MaybeRefOrGetter<UseKeyboardShortcutOptions | undefined>,
): void
keys— One combination or several ('mod+k'is expanded toctrl+kandcommand+k), or a ref/getter of either.handler— Called with the keyboard event; or a ref of one, read at call time.options— Enable flag, description, scope, preventDefault; or a ref/getter of them.
Injection Notes
Requirements
Peer dependencies:
@molecule/app-keyboard-shortcuts^1.0.1@molecule/app-ui^1.0.1@molecule/app-ui-vue^1.1.0@molecule/app-vue^3.5.0 || ^4.0.0vue^3.4.0
Runtime Dependencies
-
@molecule/app-keyboard-shortcuts -
@molecule/app-ui -
@molecule/app-ui-vue -
@molecule/app-vue -
vue -
mod+means Ctrl on Windows and Linux, Command on macOS — the composable 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 composable 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
setup()(a<script setup>block or thesetup()option). Passkeysoroptionsas a ref/getter to follow a change on the same instance; pass a changing handler as a ref, which is read at call time.