@vaadin/vaadin-themable-mixin 25.0.0-alpha9 → 25.0.0-beta1
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/lumo-injection-mixin.js +1 -1
- package/package.json +6 -5
- package/src/css-property-observer.js +14 -13
- package/src/lumo-injector.js +10 -10
- package/src/lumo-modules.js +34 -18
- package/vaadin-themable-mixin.d.ts +1 -16
- package/vaadin-themable-mixin.js +3 -18
package/lumo-injection-mixin.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vaadin/vaadin-themable-mixin",
|
|
3
|
-
"version": "25.0.0-
|
|
3
|
+
"version": "25.0.0-beta1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -33,14 +33,15 @@
|
|
|
33
33
|
],
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@open-wc/dedupe-mixin": "^1.3.0",
|
|
36
|
+
"@vaadin/component-base": "25.0.0-beta1",
|
|
36
37
|
"lit": "^3.0.0"
|
|
37
38
|
},
|
|
38
39
|
"devDependencies": {
|
|
39
40
|
"@polymer/polymer": "^3.0.0",
|
|
40
|
-
"@vaadin/chai-plugins": "25.0.0-
|
|
41
|
-
"@vaadin/test-runner-commands": "25.0.0-
|
|
41
|
+
"@vaadin/chai-plugins": "25.0.0-beta1",
|
|
42
|
+
"@vaadin/test-runner-commands": "25.0.0-beta1",
|
|
42
43
|
"@vaadin/testing-helpers": "^2.0.0",
|
|
43
|
-
"sinon": "^
|
|
44
|
+
"sinon": "^21.0.0"
|
|
44
45
|
},
|
|
45
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "1d20cf54e582d1f2e209126d4586f8b4c01c50e0"
|
|
46
47
|
}
|
|
@@ -11,16 +11,15 @@
|
|
|
11
11
|
*/
|
|
12
12
|
export class CSSPropertyObserver {
|
|
13
13
|
#root;
|
|
14
|
-
#name;
|
|
15
14
|
#callback;
|
|
16
15
|
#properties = new Set();
|
|
17
16
|
#styleSheet;
|
|
18
17
|
#isConnected = false;
|
|
19
18
|
|
|
20
|
-
constructor(root,
|
|
19
|
+
constructor(root, callback) {
|
|
21
20
|
this.#root = root;
|
|
22
|
-
this.#name = name;
|
|
23
21
|
this.#callback = callback;
|
|
22
|
+
this.#styleSheet = new CSSStyleSheet();
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
#handleTransitionEvent(event) {
|
|
@@ -33,27 +32,30 @@ export class CSSPropertyObserver {
|
|
|
33
32
|
observe(property) {
|
|
34
33
|
this.connect();
|
|
35
34
|
|
|
36
|
-
this.#properties.
|
|
37
|
-
this.#rootHost.style.setProperty(`--${this.#name}-props`, [...this.#properties].join(', '));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
connect() {
|
|
41
|
-
if (this.#isConnected) {
|
|
35
|
+
if (this.#properties.has(property)) {
|
|
42
36
|
return;
|
|
43
37
|
}
|
|
44
38
|
|
|
45
|
-
this.#
|
|
39
|
+
this.#properties.add(property);
|
|
40
|
+
|
|
46
41
|
this.#styleSheet.replaceSync(`
|
|
47
|
-
:
|
|
42
|
+
:root::before, :host::before {
|
|
48
43
|
content: '' !important;
|
|
49
44
|
position: absolute !important;
|
|
50
45
|
top: -9999px !important;
|
|
51
46
|
left: -9999px !important;
|
|
52
47
|
visibility: hidden !important;
|
|
53
48
|
transition: 1ms allow-discrete step-end !important;
|
|
54
|
-
transition-property:
|
|
49
|
+
transition-property: ${[...this.#properties].join(', ')} !important;
|
|
55
50
|
}
|
|
56
51
|
`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
connect() {
|
|
55
|
+
if (this.#isConnected) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
57
59
|
this.#root.adoptedStyleSheets.unshift(this.#styleSheet);
|
|
58
60
|
|
|
59
61
|
this.#rootHost.addEventListener('transitionstart', (event) => this.#handleTransitionEvent(event));
|
|
@@ -69,7 +71,6 @@ export class CSSPropertyObserver {
|
|
|
69
71
|
|
|
70
72
|
this.#rootHost.removeEventListener('transitionstart', this.#handleTransitionEvent);
|
|
71
73
|
this.#rootHost.removeEventListener('transitionend', this.#handleTransitionEvent);
|
|
72
|
-
this.#rootHost.style.removeProperty(`--${this.#name}-props`);
|
|
73
74
|
|
|
74
75
|
this.#isConnected = false;
|
|
75
76
|
}
|
package/src/lumo-injector.js
CHANGED
|
@@ -34,26 +34,26 @@ import { parseStyleSheets } from './lumo-modules.js';
|
|
|
34
34
|
* }
|
|
35
35
|
*
|
|
36
36
|
* html {
|
|
37
|
-
* --vaadin-text-field-
|
|
38
|
-
* --vaadin-text-field-
|
|
37
|
+
* --_lumo-vaadin-text-field-inject: 1;
|
|
38
|
+
* --_lumo-vaadin-text-field-inject-modules:
|
|
39
39
|
* lumo_base-field,
|
|
40
40
|
* lumo_text-field;
|
|
41
41
|
*
|
|
42
|
-
* --vaadin-email-field-
|
|
43
|
-
* --vaadin-email-field-
|
|
42
|
+
* --_lumo-vaadin-email-field-inject: 1;
|
|
43
|
+
* --_lumo-vaadin-email-field-inject-modules:
|
|
44
44
|
* lumo_base-field,
|
|
45
45
|
* lumo_email-field;
|
|
46
46
|
* }
|
|
47
47
|
* ```
|
|
48
48
|
*
|
|
49
|
-
* The class observes the custom property `--{tagName}-
|
|
49
|
+
* The class observes the custom property `--_lumo-{tagName}-inject`,
|
|
50
50
|
* which indicates whether styles are present for the given component
|
|
51
51
|
* in the document style sheets. When the property is set to `1`, the
|
|
52
52
|
* class recursively searches all document style sheets for CSS modules
|
|
53
|
-
* listed in the `--{tagName}-
|
|
53
|
+
* listed in the `--_lumo-{tagName}-inject-modules` property that apply to
|
|
54
54
|
* the given component tag name. The found rules are then injected
|
|
55
55
|
* into the component's Shadow DOM using the `adoptedStyleSheets` API,
|
|
56
|
-
* in the order specified in the `--{tagName}-
|
|
56
|
+
* in the order specified in the `--_lumo-{tagName}-inject-modules` property.
|
|
57
57
|
* The same module can be used in multiple components.
|
|
58
58
|
*
|
|
59
59
|
* The class also removes the injected styles when the property is set to `0`.
|
|
@@ -82,8 +82,8 @@ export class LumoInjector {
|
|
|
82
82
|
|
|
83
83
|
constructor(root = document) {
|
|
84
84
|
this.#root = root;
|
|
85
|
-
this.#cssPropertyObserver = new CSSPropertyObserver(this.#root,
|
|
86
|
-
const tagName = propertyName.
|
|
85
|
+
this.#cssPropertyObserver = new CSSPropertyObserver(this.#root, (propertyName) => {
|
|
86
|
+
const tagName = propertyName.match(/^--_lumo-(.*)-inject$/u)?.[1];
|
|
87
87
|
this.#updateStyleSheet(tagName);
|
|
88
88
|
});
|
|
89
89
|
}
|
|
@@ -98,7 +98,7 @@ export class LumoInjector {
|
|
|
98
98
|
* Adds a component to the list of elements monitored for style injection.
|
|
99
99
|
* If the styles have already been detected, they are injected into the
|
|
100
100
|
* component's shadow DOM immediately. Otherwise, the class watches the
|
|
101
|
-
* custom property `--{tagName}-
|
|
101
|
+
* custom property `--_lumo-{tagName}-inject` to trigger injection when
|
|
102
102
|
* the styles are added to the document or root element.
|
|
103
103
|
*
|
|
104
104
|
* @param {HTMLElement} component
|
package/src/lumo-modules.js
CHANGED
|
@@ -3,9 +3,33 @@
|
|
|
3
3
|
* Copyright (c) 2000 - 2025 Vaadin Ltd.
|
|
4
4
|
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
5
|
*/
|
|
6
|
+
import { issueWarning } from '@vaadin/component-base/src/warnings.js';
|
|
7
|
+
|
|
6
8
|
/** @type {WeakMap<CSSStyleSheet, Record<string, Map>>} */
|
|
7
9
|
const cache = new WeakMap();
|
|
8
10
|
|
|
11
|
+
function getRuleMediaText(rule) {
|
|
12
|
+
try {
|
|
13
|
+
return rule.media.mediaText;
|
|
14
|
+
} catch {
|
|
15
|
+
issueWarning(
|
|
16
|
+
'[LumoInjector] Browser denied to access property "mediaText" for some CSS rules, so they were skipped.',
|
|
17
|
+
);
|
|
18
|
+
return '';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getStyleSheetRules(styleSheet) {
|
|
23
|
+
try {
|
|
24
|
+
return styleSheet.cssRules;
|
|
25
|
+
} catch {
|
|
26
|
+
issueWarning(
|
|
27
|
+
'[LumoInjector] Browser denied to access property "cssRules" for some CSS stylesheets, so they were skipped.',
|
|
28
|
+
);
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
9
33
|
function parseStyleSheet(
|
|
10
34
|
styleSheet,
|
|
11
35
|
result = {
|
|
@@ -13,20 +37,11 @@ function parseStyleSheet(
|
|
|
13
37
|
modules: new Map(),
|
|
14
38
|
},
|
|
15
39
|
) {
|
|
16
|
-
|
|
17
|
-
try {
|
|
18
|
-
cssRules = styleSheet.cssRules;
|
|
19
|
-
} catch {
|
|
20
|
-
// External stylesheets may not be accessible due to CORS security restrictions.
|
|
21
|
-
cssRules = [];
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
for (const rule of cssRules) {
|
|
25
|
-
const { media } = rule;
|
|
26
|
-
|
|
40
|
+
for (const rule of getStyleSheetRules(styleSheet)) {
|
|
27
41
|
if (rule instanceof CSSImportRule) {
|
|
28
|
-
|
|
29
|
-
|
|
42
|
+
const mediaText = getRuleMediaText(rule);
|
|
43
|
+
if (mediaText.startsWith('lumo_')) {
|
|
44
|
+
result.modules.set(mediaText, [...rule.styleSheet.cssRules]);
|
|
30
45
|
} else {
|
|
31
46
|
parseStyleSheet(rule.styleSheet, result);
|
|
32
47
|
}
|
|
@@ -35,16 +50,17 @@ function parseStyleSheet(
|
|
|
35
50
|
}
|
|
36
51
|
|
|
37
52
|
if (rule instanceof CSSMediaRule) {
|
|
38
|
-
|
|
39
|
-
|
|
53
|
+
const mediaText = getRuleMediaText(rule);
|
|
54
|
+
if (mediaText.startsWith('lumo_')) {
|
|
55
|
+
result.modules.set(mediaText, [...rule.cssRules]);
|
|
40
56
|
}
|
|
41
57
|
|
|
42
58
|
continue;
|
|
43
59
|
}
|
|
44
60
|
|
|
45
|
-
if (rule instanceof CSSStyleRule && rule.cssText.includes('-
|
|
61
|
+
if (rule instanceof CSSStyleRule && rule.cssText.includes('-inject')) {
|
|
46
62
|
for (const property of rule.style) {
|
|
47
|
-
const tagName = property.match(/^--(.*)-
|
|
63
|
+
const tagName = property.match(/^--_lumo-(.*)-inject-modules$/u)?.[1];
|
|
48
64
|
if (!tagName) {
|
|
49
65
|
continue;
|
|
50
66
|
}
|
|
@@ -95,7 +111,7 @@ function parseStyleSheet(
|
|
|
95
111
|
*
|
|
96
112
|
* ```css
|
|
97
113
|
* html {
|
|
98
|
-
* --vaadin-text-field-
|
|
114
|
+
* --_lumo-vaadin-text-field-inject-modules:
|
|
99
115
|
* lumo_base-field,
|
|
100
116
|
* lumo_text-field;
|
|
101
117
|
* }
|
|
@@ -7,9 +7,6 @@ import type { Constructor } from '@open-wc/dedupe-mixin';
|
|
|
7
7
|
import type { CSSResult, CSSResultGroup } from 'lit';
|
|
8
8
|
import type { ThemePropertyMixinClass } from './vaadin-theme-property-mixin.js';
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
* A mixin for `nav` elements, facilitating navigation and selection of childNodes.
|
|
12
|
-
*/
|
|
13
10
|
export declare function ThemableMixin<T extends Constructor<HTMLElement>>(
|
|
14
11
|
base: T,
|
|
15
12
|
): Constructor<ThemableMixinClass> & Constructor<ThemePropertyMixinClass> & T;
|
|
@@ -29,18 +26,6 @@ export declare interface ThemableMixinClass extends ThemePropertyMixinClass {}
|
|
|
29
26
|
*/
|
|
30
27
|
declare function registerStyles(themeFor: string | null, styles: CSSResultGroup, options?: object | null): void;
|
|
31
28
|
|
|
32
|
-
type Theme = {
|
|
33
|
-
themeFor: string;
|
|
34
|
-
styles: CSSResult[];
|
|
35
|
-
moduleId?: string;
|
|
36
|
-
include?: string[] | string;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* For internal purposes only.
|
|
41
|
-
*/
|
|
42
|
-
declare const __themeRegistry: Theme[];
|
|
43
|
-
|
|
44
29
|
export { css, unsafeCSS } from 'lit';
|
|
45
30
|
|
|
46
|
-
export { registerStyles
|
|
31
|
+
export { registerStyles };
|
package/vaadin-themable-mixin.js
CHANGED
|
@@ -55,7 +55,6 @@ function hasThemes(tagName) {
|
|
|
55
55
|
/**
|
|
56
56
|
* Flattens the styles into a single array of styles.
|
|
57
57
|
* @param {CSSResultGroup} styles
|
|
58
|
-
* @param {CSSResult[]} result
|
|
59
58
|
* @returns {CSSResult[]}
|
|
60
59
|
*/
|
|
61
60
|
function flattenStyles(styles = []) {
|
|
@@ -234,18 +233,6 @@ export function registerStyles(themeFor, styles, options = {}) {
|
|
|
234
233
|
}
|
|
235
234
|
}
|
|
236
235
|
|
|
237
|
-
/**
|
|
238
|
-
* Returns all registered themes. By default the themeRegistry is returned as is.
|
|
239
|
-
* In case the style-modules adapter is imported, the themes are obtained from there instead
|
|
240
|
-
* @returns {Theme[]}
|
|
241
|
-
*/
|
|
242
|
-
function getAllThemes() {
|
|
243
|
-
if (window.Vaadin && window.Vaadin.styleModules) {
|
|
244
|
-
return window.Vaadin.styleModules.getAllThemes();
|
|
245
|
-
}
|
|
246
|
-
return themeRegistry;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
236
|
/**
|
|
250
237
|
* Maps the moduleName to an include priority number which is used for
|
|
251
238
|
* determining the order in which styles are applied.
|
|
@@ -271,7 +258,7 @@ function getIncludedStyles(theme) {
|
|
|
271
258
|
const includedStyles = [];
|
|
272
259
|
if (theme.include) {
|
|
273
260
|
[].concat(theme.include).forEach((includeModuleId) => {
|
|
274
|
-
const includedTheme =
|
|
261
|
+
const includedTheme = themeRegistry.find((s) => s.moduleId === includeModuleId);
|
|
275
262
|
if (includedTheme) {
|
|
276
263
|
includedStyles.push(...getIncludedStyles(includedTheme), ...includedTheme.styles);
|
|
277
264
|
} else {
|
|
@@ -291,7 +278,7 @@ function getIncludedStyles(theme) {
|
|
|
291
278
|
function getThemes(tagName) {
|
|
292
279
|
const defaultModuleName = `${tagName}-default-theme`;
|
|
293
280
|
|
|
294
|
-
const themes =
|
|
281
|
+
const themes = themeRegistry
|
|
295
282
|
// Filter by matching themeFor properties
|
|
296
283
|
.filter((theme) => theme.moduleId !== defaultModuleName && matchesThemeFor(theme.themeFor, tagName))
|
|
297
284
|
.map((theme) => ({
|
|
@@ -308,7 +295,7 @@ function getThemes(tagName) {
|
|
|
308
295
|
return themes;
|
|
309
296
|
}
|
|
310
297
|
// No theme modules found, return the default module if it exists
|
|
311
|
-
return
|
|
298
|
+
return themeRegistry.filter((theme) => theme.moduleId === defaultModuleName);
|
|
312
299
|
}
|
|
313
300
|
|
|
314
301
|
/**
|
|
@@ -380,5 +367,3 @@ export const ThemableMixin = (superClass) =>
|
|
|
380
367
|
return themeStyles.filter((style, index) => index === themeStyles.lastIndexOf(style));
|
|
381
368
|
}
|
|
382
369
|
};
|
|
383
|
-
|
|
384
|
-
export { themeRegistry as __themeRegistry };
|