@vaadin/vaadin-themable-mixin 22.0.2 → 23.0.0-alpha4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/vaadin-themable-mixin",
3
- "version": "22.0.2",
3
+ "version": "23.0.0-alpha4",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -18,6 +18,7 @@
18
18
  },
19
19
  "main": "vaadin-themable-mixin.js",
20
20
  "module": "vaadin-themable-mixin.js",
21
+ "type": "module",
21
22
  "files": [
22
23
  "*.d.ts",
23
24
  "register-styles.js",
@@ -39,5 +40,5 @@
39
40
  "@vaadin/testing-helpers": "^0.3.2",
40
41
  "sinon": "^9.2.4"
41
42
  },
42
- "gitHead": "df21370c4a655a38eac11f79686021ab3b0887ad"
43
+ "gitHead": "81e2deee5147bb7c1f4884760f4598613306f1fb"
43
44
  }
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2017 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  export { registerStyles, css, unsafeCSS } from './vaadin-themable-mixin.js';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2017 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  export { registerStyles, css, unsafeCSS } from './vaadin-themable-mixin.js';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2017 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { Constructor } from '@open-wc/dedupe-mixin';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2017 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { css, CSSResult, unsafeCSS } from 'lit';
@@ -35,8 +35,7 @@ const themeRegistry = [];
35
35
  */
36
36
  export function registerStyles(themeFor, styles, options = {}) {
37
37
  if (themeFor) {
38
- const elementClass = customElements.get(themeFor);
39
- if (elementClass && Object.prototype.hasOwnProperty.call(elementClass, '__finalized')) {
38
+ if (hasThemes(themeFor)) {
40
39
  console.warn(`The custom element definition for "${themeFor}"
41
40
  was finalized before a style module was registered.
42
41
  Make sure to add component specific style modules before
@@ -44,7 +43,7 @@ export function registerStyles(themeFor, styles, options = {}) {
44
43
  }
45
44
  }
46
45
 
47
- styles = recursiveFlattenStyles(styles);
46
+ styles = flattenStyles(styles);
48
47
 
49
48
  if (window.Vaadin && window.Vaadin.styleModules) {
50
49
  window.Vaadin.styleModules.registerStyles(themeFor, styles, options);
@@ -59,16 +58,15 @@ export function registerStyles(themeFor, styles, options = {}) {
59
58
  }
60
59
 
61
60
  /**
62
- * Returns all registered themes. By default the themeRegistry is returend as is.
61
+ * Returns all registered themes. By default the themeRegistry is returned as is.
63
62
  * In case the style-modules adapter is imported, the themes are obtained from there instead
64
63
  * @returns {Theme[]}
65
64
  */
66
65
  function getAllThemes() {
67
66
  if (window.Vaadin && window.Vaadin.styleModules) {
68
67
  return window.Vaadin.styleModules.getAllThemes();
69
- } else {
70
- return themeRegistry;
71
68
  }
69
+ return themeRegistry;
72
70
  }
73
71
 
74
72
  /**
@@ -105,15 +103,14 @@ function getIncludePriority(moduleName = '') {
105
103
  * @param {CSSResult[]} result
106
104
  * @returns {CSSResult[]}
107
105
  */
108
- function recursiveFlattenStyles(styles = [], result = []) {
109
- if (styles instanceof CSSResult) {
110
- result.push(styles);
111
- } else if (Array.isArray(styles)) {
112
- styles.forEach((style) => recursiveFlattenStyles(style, result));
113
- } else {
106
+ function flattenStyles(styles = []) {
107
+ return [styles].flat(Infinity).filter((style) => {
108
+ if (style instanceof CSSResult) {
109
+ return true;
110
+ }
114
111
  console.warn('An item in styles is not of type CSSResult. Use `unsafeCSS` or `css`.');
115
- }
116
- return result;
112
+ return false;
113
+ });
117
114
  }
118
115
 
119
116
  /**
@@ -143,11 +140,7 @@ function getIncludedStyles(theme) {
143
140
  */
144
141
  function addStylesToTemplate(styles, template) {
145
142
  const styleEl = document.createElement('style');
146
- styleEl.innerHTML = styles
147
- // Remove duplicates so that the last occurrence remains
148
- .filter((style, index) => index === styles.lastIndexOf(style))
149
- .map((style) => style.cssText)
150
- .join('\n');
143
+ styleEl.innerHTML = styles.map((style) => style.cssText).join('\n');
151
144
  template.content.appendChild(styleEl);
152
145
  }
153
146
 
@@ -175,10 +168,19 @@ function getThemes(tagName) {
175
168
 
176
169
  if (themes.length > 0) {
177
170
  return themes;
178
- } else {
179
- // No theme modules found, return the default module if it exists
180
- return getAllThemes().filter((theme) => theme.moduleId === defaultModuleName);
181
171
  }
172
+ // No theme modules found, return the default module if it exists
173
+ return getAllThemes().filter((theme) => theme.moduleId === defaultModuleName);
174
+ }
175
+
176
+ /**
177
+ * Check if the custom element type has themes applied.
178
+ * @param {string} tagName
179
+ * @returns {boolean}
180
+ */
181
+ function hasThemes(tagName) {
182
+ const elementClass = customElements.get(tagName);
183
+ return elementClass && Object.prototype.hasOwnProperty.call(elementClass, '__themes');
182
184
  }
183
185
 
184
186
  /**
@@ -195,35 +197,38 @@ export const ThemableMixin = (superClass) =>
195
197
  super.finalize();
196
198
 
197
199
  const template = this.prototype._template;
198
- if (!template || template.__themes) {
200
+ if (!template || hasThemes(this.is)) {
199
201
  return;
200
202
  }
201
203
 
202
- const inheritedTemplate = Object.getPrototypeOf(this.prototype)._template;
203
- const inheritedThemes = (inheritedTemplate ? inheritedTemplate.__themes : []) || [];
204
-
205
- template.__themes = [...inheritedThemes, ...getThemes(this.is)];
206
-
207
- // Get flattened styles array
208
- const styles = template.__themes.reduce((styles, theme) => [...styles, ...theme.styles], []);
209
- addStylesToTemplate(styles, template);
204
+ addStylesToTemplate(this.getStylesForThis(), template);
210
205
  }
211
206
 
212
207
  /**
213
208
  * Covers LitElement based component styling
214
209
  *
215
- * NOTE: This is not yet an offically supported API!
216
- *
217
- * TODO: Add tests (run a variation of themable-mixin.test.js where the components get created as LitElements)
218
210
  * @protected
219
211
  */
220
212
  static finalizeStyles(styles) {
221
- return (
222
- getThemes(this.is)
223
- // Get flattened styles array
224
- .reduce((styles, theme) => [...styles, ...theme.styles], [])
225
- .concat(styles)
226
- );
213
+ // The "styles" object originates from the "static get styles()" function of
214
+ // a LitElement based component. The theme styles are added after it
215
+ // so that they can override the component styles.
216
+ const themeStyles = this.getStylesForThis();
217
+ return styles ? [styles, ...themeStyles] : themeStyles;
218
+ }
219
+
220
+ /**
221
+ * Get styles for the component type
222
+ *
223
+ * @private
224
+ */
225
+ static getStylesForThis() {
226
+ const parent = Object.getPrototypeOf(this.prototype);
227
+ const inheritedThemes = (parent ? parent.constructor.__themes : []) || [];
228
+ this.__themes = [...inheritedThemes, ...getThemes(this.is)];
229
+ const themeStyles = this.__themes.flatMap((theme) => theme.styles);
230
+ // Remove duplicates
231
+ return themeStyles.filter((style, index) => index === themeStyles.lastIndexOf(style));
227
232
  }
228
233
  };
229
234
 
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2017 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { Constructor } from '@open-wc/dedupe-mixin';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2017 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  /**