@rokkit/actions 1.3.1 → 1.3.3
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/package.json +1 -1
- package/src/navigator.js +91 -0
package/package.json
CHANGED
package/src/navigator.js
CHANGED
|
@@ -45,6 +45,78 @@ function getClickAction(event) {
|
|
|
45
45
|
return 'select'
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
// ─── Nested interactive detection ─────────────────────────────────────────────
|
|
49
|
+
|
|
50
|
+
// Elements that own their own click / keyboard activation. When one of these
|
|
51
|
+
// sits *inside* a [data-path] item (e.g. a Toggle / Switch inside a List header
|
|
52
|
+
// or config-style row), Navigator must not hijack the event — the nested
|
|
53
|
+
// control owns it.
|
|
54
|
+
const INTERACTIVE_SELECTOR = [
|
|
55
|
+
'button',
|
|
56
|
+
'a[href]',
|
|
57
|
+
'input',
|
|
58
|
+
'select',
|
|
59
|
+
'textarea',
|
|
60
|
+
'[role="button"]',
|
|
61
|
+
'[role="switch"]',
|
|
62
|
+
'[role="checkbox"]',
|
|
63
|
+
'[role="menuitem"]',
|
|
64
|
+
'[role="menuitemcheckbox"]',
|
|
65
|
+
'[role="menuitemradio"]',
|
|
66
|
+
'[role="radio"]',
|
|
67
|
+
'[role="tab"]',
|
|
68
|
+
'[role="link"]',
|
|
69
|
+
'[contenteditable=""]',
|
|
70
|
+
'[contenteditable="true"]'
|
|
71
|
+
].join(',')
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* True when `target` sits inside an interactive element that is a *descendant*
|
|
75
|
+
* of the nearest [data-path] element — i.e. the interactive is embedded inside
|
|
76
|
+
* the item, not the item itself. In that case Navigator defers.
|
|
77
|
+
*
|
|
78
|
+
* An interactive marked with data-accordion-trigger is treated as a legitimate
|
|
79
|
+
* Navigator hook (custom expand/collapse control) and does NOT trigger the
|
|
80
|
+
* skip — Navigator will still dispatch toggle for it.
|
|
81
|
+
*
|
|
82
|
+
* @param {EventTarget|null} target
|
|
83
|
+
* @param {HTMLElement} root
|
|
84
|
+
* @returns {boolean}
|
|
85
|
+
*/
|
|
86
|
+
function isNestedInteractive(target, root) {
|
|
87
|
+
const el = /** @type {HTMLElement|null} */ (target)
|
|
88
|
+
if (!el) return false
|
|
89
|
+
const interactive = /** @type {HTMLElement|null} */ (el.closest(INTERACTIVE_SELECTOR))
|
|
90
|
+
if (!interactive || !root.contains(interactive)) return false
|
|
91
|
+
const dataPathEl = /** @type {HTMLElement|null} */ (el.closest('[data-path]'))
|
|
92
|
+
if (!dataPathEl || interactive === dataPathEl) return false
|
|
93
|
+
if (!dataPathEl.contains(interactive)) return false
|
|
94
|
+
// A nested element explicitly declared as an accordion trigger is a
|
|
95
|
+
// Navigator-controlled control, not a stray interactive — let it through.
|
|
96
|
+
if (interactive.hasAttribute('data-accordion-trigger')) return false
|
|
97
|
+
return true
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* True when the [data-path] element containing `target` is marked disabled
|
|
102
|
+
* via `data-disabled` (attribute present) or `aria-disabled="true"`. Native
|
|
103
|
+
* `<button disabled>` is already blocked by the browser, so this only matters
|
|
104
|
+
* for div/span-based items that opt into disabled via attributes.
|
|
105
|
+
*
|
|
106
|
+
* @param {EventTarget|null} target
|
|
107
|
+
* @param {HTMLElement} root
|
|
108
|
+
* @returns {boolean}
|
|
109
|
+
*/
|
|
110
|
+
function isDisabledItem(target, root) {
|
|
111
|
+
const el = /** @type {HTMLElement|null} */ (target)
|
|
112
|
+
if (!el) return false
|
|
113
|
+
const dataPathEl = /** @type {HTMLElement|null} */ (el.closest('[data-path]'))
|
|
114
|
+
if (!dataPathEl || !root.contains(dataPathEl)) return false
|
|
115
|
+
if (dataPathEl.hasAttribute('data-disabled')) return true
|
|
116
|
+
if (dataPathEl.getAttribute('aria-disabled') === 'true') return true
|
|
117
|
+
return false
|
|
118
|
+
}
|
|
119
|
+
|
|
48
120
|
// ─── Path resolution ──────────────────────────────────────────────────────────
|
|
49
121
|
|
|
50
122
|
/**
|
|
@@ -110,6 +182,10 @@ export class Navigator {
|
|
|
110
182
|
// ─── Keydown ────────────────────────────────────────────────────────────
|
|
111
183
|
|
|
112
184
|
#onKeydown = (/** @type {KeyboardEvent} */ event) => {
|
|
185
|
+
// Defer to a nested interactive descendant (Toggle / Switch / input /
|
|
186
|
+
// contenteditable inside an item snippet) — it owns its own keys.
|
|
187
|
+
if (isNestedInteractive(event.target, this.#root)) return
|
|
188
|
+
|
|
113
189
|
// Typeahead: single printable character (no modifiers except shift for caps)
|
|
114
190
|
if (this.#tryTypeahead(event)) return
|
|
115
191
|
|
|
@@ -119,6 +195,10 @@ export class Navigator {
|
|
|
119
195
|
// Links handle Enter/Space natively — browser fires a synthetic click
|
|
120
196
|
if (action === 'select' && event.target.closest('a[href]')) return
|
|
121
197
|
|
|
198
|
+
// Item explicitly marked disabled via attributes (div/span items opt in
|
|
199
|
+
// this way since they cannot use the native `disabled` attribute).
|
|
200
|
+
if (isDisabledItem(document.activeElement, this.#root)) return
|
|
201
|
+
|
|
122
202
|
event.preventDefault()
|
|
123
203
|
event.stopPropagation()
|
|
124
204
|
|
|
@@ -133,9 +213,15 @@ export class Navigator {
|
|
|
133
213
|
// ─── Click ──────────────────────────────────────────────────────────────
|
|
134
214
|
|
|
135
215
|
#onClick = (/** @type {MouseEvent} */ event) => {
|
|
216
|
+
// Defer to a nested interactive descendant — the click is meant for it.
|
|
217
|
+
if (isNestedInteractive(event.target, this.#root)) return
|
|
218
|
+
|
|
136
219
|
const path = getPath(event.target, this.#root)
|
|
137
220
|
if (path === null) return
|
|
138
221
|
|
|
222
|
+
// Item explicitly marked disabled via attributes.
|
|
223
|
+
if (isDisabledItem(event.target, this.#root)) return
|
|
224
|
+
|
|
139
225
|
const action = getClickAction(event)
|
|
140
226
|
|
|
141
227
|
// Links: let browser navigate naturally, still update state
|
|
@@ -150,6 +236,11 @@ export class Navigator {
|
|
|
150
236
|
// ─── Focusin ────────────────────────────────────────────────────────────
|
|
151
237
|
|
|
152
238
|
#onFocusin = (/** @type {FocusEvent} */ event) => {
|
|
239
|
+
// Focus landed inside a nested interactive (e.g. tabbed to a Switch
|
|
240
|
+
// inside a row) — leave the wrapper's focusedKey alone and let the
|
|
241
|
+
// nested control own the focus context.
|
|
242
|
+
if (isNestedInteractive(event.target, this.#root)) return
|
|
243
|
+
|
|
153
244
|
const path = getPath(event.target, this.#root)
|
|
154
245
|
|
|
155
246
|
if (path !== null) {
|