@webtides/element-js 1.0.0-alpha.9 → 1.0.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/CHANGELOG.md ADDED
@@ -0,0 +1,319 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
6
+
7
+ <!--
8
+ PRs should document their user-visible changes (if any) in the
9
+ Unreleased section, uncommenting the header as necessary.
10
+ -->
11
+
12
+ <!-- ## Unreleased -->
13
+ <!-- ### Added -->
14
+ <!-- ### Changed -->
15
+ <!-- ### Removed -->
16
+ <!-- ### Fixed -->
17
+
18
+ ## [1.0.0] - 2024-05-24
19
+
20
+ ### Added
21
+
22
+ - Added our own versions of directives and template helpers ([#67](https://github.com/webtides/element-js/pull/67))
23
+ - Added JSDocs for everything in the codebase. This will especially be helpful when using constructor options.
24
+ - Added state serialization. This improves hydration from SSR a lot. ([#106](https://github.com/webtides/element-js/pull/106))
25
+ - added declarative shadow dom rendering for template elements
26
+
27
+ ### Changed
28
+
29
+ - _BRAKING_ Uses a new custom renderer for the `TemplateElement`. It replaces the old `lit-html` renderer. The API and usage should be exactly the same. The only thing to do/change is the use of the old `lit-html` directives. For detailed instructions see the upgrade guide. ([#67](https://github.com/webtides/element-js/pull/67))
30
+ - _BREAKING_ `element-js` will now also trigger the `afterUpdate` hook right after the `connected` hook. This is to reduce the cases where you had to do the same things in `connected` and `afterUpdate`. You can now remove those duplicate calls from the `connected` hook. ([#60](https://github.com/webtides/element-js/pull/60))
31
+ - _BREAKING_ Changed `package.json` type to `module`. This should hopefully not break anything. `element-js` is either used directly from the browser or through a bundler. In both cases the added type: module should not matter. ([#64](https://github.com/webtides/element-js/pull/64))
32
+ - _BRAKING_ Changed the default options when using the `dispatch` helper function. `bubbles`, `cancelable` and `composed` will now be `true` by default.
33
+ - _BRAKING_ Changed adopting global styles to include ALL global styles. Previously only one inline <style> with the ID of '#globalStyles' would be adopted. For more information see the docs.
34
+
35
+ ### Removed
36
+
37
+ - _BREAKING_ Removed the `lit-html` dependency. `element-js` is now officially dependency free! ([#67](https://github.com/webtides/element-js/pull/67))
38
+ - _BREAKING_ Removed the deprecated `BaseElement` constructor option `childListUpdate`. Please use the "mutationObserverOptions" dictionary instead. See the docs for more info. ([#99](https://github.com/webtides/element-js/pull/99))
39
+ - _BREAKING_ Removed the `hooks` map in the `BaseElement`. Using the hooks() map for lifecycle hooks is deprecated! Please overwrite the existing lifecycle hook functions. See the docs for more info. ([#99](https://github.com/webtides/element-js/pull/99))
40
+ - _BREAKING_ Removed the `computed` map in the `BaseElement`. Using the computed() map for computed properties is deprecated! Please use regular JS getters and return the computed value. See the docs for more info. ([#99](https://github.com/webtides/element-js/pull/99))
41
+ - _BREAKING_ Removed all occurrences of `ShadyCSS` in `StyledElement`. ([#99](https://github.com/webtides/element-js/pull/99))
42
+ - _BRAKING_ Removed the `i18n` helper function. See CHANGELOG how to implement it yourself or use a third party library.
43
+
44
+ ## [1.0.0-alpha.13] - 2024-05-08
45
+
46
+ ### Fixed
47
+
48
+ - catch security errors when `GlobalStylesStore` tries to read css rules from cross-origin stylesheets.
49
+
50
+ ## [1.0.0-alpha.12] - 2024-03-03
51
+
52
+ ### Added
53
+
54
+ - added declarative shadow dom rendering for template elements
55
+
56
+ ### Fixed
57
+
58
+ - fixed issues with whitespace being returned untrimmed in a template result
59
+
60
+ ## [1.0.0-alpha.11] - 2024-03-13
61
+
62
+ ### Fixed
63
+
64
+ - fixed missing return type `string` for the `template()` function in `TemplateElement`
65
+ - fixed types for the `propertyOptions` option in `BaseElement`
66
+
67
+ ## [1.0.0-alpha.10] - 2024-02-20
68
+
69
+ ### Fixed
70
+
71
+ - fixed unsafeHTML not rendering anything when toString was called
72
+ - fixed using = sign in attribute positions with interpolations
73
+ - fixed rendering null values in node part positions in SSR
74
+ - fixed missing .js file extensions
75
+
76
+ ## [1.0.0-alpha.9] - 2024-01-05
77
+
78
+ ### Changed
79
+
80
+ - moved DOM methods into connected callbacks to make it safe for the elements to be created in SSR environments
81
+
82
+ ## [1.0.0-alpha.8] - 2024-01-04
83
+
84
+ ### Fixed
85
+
86
+ - fixed ChildNodePart not replacing dom nodes from unsafeHTML
87
+
88
+ ## [1.0.0-alpha.7] - 2023-12-19
89
+
90
+ ### Fixed
91
+
92
+ - fixed wrong attribute parts for the same name on different elements
93
+
94
+ ## [1.0.0-alpha.6] - 2023-12-18
95
+
96
+ ### Fixed
97
+
98
+ - fixed hyphens in attribute names being split in SSR mode
99
+
100
+ ## [1.0.0-alpha.5] - 2023-12-07
101
+
102
+ ### Added
103
+
104
+ - Added the /docs directory to the files array in package.json
105
+ - Added part instances as \_\_part to comment nodes
106
+
107
+ ### Fixed
108
+
109
+ - fixed escaped unsafeHTML in SSR mode
110
+
111
+ ## [1.0.0-alpha.4] - 2023-12-01
112
+
113
+ ### Fixed
114
+
115
+ - falsy creation of directive parts for non-directive attributes
116
+ - creating parts with wrong comment marker positions
117
+
118
+ ## [1.0.0-alpha.3] - 2023-12-01
119
+
120
+ ### Added
121
+
122
+ - Added state serialization. This improves hydration from SSR a lot. ([#106](https://github.com/webtides/element-js/pull/106))
123
+
124
+ ### Changed
125
+
126
+ - _BRAKING_ Changed the default options when using the `dispatch` helper function. `bubbles`, `cancelable` and `composed` will now be `true` by default.
127
+ - _BRAKING_ Changed adopting global styles to include ALL global styles. Previously only one inline <style> with the ID of '#globalStyles' would be adopted. For more information see the docs.
128
+
129
+ ### Removed
130
+
131
+ - _BRAKING_ Removed `i18n` helper function. See CHANGELOG how to implement it yourself or use a third party library.
132
+
133
+ ## [1.0.0-alpha.2] - 2023-08-23
134
+
135
+ ### Added
136
+
137
+ - Added JSDocs for everything in the codebase. This will especially be helpful when using constructor options.
138
+
139
+ ## [1.0.0-alpha.1] - 2023-08-15
140
+
141
+ ### Added
142
+
143
+ - Added our own versions of directives and template helpers ([#67](https://github.com/webtides/element-js/pull/67))
144
+
145
+ ### Changed
146
+
147
+ - _BRAKING_ Uses a new custom renderer for the `TemplateElement`. It replaces the old `lit-html` renderer. The API and usage should be exactly the same. The only thing to do/change is the use of the old `lit-html` directives. For detailed instructions see the upgrade guide. ([#67](https://github.com/webtides/element-js/pull/67))
148
+ - _BREAKING_ `element-js` will now also trigger the `afterUpdate` hook right after the `connected` hook. This is to reduce the cases where you had to do the same things in `connected` and `afterUpdate`. You can now remove those duplicate calls from the `connected` hook. ([#60](https://github.com/webtides/element-js/pull/60))
149
+ - _BREAKING_ Changed `package.json` type to `module`. This should hopefully not break anything. `element-js` is either used directly from the browser or through a bundler. In both cases the added type: module should not matter. ([#64](https://github.com/webtides/element-js/pull/64))
150
+
151
+ ### Removed
152
+
153
+ - _BREAKING_ Removed the `lit-html` dependency. `element-js` is now officially dependency free! ([#67](https://github.com/webtides/element-js/pull/67))
154
+ - _BREAKING_ Removed the deprecated `BaseElement` constructor option `childListUpdate`. Please use the "mutationObserverOptions" dictionary instead. See the docs for more info. ([#99](https://github.com/webtides/element-js/pull/99))
155
+ - _BREAKING_ Removed the `hooks` map in the `BaseElement`. Using the hooks() map for lifecycle hooks is deprecated! Please overwrite the existing lifecycle hook functions. See the docs for more info. ([#99](https://github.com/webtides/element-js/pull/99))
156
+ - _BREAKING_ Removed the `computed` map in the `BaseElement`. Using the computed() map for computed properties is deprecated! Please use regular JS getters and return the computed value. See the docs for more info. ([#99](https://github.com/webtides/element-js/pull/99))
157
+ - _BREAKING_ Removed all occurrences of `ShadyCSS` in `StyledElement`. ([#99](https://github.com/webtides/element-js/pull/99))
158
+
159
+ ## [0.7.4] - 2023-11-24
160
+
161
+ ### Added
162
+
163
+ - storeception (reactive store properties in parent stores)
164
+ - context injection in shadow elements requests travel though shadow roots
165
+ - defer element connection (disables client side code until called manually)
166
+
167
+ ### Fixed
168
+
169
+ - late context injection if parent mounts after child (loading order)
170
+ - stores updating elements that want to be updated
171
+
172
+ ## [0.7.3] - 2023-03-07
173
+
174
+ ### Fixed
175
+
176
+ - last release was tagged wrong
177
+
178
+ ## [0.7.2] - 2023-03-03
179
+
180
+ ### Fixed
181
+
182
+ - watcher callbacks for injected stores
183
+
184
+ ## [0.7.1] - 2023-03-03
185
+
186
+ ### Added
187
+
188
+ - Elements are now able to watch Stores
189
+
190
+ ### Fixed
191
+
192
+ - Injection of falsy values
193
+
194
+ ## [0.7.0] - 2023-02-13
195
+
196
+ ### Added
197
+
198
+ - Dependency Injection via Context Protocol 💉
199
+ - Provide reactive Properties or entire Stores via: provideProperties() { return { name : value}}
200
+ - Inject / Request them via injectProperties() { return { name : 'defaultValue'}}
201
+
202
+ ### Changed
203
+
204
+ - docs structure
205
+
206
+ ### Fixed
207
+
208
+ - Cannot re-render slotted light dom when using the vanilla renderer https://github.com/webtides/element-js/issues/76
209
+ - adds Vanilla Renderer Tests
210
+
211
+ ## [0.6.2] - 2022-11-18
212
+
213
+ ### FIXED
214
+
215
+ - Store always switching to singleProperty Mode if constructed without any arguments.
216
+
217
+ ## [0.6.1] - 2022-11-18
218
+
219
+ ### ADDED
220
+
221
+ - Store watch() map to control internal store state scope
222
+
223
+ ## [0.6.0] - 2022-11-17
224
+
225
+ ### ADDED
226
+
227
+ - StoreProperty for shared application state
228
+ - Directives for Vanilla Renderer
229
+
230
+ ## [0.5.0] - 2022-09-13
231
+
232
+ ### Changed
233
+
234
+ - improved the performance of the vanilla-renderer
235
+
236
+ ### Removed
237
+
238
+ - setting attributes as properties during dom diffing for the vanilla renderer. This is potentially a **breaking** change if you used attributes with the "." (dot) notation
239
+
240
+ ## [0.4.3] - 2022-07-06
241
+
242
+ ### ADDED
243
+
244
+ - parse option to propertyOptions that can be either a boolean OR a function for custom parsing
245
+ - enhance reflect option in propertyOptions that it can also be a function for custom reflection
246
+
247
+ ## [0.4.2] - 2022-02-03
248
+
249
+ ### Fixed
250
+
251
+ - vanilla-renderer: fixes attribute comparsion
252
+
253
+ ## [0.4.1] - 2022-02-01
254
+
255
+ ### Fixed
256
+
257
+ - refs="id[]" indexing keys not nodes _rolleyes_
258
+
259
+ ### Added
260
+
261
+ - refs="id[]" aka possibbility to refrence a list of nodes to the $refs map.
262
+
263
+ ## [0.4.0] - 2022-01-28
264
+
265
+ ### Added
266
+
267
+ - a "vanilla" renderer with support for hydration and no dependencies
268
+
269
+ ### Changed
270
+
271
+ - uses new @web/test-runner isntead of karma (#27)
272
+
273
+ ## [0.3.3] - 2021-09-22
274
+
275
+ ### Added
276
+
277
+ - optional complex event callback notation to support addEventListener options (i.E. for passive Event Listeners )
278
+ - Example: {listener: () => {}, options: { passive: true } }
279
+ - docs
280
+ - tests
281
+
282
+ ## [0.3.2] - 2021-03-09
283
+
284
+ ### Added
285
+
286
+ - bundled version of `element-js` to use from a CDN and be able to prototype in tools like CodePen etc.
287
+
288
+ ## [0.3.1] - 2020-12-08
289
+
290
+ ### Fixed
291
+
292
+ - i18n fallback values
293
+
294
+ ### Added
295
+
296
+ - i18n tests
297
+
298
+ ## [0.3.0] - 2020-07-27
299
+
300
+ ### Added
301
+
302
+ - first batch/draft of the documentation
303
+ - property/attribute reflection via constructor options (see the [Documentation](/docs/README.md#propertyoptions) for more info)
304
+ - mutationObserverOptions to enable observing subtree mutations (see the [Documentation](/docs/README.md#mutationobserveroptions) for more info)
305
+
306
+ ### Removed
307
+
308
+ - `childListUpdate` constructor option has been deprecated and will be removed before `1.0`
309
+
310
+ ## [0.2.0] - 2020-07-01
311
+
312
+ ### Changed
313
+
314
+ - uses shady-render instead of render from `lit-html` for rendering templates to be compatible with the shady-css polyfill
315
+ - refactored and simplified `StyledElement`s handling/loading of style sheets
316
+
317
+ ## [0.1.0] - 2020-06-26
318
+
319
+ - initial release
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtides/element-js",
3
- "version": "1.0.0-alpha.9",
3
+ "version": "1.0.0",
4
4
  "module": "index.js",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -24,7 +24,12 @@ export { toString } from './util/toString.js';
24
24
 
25
25
  /**
26
26
  * Options object for the BaseElement propertyOptions option
27
- * @typedef {Object} PropertyOptions
27
+ * @typedef {Object<string, PropertyOption>} PropertyOptions
28
+ */
29
+
30
+ /**
31
+ * Option object for a property of the BaseElement propertyOptions option
32
+ * @typedef {Object} PropertyOption
28
33
  * @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
34
  * @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
35
  * @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,13 +31,13 @@ class TemplateElement extends StyledElement {
31
31
  * This will attach a shadow DOM if the element is supposed to render in shadow DOM.
32
32
  */
33
33
  connectedCallback() {
34
- if (this._options.shadowRender) this.attachShadow({ mode: 'open' });
34
+ if (this._options.shadowRender && !this.shadowRoot) this.attachShadow({ mode: 'open' });
35
35
  super.connectedCallback();
36
36
  }
37
37
 
38
38
  /**
39
39
  * The template method should be overridden in extending elements and return the template to be rendered to the root
40
- * @returns {TemplateResult}
40
+ * @returns {TemplateResult | string}
41
41
  */
42
42
  template() {
43
43
  return html``;
@@ -57,12 +57,7 @@ class TemplateElement extends StyledElement {
57
57
  */
58
58
  renderTemplate() {
59
59
  const template = this._template || this.template();
60
- if (typeof template === 'string') {
61
- // just a plain string literal. no lit-html required
62
- this.getRoot().innerHTML = `${template}`;
63
- } else {
64
- render(template, this.getRoot());
65
- }
60
+ render(template, this.getRoot());
66
61
  }
67
62
 
68
63
  /**
@@ -310,7 +310,7 @@ export class ChildNodePart extends Part {
310
310
  } else if (Array.isArray(value)) {
311
311
  let childNodePart = this.parts[index];
312
312
  if (!childNodePart) {
313
- // TODO: this seems not correct :(
313
+ // TODO: this seems not correct :( (maybe Template vs DOM Parts !?)
314
314
  const templatePartCommentNodes = this.childNodes?.filter(
315
315
  (node) => node && node.nodeType === COMMENT_NODE && node.data === 'template-part',
316
316
  );
@@ -1,4 +1,4 @@
1
- import { COMMENT_NODE, getNodePath } from '../util/DOMHelper';
1
+ import { COMMENT_NODE, getNodePath } from '../util/DOMHelper.js';
2
2
  import { encodeAttribute, isObjectLike } from '../util/AttributeParser.js';
3
3
  import { TemplatePart } from './TemplatePart.js';
4
4
 
@@ -29,6 +29,7 @@ export const createTemplateString = (templateStrings, attributePlaceholders = ''
29
29
  // find (match) all elements to identify their attributes
30
30
  template = template.replace(elements, (_, name, attributesString, trailingSlash) => {
31
31
  let elementTagWithAttributes = name + attributesString.replaceAll('\x01', attributePlaceholders).trimEnd();
32
+ // TODO the closing case is weird.
32
33
  if (trailingSlash.length) elementTagWithAttributes += voidElements.test(name) ? ' /' : '></' + name;
33
34
  // collect all attribute parts so that we can place matching comment nodes
34
35
  const attributeParts = attributesString.replace(attributes, (attribute, name, valueWithQuotes, directive) => {
@@ -48,26 +49,23 @@ export const createTemplateString = (templateStrings, attributePlaceholders = ''
48
49
  }
49
50
  return parts.join('');
50
51
  });
52
+ // TODO create test to check if lf are in place
51
53
  return `
52
- ${attributesString.includes('\x01') ? attributeParts : ''}
53
- <${elementTagWithAttributes}>
54
- `;
54
+ ${attributesString.includes('\x01') ? attributeParts : ''}\n
55
+ <${elementTagWithAttributes}>\n
56
+ `.trim();
55
57
  });
56
58
  // replace interpolation placeholders with our indexed markers
57
59
  template = template.replace(partPositions, (partPosition) => {
58
60
  if (partPosition === '\x01') {
59
61
  return `<!--dom-part-${partIndex}--><!--/dom-part-${partIndex++}-->`;
60
- }
61
- if (partPosition === '\x02') {
62
+ } else if (partPosition === '\x02') {
62
63
  return `dom-part-${partIndex++}`;
63
64
  }
64
65
  });
65
- return `
66
- <!--template-part-->
67
- ${template}
68
- <!--/template-part-->
69
- `;
70
- // TODO: ^ this is actually important because of the whitespace
66
+ // TODO create test to check if lf are in place
67
+ // /n is important in the returns as we expect a certain order of text / comments an nodes
68
+ return `<!--template-part-->\n${template}\n<!--/template-part-->`.trim();
71
69
  };
72
70
 
73
71
  /**
@@ -95,12 +93,16 @@ const getValue = (value) => {
95
93
  return String(value);
96
94
  case 'object':
97
95
  switch (true) {
96
+ case value === null:
97
+ return '';
98
98
  case Array.isArray(value):
99
99
  return value.map(getValue).join('');
100
100
  case value instanceof TemplateResult:
101
101
  return value.toString();
102
102
  case value.__unsafeHTML:
103
103
  return value.string;
104
+ default:
105
+ console.log('Cannot getValue for', value);
104
106
  }
105
107
  break;
106
108
  case 'function':
@@ -283,8 +285,13 @@ export class TemplateResult {
283
285
  }
284
286
  if (/^dom-part-\d+:/.test(node.data)) {
285
287
  const [_, ...attribute] = node.data.split(':');
286
- const [name, initialValue] = attribute.join(':').split('=');
287
- parts.push({ type: 'attribute', path: getNodePath(node), name: name, initialValue });
288
+ const [name, ...initialValue] = attribute.join(':').split('=');
289
+ parts.push({
290
+ type: 'attribute',
291
+ path: getNodePath(node),
292
+ name: name,
293
+ initialValue: initialValue.join('='),
294
+ });
288
295
  continue;
289
296
  }
290
297
  if (/^dom-part-\d+\$/.test(node.data)) {
@@ -1,4 +1,4 @@
1
- import { TemplateResult } from './TemplateResult';
1
+ import { TemplateResult } from './TemplateResult.js';
2
2
 
3
3
  /**
4
4
  * @param {TemplateStringsArray} strings
@@ -4,7 +4,12 @@
4
4
  * @param {Element} domNode
5
5
  */
6
6
  const render = (template, domNode) => {
7
- template.renderInto(domNode);
7
+ if (typeof template === 'string') {
8
+ // just a plain string (or literal)
9
+ domNode.innerHTML = template;
10
+ } else {
11
+ template.renderInto(domNode);
12
+ }
8
13
  };
9
14
 
10
15
  export { render };
@@ -153,8 +153,7 @@ export const convertStringToHTML = (template) => {
153
153
  */
154
154
  export const convertStringToTemplate = (string) => {
155
155
  const template = globalThis.document?.createElement('template');
156
- // TODO: ensure that SSRed and CSRed templates have the same amount of whitespace text nodes
157
- template.innerHTML = string.trimStart().trimEnd();
156
+ template.innerHTML = string;
158
157
  return template.content;
159
158
  };
160
159
 
@@ -30,6 +30,13 @@ export class Directive {
30
30
  */
31
31
  export const defineDirective = (directiveClass) => {
32
32
  return (...values) => {
33
- return { directiveClass, values };
33
+ return {
34
+ directiveClass,
35
+ values,
36
+ toString: () => {
37
+ const directive = new directiveClass();
38
+ return directive.stringify(...values);
39
+ },
40
+ };
34
41
  };
35
42
  };
@@ -53,7 +53,11 @@ class GlobalStylesStore extends Store {
53
53
  if (styleSheet.ownerNode.tagName === 'STYLE') {
54
54
  cssStyleSheet.replaceSync(styleSheet.ownerNode.textContent);
55
55
  } else if (styleSheet.ownerNode.tagName === 'LINK') {
56
- Array.from(styleSheet.cssRules).map((rule) => cssStyleSheet.insertRule(rule.cssText));
56
+ try {
57
+ Array.from(styleSheet?.cssRules ?? []).map((rule) => cssStyleSheet.insertRule(rule.cssText));
58
+ } catch (e) {
59
+ console.error('GlobalStylesStore: cannot read cssRules. Maybe add crossorigin="anonymous" to your style link?', e);
60
+ }
57
61
  }
58
62
  }
59
63
  cssStyleSheets.push(cssStyleSheet);