@rokkit/actions 1.1.13 → 1.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -7,7 +7,6 @@ export { swipeable } from "./swipeable.svelte.js";
7
7
  export { themable } from "./themable.svelte.js";
8
8
  export { skinnable } from "./skinnable.svelte.js";
9
9
  export { dismissable } from "./dismissable.svelte.js";
10
- export { navigable } from "./navigable.svelte.js";
11
10
  export { fillable } from "./fillable.svelte.js";
12
11
  export { delegateKeyboardEvents } from "./delegate.svelte.js";
13
12
  export { reveal } from "./reveal.svelte.js";
@@ -15,4 +14,5 @@ export { hoverLift } from "./hover-lift.svelte.js";
15
14
  export { magnetic } from "./magnetic.svelte.js";
16
15
  export { ripple } from "./ripple.svelte.js";
17
16
  export { tooltip } from "./tooltip.svelte.js";
17
+ export { shortcuts } from "./shortcuts.svelte.js";
18
18
  export { buildKeymap, resolveAction, ACTIONS } from "./keymap.js";
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Global keyboard-shortcut capture. Resolves each keydown against a command
3
+ * registry and dispatches the match. Takes the registry as its parameter so
4
+ * `@rokkit/actions` stays independent of `@rokkit/states`.
5
+ *
6
+ * @param {HTMLElement} node
7
+ * @param {{ resolve: (e: KeyboardEvent) => (null | { id: string, global?: boolean }), execute: (id: string) => void }} registry
8
+ */
9
+ export function shortcuts(node: HTMLElement, registry: {
10
+ resolve: (e: KeyboardEvent) => (null | {
11
+ id: string;
12
+ global?: boolean;
13
+ });
14
+ execute: (id: string) => void;
15
+ }): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rokkit/actions",
3
- "version": "1.1.13",
3
+ "version": "1.1.14",
4
4
  "description": "Contains generic actions that can be used in various components.",
5
5
  "repository": {
6
6
  "type": "git",
package/src/index.js CHANGED
@@ -9,7 +9,6 @@ export { swipeable } from './swipeable.svelte.js'
9
9
  export { themable } from './themable.svelte.js'
10
10
  export { skinnable } from './skinnable.svelte.js'
11
11
  export { dismissable } from './dismissable.svelte.js'
12
- export { navigable } from './navigable.svelte.js'
13
12
  export { fillable } from './fillable.svelte.js'
14
13
  export { delegateKeyboardEvents } from './delegate.svelte.js'
15
14
  export { reveal } from './reveal.svelte.js'
@@ -17,3 +16,4 @@ export { hoverLift } from './hover-lift.svelte.js'
17
16
  export { magnetic } from './magnetic.svelte.js'
18
17
  export { ripple } from './ripple.svelte.js'
19
18
  export { tooltip } from './tooltip.svelte.js'
19
+ export { shortcuts } from './shortcuts.svelte.js'
@@ -0,0 +1,37 @@
1
+ import { on } from 'svelte/events'
2
+
3
+ const TYPEABLE = /^(input|textarea|select)$/i
4
+
5
+ function inEditable() {
6
+ const el = document.activeElement
7
+ if (!el) return false
8
+ if (el.isContentEditable) return true
9
+ return TYPEABLE.test(el.tagName)
10
+ }
11
+
12
+ /**
13
+ * Global keyboard-shortcut capture. Resolves each keydown against a command
14
+ * registry and dispatches the match. Takes the registry as its parameter so
15
+ * `@rokkit/actions` stays independent of `@rokkit/states`.
16
+ *
17
+ * @param {HTMLElement} node
18
+ * @param {{ resolve: (e: KeyboardEvent) => (null | { id: string, global?: boolean }), execute: (id: string) => void }} registry
19
+ */
20
+ export function shortcuts(node, registry) {
21
+ let current = registry
22
+
23
+ const handleKeydown = (event) => {
24
+ if (!current?.resolve) return
25
+ const cmd = current.resolve(event)
26
+ if (!cmd) return
27
+ if (inEditable() && !cmd.global) return
28
+ event.preventDefault()
29
+ current.execute(cmd.id)
30
+ }
31
+
32
+ $effect(() => {
33
+ current = registry
34
+ const cleanup = on(window, 'keydown', handleKeydown)
35
+ return () => cleanup()
36
+ })
37
+ }
package/src/utils.js CHANGED
@@ -1,11 +1,5 @@
1
1
  import { find, toPairs } from 'ramda'
2
2
 
3
- // const defaultNavigationOptions = {
4
- // orientation: 'vertical',
5
- // dir: 'ltr',
6
- // nested: false,
7
- // enabled: true
8
- // }
9
3
  /**
10
4
  * Finds the closest ancestor of the given element that has the given attribute.
11
5
  *
@@ -47,32 +41,6 @@ export function handleAction(actions, event) {
47
41
  }
48
42
  }
49
43
 
50
- // /**
51
- // * Maps keys to actions based on the configuration.
52
- // *
53
- // * @param {import('./types').NavigableOptions} options
54
- // * @param {import('./types').NavigableHandlers} handlers
55
- // */
56
- // export function getKeyboardActions(options, handlers) {
57
- // const { horizontal, nested } = options
58
- // if (!options.enabled) return {}
59
-
60
- // const common = {
61
- // Enter: handlers.select,
62
- // ' ': handlers.select
63
- // }
64
- // const movement = horizontal
65
- // ? { ArrowLeft: handlers.previous, ArrowRight: handlers.next }
66
- // : { ArrowUp: handlers.previous, ArrowDown: handlers.next }
67
- // const change = horizontal
68
- // ? { ArrowUp: handlers.collapse, ArrowDown: handlers.expand }
69
- // : { ArrowLeft: handlers.collapse, ArrowRight: handlers.expand }
70
-
71
- // if (nested) return { ...common, ...movement, ...change }
72
- // return { ...common, ...movement }
73
- // }
74
- // Keyboard related functions moved to kbd.js
75
-
76
44
  /**
77
45
  * Finds and returns an index path based on data-path attribute
78
46
  *
@@ -85,9 +53,6 @@ export function getPathFromEvent(event) {
85
53
  // return node ? getPathFromKey(node.getAttribute('data-path')) : null
86
54
  }
87
55
 
88
- // createKeyboardActionMap moved to kbd.js
89
-
90
- // createModifierKeyboardActionMap moved to kbd.js
91
56
  /**
92
57
  * Identifies if an element is a collapsible icon
93
58
  * @param {HTMLElement} target
@@ -123,8 +88,6 @@ function isAccordionTrigger(target) {
123
88
  const trigger = findClosestWithAttribute(target, 'data-accordion-trigger')
124
89
  return trigger !== null
125
90
  }
126
- // getKeyboardAction moved to kbd.js
127
-
128
91
  function isToggleTarget(target) {
129
92
  return isNodeToggle(target) || isNodeToggle(target.parentElement) || isAccordionTrigger(target)
130
93
  }
package/dist/kbd.d.ts DELETED
@@ -1,51 +0,0 @@
1
- /**
2
- * Gets keyboard action handlers based on orientation and direction
3
- * @param {Object} options - Configuration options
4
- * @param {Object} handlers - Event handler functions
5
- * @returns {Object} Object mapping key presses to handler functions
6
- */
7
- export function getKeyboardActions(options: Object, handlers: Object): Object;
8
- /**
9
- * Creates a keyboard action mapping based on navigation options
10
- *
11
- * @param {Object} options - Navigation options
12
- * @param {string} options.orientation - Whether navigation is horizontal or vertical
13
- * @param {string} options.dir - Text direction ('ltr' or 'rtl')
14
- * @param {boolean} options.nested - Whether navigation is nested
15
- * @returns {Object} Mapping of keys to actions
16
- */
17
- export function createKeyboardActionMap(options: {
18
- orientation: string;
19
- dir: string;
20
- nested: boolean;
21
- }): Object;
22
- /**
23
- * Creates a keyboard action mapping for modifier keys based on navigation options
24
- *
25
- * @param {Object} options - Navigation options
26
- * @param {string} options.orientation - Whether navigation is horizontal or vertical
27
- * @returns {Object} Mapping of keys to actions
28
- */
29
- export function createModifierKeyboardActionMap(options: {
30
- orientation: string;
31
- }): Object;
32
- /**
33
- * Creates a keyboard action mapping for shift key combinations
34
- *
35
- * @returns {Object} Mapping of keys to actions
36
- */
37
- export function createShiftKeyboardActionMap(): Object;
38
- /**
39
- * Gets the keyboard action for a key event
40
- * @param {KeyboardEvent} event - The keyboard event
41
- * @param {Object} options - Configuration options
42
- * @returns {string|null} The action to perform, or null if no action is defined
43
- */
44
- export function getKeyboardAction(event: KeyboardEvent, options?: Object): string | null;
45
- export namespace defaultNavigationOptions {
46
- let orientation: string;
47
- let dir: string;
48
- let nested: boolean;
49
- let enabled: boolean;
50
- let typeahead: boolean;
51
- }
@@ -1,8 +0,0 @@
1
- /**
2
- * A svelte action function that captures keyboard evvents and emits event for corresponding movements.
3
- *
4
- * @param {HTMLElement} node
5
- * @param {import('./types').NavigableOptions} options
6
- * @returns {import('./types').SvelteActionReturn}
7
- */
8
- export function navigable(node: HTMLElement, options: import("./types").NavigableOptions): import("./types").SvelteActionReturn;
package/src/kbd.js DELETED
@@ -1,191 +0,0 @@
1
- /**
2
- * Gets horizontal movement actions based on text direction
3
- * @param {Object} handlers - Handler functions
4
- * @param {string} [dir='ltr'] - Text direction ('ltr' or 'rtl')
5
- * @returns {Object} Object mapping arrow keys to movement handlers
6
- */
7
- function getHorizontalMovementActions(handlers, dir = 'ltr') {
8
- return dir === 'rtl'
9
- ? { ArrowRight: handlers.previous, ArrowLeft: handlers.next }
10
- : { ArrowLeft: handlers.previous, ArrowRight: handlers.next }
11
- }
12
-
13
- /**
14
- * Gets vertical movement actions (not affected by direction)
15
- * @param {Object} handlers - Handler functions
16
- * @returns {Object} Object mapping arrow keys to movement handlers
17
- */
18
- function getVerticalMovementActions(handlers) {
19
- return { ArrowUp: handlers.previous, ArrowDown: handlers.next }
20
- }
21
-
22
- /**
23
- * Gets horizontal expand/collapse actions (not affected by direction)
24
- * @param {Object} handlers - Handler functions
25
- * @returns {Object} Object mapping arrow keys to expand/collapse handlers
26
- */
27
- function getHorizontalExpandActions(handlers) {
28
- return { ArrowUp: handlers.collapse, ArrowDown: handlers.expand }
29
- }
30
-
31
- /**
32
- * Gets vertical expand/collapse actions based on text direction
33
- * @param {Object} handlers - Handler functions
34
- * @param {string} [dir='ltr'] - Text direction ('ltr' or 'rtl')
35
- * @returns {Object} Object mapping arrow keys to expand/collapse handlers
36
- */
37
- function getVerticalExpandActions(handlers, dir = 'ltr') {
38
- return dir === 'rtl'
39
- ? { ArrowRight: handlers.collapse, ArrowLeft: handlers.expand }
40
- : { ArrowLeft: handlers.collapse, ArrowRight: handlers.expand }
41
- }
42
-
43
- /**
44
- * Gets common selection actions
45
- * @param {Object} handlers - Handler functions
46
- * @returns {Object} Object mapping keys to selection handlers
47
- */
48
- function getCommonActions(handlers) {
49
- return {
50
- Enter: handlers.select,
51
- ' ': handlers.select
52
- }
53
- }
54
-
55
- // Default navigation options
56
- export const defaultNavigationOptions = {
57
- orientation: 'vertical',
58
- dir: 'ltr',
59
- nested: false,
60
- enabled: true,
61
- typeahead: false
62
- }
63
-
64
- /**
65
- * Gets keyboard action handlers based on orientation and direction
66
- * @param {Object} options - Configuration options
67
- * @param {Object} handlers - Event handler functions
68
- * @returns {Object} Object mapping key presses to handler functions
69
- */
70
- export function getKeyboardActions(options, handlers) {
71
- const { orientation, dir, nested, enabled } = { ...defaultNavigationOptions, ...options }
72
-
73
- if (!enabled) return {}
74
-
75
- const common = getCommonActions(handlers)
76
-
77
- // Determine movement actions based on orientation
78
- const isHorizontal = orientation === 'horizontal'
79
- const movement = isHorizontal
80
- ? getHorizontalMovementActions(handlers, dir)
81
- : getVerticalMovementActions(handlers)
82
-
83
- // If not nested, we don't need expand/collapse actions
84
- if (!nested) {
85
- return { ...common, ...movement }
86
- }
87
-
88
- // Determine expand/collapse actions based on orientation
89
- const expandCollapse = isHorizontal
90
- ? getHorizontalExpandActions(handlers)
91
- : getVerticalExpandActions(handlers, dir)
92
-
93
- return { ...common, ...movement, ...expandCollapse }
94
- }
95
-
96
- function buildHorizontalMovementMap(dir) {
97
- return dir === 'rtl'
98
- ? { ArrowRight: 'previous', ArrowLeft: 'next' }
99
- : { ArrowLeft: 'previous', ArrowRight: 'next' }
100
- }
101
-
102
- function buildVerticalNestedMap(dir) {
103
- return dir === 'rtl'
104
- ? { ArrowRight: 'collapse', ArrowLeft: 'expand' }
105
- : { ArrowLeft: 'collapse', ArrowRight: 'expand' }
106
- }
107
-
108
- function buildNestedActions(isHorizontal, dir) {
109
- if (isHorizontal) return { ArrowUp: 'collapse', ArrowDown: 'expand' }
110
- return buildVerticalNestedMap(dir)
111
- }
112
-
113
- /**
114
- * Creates a keyboard action mapping based on navigation options
115
- *
116
- * @param {Object} options - Navigation options
117
- * @param {string} options.orientation - Whether navigation is horizontal or vertical
118
- * @param {string} options.dir - Text direction ('ltr' or 'rtl')
119
- * @param {boolean} options.nested - Whether navigation is nested
120
- * @returns {Object} Mapping of keys to actions
121
- */
122
- export function createKeyboardActionMap(options) {
123
- const { orientation, dir, nested } = options
124
- const isHorizontal = orientation === 'horizontal'
125
-
126
- const movementActions = isHorizontal
127
- ? buildHorizontalMovementMap(dir)
128
- : { ArrowUp: 'previous', ArrowDown: 'next' }
129
-
130
- const nestedActions = nested ? buildNestedActions(isHorizontal, dir) : {}
131
-
132
- const commonActions = {
133
- Enter: 'select',
134
- ' ': 'select',
135
- Home: 'first',
136
- End: 'last'
137
- }
138
-
139
- return { ...commonActions, ...movementActions, ...nestedActions }
140
- }
141
-
142
- /**
143
- * Creates a keyboard action mapping for modifier keys based on navigation options
144
- *
145
- * @param {Object} options - Navigation options
146
- * @param {string} options.orientation - Whether navigation is horizontal or vertical
147
- * @returns {Object} Mapping of keys to actions
148
- */
149
- export function createModifierKeyboardActionMap(options) {
150
- const isHorizontal = options.orientation === 'horizontal'
151
- const common = { ' ': 'extend', Home: 'first', End: 'last' }
152
- const directional = isHorizontal
153
- ? { ArrowLeft: 'first', ArrowRight: 'last' }
154
- : { ArrowUp: 'first', ArrowDown: 'last' }
155
- return { ...common, ...directional }
156
- }
157
-
158
- /**
159
- * Creates a keyboard action mapping for shift key combinations
160
- *
161
- * @returns {Object} Mapping of keys to actions
162
- */
163
- export function createShiftKeyboardActionMap() {
164
- return { ' ': 'range' }
165
- }
166
-
167
- const KEY_LAYER_RESOLVERS = {
168
- shift: (key, _opts) => createShiftKeyboardActionMap()[key] || null,
169
- modifier: (key, opts) => createModifierKeyboardActionMap(opts)[key] || null,
170
- plain: (key, opts) => createKeyboardActionMap(opts)[key] || null
171
- }
172
-
173
- function getKeyLayer(ctrlKey, metaKey, shiftKey) {
174
- if (ctrlKey) return 'modifier'
175
- if (metaKey) return 'modifier'
176
- if (shiftKey) return 'shift'
177
- return 'plain'
178
- }
179
-
180
- /**
181
- * Gets the keyboard action for a key event
182
- * @param {KeyboardEvent} event - The keyboard event
183
- * @param {Object} options - Configuration options
184
- * @returns {string|null} The action to perform, or null if no action is defined
185
- */
186
- export function getKeyboardAction(event, options = {}) {
187
- const { key, ctrlKey, metaKey, shiftKey } = event
188
- const mergedOptions = { ...defaultNavigationOptions, ...options }
189
- const layer = getKeyLayer(ctrlKey, metaKey, shiftKey)
190
- return KEY_LAYER_RESOLVERS[layer](key, mergedOptions)
191
- }
@@ -1,40 +0,0 @@
1
- import { on } from 'svelte/events'
2
- import { getKeyboardActions, defaultNavigationOptions } from './kbd'
3
-
4
- // Handle keyboard events
5
- function handleAction(actions, event) {
6
- if (event.key in actions) {
7
- event.preventDefault()
8
- event.stopPropagation()
9
- actions[event.key]()
10
- }
11
- }
12
- /**
13
- * A svelte action function that captures keyboard evvents and emits event for corresponding movements.
14
- *
15
- * @param {HTMLElement} node
16
- * @param {import('./types').NavigableOptions} options
17
- * @returns {import('./types').SvelteActionReturn}
18
- */
19
- export function navigable(node, options) {
20
- const handlers = {
21
- previous: () => node.dispatchEvent(new CustomEvent('previous')),
22
- next: () => node.dispatchEvent(new CustomEvent('next')),
23
- collapse: () => node.dispatchEvent(new CustomEvent('collapse')),
24
- expand: () => node.dispatchEvent(new CustomEvent('expand')),
25
- select: () => node.dispatchEvent(new CustomEvent('select'))
26
- }
27
-
28
- let actions = {}
29
- const handleKeydown = (event) => handleAction(actions, event)
30
-
31
- $effect(() => {
32
- // Use defaultNavigationOptions from kbd.js
33
- const props = { ...defaultNavigationOptions, ...options }
34
- actions = getKeyboardActions(props, handlers)
35
-
36
- const cleanup = on(node, 'keyup', handleKeydown)
37
-
38
- return () => cleanup()
39
- })
40
- }