@rokkit/actions 1.4.0 → 1.4.2
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 +3 -2
- package/src/navigator.js +23 -4
- package/src/themable.svelte.js +53 -38
- package/src/utils.js +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rokkit/actions",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"description": "Contains generic actions that can be used in various components.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"prepublishOnly": "cp ../../LICENSE . && tsc --project tsconfig.build.json",
|
|
17
17
|
"postpublish": "rm -f LICENSE",
|
|
18
18
|
"clean": "rm -rf dist",
|
|
19
|
-
"build": "bun clean && bun prepublishOnly"
|
|
19
|
+
"build": "bun clean && bun prepublishOnly",
|
|
20
|
+
"check:build": "bun run clean && tsc --project tsconfig.build.json"
|
|
20
21
|
},
|
|
21
22
|
"files": [
|
|
22
23
|
"src/**/*.js",
|
package/src/navigator.js
CHANGED
|
@@ -85,16 +85,33 @@ const INTERACTIVE_SELECTOR = [
|
|
|
85
85
|
*/
|
|
86
86
|
function isNestedInteractive(target, root) {
|
|
87
87
|
const el = /** @type {HTMLElement|null} */ (target)
|
|
88
|
+
/* v8 ignore start -- a dispatched DOM event always carries a target; the null
|
|
89
|
+
branch exists for the EventTarget|null type, not for a reachable state.
|
|
90
|
+
`ignore next` does not fire on a single-line `if (...) return`. */
|
|
88
91
|
if (!el) return false
|
|
92
|
+
/* v8 ignore stop */
|
|
89
93
|
const interactive = /** @type {HTMLElement|null} */ (el.closest(INTERACTIVE_SELECTOR))
|
|
90
94
|
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
95
|
// A nested element explicitly declared as an accordion trigger is a
|
|
95
96
|
// Navigator-controlled control, not a stray interactive — let it through.
|
|
96
97
|
if (interactive.hasAttribute('data-accordion-trigger')) return false
|
|
97
|
-
return
|
|
98
|
+
return isStrictlyInsideItem(el, interactive)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* True when `interactive` sits STRICTLY inside the `[data-path]` item that
|
|
103
|
+
* contains `el` — i.e. the item exists, is not the interactive itself, and
|
|
104
|
+
* really is its ancestor. Being the item (a `<button data-path>` list row) is
|
|
105
|
+
* the normal case and must not count as nesting.
|
|
106
|
+
*
|
|
107
|
+
* @param {HTMLElement} el
|
|
108
|
+
* @param {HTMLElement} interactive
|
|
109
|
+
* @returns {boolean}
|
|
110
|
+
*/
|
|
111
|
+
function isStrictlyInsideItem(el, interactive) {
|
|
112
|
+
const item = /** @type {HTMLElement|null} */ (el.closest('[data-path]'))
|
|
113
|
+
if (!item || interactive === item) return false
|
|
114
|
+
return item.contains(interactive)
|
|
98
115
|
}
|
|
99
116
|
|
|
100
117
|
/**
|
|
@@ -109,7 +126,9 @@ function isNestedInteractive(target, root) {
|
|
|
109
126
|
*/
|
|
110
127
|
function isDisabledItem(target, root) {
|
|
111
128
|
const el = /** @type {HTMLElement|null} */ (target)
|
|
129
|
+
/* v8 ignore start -- see isNestedInteractive: event.target is never null */
|
|
112
130
|
if (!el) return false
|
|
131
|
+
/* v8 ignore stop */
|
|
113
132
|
const dataPathEl = /** @type {HTMLElement|null} */ (el.closest('[data-path]'))
|
|
114
133
|
if (!dataPathEl || !root.contains(dataPathEl)) return false
|
|
115
134
|
if (dataPathEl.hasAttribute('data-disabled')) return true
|
package/src/themable.svelte.js
CHANGED
|
@@ -1,5 +1,54 @@
|
|
|
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
|
+
// A plain $effect, not $effect.root. `$effect.root` creates a root detached
|
|
41
|
+
// from the surrounding lifecycle and hands the caller its disposer — which this
|
|
42
|
+
// code discarded, so the listener was never removed and every re-application of
|
|
43
|
+
// the action added another one. The sibling $effect above establishes that we
|
|
44
|
+
// are in an effect context, so a plain $effect ties the listener to the action's
|
|
45
|
+
// lifetime and its teardown actually runs.
|
|
46
|
+
$effect(() => {
|
|
47
|
+
window.addEventListener('storage', handleStorage)
|
|
48
|
+
return () => window.removeEventListener('storage', handleStorage)
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
3
52
|
/**
|
|
4
53
|
* Update the theme attributes when the state changes.
|
|
5
54
|
*
|
|
@@ -9,38 +58,10 @@ const DEFAULT_THEME = { style: 'rokkit', mode: 'dark', density: 'comfortable', s
|
|
|
9
58
|
export function themable(root, options) {
|
|
10
59
|
const { theme = DEFAULT_THEME, storageKey } = options ?? {}
|
|
11
60
|
|
|
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
|
-
}
|
|
61
|
+
if (storageKey) syncWithStorage(theme, storageKey)
|
|
62
|
+
|
|
39
63
|
$effect(() => {
|
|
40
|
-
root
|
|
41
|
-
root.dataset.mode = theme.mode
|
|
42
|
-
root.dataset.density = theme.density
|
|
43
|
-
root.dataset.skin = theme.skin
|
|
64
|
+
applyThemeAttrs(root, theme)
|
|
44
65
|
|
|
45
66
|
// Mirror onto documentElement too. The flash-prevention init script
|
|
46
67
|
// sets `html.dataset.*` before body parses; without this mirror,
|
|
@@ -50,13 +71,7 @@ export function themable(root, options) {
|
|
|
50
71
|
// so both the old html value and the new body value would apply,
|
|
51
72
|
// causing the partial / no-op style change users see when toggling.
|
|
52
73
|
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
|
|
74
|
+
applyThemeAttrs(document.documentElement, theme)
|
|
58
75
|
}
|
|
59
|
-
|
|
60
|
-
// if (storageKey) theme.save(storageKey)
|
|
61
76
|
})
|
|
62
77
|
}
|
package/src/utils.js
CHANGED
|
@@ -75,7 +75,10 @@ function findClosestWithAttribute(element, attribute) {
|
|
|
75
75
|
* @returns {boolean}
|
|
76
76
|
*/
|
|
77
77
|
function isAccordionTrigger(target) {
|
|
78
|
+
/* v8 ignore start -- unreachable: the only caller is isToggleTarget, which
|
|
79
|
+
dereferences target.parentElement first and would throw on null. */
|
|
78
80
|
if (!target) return false
|
|
81
|
+
/* v8 ignore stop */
|
|
79
82
|
const trigger = findClosestWithAttribute(target, 'data-accordion-trigger')
|
|
80
83
|
return trigger !== null
|
|
81
84
|
}
|