UNPKG

@waradu/keyboard

Version:

A keyboard manager compatible with JavaScript, TypeScript and Nuxt

31 lines (30 loc) 670 B
import { useKeybind } from "./composables.mjs"; const KEY = Symbol("keybind"); function cleanup(el) { el[KEY]?.(); el[KEY] = void 0; } function register(el, arg, modifiers, run) { cleanup(el); if (!arg || typeof run !== "function") return; el[KEY] = useKeybind({ keys: arg, run, config: { prevent: modifiers.prevent, stop: modifiers.stop, runIfFocused: [el] } }); } export const vKeybind = { mounted(el, binding) { register(el, binding.arg, binding.modifiers, binding.value); }, updated(el, binding) { register(el, binding.arg, binding.modifiers, binding.value); }, unmounted(el) { cleanup(el); } };