@wcomp/elementum 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +282 -2
- package/custom-elements.json +3613 -0
- package/mcp/server.js +593 -0
- package/package.json +77 -4
- package/skills/elementum-break-ui/CATALOG.md +11 -0
- package/skills/elementum-break-ui/SKILL.md +16 -0
- package/skills/elementum-mobile/SKILL.md +18 -0
- package/skills/elementum-motion/SKILL.md +18 -0
- package/skills/elementum-motion/STANDARDS.md +15 -0
- package/skills/elementum-penpot/SKILL.md +45 -0
- package/skills/elementum-ui/CATALOG.md +14 -0
- package/skills/elementum-ui/SKILL.md +21 -0
- package/src/ag-ui.js +474 -0
- package/src/behaviors/index.js +760 -0
- package/src/components/el-accordion.js +201 -0
- package/src/components/el-alert-dialog.js +186 -0
- package/src/components/el-alert.js +254 -0
- package/src/components/el-aspect-ratio.js +73 -0
- package/src/components/el-autocomplete.js +775 -0
- package/src/components/el-avatar.js +271 -0
- package/src/components/el-badge.js +131 -0
- package/src/components/el-border-beam.js +135 -0
- package/src/components/el-breadcrumb.js +203 -0
- package/src/components/el-button.js +231 -0
- package/src/components/el-calendar.js +436 -0
- package/src/components/el-callout.js +158 -0
- package/src/components/el-card.js +298 -0
- package/src/components/el-carousel.js +308 -0
- package/src/components/el-chart.js +632 -0
- package/src/components/el-checkbox-card.js +221 -0
- package/src/components/el-checkbox.js +208 -0
- package/src/components/el-chip.js +276 -0
- package/src/components/el-code-block.js +230 -0
- package/src/components/el-collapsible.js +124 -0
- package/src/components/el-command.js +556 -0
- package/src/components/el-context-menu.js +328 -0
- package/src/components/el-data-list.js +210 -0
- package/src/components/el-datepicker.js +192 -0
- package/src/components/el-dialog.js +272 -0
- package/src/components/el-diff-viewer.js +441 -0
- package/src/components/el-drawer.js +782 -0
- package/src/components/el-dropdown.js +182 -0
- package/src/components/el-dropzone.js +242 -0
- package/src/components/el-empty-state.js +130 -0
- package/src/components/el-field.js +394 -0
- package/src/components/el-form.js +185 -0
- package/src/components/el-input.js +229 -0
- package/src/components/el-inset.js +90 -0
- package/src/components/el-insight-card.js +263 -0
- package/src/components/el-kbd.js +151 -0
- package/src/components/el-menubar.js +254 -0
- package/src/components/el-meter.js +183 -0
- package/src/components/el-navbar.js +294 -0
- package/src/components/el-navigation-menu.js +327 -0
- package/src/components/el-number-field.js +510 -0
- package/src/components/el-number-flow.js +164 -0
- package/src/components/el-otp-input.js +266 -0
- package/src/components/el-pagination.js +231 -0
- package/src/components/el-popover.js +288 -0
- package/src/components/el-preview-card.js +202 -0
- package/src/components/el-progress.js +179 -0
- package/src/components/el-prompt-input.js +383 -0
- package/src/components/el-radio-card.js +360 -0
- package/src/components/el-radio.js +257 -0
- package/src/components/el-rating.js +165 -0
- package/src/components/el-reasoning-card.js +293 -0
- package/src/components/el-resizable.js +324 -0
- package/src/components/el-router.js +305 -0
- package/src/components/el-scroll-area.js +150 -0
- package/src/components/el-segmented-control.js +282 -0
- package/src/components/el-select.js +220 -0
- package/src/components/el-separator.js +85 -0
- package/src/components/el-sidebar.js +386 -0
- package/src/components/el-skeleton.js +156 -0
- package/src/components/el-slider.js +351 -0
- package/src/components/el-sources.js +288 -0
- package/src/components/el-spinner.js +159 -0
- package/src/components/el-streaming-text.js +199 -0
- package/src/components/el-switch.js +221 -0
- package/src/components/el-table.js +397 -0
- package/src/components/el-tabs.js +235 -0
- package/src/components/el-task-list.js +230 -0
- package/src/components/el-textarea.js +175 -0
- package/src/components/el-theme-switch.js +120 -0
- package/src/components/el-timeline.js +254 -0
- package/src/components/el-toast.js +778 -0
- package/src/components/el-toggle.js +311 -0
- package/src/components/el-tool-call.js +386 -0
- package/src/components/el-toolbar.js +183 -0
- package/src/components/el-tooltip.js +172 -0
- package/src/components/el-tree-view.js +261 -0
- package/src/components/el-visually-hidden.js +32 -0
- package/src/elementum.js +504 -0
- package/src/index.d.ts +1093 -0
- package/src/index.js +112 -0
- package/src/signal.js +215 -0
- package/src/ssr.js +136 -0
- package/src/template.js +321 -0
- package/src/tokens.css +416 -0
- package/tokens/figma-tokens.json +69 -0
- package/tokens/penpot-tokens.json +98 -0
package/src/elementum.js
ADDED
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Elementum Base Class
|
|
3
|
+
* Ultra-lightweight custom element with shadow root, signal lifecycle, and styles adoption.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { render, CSSResult, html, css } from './template.js';
|
|
7
|
+
import { signal, isSignal, effect } from './signal.js';
|
|
8
|
+
import { setHostState } from './behaviors/index.js';
|
|
9
|
+
|
|
10
|
+
export { html, css };
|
|
11
|
+
|
|
12
|
+
const BaseElement = typeof HTMLElement !== 'undefined' ? HTMLElement : class {};
|
|
13
|
+
|
|
14
|
+
function toCamelCase(attr) {
|
|
15
|
+
return attr.replace(/-([a-z])/g, (_, ch) => ch.toUpperCase());
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function toKebabCase(prop) {
|
|
19
|
+
return prop.replace(/[A-Z]/g, (ch) => `-${ch.toLowerCase()}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function hasCustomSubclassAccessor(instance, prop) {
|
|
23
|
+
let proto = Object.getPrototypeOf(instance);
|
|
24
|
+
while (proto && proto !== Elementum.prototype && proto !== BaseElement.prototype && proto !== Object.prototype) {
|
|
25
|
+
const desc = Object.getOwnPropertyDescriptor(proto, prop);
|
|
26
|
+
if (desc && (typeof desc.get === 'function' || typeof desc.set === 'function')) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
proto = Object.getPrototypeOf(proto);
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class Elementum extends BaseElement {
|
|
35
|
+
/**
|
|
36
|
+
* Set to false to disable Shadow DOM and render into Light DOM
|
|
37
|
+
*/
|
|
38
|
+
static shadowRoot = true;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Set to true to participate in native HTML <form> submission & validation
|
|
42
|
+
*/
|
|
43
|
+
static formAssociated = false;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* CSSResult or array of CSSResults (created via css`...`)
|
|
47
|
+
*/
|
|
48
|
+
static styles = null;
|
|
49
|
+
|
|
50
|
+
#abortController = null;
|
|
51
|
+
#mounted = false;
|
|
52
|
+
#syncingAttr = false;
|
|
53
|
+
#upgradedProps = new Set();
|
|
54
|
+
#reflectedSignals = new Set();
|
|
55
|
+
#boolAttrs = new Set();
|
|
56
|
+
#syntheticProps = new Set();
|
|
57
|
+
#signalEntries = [];
|
|
58
|
+
internals = null;
|
|
59
|
+
#validity = {
|
|
60
|
+
valid: true,
|
|
61
|
+
valueMissing: false,
|
|
62
|
+
typeMismatch: false,
|
|
63
|
+
patternMismatch: false,
|
|
64
|
+
tooLong: false,
|
|
65
|
+
tooShort: false,
|
|
66
|
+
rangeUnderflow: false,
|
|
67
|
+
rangeOverflow: false,
|
|
68
|
+
stepMismatch: false,
|
|
69
|
+
badInput: false,
|
|
70
|
+
customError: false,
|
|
71
|
+
};
|
|
72
|
+
#validationMessage = '';
|
|
73
|
+
|
|
74
|
+
constructor() {
|
|
75
|
+
super();
|
|
76
|
+
if (this.constructor.formAssociated && typeof this.attachInternals === 'function') {
|
|
77
|
+
this.internals = this.attachInternals();
|
|
78
|
+
}
|
|
79
|
+
queueMicrotask(() => this.#setupReactiveProperties());
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
setFormValue(value) {
|
|
83
|
+
if (this.internals && typeof this.internals.setFormValue === 'function') {
|
|
84
|
+
this.internals.setFormValue(value);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
get validity() {
|
|
89
|
+
return this.internals?.validity || this.#validity;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get validationMessage() {
|
|
93
|
+
return this.internals?.validationMessage || this.#validationMessage;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
get willValidate() {
|
|
97
|
+
return this.internals?.willValidate ?? true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
checkValidity() {
|
|
101
|
+
if (this.internals && typeof this.internals.checkValidity === 'function') {
|
|
102
|
+
return this.internals.checkValidity();
|
|
103
|
+
}
|
|
104
|
+
return this.validity.valid;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
reportValidity() {
|
|
108
|
+
if (this.internals && typeof this.internals.reportValidity === 'function') {
|
|
109
|
+
return this.internals.reportValidity();
|
|
110
|
+
}
|
|
111
|
+
const isValid = this.checkValidity();
|
|
112
|
+
if (!isValid) {
|
|
113
|
+
this.dispatchEvent(new Event('invalid', { bubbles: false, cancelable: true }));
|
|
114
|
+
}
|
|
115
|
+
return isValid;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
setValidity(flags = {}, message = '', anchor) {
|
|
119
|
+
const isInvalid = Object.values(flags).some(Boolean);
|
|
120
|
+
this.#validity = {
|
|
121
|
+
valid: !isInvalid,
|
|
122
|
+
valueMissing: !!flags.valueMissing,
|
|
123
|
+
typeMismatch: !!flags.typeMismatch,
|
|
124
|
+
patternMismatch: !!flags.patternMismatch,
|
|
125
|
+
tooLong: !!flags.tooLong,
|
|
126
|
+
tooShort: !!flags.tooShort,
|
|
127
|
+
rangeUnderflow: !!flags.rangeUnderflow,
|
|
128
|
+
rangeOverflow: !!flags.rangeOverflow,
|
|
129
|
+
stepMismatch: !!flags.stepMismatch,
|
|
130
|
+
badInput: !!flags.badInput,
|
|
131
|
+
customError: !!flags.customError,
|
|
132
|
+
};
|
|
133
|
+
this.#validationMessage = message || '';
|
|
134
|
+
|
|
135
|
+
if (this.internals && typeof this.internals.setValidity === 'function') {
|
|
136
|
+
if (anchor && typeof HTMLElement !== 'undefined' && anchor instanceof HTMLElement) {
|
|
137
|
+
this.internals.setValidity(flags, message, anchor);
|
|
138
|
+
} else {
|
|
139
|
+
this.internals.setValidity(flags, message);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
this.setHostState('invalid', isInvalid);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Reflect a standardized component state onto the host element via both
|
|
147
|
+
* `data-<name>` attributes and `ElementInternals.states` (`:state(<name>)`).
|
|
148
|
+
*/
|
|
149
|
+
setHostState(name, active) {
|
|
150
|
+
setHostState(this, name, active);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
setCustomValidity(message = '') {
|
|
154
|
+
if (message) {
|
|
155
|
+
this.setValidity({ customError: true }, message);
|
|
156
|
+
} else {
|
|
157
|
+
this.setValidity({});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
formResetCallback() {
|
|
162
|
+
if (typeof this.formReset === 'function') {
|
|
163
|
+
this.formReset();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
formDisabledCallback(disabled) {
|
|
168
|
+
if (typeof this.formDisabled === 'function') {
|
|
169
|
+
this.formDisabled(disabled);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
get signal() {
|
|
174
|
+
return this.#abortController?.signal;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
#reflectToAttribute(attrName, val, initialBoolDefault = false) {
|
|
178
|
+
if (this.#syncingAttr || !attrName) return;
|
|
179
|
+
if (typeof val === 'object' && val !== null) return;
|
|
180
|
+
if (this.constructor.formAssociated && (attrName === 'value' || attrName === 'checked')) return;
|
|
181
|
+
|
|
182
|
+
if (typeof val === 'boolean' || initialBoolDefault) {
|
|
183
|
+
const boolVal = typeof val === 'boolean' ? val : (val === 'false' ? false : Boolean(val));
|
|
184
|
+
if (boolVal) {
|
|
185
|
+
if (!this.hasAttribute(attrName) || this.getAttribute(attrName) === 'false') {
|
|
186
|
+
this.setAttribute(attrName, '');
|
|
187
|
+
}
|
|
188
|
+
} else if (this.hasAttribute(attrName)) {
|
|
189
|
+
this.removeAttribute(attrName);
|
|
190
|
+
}
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (val === null || val === undefined) {
|
|
195
|
+
if (this.hasAttribute(attrName)) {
|
|
196
|
+
this.removeAttribute(attrName);
|
|
197
|
+
}
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const strVal = String(val);
|
|
202
|
+
if (this.getAttribute(attrName) !== strVal) {
|
|
203
|
+
this.setAttribute(attrName, strVal);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
#getMappedAttrValue(attr) {
|
|
208
|
+
if (!this.hasAttribute(attr)) {
|
|
209
|
+
return this.#boolAttrs.has(attr) ? false : null;
|
|
210
|
+
}
|
|
211
|
+
const val = this.getAttribute(attr);
|
|
212
|
+
if (this.#boolAttrs.has(attr)) {
|
|
213
|
+
return val !== 'false';
|
|
214
|
+
}
|
|
215
|
+
return val;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
#syncAttributeToProperty(attr, rawVal) {
|
|
219
|
+
const camel = toCamelCase(attr);
|
|
220
|
+
const propKey = (camel !== attr && camel in this) ? camel : attr;
|
|
221
|
+
if (!(propKey in this)) return;
|
|
222
|
+
if (typeof this[propKey] === 'function' || this.#syntheticProps.has(propKey)) return;
|
|
223
|
+
|
|
224
|
+
const current = this[propKey];
|
|
225
|
+
const isBool = this.#boolAttrs.has(attr) ||
|
|
226
|
+
(isSignal(current) ? typeof current.peek() === 'boolean' : typeof current === 'boolean');
|
|
227
|
+
const mappedVal = isBool ? (rawVal !== null && rawVal !== 'false') : rawVal;
|
|
228
|
+
|
|
229
|
+
if (isSignal(current)) {
|
|
230
|
+
current.value = mappedVal;
|
|
231
|
+
} else {
|
|
232
|
+
this[propKey] = mappedVal;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
#defineSignalProperty(propKey, attrName, sig, isBool, initialVal) {
|
|
237
|
+
if (isBool && attrName) {
|
|
238
|
+
this.#boolAttrs.add(attrName);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
this.#upgradedProps.add(propKey);
|
|
242
|
+
if (attrName) {
|
|
243
|
+
this.#signalEntries.push({ attrName, sig, isBool, initialVal });
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
Object.defineProperty(this, propKey, {
|
|
247
|
+
configurable: true,
|
|
248
|
+
enumerable: true,
|
|
249
|
+
get: () => sig,
|
|
250
|
+
set: (val) => {
|
|
251
|
+
if (isSignal(val)) return;
|
|
252
|
+
sig.value = val;
|
|
253
|
+
if (attrName) {
|
|
254
|
+
this.#reflectToAttribute(attrName, val, isBool);
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
#setupReactiveProperties() {
|
|
261
|
+
const observed = this.constructor.observedAttributes;
|
|
262
|
+
const observedSet = Array.isArray(observed) ? new Set(observed) : null;
|
|
263
|
+
|
|
264
|
+
// 1. Upgrade own Signal properties into two-way reactive accessors
|
|
265
|
+
for (const propKey of Object.keys(this)) {
|
|
266
|
+
if (propKey.startsWith('_') || this.#upgradedProps.has(propKey)) continue;
|
|
267
|
+
if (hasCustomSubclassAccessor(this, propKey)) continue;
|
|
268
|
+
|
|
269
|
+
const desc = Object.getOwnPropertyDescriptor(this, propKey);
|
|
270
|
+
if (!desc || !desc.configurable || !('value' in desc) || !isSignal(desc.value)) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const sig = desc.value;
|
|
275
|
+
const initialVal = sig.peek();
|
|
276
|
+
const isBool = typeof initialVal === 'boolean';
|
|
277
|
+
const kebab = toKebabCase(propKey);
|
|
278
|
+
const attrName = observedSet
|
|
279
|
+
? (observedSet.has(propKey) ? propKey : (observedSet.has(kebab) ? kebab : null))
|
|
280
|
+
: null;
|
|
281
|
+
|
|
282
|
+
this.#defineSignalProperty(propKey, attrName, sig, isBool, initialVal);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// 2. Ensure remaining observedAttributes have reactive property accessors
|
|
286
|
+
if (!observedSet) return;
|
|
287
|
+
for (const attr of observedSet) {
|
|
288
|
+
const camel = toCamelCase(attr);
|
|
289
|
+
const propKey = (attr in this && !(camel in this)) ? attr : camel;
|
|
290
|
+
|
|
291
|
+
if (this.#upgradedProps.has(propKey) || this.#upgradedProps.has(attr)) continue;
|
|
292
|
+
if (hasCustomSubclassAccessor(this, propKey) || (propKey !== attr && hasCustomSubclassAccessor(this, attr))) {
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const ownDesc = Object.getOwnPropertyDescriptor(this, propKey);
|
|
297
|
+
if (!ownDesc && propKey in BaseElement.prototype) {
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (ownDesc && !ownDesc.configurable) continue;
|
|
302
|
+
if (ownDesc && !('value' in ownDesc)) continue;
|
|
303
|
+
|
|
304
|
+
if (typeof this[propKey] === 'function') {
|
|
305
|
+
const origMethod = this[propKey];
|
|
306
|
+
this.#upgradedProps.add(propKey);
|
|
307
|
+
this.#boolAttrs.add(attr);
|
|
308
|
+
Object.defineProperty(this, propKey, {
|
|
309
|
+
configurable: true,
|
|
310
|
+
enumerable: true,
|
|
311
|
+
get: () => origMethod,
|
|
312
|
+
set: (val) => {
|
|
313
|
+
const boolVal = typeof val === 'boolean' ? val : (val === 'false' ? false : Boolean(val));
|
|
314
|
+
this.#reflectToAttribute(attr, boolVal, true);
|
|
315
|
+
},
|
|
316
|
+
});
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Reconstruct Signal when a pre-mount synchronous assignment overwrote a field before microtask setup
|
|
321
|
+
const hasInitialOwnValue = Boolean(ownDesc && 'value' in ownDesc);
|
|
322
|
+
if (hasInitialOwnValue) {
|
|
323
|
+
const initialVal = ownDesc.value;
|
|
324
|
+
const sig = signal(initialVal);
|
|
325
|
+
const isBool = typeof initialVal === 'boolean';
|
|
326
|
+
this.#defineSignalProperty(propKey, attr, sig, isBool, initialVal);
|
|
327
|
+
if (initialVal !== undefined && !this.hasAttribute(attr)) {
|
|
328
|
+
this.#reflectToAttribute(attr, initialVal, isBool);
|
|
329
|
+
}
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
this.#upgradedProps.add(propKey);
|
|
334
|
+
this.#syntheticProps.add(propKey);
|
|
335
|
+
|
|
336
|
+
Object.defineProperty(this, propKey, {
|
|
337
|
+
configurable: true,
|
|
338
|
+
enumerable: true,
|
|
339
|
+
get: () => this.#getMappedAttrValue(attr),
|
|
340
|
+
set: (val) => {
|
|
341
|
+
if (typeof val === 'boolean') {
|
|
342
|
+
this.#boolAttrs.add(attr);
|
|
343
|
+
}
|
|
344
|
+
this.#reflectToAttribute(attr, val, this.#boolAttrs.has(attr));
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
#setupAttributeReflection() {
|
|
351
|
+
if (!this.#abortController) return;
|
|
352
|
+
for (const { attrName, sig, isBool, initialVal } of this.#signalEntries) {
|
|
353
|
+
if (!attrName || this.#reflectedSignals.has(sig)) continue;
|
|
354
|
+
this.#reflectedSignals.add(sig);
|
|
355
|
+
|
|
356
|
+
let initialized = false;
|
|
357
|
+
effect(() => {
|
|
358
|
+
const val = sig.value;
|
|
359
|
+
if (!initialized) {
|
|
360
|
+
initialized = true;
|
|
361
|
+
if (val !== initialVal && !this.hasAttribute(attrName)) {
|
|
362
|
+
this.#reflectToAttribute(attrName, val, isBool);
|
|
363
|
+
}
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
this.#reflectToAttribute(attrName, val, isBool);
|
|
367
|
+
}, { signal: this.#abortController.signal });
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
connectedCallback() {
|
|
372
|
+
this.#abortController = new AbortController();
|
|
373
|
+
|
|
374
|
+
// 1. Shadow Root initialization
|
|
375
|
+
if (this.constructor.shadowRoot !== false) {
|
|
376
|
+
if (!this.shadowRoot) {
|
|
377
|
+
this.attachShadow({ mode: 'open' });
|
|
378
|
+
}
|
|
379
|
+
this.root = this.shadowRoot;
|
|
380
|
+
|
|
381
|
+
// 2. Adopt Constructable Stylesheets (skip when `unstyled` attribute is present for headless styling)
|
|
382
|
+
if (this.constructor.styles && !this.hasAttribute('unstyled')) {
|
|
383
|
+
const rawStyles = Array.isArray(this.constructor.styles)
|
|
384
|
+
? this.constructor.styles
|
|
385
|
+
: [this.constructor.styles];
|
|
386
|
+
|
|
387
|
+
const sheets = [];
|
|
388
|
+
const fallbackStyles = [];
|
|
389
|
+
|
|
390
|
+
for (const s of rawStyles) {
|
|
391
|
+
if (s instanceof CSSResult && s.sheet) {
|
|
392
|
+
sheets.push(s.sheet);
|
|
393
|
+
} else {
|
|
394
|
+
fallbackStyles.push(String(s));
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (sheets.length > 0 && typeof Document !== 'undefined' && 'adoptedStyleSheets' in Document.prototype) {
|
|
399
|
+
this.shadowRoot.adoptedStyleSheets = sheets;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (fallbackStyles.length > 0) {
|
|
403
|
+
const styleEl = document.createElement('style');
|
|
404
|
+
styleEl.textContent = fallbackStyles.join('\n');
|
|
405
|
+
this.shadowRoot.appendChild(styleEl);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
} else {
|
|
409
|
+
this.root = this;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// 3. Ensure reactive properties are initialized before syncing attributes or rendering
|
|
413
|
+
this.#setupReactiveProperties();
|
|
414
|
+
|
|
415
|
+
// Sync initial HTML attributes into signals or properties
|
|
416
|
+
const observed = this.constructor.observedAttributes;
|
|
417
|
+
if (Array.isArray(observed)) {
|
|
418
|
+
this.#syncingAttr = true;
|
|
419
|
+
try {
|
|
420
|
+
for (const attr of observed) {
|
|
421
|
+
if (this.hasAttribute(attr)) {
|
|
422
|
+
this.#syncAttributeToProperty(attr, this.getAttribute(attr));
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
} finally {
|
|
426
|
+
this.#syncingAttr = false;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// Reflect initial disabled host state if present
|
|
431
|
+
if (this.hasAttribute('disabled') && this.getAttribute('disabled') !== 'false') {
|
|
432
|
+
this.setHostState('disabled', true);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// 4. User lifecycle hook
|
|
436
|
+
if (typeof this.connected === 'function') {
|
|
437
|
+
this.connected();
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Reflect signal updates back to host HTML attributes while connected
|
|
441
|
+
this.#setupAttributeReflection();
|
|
442
|
+
|
|
443
|
+
// 5. Initial Render
|
|
444
|
+
if (typeof this.render === 'function') {
|
|
445
|
+
const templateResult = this.render();
|
|
446
|
+
if (templateResult) {
|
|
447
|
+
render(templateResult, this.root, this.#abortController.signal);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
this.#mounted = true;
|
|
452
|
+
|
|
453
|
+
if (typeof this.rendered === 'function') {
|
|
454
|
+
this.rendered();
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
disconnectedCallback() {
|
|
459
|
+
if (this.#abortController) {
|
|
460
|
+
this.#abortController.abort();
|
|
461
|
+
}
|
|
462
|
+
this.#reflectedSignals.clear();
|
|
463
|
+
this.#mounted = false;
|
|
464
|
+
|
|
465
|
+
if (typeof this.disconnected === 'function') {
|
|
466
|
+
this.disconnected();
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
471
|
+
if (oldValue === newValue) return;
|
|
472
|
+
|
|
473
|
+
if (name === 'disabled') {
|
|
474
|
+
this.setHostState('disabled', newValue !== null && newValue !== 'false');
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
this.#syncingAttr = true;
|
|
478
|
+
try {
|
|
479
|
+
if (typeof this.attributeChanged === 'function') {
|
|
480
|
+
this.attributeChanged(name, oldValue, newValue);
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// Sync attribute changes directly into matching signals or properties
|
|
485
|
+
this.#syncAttributeToProperty(name, newValue);
|
|
486
|
+
} finally {
|
|
487
|
+
this.#syncingAttr = false;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Helper to dispatch standard bubbling, composed CustomEvents
|
|
493
|
+
*/
|
|
494
|
+
emit(eventName, detail = null, options = {}) {
|
|
495
|
+
return this.dispatchEvent(
|
|
496
|
+
new CustomEvent(eventName, {
|
|
497
|
+
bubbles: true,
|
|
498
|
+
composed: true,
|
|
499
|
+
detail,
|
|
500
|
+
...options,
|
|
501
|
+
})
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
}
|