@rokkit/actions 1.3.15 → 1.4.1
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/utils.d.ts +2 -8
- package/package.json +1 -1
- package/src/navigator.js +32 -6
- package/src/themable.svelte.js +47 -38
- package/src/utils.js +4 -13
package/dist/utils.d.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Finds the closest ancestor of the given element that has the given attribute.
|
|
3
|
-
*
|
|
4
|
-
* @param {HTMLElement} element
|
|
5
|
-
* @param {string} attribute
|
|
6
|
-
* @returns {HTMLElement|null}
|
|
7
|
-
*/
|
|
8
|
-
export function getClosestAncestorWithAttribute(element: HTMLElement, attribute: string): HTMLElement | null;
|
|
9
1
|
export function handleAction(actions: any, event: any): void;
|
|
10
2
|
/**
|
|
11
3
|
* Finds and returns an index path based on data-path attribute
|
|
@@ -14,5 +6,7 @@ export function handleAction(actions: any, event: any): void;
|
|
|
14
6
|
* @returns {number[]|null} null or index path array
|
|
15
7
|
*/
|
|
16
8
|
export function getPathFromEvent(event: MouseEvent): number[] | null;
|
|
9
|
+
export { getClosestAncestorWithAttribute };
|
|
17
10
|
export function getEventForKey(keyMapping: import("./types.js").KeyboardConfig, key: string): string | null;
|
|
18
11
|
export function getClickAction(event: MouseEvent): string;
|
|
12
|
+
import { getClosestAncestorWithAttribute } from '@rokkit/core';
|
package/package.json
CHANGED
package/src/navigator.js
CHANGED
|
@@ -88,13 +88,26 @@ function isNestedInteractive(target, root) {
|
|
|
88
88
|
if (!el) return false
|
|
89
89
|
const interactive = /** @type {HTMLElement|null} */ (el.closest(INTERACTIVE_SELECTOR))
|
|
90
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
91
|
// A nested element explicitly declared as an accordion trigger is a
|
|
95
92
|
// Navigator-controlled control, not a stray interactive — let it through.
|
|
96
93
|
if (interactive.hasAttribute('data-accordion-trigger')) return false
|
|
97
|
-
return
|
|
94
|
+
return isStrictlyInsideItem(el, interactive)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* True when `interactive` sits STRICTLY inside the `[data-path]` item that
|
|
99
|
+
* contains `el` — i.e. the item exists, is not the interactive itself, and
|
|
100
|
+
* really is its ancestor. Being the item (a `<button data-path>` list row) is
|
|
101
|
+
* the normal case and must not count as nesting.
|
|
102
|
+
*
|
|
103
|
+
* @param {HTMLElement} el
|
|
104
|
+
* @param {HTMLElement} interactive
|
|
105
|
+
* @returns {boolean}
|
|
106
|
+
*/
|
|
107
|
+
function isStrictlyInsideItem(el, interactive) {
|
|
108
|
+
const item = /** @type {HTMLElement|null} */ (el.closest('[data-path]'))
|
|
109
|
+
if (!item || interactive === item) return false
|
|
110
|
+
return item.contains(interactive)
|
|
98
111
|
}
|
|
99
112
|
|
|
100
113
|
/**
|
|
@@ -143,6 +156,8 @@ export class Navigator {
|
|
|
143
156
|
#wrapper
|
|
144
157
|
#keymap
|
|
145
158
|
#containScroll
|
|
159
|
+
// Set by destroy() so a deferred focusout can tell it's been torn down.
|
|
160
|
+
#destroyed = false
|
|
146
161
|
|
|
147
162
|
// Typeahead state
|
|
148
163
|
#buffer = ''
|
|
@@ -169,6 +184,7 @@ export class Navigator {
|
|
|
169
184
|
}
|
|
170
185
|
|
|
171
186
|
destroy() {
|
|
187
|
+
this.#destroyed = true
|
|
172
188
|
this.#root.removeEventListener('keydown', this.#onKeydown)
|
|
173
189
|
this.#root.removeEventListener('click', this.#onClick)
|
|
174
190
|
this.#root.removeEventListener('focusin', this.#onFocusin)
|
|
@@ -275,8 +291,18 @@ export class Navigator {
|
|
|
275
291
|
// If it's null or outside this root, focus left the list
|
|
276
292
|
const next = /** @type {Node|null} */ (event.relatedTarget)
|
|
277
293
|
if (!next || !this.#root.contains(next)) {
|
|
278
|
-
//
|
|
279
|
-
|
|
294
|
+
// Removing a focused element also fires focusout with a null
|
|
295
|
+
// relatedTarget, so teardown looks exactly like a real blur here. Calling
|
|
296
|
+
// wrapper.blur() synchronously in that case mutates state during Svelte's
|
|
297
|
+
// effect-cleanup phase, which throws `state_unsafe_mutation`. Deferring by
|
|
298
|
+
// a microtask lets destroy() land first, so a torn-down navigator stays
|
|
299
|
+
// quiet while a genuine blur still reaches the wrapper.
|
|
300
|
+
// JSDOM never fires focusout on removal, so this only shows up in a real
|
|
301
|
+
// browser — see packages/ui/browser/Select.browser.spec.ts.
|
|
302
|
+
queueMicrotask(() => {
|
|
303
|
+
if (this.#destroyed) return
|
|
304
|
+
this.#wrapper.blur?.()
|
|
305
|
+
})
|
|
280
306
|
}
|
|
281
307
|
}
|
|
282
308
|
|
package/src/themable.svelte.js
CHANGED
|
@@ -1,5 +1,48 @@
|
|
|
1
1
|
const DEFAULT_THEME = { style: 'rokkit', mode: 'dark', density: 'comfortable', skin: 'default' }
|
|
2
2
|
|
|
3
|
+
/** The four data-attributes that together select a theme. */
|
|
4
|
+
const THEME_ATTRS = ['style', 'mode', 'density', 'skin']
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Write the theme's attributes onto one element's dataset.
|
|
8
|
+
*
|
|
9
|
+
* @param {HTMLElement} el
|
|
10
|
+
* @param {Record<string, string>} theme
|
|
11
|
+
*/
|
|
12
|
+
function applyThemeAttrs(el, theme) {
|
|
13
|
+
for (const attr of THEME_ATTRS) el.dataset[attr] = theme[attr]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Persist the theme to storage, and adopt changes another document writes to the
|
|
18
|
+
* same key — that second half is what keeps two open tabs in agreement.
|
|
19
|
+
*
|
|
20
|
+
* @param {import('./types.js').Themable} theme
|
|
21
|
+
* @param {string} storageKey
|
|
22
|
+
*/
|
|
23
|
+
function syncWithStorage(theme, storageKey) {
|
|
24
|
+
theme.load(storageKey)
|
|
25
|
+
|
|
26
|
+
$effect(() => {
|
|
27
|
+
theme.save(storageKey)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
const handleStorage = (event) => {
|
|
31
|
+
if (event.key !== storageKey || event.newValue === null) return
|
|
32
|
+
try {
|
|
33
|
+
theme.update(JSON.parse(event.newValue))
|
|
34
|
+
} catch (e) {
|
|
35
|
+
// eslint-disable-next-line no-console
|
|
36
|
+
console.warn('Failed to parse theme from storage event for key "%s"', storageKey, e)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
$effect.root(() => {
|
|
41
|
+
window.addEventListener('storage', handleStorage)
|
|
42
|
+
return () => window.removeEventListener('storage', handleStorage)
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
3
46
|
/**
|
|
4
47
|
* Update the theme attributes when the state changes.
|
|
5
48
|
*
|
|
@@ -9,38 +52,10 @@ const DEFAULT_THEME = { style: 'rokkit', mode: 'dark', density: 'comfortable', s
|
|
|
9
52
|
export function themable(root, options) {
|
|
10
53
|
const { theme = DEFAULT_THEME, storageKey } = options ?? {}
|
|
11
54
|
|
|
12
|
-
if (storageKey)
|
|
13
|
-
|
|
14
|
-
theme.load(storageKey)
|
|
15
|
-
|
|
16
|
-
// Save changes to storage
|
|
17
|
-
$effect(() => {
|
|
18
|
-
theme.save(storageKey)
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
// Handle storage events
|
|
22
|
-
const handleStorage = (event) => {
|
|
23
|
-
if (event.key === storageKey && event.newValue !== null) {
|
|
24
|
-
try {
|
|
25
|
-
const newTheme = JSON.parse(event.newValue)
|
|
26
|
-
theme.update(newTheme)
|
|
27
|
-
} catch (e) {
|
|
28
|
-
// eslint-disable-next-line no-console
|
|
29
|
-
console.warn('Failed to parse theme from storage event for key "%s"', storageKey, e)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
// Set up storage event listener
|
|
34
|
-
$effect.root(() => {
|
|
35
|
-
window.addEventListener('storage', handleStorage)
|
|
36
|
-
return () => window.removeEventListener('storage', handleStorage)
|
|
37
|
-
})
|
|
38
|
-
}
|
|
55
|
+
if (storageKey) syncWithStorage(theme, storageKey)
|
|
56
|
+
|
|
39
57
|
$effect(() => {
|
|
40
|
-
root
|
|
41
|
-
root.dataset.mode = theme.mode
|
|
42
|
-
root.dataset.density = theme.density
|
|
43
|
-
root.dataset.skin = theme.skin
|
|
58
|
+
applyThemeAttrs(root, theme)
|
|
44
59
|
|
|
45
60
|
// Mirror onto documentElement too. The flash-prevention init script
|
|
46
61
|
// sets `html.dataset.*` before body parses; without this mirror,
|
|
@@ -50,13 +65,7 @@ export function themable(root, options) {
|
|
|
50
65
|
// so both the old html value and the new body value would apply,
|
|
51
66
|
// causing the partial / no-op style change users see when toggling.
|
|
52
67
|
if (typeof document !== 'undefined' && root !== document.documentElement) {
|
|
53
|
-
|
|
54
|
-
el.dataset.style = theme.style
|
|
55
|
-
el.dataset.mode = theme.mode
|
|
56
|
-
el.dataset.density = theme.density
|
|
57
|
-
el.dataset.skin = theme.skin
|
|
68
|
+
applyThemeAttrs(document.documentElement, theme)
|
|
58
69
|
}
|
|
59
|
-
|
|
60
|
-
// if (storageKey) theme.save(storageKey)
|
|
61
70
|
})
|
|
62
71
|
}
|
package/src/utils.js
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
1
|
import { find, toPairs } from 'ramda'
|
|
2
|
+
import { getClosestAncestorWithAttribute } from '@rokkit/core'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* @param {HTMLElement} element
|
|
7
|
-
* @param {string} attribute
|
|
8
|
-
* @returns {HTMLElement|null}
|
|
9
|
-
*/
|
|
10
|
-
export function getClosestAncestorWithAttribute(element, attribute) {
|
|
11
|
-
if (!element) return null
|
|
12
|
-
if (element.getAttribute(attribute)) return element
|
|
13
|
-
return getClosestAncestorWithAttribute(element.parentElement, attribute)
|
|
14
|
-
}
|
|
4
|
+
// Re-exported so `@rokkit/actions` keeps its existing public surface; the single
|
|
5
|
+
// implementation lives in @rokkit/core.
|
|
6
|
+
export { getClosestAncestorWithAttribute }
|
|
15
7
|
|
|
16
8
|
/**
|
|
17
9
|
* Get the event name for a given key.
|
|
@@ -50,7 +42,6 @@ export function handleAction(actions, event) {
|
|
|
50
42
|
export function getPathFromEvent(event) {
|
|
51
43
|
const node = getClosestAncestorWithAttribute(event.target, 'data-path')
|
|
52
44
|
return node?.getAttribute('data-path')
|
|
53
|
-
// return node ? getPathFromKey(node.getAttribute('data-path')) : null
|
|
54
45
|
}
|
|
55
46
|
|
|
56
47
|
/**
|