@skirbi/sugar 0.1.8 → 0.1.10
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/Changes +35 -0
- package/lib/boolean.mjs +1 -22
- package/lib/fakedom.mjs +1 -0
- package/lib/htmlelement-select.mjs +16 -5
- package/lib/htmlelement.mjs +1 -0
- package/lib/index.mjs +2 -2
- package/lib/livewire.mjs +99 -21
- package/lib/testing.mjs +1 -1
- package/lib/with-connected-sugar.mjs +64 -1
- package/package.json +9 -3
package/Changes
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
Revision history for @skirbi/sugar
|
|
2
2
|
|
|
3
|
+
0.1.10 2026-07-01 01:54:46Z
|
|
4
|
+
|
|
5
|
+
* Add tests to semtic-select
|
|
6
|
+
* Debugging artifacts in livewire.mjs
|
|
7
|
+
|
|
8
|
+
0.1.9 2026-06-10 21:49:27Z
|
|
9
|
+
|
|
10
|
+
* Add wc:rendered custom event to sugar
|
|
11
|
+
|
|
12
|
+
When components load the order in which they load seems kind of totally
|
|
13
|
+
random. This causes multiple connects and disconnects. Now, the Custom
|
|
14
|
+
Element spec defines a `moveBefore()` method, but unfortunatly, that
|
|
15
|
+
isn't supported by all browsers, most notably Safari. Which is also a
|
|
16
|
+
massive consumer of webcomponents in the whole Apple ecosystem.
|
|
17
|
+
|
|
18
|
+
The workaround to this problem is by giving sugar a way to detect if it
|
|
19
|
+
is inside the DOM-tree of another webcomponent. If sugar is inside a
|
|
20
|
+
webcomponent it will wait until its parent is rendered to actually
|
|
21
|
+
render itself. Within a page where all components are sugared, we can
|
|
22
|
+
safely say that parents are always rendered first, minimizing the amount
|
|
23
|
+
of (dis)connect calls.
|
|
24
|
+
|
|
25
|
+
It does this by looking at `el._internals` and checks for the rendered
|
|
26
|
+
state.
|
|
27
|
+
|
|
28
|
+
When we are part of a webcomponent chain where the parent isn't a
|
|
29
|
+
sugared component, we still wait for the parent to finish and hope our
|
|
30
|
+
parent does similar logic.
|
|
31
|
+
|
|
32
|
+
This approach is simply a workaround until Safari (and others) supports
|
|
33
|
+
`moveBefore`.
|
|
34
|
+
|
|
35
|
+
* Made sideEffects actually target files. The true was breaking things in
|
|
36
|
+
ways I didn't expect.
|
|
37
|
+
|
|
3
38
|
0.1.8 2026-06-08 22:43:46Z
|
|
4
39
|
|
|
5
40
|
* Forward value for htmlelement-input.mjs in all cases not just with
|
package/lib/boolean.mjs
CHANGED
|
@@ -1,22 +1 @@
|
|
|
1
|
-
|
|
2
|
-
//
|
|
3
|
-
// SPDX-License-Identifier: MIT
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Normalize an HTML attribute value to a boolean.
|
|
7
|
-
*
|
|
8
|
-
* This function interprets a variety of string-like inputs as either `true` or `false`:
|
|
9
|
-
* - Empty string ("") becomes true (e.g. <input disabled>)
|
|
10
|
-
* - "false" and "0" become false
|
|
11
|
-
* - All other defined values become true
|
|
12
|
-
*
|
|
13
|
-
* @param {string | null | undefined} value - The attribute value to parse
|
|
14
|
-
* @returns {boolean} The normalized boolean result
|
|
15
|
-
*/
|
|
16
|
-
export function parseBoolean(value) {
|
|
17
|
-
if (value === null || value === undefined) return false;
|
|
18
|
-
|
|
19
|
-
const val = String(value).toLowerCase().trim();
|
|
20
|
-
|
|
21
|
-
return val !== 'false' && val !== '0';
|
|
22
|
-
}
|
|
1
|
+
export { parseBoolean } from '@opndev/html';
|
package/lib/fakedom.mjs
CHANGED
|
@@ -40,6 +40,7 @@ export async function setupDomForTesting(
|
|
|
40
40
|
global.document = dom.window.document;
|
|
41
41
|
global.window = dom.window;
|
|
42
42
|
global.MutationObserver = dom.window.MutationObserver;
|
|
43
|
+
global.CustomEvent = dom.window.CustomEvent;
|
|
43
44
|
|
|
44
45
|
const projectRoot = process.cwd();
|
|
45
46
|
|
|
@@ -156,11 +156,12 @@ export class HTMLElementSugarSelect extends HTMLElementSugarInput {
|
|
|
156
156
|
else if (hasEndpoint) {
|
|
157
157
|
this._setupRemote(select, searchEl, cfg);
|
|
158
158
|
}
|
|
159
|
-
else
|
|
160
|
-
if (
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
159
|
+
else {
|
|
160
|
+
if (hasAuthored) {
|
|
161
|
+
this._setAuthoredOptions(select, authoredOptions, cfg);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
this._setOptions(select, opts, cfg);
|
|
164
165
|
}
|
|
165
166
|
|
|
166
167
|
if (searchEl) this._setupLocalSearch(select, searchEl);
|
|
@@ -173,6 +174,16 @@ export class HTMLElementSugarSelect extends HTMLElementSugarInput {
|
|
|
173
174
|
return select;
|
|
174
175
|
}
|
|
175
176
|
|
|
177
|
+
_setAuthoredOptions(select, authoredOptions, cfg) {
|
|
178
|
+
this._clearOptions(select);
|
|
179
|
+
|
|
180
|
+
if (cfg.placeholder) this._addPlaceholder(select, cfg.placeholder);
|
|
181
|
+
|
|
182
|
+
for (const node of authoredOptions) {
|
|
183
|
+
select.appendChild(node);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
176
187
|
// TODO: getByPath(x, json) might want to return a console.warn when it
|
|
177
188
|
// returns undefined. This would help developers spot mistakes? Yes/no/maybe?
|
|
178
189
|
|
package/lib/htmlelement.mjs
CHANGED
package/lib/index.mjs
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
//
|
|
3
3
|
// SPDX-License-Identifier: MIT
|
|
4
4
|
|
|
5
|
-
const VERSION = "0.1.
|
|
5
|
+
const VERSION = "0.1.10";
|
|
6
6
|
|
|
7
7
|
export { HTMLElementSugar } from './htmlelement.mjs';
|
|
8
8
|
export { HTMLElementSugarInput } from './htmlelement-input.mjs';
|
|
9
9
|
export { HTMLElementSugarSelect } from './htmlelement-select.mjs';
|
|
10
|
-
export { parseBoolean } from '
|
|
10
|
+
export { parseBoolean } from '@opndev/html';
|
|
11
11
|
export { registerDevAlias, applyDevAliases } from './aliases.mjs';
|
|
12
12
|
export { registerAliases } from './aliases-register.mjs';
|
|
13
13
|
export { withConnectedSugar } from './with-connected-sugar.mjs';
|
package/lib/livewire.mjs
CHANGED
|
@@ -16,27 +16,105 @@
|
|
|
16
16
|
* Only import this when using Sugar components inside a Livewire application.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
/*
|
|
20
|
+
console.log('[sugar-livewire] module loaded', {
|
|
21
|
+
hasLivewire: !!window.Livewire,
|
|
22
|
+
hasHook: !!window.Livewire?.hook,
|
|
23
|
+
});
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
let registered = false;
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Registers the Sugar Livewire hydration hook.
|
|
31
|
+
*
|
|
32
|
+
* @returns {void}
|
|
33
|
+
*/
|
|
34
|
+
function registerSugarLivewire() {
|
|
35
|
+
/*
|
|
36
|
+
console.log('[sugar-livewire] register called', {
|
|
37
|
+
registered,
|
|
38
|
+
hasLivewire: !!window.Livewire,
|
|
39
|
+
hasHook: !!window.Livewire?.hook,
|
|
40
|
+
});
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
if (registered) return;
|
|
44
|
+
if (!window.Livewire?.hook) return;
|
|
45
|
+
|
|
46
|
+
registered = true;
|
|
47
|
+
|
|
48
|
+
window.Livewire.hook('component.init', ({ component }) => {
|
|
49
|
+
|
|
50
|
+
// console.log('[sugar-livewire] component.init hit', component.id);
|
|
51
|
+
|
|
52
|
+
const original = component.processEffects.bind(component);
|
|
53
|
+
|
|
54
|
+
component.processEffects = function processSugarEffects(effects) {
|
|
55
|
+
/*
|
|
56
|
+
console.log('[sugar-livewire] definitions', {
|
|
57
|
+
dibuho: !!customElements.get('dibuho-container-panel'),
|
|
58
|
+
panel: !!customElements.get('semtic-panel'),
|
|
59
|
+
header: !!customElements.get('semtic-header'),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
console.log('[sugar-livewire] processEffects hit', {
|
|
63
|
+
hasHtml: !!effects.html,
|
|
64
|
+
sugarHydrated: !!effects._sugarHydrated,
|
|
65
|
+
});
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
if (!effects.html) return original(effects);
|
|
69
|
+
if (effects._sugarHydrated) return original(effects);
|
|
70
|
+
|
|
71
|
+
const before = effects.html;
|
|
72
|
+
|
|
73
|
+
const staging = document.createElement('div');
|
|
74
|
+
staging.style.display = 'none';
|
|
75
|
+
document.body.appendChild(staging);
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
staging.innerHTML = before;
|
|
79
|
+
|
|
80
|
+
const after = staging.innerHTML;
|
|
81
|
+
|
|
82
|
+
effects.html = after;
|
|
83
|
+
effects._sugarHydrated = true;
|
|
84
|
+
|
|
85
|
+
/*
|
|
86
|
+
console.group('[sugar-livewire] html');
|
|
87
|
+
console.log('before:\n', before);
|
|
88
|
+
console.log('after:\n', after);
|
|
89
|
+
console.log('changed:', before !== after);
|
|
90
|
+
console.log('handoff:\n', effects.html);
|
|
91
|
+
console.log('handoff is after:', effects.html === after);
|
|
92
|
+
console.groupEnd();
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
return original(effects);
|
|
96
|
+
} finally {
|
|
97
|
+
staging.remove();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
19
104
|
if (typeof document !== 'undefined') {
|
|
20
|
-
document.addEventListener('livewire:init',
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
component.processEffects = function(effects) {
|
|
25
|
-
if (!effects.html) return original(effects);
|
|
26
|
-
if (effects._sugarHydrated) return original(effects);
|
|
27
|
-
|
|
28
|
-
const staging = document.createElement('div');
|
|
29
|
-
staging.style.display = 'none';
|
|
30
|
-
document.body.appendChild(staging);
|
|
31
|
-
staging.innerHTML = effects.html;
|
|
32
|
-
|
|
33
|
-
Promise.resolve().then(() => Promise.resolve().then(() => {
|
|
34
|
-
effects.html = staging.innerHTML;
|
|
35
|
-
effects._sugarHydrated = true;
|
|
36
|
-
staging.remove();
|
|
37
|
-
original(effects);
|
|
38
|
-
}));
|
|
39
|
-
};
|
|
40
|
-
});
|
|
105
|
+
document.addEventListener('livewire:init', registerSugarLivewire);
|
|
106
|
+
|
|
107
|
+
queueMicrotask(() => {
|
|
108
|
+
registerSugarLivewire();
|
|
41
109
|
});
|
|
42
110
|
}
|
|
111
|
+
|
|
112
|
+
document.addEventListener('livewire:init', () => {
|
|
113
|
+
///console.log('[sugar-livewire] livewire:init heard');
|
|
114
|
+
registerSugarLivewire();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
queueMicrotask(() => {
|
|
118
|
+
//console.log('[sugar-livewire] microtask fallback');
|
|
119
|
+
registerSugarLivewire();
|
|
120
|
+
});
|
package/lib/testing.mjs
CHANGED
|
@@ -12,7 +12,7 @@ import t from 'tap';
|
|
|
12
12
|
* This helper verifies:
|
|
13
13
|
*
|
|
14
14
|
* 1. `Class.exampleHTML` matches the provided `options.example` snippet.
|
|
15
|
-
*
|
|
15
|
+
* 1. After mounting and lifecycle execution, the rendered output matches
|
|
16
16
|
* `Class.exampleRenderedHTML`.
|
|
17
17
|
*
|
|
18
18
|
* The component class must define:
|
|
@@ -2,20 +2,83 @@
|
|
|
2
2
|
//
|
|
3
3
|
// SPDX-License-Identifier: MIT
|
|
4
4
|
|
|
5
|
+
const sugarState = new WeakMap();
|
|
6
|
+
|
|
5
7
|
export const withConnectedSugar = (Base) =>
|
|
6
8
|
class extends Base {
|
|
7
9
|
static morphTriggerSelector = '';
|
|
8
10
|
|
|
9
11
|
connectedCallback() {
|
|
12
|
+
if (!sugarState.has(this)) {
|
|
13
|
+
sugarState.set(this, {
|
|
14
|
+
rendered: false,
|
|
15
|
+
firstUpdatedCalled: false,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
const state = sugarState.get(this);
|
|
19
|
+
|
|
10
20
|
this._syncObservedAttributesToConfig();
|
|
11
21
|
this._armMorphObserver();
|
|
22
|
+
|
|
12
23
|
if (this.connectedCallbackSugarOnce) {
|
|
13
24
|
this.connectedCallbackSugarOnce();
|
|
14
25
|
}
|
|
15
26
|
|
|
16
27
|
if (this._shouldRenderOnConnect()) {
|
|
17
|
-
this.
|
|
28
|
+
const wcParent = this._findWcParent();
|
|
29
|
+
|
|
30
|
+
const render = () => {
|
|
31
|
+
|
|
32
|
+
this.connectedCallbackSugar();
|
|
33
|
+
state.rendered = true;
|
|
34
|
+
|
|
35
|
+
const event = new CustomEvent('wc:rendered', { bubbles: false });
|
|
36
|
+
this.dispatchEvent(event);
|
|
37
|
+
|
|
38
|
+
if (!state.firstUpdatedCalled) {
|
|
39
|
+
Promise.resolve().then(() => {
|
|
40
|
+
state.firstUpdatedCalled = true;
|
|
41
|
+
this.firstUpdatedSugar?.();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
this.renderIfParent(wcParent, render);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
renderIfParent(parent = null, cb) {
|
|
51
|
+
|
|
52
|
+
if (!parent || !sugarState.has(parent)) {
|
|
53
|
+
cb();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const parentState = sugarState.get(parent);
|
|
57
|
+
if (parentState.rendered) {
|
|
58
|
+
cb();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const timeout = setTimeout(cb, 1000);
|
|
63
|
+
parent.addEventListener('wc:rendered', () => {
|
|
64
|
+
clearTimeout(timeout);
|
|
65
|
+
cb();
|
|
66
|
+
}, { once: true });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
_findWcParent() {
|
|
71
|
+
let el = this.parentElement;
|
|
72
|
+
while (el) {
|
|
73
|
+
if (el.tagName?.includes('-')) {
|
|
74
|
+
if (el._internals || el._sugarRendered !== undefined) {
|
|
75
|
+
return el;
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
el = el.parentElement;
|
|
18
80
|
}
|
|
81
|
+
return null;
|
|
19
82
|
}
|
|
20
83
|
|
|
21
84
|
_syncObservedAttributesToConfig() {
|
package/package.json
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
"url": "https://gitlab.com/skirbi/skirbi/-/issues"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@opndev/
|
|
10
|
+
"@opndev/html": ">=0.0.3",
|
|
11
|
+
"@opndev/util": "*"
|
|
11
12
|
},
|
|
12
13
|
"description": "Lightweight base layer for writing custom elements with declarative attributes and template sugar.",
|
|
13
14
|
"devDependencies": {
|
|
@@ -54,7 +55,12 @@
|
|
|
54
55
|
"release": "rzil release",
|
|
55
56
|
"test": "tap"
|
|
56
57
|
},
|
|
57
|
-
"sideEffects":
|
|
58
|
+
"sideEffects": [
|
|
59
|
+
"lib/livewire.mjs",
|
|
60
|
+
"lib/aliases-register.mjs",
|
|
61
|
+
"./livewire",
|
|
62
|
+
"./aliases-register"
|
|
63
|
+
],
|
|
58
64
|
"type": "module",
|
|
59
|
-
"version": "0.1.
|
|
65
|
+
"version": "0.1.10"
|
|
60
66
|
}
|