@selenite/commons
Version:
This typescript package provides a set of frequently used utilities, types and svelte actions for building projects with Typescript and Svelte.
61 lines (60 loc) • 2.01 kB
TypeScript
/**
* Svelte action to add keyboard shortcuts.
*
* @see {@link shortcut}
* @module
*/
import type { ActionReturn } from 'svelte/action';
/**
* Basic definition of a keyboard shortcut based on key and modifier keys.
*/
export type KeyboardShortcut = {
/** Keyboard key. */
key?: string;
/** Needs ctrl ? */
ctrl?: boolean;
/** Needs alt ? */
alt?: boolean;
/** Needs shift ? */
shift?: boolean;
};
/**
* Settings for a keyboard shortcut.
*/
export type ShortcutSettings<E extends Element> = KeyboardShortcut & {
/** List of shortcuts that trigger the action.
*
* Can be a single shortcut or an array of shortcuts.
* If a function is provided, it should return a boolean
* indicating whether the shortcut is triggered
* based on the keyboard event.
*/
shortcuts?: KeyboardShortcut[] | ((e: KeyboardEvent) => boolean) | KeyboardShortcut;
/** Callback to be triggered when shortcut is activated. */
action?: (node: E, e: KeyboardEvent) => unknown;
/** Element types that prevents the shortcut from triggering.
*
* Defaults to ['INPUT', 'TEXTAREA']. */
ignoreElements?: string[];
/** Callback to be triggered when key is released. */
endAction?: (node: E, e: KeyboardEvent) => void;
/** Should keep triggering if key is still pressed ?
*
* Defaults to true. */
repeats?: boolean;
};
/**
* Converts a keyboard shortcut definition to a string.
* @param param0 - Keyboard shortcut definition
* @returns
*/
export declare function shortcutToString({ ctrl, alt, shift, key }: KeyboardShortcut): string;
/**
* Action to add a keyboard shortcut.
*
* All keyboard listeners are attached to document.
* @param node - The element the shortcut is bound to
* @param params - Settings for the shortcut
* @returns svelte action to add a keyboard shortcut
*/
export declare function shortcut<E extends Element>(node: E, params: ShortcutSettings<E>): ActionReturn<ShortcutSettings<E>>;