Skip to content

Commit

Permalink
callpacb or click node
Browse files Browse the repository at this point in the history
  • Loading branch information
gzimbron committed Nov 4, 2024
1 parent b24f6b7 commit 95954ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-poems-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gzim/svelte-actions": patch
---

hotkey now do node click if not callback setted
11 changes: 8 additions & 3 deletions packages/svelte-actions/src/lib/actions/hotkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import type { Action } from 'svelte/action';
interface HotKeyParams {
/**
* Callback function to be executed when the specified hotkey is pressed.
* If not provided, the node will be clicked.
*/
callback: () => void;
callback?: () => void;

/**
* If `true`, the Shift key must be held down for the callback to be executed.
Expand Down Expand Up @@ -51,7 +52,7 @@ const KeysToEventMap: Record<HotKeyParamsWithoutCB, keyof KeyboardEvent> = {
code: 'code'
};

export const hotkey: Action<HTMLElement, HotKeyParams> = (_, params: HotKeyParams) => {
export const hotkey: Action<HTMLElement, HotKeyParams> = (node, params: HotKeyParams) => {
let stop: () => void;

const destroy = () => {
Expand All @@ -76,7 +77,11 @@ export const hotkey: Action<HTMLElement, HotKeyParams> = (_, params: HotKeyParam
!undefinedKeys.some((key) => !!event[KeysToEventMap[key as HotKeyParamsWithoutCB]])
) {
event.preventDefault();
params.callback?.();
if (params.callback) {
params.callback();
} else {
node.click();
}
}
}

Expand Down

0 comments on commit 95954ea

Please sign in to comment.