@webtides/element-js 1.0.0-alpha.1 → 1.0.0-alpha.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/docs/README.md +174 -0
- package/docs/concepts/classes.md +167 -0
- package/docs/concepts/hierarchy.md +61 -0
- package/docs/concepts/lifecycle.md +45 -0
- package/docs/concepts/reactivity.md +14 -0
- package/docs/features/computed-properties.md +28 -0
- package/docs/features/dependency-injection-context-protocol.md +132 -0
- package/docs/features/events.md +155 -0
- package/docs/features/global-styles.md +131 -0
- package/docs/features/methods.md +34 -0
- package/docs/features/properties.md +56 -0
- package/docs/features/refs.md +47 -0
- package/docs/features/renderless.md +70 -0
- package/docs/features/store.md +143 -0
- package/docs/features/styles.md +169 -0
- package/docs/features/templates.md +308 -0
- package/docs/features/watch.md +54 -0
- package/docs/getting-started/upgrade-to-v1.md +243 -0
- package/docs/guide/guide.md +112 -0
- package/index.js +9 -2
- package/package.json +15 -19
- package/src/BaseElement.js +201 -42
- package/src/StyledElement.js +67 -48
- package/src/TemplateElement.js +31 -1
- package/src/dom-parts/AttributePart.js +16 -9
- package/src/dom-parts/ChildNodePart.js +8 -22
- package/src/dom-parts/NodePart.js +1 -0
- package/src/dom-parts/TemplatePart.js +19 -25
- package/src/dom-parts/TemplateResult.js +46 -35
- package/src/dom-parts/directives.js +43 -16
- package/src/dom-parts/html.js +1 -1
- package/src/util/AttributeParser.js +54 -21
- package/src/util/DOMHelper.js +63 -14
- package/src/util/Directive.js +21 -1
- package/src/util/GlobalStylesStore.js +66 -0
- package/src/util/SerializeStateHelper.js +103 -0
- package/src/util/Store.js +111 -15
- package/src/util/defineElement.js +2 -2
- package/src/util/toString.js +2 -2
- package/dist/index.js +0 -1330
- package/dist/index.js.map +0 -7
- package/src/util/i18n.js +0 -20
package/src/BaseElement.js
CHANGED
|
@@ -1,11 +1,54 @@
|
|
|
1
1
|
import { parseAttribute, isNaN, dashToCamel, camelToDash, isObjectLike } from './util/AttributeParser.js';
|
|
2
|
-
import { getClosestParentCustomElementNode, isOfSameNodeType } from './util/DOMHelper.js';
|
|
2
|
+
import { getAllElementChildren, getClosestParentCustomElementNode, isOfSameNodeType } from './util/DOMHelper.js';
|
|
3
3
|
import { Store } from './util/Store.js';
|
|
4
|
-
|
|
4
|
+
import { serializeState, deserializeState, hasSerializedState } from './util/SerializeStateHelper.js';
|
|
5
5
|
export { defineElement } from './util/defineElement.js';
|
|
6
6
|
export { toString } from './util/toString.js';
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Options object for the BaseElement
|
|
10
|
+
* @typedef {Object} BaseElementOptions
|
|
11
|
+
* @property {boolean} [autoUpdate] - When set to true the element will call the requestUpdate() method on the instance every time a property or attribute was changed. This will re-evaluate everything on the element and trigger a re-render (if a template is provided) and trigger the watchers for the affected properties/attributes. Default is `true`
|
|
12
|
+
* @property {boolean} [deferUpdate] - When set to true the element will not call the requestUpdate() method upon connecting and therefore will not render (if template was provided) initially. Default is `true`
|
|
13
|
+
* @property {boolean} [deferConnected] - When set to true the element will skip the initialization in connectedCallback() before connecting and therefore will not render (if template was provided) or hydrate initially. Default is `false`
|
|
14
|
+
* @property {MutationObserverOptions} [mutationObserverOptions]
|
|
15
|
+
* @property {PropertyOptions} [propertyOptions]
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Options object for the BaseElement mutationObserverOptions option
|
|
20
|
+
* @typedef {Object} MutationObserverOptions
|
|
21
|
+
* @property {boolean} [childList] - When childList is set to true the element will also call the requestUpdate method for childList modifications (eg. Adding or removing child elements). Default is `true`
|
|
22
|
+
* @property {boolean} [subtree] - Please note that observing subtree mutations might have performance implications and use it only if necessary. Default is `false`
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Options object for the BaseElement propertyOptions option
|
|
27
|
+
* @typedef {Object} PropertyOptions
|
|
28
|
+
* @property {undefined | boolean | function} [reflect] - When set to true the element will reflect property changes back to attributes if the attribute was not present when connecting the element. By default, all attributes that are present when connecting the element will be reflected anyway. Default is `undefined`
|
|
29
|
+
* @property {undefined | boolean | function} [parse] - When set to false the element will not automatically try to parse the attributes string value to a complex type (number, array, object). Default is `undefined`
|
|
30
|
+
* @property {undefined | boolean | function} [notify] - When set to true the element will dispatch CustomEvents for every property/attribute change. The event name will be the property name all lowercase and camel to dashes with a postfix of -changed. Default is `undefined`
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Options object for updating properties
|
|
35
|
+
* @typedef {Object} PropertyUpdateOptions
|
|
36
|
+
* @property {boolean} [notify] - Whether the update should trigger the lifecycle methods. The default is `true`
|
|
37
|
+
* @property {string} property - The property name
|
|
38
|
+
* @property {any} newValue - The new value for the property
|
|
39
|
+
* @property {string} newValueString - The new value as `string` for the property (for comparison reasons)
|
|
40
|
+
* @property {any} oldValue - The old value for the property
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @implements {Serializable}
|
|
45
|
+
*/
|
|
8
46
|
class BaseElement extends HTMLElement {
|
|
47
|
+
_serializationKey;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {BaseElementOptions} options
|
|
51
|
+
*/
|
|
9
52
|
constructor(options = {}) {
|
|
10
53
|
super();
|
|
11
54
|
this.$refs = {};
|
|
@@ -17,6 +60,7 @@ class BaseElement extends HTMLElement {
|
|
|
17
60
|
this._options = {
|
|
18
61
|
autoUpdate: true,
|
|
19
62
|
deferUpdate: true,
|
|
63
|
+
deferConnected: false,
|
|
20
64
|
mutationObserverOptions: {
|
|
21
65
|
attributes: true,
|
|
22
66
|
childList: true,
|
|
@@ -28,7 +72,32 @@ class BaseElement extends HTMLElement {
|
|
|
28
72
|
};
|
|
29
73
|
}
|
|
30
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Overrides the native `connectedCallback` of the HTMLElement to set up and initialize our element.
|
|
77
|
+
* This will define the attributes and properties as reactive getters and setter,
|
|
78
|
+
* register observers, refs and events.
|
|
79
|
+
*/
|
|
31
80
|
connectedCallback() {
|
|
81
|
+
if (this.hasAttribute('defer-update')) {
|
|
82
|
+
this._options.deferUpdate = true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (this.hasAttribute('defer-connected') || this._options.deferConnected) {
|
|
86
|
+
this._options.deferConnected = false;
|
|
87
|
+
this.removeAttribute('defer-connected');
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (this.hasAttribute('ejs:key')) {
|
|
92
|
+
this._serializationKey = this.getAttribute('ejs:key');
|
|
93
|
+
deserializeState(this);
|
|
94
|
+
} else {
|
|
95
|
+
this._serializationKey = globalThis.crypto.randomUUID();
|
|
96
|
+
if (globalThis.elementJsConfig?.serializeState) {
|
|
97
|
+
this.setAttribute('ejs:key', this._serializationKey);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
32
101
|
// define all attributes to "this" as properties
|
|
33
102
|
this.defineAttributesAsProperties();
|
|
34
103
|
|
|
@@ -41,7 +110,7 @@ class BaseElement extends HTMLElement {
|
|
|
41
110
|
// define everything that should be observed
|
|
42
111
|
this.defineObserver();
|
|
43
112
|
|
|
44
|
-
if (this.
|
|
113
|
+
if (this._options.deferUpdate) {
|
|
45
114
|
// don't update/render, but register refs and events
|
|
46
115
|
this.registerEventsAndRefs();
|
|
47
116
|
|
|
@@ -54,6 +123,9 @@ class BaseElement extends HTMLElement {
|
|
|
54
123
|
}
|
|
55
124
|
}
|
|
56
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Define a MutationObserver on the element to get notified on attribute changes and childList updates according to the options specified via the constructor options.
|
|
128
|
+
*/
|
|
57
129
|
defineObserver() {
|
|
58
130
|
// see: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver#Example_usage
|
|
59
131
|
this._mutationObserver = new MutationObserver((mutations) => {
|
|
@@ -86,6 +158,9 @@ class BaseElement extends HTMLElement {
|
|
|
86
158
|
});
|
|
87
159
|
}
|
|
88
160
|
|
|
161
|
+
/**
|
|
162
|
+
* Native `disconnectedCallback` of the HTMLElement
|
|
163
|
+
*/
|
|
89
164
|
disconnectedCallback() {
|
|
90
165
|
// remove events for elements in the component
|
|
91
166
|
this.removeEvents();
|
|
@@ -107,6 +182,11 @@ class BaseElement extends HTMLElement {
|
|
|
107
182
|
this.triggerHook('disconnected');
|
|
108
183
|
}
|
|
109
184
|
|
|
185
|
+
/**
|
|
186
|
+
* Request and batch an asynchronous update cycle
|
|
187
|
+
* @param {PropertyUpdateOptions} options
|
|
188
|
+
* @returns {Promise<unknown>}
|
|
189
|
+
*/
|
|
110
190
|
requestUpdate(options = { notify: true }) {
|
|
111
191
|
if (options.notify === true) {
|
|
112
192
|
this.triggerHook('beforeUpdate');
|
|
@@ -136,6 +216,7 @@ class BaseElement extends HTMLElement {
|
|
|
136
216
|
* This should be called by the template component AFTER adding the template to
|
|
137
217
|
* the DOM.
|
|
138
218
|
* Here we will register the events and the refs for the element.
|
|
219
|
+
* @param {PropertyUpdateOptions} options
|
|
139
220
|
*/
|
|
140
221
|
update(options = { notify: true }) {
|
|
141
222
|
this.registerEventsAndRefs();
|
|
@@ -202,12 +283,11 @@ class BaseElement extends HTMLElement {
|
|
|
202
283
|
* Define a property on the element
|
|
203
284
|
* Will trigger watch() function when a property was changed
|
|
204
285
|
* Will trigger update() when a property was changed
|
|
286
|
+
* @param {string} property
|
|
287
|
+
* @param {any} value
|
|
288
|
+
* @param {boolean} reflectAttribute
|
|
205
289
|
*/
|
|
206
290
|
defineProperty(property, value, reflectAttribute = false) {
|
|
207
|
-
if (value instanceof Store) {
|
|
208
|
-
value.subscribe(this);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
291
|
if (this._state.hasOwnProperty(property)) {
|
|
212
292
|
// property has already been defined as an attribute nothing to do here
|
|
213
293
|
return;
|
|
@@ -223,8 +303,16 @@ class BaseElement extends HTMLElement {
|
|
|
223
303
|
this.removeAttribute(camelToDash(property));
|
|
224
304
|
}
|
|
225
305
|
|
|
306
|
+
if (hasSerializedState(this._serializationKey) && Object.keys(this.serializeState())?.includes(property)) {
|
|
307
|
+
value = this[property];
|
|
308
|
+
}
|
|
309
|
+
|
|
226
310
|
this._state[property] = value;
|
|
227
311
|
|
|
312
|
+
if (this._state[property] instanceof Store) {
|
|
313
|
+
this._state[property].subscribe(this);
|
|
314
|
+
}
|
|
315
|
+
|
|
228
316
|
Object.defineProperty(this, property, {
|
|
229
317
|
get: () => {
|
|
230
318
|
return this._state[property];
|
|
@@ -235,6 +323,8 @@ class BaseElement extends HTMLElement {
|
|
|
235
323
|
if (JSON.stringify(oldValue) !== newValueString) {
|
|
236
324
|
this._state[property] = newValue;
|
|
237
325
|
|
|
326
|
+
serializeState(this);
|
|
327
|
+
|
|
238
328
|
if (newValue instanceof Store) {
|
|
239
329
|
newValue.subscribe(this);
|
|
240
330
|
}
|
|
@@ -265,12 +355,10 @@ class BaseElement extends HTMLElement {
|
|
|
265
355
|
}
|
|
266
356
|
|
|
267
357
|
/**
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
* @param
|
|
271
|
-
* @param
|
|
272
|
-
* @param newValue
|
|
273
|
-
* @param oldValue
|
|
358
|
+
* Call the watch callbacks on property changes
|
|
359
|
+
* @param {string} property property that changes
|
|
360
|
+
* @param {any} newValue
|
|
361
|
+
* @param {any} oldValue
|
|
274
362
|
*/
|
|
275
363
|
callPropertyWatcher(property, newValue, oldValue) {
|
|
276
364
|
// notify watched properties (after update())
|
|
@@ -279,12 +367,11 @@ class BaseElement extends HTMLElement {
|
|
|
279
367
|
watch[property](newValue, oldValue);
|
|
280
368
|
}
|
|
281
369
|
}
|
|
370
|
+
|
|
282
371
|
/**
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
* @param
|
|
286
|
-
* @param property property that changes
|
|
287
|
-
* @param newValue
|
|
372
|
+
* Notify property observer via change event
|
|
373
|
+
* @param {string} property property that changes
|
|
374
|
+
* @param {any} newValue
|
|
288
375
|
*/
|
|
289
376
|
notifyPropertyChange(property, newValue) {
|
|
290
377
|
// dispatch change event
|
|
@@ -292,10 +379,14 @@ class BaseElement extends HTMLElement {
|
|
|
292
379
|
property in this._options['propertyOptions'] &&
|
|
293
380
|
this._options['propertyOptions'][property]['notify'] === true
|
|
294
381
|
) {
|
|
295
|
-
this.dispatch(`${camelToDash(property)}-changed`, newValue
|
|
382
|
+
this.dispatch(`${camelToDash(property)}-changed`, newValue);
|
|
296
383
|
}
|
|
297
384
|
}
|
|
298
385
|
|
|
386
|
+
/**
|
|
387
|
+
* Reflects a property as attribute on the element
|
|
388
|
+
* @param {PropertyUpdateOptions} options
|
|
389
|
+
*/
|
|
299
390
|
reflectProperty(options) {
|
|
300
391
|
const { property, newValue } = options;
|
|
301
392
|
const newValueString = options.newValueString || JSON.stringify(newValue);
|
|
@@ -337,30 +428,40 @@ class BaseElement extends HTMLElement {
|
|
|
337
428
|
this.requestContext(key, value);
|
|
338
429
|
});
|
|
339
430
|
// define context provider
|
|
340
|
-
|
|
431
|
+
const providedKeys = Object.keys(this.provideProperties());
|
|
432
|
+
if (providedKeys.length > 0) {
|
|
341
433
|
this.addEventListener('request-context', this.onRequestContext);
|
|
434
|
+
// check if there are already connected elements in child dom and restart requests
|
|
435
|
+
getAllElementChildren(this.getRoot(), BaseElement).forEach((customChild) => {
|
|
436
|
+
// if injectProperties?.() is defined it means that the child got connected BEFORE the parent (Runtime Issue)
|
|
437
|
+
const requestedProperties = customChild.injectProperties?.() ?? {};
|
|
438
|
+
Object.entries(requestedProperties).forEach(([key, value]) => {
|
|
439
|
+
// iterate over requested properties to find missed context requests
|
|
440
|
+
if (providedKeys.includes(key)) {
|
|
441
|
+
// owns the provided key -> replay request
|
|
442
|
+
customChild.requestContext(key, value);
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
});
|
|
342
446
|
}
|
|
343
447
|
}
|
|
344
448
|
|
|
345
449
|
/**
|
|
346
450
|
* Helper Function to initialize the context requests
|
|
347
|
-
*
|
|
348
|
-
* @param
|
|
349
|
-
* @param valueOrCallback
|
|
451
|
+
* @param {string} propertyName
|
|
452
|
+
* @param {any | function} valueOrCallback
|
|
350
453
|
*/
|
|
351
454
|
requestContext(propertyName, valueOrCallback) {
|
|
352
|
-
this.dispatch('request-context', { [propertyName]: valueOrCallback }
|
|
455
|
+
this.dispatch('request-context', { [propertyName]: valueOrCallback });
|
|
353
456
|
}
|
|
354
457
|
|
|
355
458
|
/**
|
|
356
459
|
* Internal Context Request Handler
|
|
357
460
|
* This will check if the current/receiving instance actually provides a value under the requested name.
|
|
358
461
|
* If there is something to provide it will either assign the value to the requesting element or call the callback.
|
|
359
|
-
*
|
|
360
|
-
* @param event
|
|
462
|
+
* @param {CustomEvent} event
|
|
361
463
|
*/
|
|
362
464
|
onRequestContext(event) {
|
|
363
|
-
const properties = this.properties();
|
|
364
465
|
const provideProperties = this.provideProperties();
|
|
365
466
|
|
|
366
467
|
Object.entries(event.detail ?? {}).forEach(([key, valueOrCallback]) => {
|
|
@@ -373,25 +474,38 @@ class BaseElement extends HTMLElement {
|
|
|
373
474
|
valueOrCallback(providedValue);
|
|
374
475
|
} else {
|
|
375
476
|
// assign to prop
|
|
376
|
-
|
|
477
|
+
const eventPath = event.composedPath();
|
|
478
|
+
const target = eventPath[0] || event.target;
|
|
479
|
+
target[key] = providedValue;
|
|
377
480
|
}
|
|
378
481
|
}
|
|
379
482
|
});
|
|
380
483
|
}
|
|
381
484
|
|
|
382
|
-
|
|
485
|
+
/**
|
|
486
|
+
* Connected lifecycle hook
|
|
487
|
+
*/
|
|
383
488
|
connected() {}
|
|
384
489
|
|
|
385
|
-
|
|
490
|
+
/**
|
|
491
|
+
* BeforeUpdate lifecycle hook
|
|
492
|
+
*/
|
|
386
493
|
beforeUpdate() {}
|
|
387
494
|
|
|
388
|
-
|
|
495
|
+
/**
|
|
496
|
+
* AfterUpdate lifecycle hook
|
|
497
|
+
*/
|
|
389
498
|
afterUpdate() {}
|
|
390
499
|
|
|
391
|
-
|
|
500
|
+
/**
|
|
501
|
+
* Disconnected lifecycle hook
|
|
502
|
+
*/
|
|
392
503
|
disconnected() {}
|
|
393
504
|
|
|
394
|
-
|
|
505
|
+
/**
|
|
506
|
+
* Triggers a lifecycle hook based on the name
|
|
507
|
+
* @param {string} name
|
|
508
|
+
*/
|
|
395
509
|
triggerHook(name) {
|
|
396
510
|
if (name in this) {
|
|
397
511
|
this[name]();
|
|
@@ -501,21 +615,66 @@ class BaseElement extends HTMLElement {
|
|
|
501
615
|
this.$refs = refsMap;
|
|
502
616
|
}
|
|
503
617
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
618
|
+
/**
|
|
619
|
+
* Helper function for dispatching custom events
|
|
620
|
+
* @param {string} name
|
|
621
|
+
* @param {Object} [data]
|
|
622
|
+
* @param {CustomEventInit} [options]
|
|
623
|
+
* @param {boolean} [cancelable] - DEPRECATED
|
|
624
|
+
* @param {boolean} [composed] - DEPRECATED
|
|
625
|
+
*/
|
|
626
|
+
dispatch(name, data, options, cancelable = false, composed = false) {
|
|
627
|
+
let eventInit = {};
|
|
628
|
+
|
|
629
|
+
if (options !== undefined && typeof options === 'boolean') {
|
|
630
|
+
eventInit['bubbles'] = options;
|
|
631
|
+
eventInit['cancelable'] = cancelable;
|
|
632
|
+
eventInit['composed'] = composed;
|
|
633
|
+
eventInit['detail'] = data;
|
|
634
|
+
console.warn(
|
|
635
|
+
`[${this.localName}] Using the old parameter options for the event is deprecated and will be removed in v2.0! Please use the new options object that is in line with the "CustomEventInit" options`,
|
|
636
|
+
);
|
|
637
|
+
} else {
|
|
638
|
+
eventInit = {
|
|
639
|
+
bubbles: true,
|
|
640
|
+
cancelable: true,
|
|
641
|
+
composed: true,
|
|
642
|
+
detail: data,
|
|
643
|
+
...options,
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
const event = new CustomEvent(name, eventInit);
|
|
512
648
|
this.dispatchEvent(event);
|
|
513
649
|
}
|
|
514
650
|
|
|
515
|
-
|
|
651
|
+
/**
|
|
652
|
+
* Get the root element
|
|
653
|
+
* @returns {HTMLElement}
|
|
654
|
+
*/
|
|
516
655
|
getRoot() {
|
|
517
656
|
return this;
|
|
518
657
|
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* This method will be used to serialize this elements state.
|
|
661
|
+
* By default, it will generate an object with all properties from this.properties().
|
|
662
|
+
* You can override this and return a different object with more/less/different properties.
|
|
663
|
+
* @return {{[string: any]: *}}
|
|
664
|
+
*/
|
|
665
|
+
serializeState() {
|
|
666
|
+
const keys = Object.keys(this.properties());
|
|
667
|
+
return Object.fromEntries(keys.map((key) => [key, this[key]]));
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* This method will be used to restore properties on the element from a previously serialized state.
|
|
672
|
+
* You can override this and handle the deserialization differently if the default is not enough.
|
|
673
|
+
* @param {{[string: any]: *}} serializedState
|
|
674
|
+
*/
|
|
675
|
+
restoreState(serializedState) {
|
|
676
|
+
Object.assign(this, serializedState);
|
|
677
|
+
}
|
|
519
678
|
}
|
|
520
679
|
|
|
521
680
|
export { BaseElement, getClosestParentCustomElementNode, isOfSameNodeType };
|
package/src/StyledElement.js
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { BaseElement } from './BaseElement.js';
|
|
2
2
|
import { supportsAdoptingStyleSheets, getShadowParentOrBody } from './util/DOMHelper.js';
|
|
3
|
-
|
|
3
|
+
import { globalStylesStore } from './util/GlobalStylesStore.js';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (StyledElement.globalStyles && StyledElement['globalStyleSheet']) {
|
|
14
|
-
//updates already adopted global styles
|
|
15
|
-
StyledElement['globalStyleSheet'].replaceSync(StyledElement.globalStyles.textContent);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
5
|
+
/**
|
|
6
|
+
* Options object for the StyledElement
|
|
7
|
+
* @typedef {Object} StyledElementOptions
|
|
8
|
+
* @extends BaseElementOptions
|
|
9
|
+
* @property {boolean} [shadowRender] - When set to true the element will render the template (if provided) in the Shadow DOM and therefore encapsulate the element and styles from the rest of the document. Default is `false`
|
|
10
|
+
* @property {[]} [styles] - Via the styles option you can add multiple styles/stylesheets to your element. Default is `[]`
|
|
11
|
+
* @property {boolean | string | string[]} [adoptGlobalStyles] - When set to true element-js will look for all style elements in the global document with and apply them before any custom/element styles inside the shadow DOM. Default is `true`.
|
|
12
|
+
*/
|
|
18
13
|
|
|
14
|
+
class StyledElement extends BaseElement {
|
|
15
|
+
/**
|
|
16
|
+
* @param {StyledElementOptions} options
|
|
17
|
+
*/
|
|
19
18
|
constructor(options) {
|
|
20
19
|
super({
|
|
21
20
|
deferUpdate: false,
|
|
@@ -27,76 +26,96 @@ class StyledElement extends BaseElement {
|
|
|
27
26
|
this._styles = [...this._options.styles, ...this.styles()];
|
|
28
27
|
}
|
|
29
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Overrides the `connectedCallback` to adopt optional styles when the element is connected
|
|
31
|
+
*/
|
|
30
32
|
connectedCallback() {
|
|
31
33
|
super.connectedCallback();
|
|
32
34
|
|
|
35
|
+
if (!this.constructor['elementStyleSheets']) {
|
|
36
|
+
this.constructor['elementStyleSheets'] = this._styles.map((style) => {
|
|
37
|
+
const cssStyleSheet = new CSSStyleSheet();
|
|
38
|
+
cssStyleSheet.replaceSync(style);
|
|
39
|
+
return cssStyleSheet;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
33
43
|
if (supportsAdoptingStyleSheets() && this._options.shadowRender) {
|
|
34
44
|
// adopting does only make sense in shadow dom. Fall back to append for light elements
|
|
35
45
|
this.adoptStyleSheets();
|
|
46
|
+
|
|
47
|
+
if (this._options.adoptGlobalStyles !== false) {
|
|
48
|
+
globalStylesStore.subscribe(() => {
|
|
49
|
+
this.adoptStyleSheets();
|
|
50
|
+
});
|
|
51
|
+
}
|
|
36
52
|
}
|
|
37
53
|
}
|
|
38
54
|
|
|
55
|
+
/**
|
|
56
|
+
* The styles method is another way to return a list of styles to be adopted when the element is connected.
|
|
57
|
+
* You can either provide a list in the constructor options or return it here.
|
|
58
|
+
* @returns {string[]}
|
|
59
|
+
*/
|
|
39
60
|
styles() {
|
|
40
61
|
return [];
|
|
41
62
|
}
|
|
42
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Overrides the `update` method to adopt optional styles
|
|
66
|
+
* @param {PropertyUpdateOptions} options
|
|
67
|
+
*/
|
|
43
68
|
update(options) {
|
|
69
|
+
// We cannot do this in connectedCallback() since the whole template will be overridden in update/render afterward
|
|
44
70
|
if (!supportsAdoptingStyleSheets() || this._options.shadowRender === false) {
|
|
45
|
-
|
|
46
|
-
const appendableStyles = [...this._styles];
|
|
47
|
-
if (this._options.shadowRender && this._options.adoptGlobalStyles) {
|
|
48
|
-
appendableStyles.unshift(StyledElement.globalStyles?.textContent ?? '');
|
|
49
|
-
}
|
|
50
|
-
this.appendStyleSheets(appendableStyles);
|
|
71
|
+
this.appendStyleSheets();
|
|
51
72
|
}
|
|
52
73
|
super.update(options);
|
|
53
74
|
}
|
|
54
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Adopt stylesheets
|
|
78
|
+
*/
|
|
55
79
|
adoptStyleSheets() {
|
|
56
|
-
|
|
57
|
-
this.constructor['cssStyleSheets'] = this._styles.map((style) => {
|
|
58
|
-
const sheet = new CSSStyleSheet();
|
|
59
|
-
if (sheet && sheet.cssRules.length === 0) {
|
|
60
|
-
sheet.replaceSync(style);
|
|
61
|
-
}
|
|
62
|
-
return sheet;
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (StyledElement.globalStyles && !StyledElement['globalStyleSheet']) {
|
|
67
|
-
StyledElement['globalStyleSheet'] = new CSSStyleSheet();
|
|
68
|
-
StyledElement['globalStyleSheet'].replaceSync(StyledElement.globalStyles.textContent);
|
|
69
|
-
}
|
|
80
|
+
const adoptGlobalStyleSheets = this._options.shadowRender && this._options.adoptGlobalStyles !== false;
|
|
70
81
|
|
|
71
|
-
// adopt styles
|
|
72
|
-
// uses proposed solution for constructable stylesheets
|
|
73
|
-
// see: https://wicg.github.io/construct-stylesheets/#proposed-solution
|
|
74
82
|
this.getRoot().adoptedStyleSheets = [
|
|
75
|
-
...(
|
|
76
|
-
|
|
77
|
-
: []),
|
|
78
|
-
...this.constructor['cssStyleSheets'],
|
|
83
|
+
...(adoptGlobalStyleSheets ? globalStylesStore.getGlobalStyleSheets(this._options.adoptGlobalStyles) : []),
|
|
84
|
+
...this.constructor['elementStyleSheets'],
|
|
79
85
|
];
|
|
80
86
|
}
|
|
81
87
|
|
|
82
|
-
|
|
83
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Custom polyfill for constructable stylesheets by appending styles to the end of an element
|
|
90
|
+
*/
|
|
91
|
+
appendStyleSheets() {
|
|
84
92
|
const parentDocument = getShadowParentOrBody(this.getRoot());
|
|
85
|
-
|
|
93
|
+
|
|
94
|
+
const adoptGlobalStyleSheets =
|
|
95
|
+
this._options.shadowRender &&
|
|
96
|
+
this._options.adoptGlobalStyles !== false &&
|
|
97
|
+
parentDocument !== globalThis.document.body;
|
|
98
|
+
|
|
99
|
+
const appendableStyles = [
|
|
100
|
+
...(adoptGlobalStyleSheets ? globalStylesStore.getGlobalStyleSheets(this._options.adoptGlobalStyles) : []),
|
|
101
|
+
...this.constructor['elementStyleSheets'],
|
|
102
|
+
];
|
|
103
|
+
|
|
104
|
+
appendableStyles.forEach((styleSheet, index) => {
|
|
86
105
|
const identifier = this.tagName + index;
|
|
87
106
|
|
|
88
107
|
// only append stylesheet if not already appended to shadowRoot or document
|
|
89
108
|
if (!parentDocument.querySelector(`#${identifier}`)) {
|
|
90
|
-
const styleElement = document
|
|
109
|
+
const styleElement = globalThis.document?.createElement('style');
|
|
91
110
|
styleElement.id = identifier;
|
|
92
111
|
styleElement.style.display = 'none';
|
|
93
|
-
styleElement.textContent =
|
|
112
|
+
styleElement.textContent = Array.from(styleSheet.cssRules)
|
|
113
|
+
.map((rule) => rule.cssText)
|
|
114
|
+
.join('');
|
|
94
115
|
parentDocument.appendChild(styleElement);
|
|
95
116
|
}
|
|
96
117
|
});
|
|
97
118
|
}
|
|
98
119
|
}
|
|
99
120
|
|
|
100
|
-
StyledElement.updateGlobalStyles();
|
|
101
|
-
|
|
102
121
|
export { StyledElement };
|
package/src/TemplateElement.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import { StyledElement } from './StyledElement.js';
|
|
2
2
|
import { html } from './dom-parts/html.js';
|
|
3
3
|
import { render } from './dom-parts/render.js';
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options object for the TemplateElement
|
|
7
|
+
* @typedef {Object} TemplateElementOptions
|
|
8
|
+
* @extends StyledElementOptions
|
|
9
|
+
*/
|
|
5
10
|
|
|
6
11
|
class TemplateElement extends StyledElement {
|
|
12
|
+
/**
|
|
13
|
+
* @param {TemplateElementOptions} options
|
|
14
|
+
*/
|
|
7
15
|
constructor(options) {
|
|
8
16
|
super({
|
|
9
17
|
deferUpdate: false,
|
|
@@ -16,19 +24,37 @@ class TemplateElement extends StyledElement {
|
|
|
16
24
|
...options,
|
|
17
25
|
});
|
|
18
26
|
this._template = this._options.template;
|
|
27
|
+
}
|
|
19
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Overrides the native `connectedCallback` of the HTMLElement to set up and initialize our element.
|
|
31
|
+
* This will attach a shadow DOM if the element is supposed to render in shadow DOM.
|
|
32
|
+
*/
|
|
33
|
+
connectedCallback() {
|
|
20
34
|
if (this._options.shadowRender) this.attachShadow({ mode: 'open' });
|
|
35
|
+
super.connectedCallback();
|
|
21
36
|
}
|
|
22
37
|
|
|
38
|
+
/**
|
|
39
|
+
* The template method should be overridden in extending elements and return the template to be rendered to the root
|
|
40
|
+
* @returns {TemplateResult}
|
|
41
|
+
*/
|
|
23
42
|
template() {
|
|
24
43
|
return html``;
|
|
25
44
|
}
|
|
26
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Override update method to render the template to the root
|
|
48
|
+
* @param {PropertyUpdateOptions} options
|
|
49
|
+
*/
|
|
27
50
|
update(options) {
|
|
28
51
|
this.renderTemplate();
|
|
29
52
|
super.update(options);
|
|
30
53
|
}
|
|
31
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Render the template to the root
|
|
57
|
+
*/
|
|
32
58
|
renderTemplate() {
|
|
33
59
|
const template = this._template || this.template();
|
|
34
60
|
if (typeof template === 'string') {
|
|
@@ -39,6 +65,10 @@ class TemplateElement extends StyledElement {
|
|
|
39
65
|
}
|
|
40
66
|
}
|
|
41
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Get the root element - either the element or the shadow root
|
|
70
|
+
* @returns {ShadowRoot | HTMLElement}
|
|
71
|
+
*/
|
|
42
72
|
getRoot() {
|
|
43
73
|
return this.shadowRoot !== null ? this.shadowRoot : this;
|
|
44
74
|
}
|
|
@@ -78,6 +78,9 @@ const processAttribute = (node, name) => {
|
|
|
78
78
|
};
|
|
79
79
|
};
|
|
80
80
|
|
|
81
|
+
/** @type {Map<Element, AttributePart>} */
|
|
82
|
+
const attributePartsCache = new WeakMap();
|
|
83
|
+
|
|
81
84
|
/**
|
|
82
85
|
* @param {Element} node
|
|
83
86
|
* @param {String} name
|
|
@@ -114,23 +117,27 @@ export class AttributePart extends Part {
|
|
|
114
117
|
initialValue = undefined;
|
|
115
118
|
|
|
116
119
|
/**
|
|
117
|
-
* @param {
|
|
120
|
+
* @param {Comment} node
|
|
118
121
|
* @param {String} name
|
|
119
122
|
* @param {String} initialValue
|
|
120
123
|
*/
|
|
121
124
|
constructor(node, name, initialValue) {
|
|
125
|
+
// If we have multiple attribute parts with the same name, it means we have multiple
|
|
126
|
+
// interpolations inside that attribute. Instead of creating a new part, we will return the same
|
|
127
|
+
// as before and let it defer the update until the last interpolation gets updated
|
|
128
|
+
const attributePart = attributePartsCache.get(node.nextElementSibling);
|
|
129
|
+
if (attributePart && attributePart.name === name) {
|
|
130
|
+
attributePart.interpolations++;
|
|
131
|
+
node.__part = attributePart; // add Part to comment node for debugging in the browser
|
|
132
|
+
return attributePart;
|
|
133
|
+
}
|
|
134
|
+
|
|
122
135
|
super();
|
|
123
136
|
this.name = name;
|
|
124
137
|
this.initialValue = initialValue;
|
|
125
138
|
this.processor = processAttributePart(node.nextElementSibling, this.name);
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* @param {Node} node
|
|
130
|
-
*/
|
|
131
|
-
addNode(node) {
|
|
132
|
-
this.interpolations++;
|
|
133
|
-
// TODO: what about the comment node? It will always be the first for now...
|
|
139
|
+
node.__part = this; // add Part to comment node for debugging in the browser
|
|
140
|
+
attributePartsCache.set(node.nextElementSibling, this);
|
|
134
141
|
}
|
|
135
142
|
|
|
136
143
|
/**
|