@rokkit/actions 1.4.0 → 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/package.json +1 -1
- package/src/navigator.js +17 -4
- package/src/themable.svelte.js +47 -38
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
|
/**
|
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
|
}
|