@vaadin/component-base 25.3.0-alpha7 → 25.3.0-dev.1fa5a51482
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 +4 -4
- package/src/define.js +1 -1
- package/src/directives/part-map.d.ts +22 -0
- package/src/directives/part-map.js +86 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vaadin/component-base",
|
|
3
|
-
"version": "25.3.0-
|
|
3
|
+
"version": "25.3.0-dev.1fa5a51482",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -38,11 +38,11 @@
|
|
|
38
38
|
"lit": "^3.0.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@vaadin/chai-plugins": "25.3.0-
|
|
42
|
-
"@vaadin/test-runner-commands": "25.3.0-
|
|
41
|
+
"@vaadin/chai-plugins": "25.3.0-dev.1fa5a51482",
|
|
42
|
+
"@vaadin/test-runner-commands": "25.3.0-dev.1fa5a51482",
|
|
43
43
|
"@vaadin/testing-helpers": "^2.0.0",
|
|
44
44
|
"sinon": "^22.0.0"
|
|
45
45
|
},
|
|
46
46
|
"customElements": "custom-elements.json",
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "1cd5964b758ffbce3ddb6248a1997f55f8e0ebf6"
|
|
48
48
|
}
|
package/src/define.js
CHANGED
|
@@ -13,7 +13,7 @@ function dashToCamelCase(dash) {
|
|
|
13
13
|
|
|
14
14
|
const experimentalMap = {};
|
|
15
15
|
|
|
16
|
-
export function defineCustomElement(CustomElement, version = '25.3.0-
|
|
16
|
+
export function defineCustomElement(CustomElement, version = '25.3.0-dev.1fa5a51482') {
|
|
17
17
|
Object.defineProperty(CustomElement, 'version', {
|
|
18
18
|
get() {
|
|
19
19
|
return version;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2026 - 2026 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import type { DirectiveResult } from 'lit/directive.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A key-value set of part names to truthy values.
|
|
10
|
+
*/
|
|
11
|
+
export interface PartNameInfo {
|
|
12
|
+
readonly [name: string]: string | boolean | number | null | undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A directive that applies dynamic shadow DOM part names.
|
|
17
|
+
*
|
|
18
|
+
* This must be used in the `part` attribute and must be the only binding in it.
|
|
19
|
+
* Each property name in `partNameInfo` is added to the element's `part` list
|
|
20
|
+
* if the property value is truthy, and removed if the value is falsy.
|
|
21
|
+
*/
|
|
22
|
+
export declare function partMap(partNameInfo: PartNameInfo): DirectiveResult;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2026 - 2026 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import { noChange } from 'lit';
|
|
7
|
+
import { Directive, directive, PartType } from 'lit/directive.js';
|
|
8
|
+
|
|
9
|
+
class PartMapDirective extends Directive {
|
|
10
|
+
// Part names applied by the directive on the previous render,
|
|
11
|
+
// used to remove names that no longer apply.
|
|
12
|
+
#previousParts;
|
|
13
|
+
|
|
14
|
+
// Part names declared statically in the attribute, never removed.
|
|
15
|
+
#staticParts;
|
|
16
|
+
|
|
17
|
+
constructor(partInfo) {
|
|
18
|
+
super(partInfo);
|
|
19
|
+
if (partInfo.type !== PartType.ATTRIBUTE || partInfo.name !== 'part' || partInfo.strings?.length > 2) {
|
|
20
|
+
throw new Error('`partMap()` can only be used in the `part` attribute and must be the only binding in it.');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
render(partNameInfo) {
|
|
25
|
+
// Add spaces to ensure separation from static parts
|
|
26
|
+
return ` ${Object.keys(partNameInfo)
|
|
27
|
+
.filter((key) => partNameInfo[key])
|
|
28
|
+
.join(' ')} `;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
update(part, [partNameInfo]) {
|
|
32
|
+
// Remember dynamic parts on the first render
|
|
33
|
+
if (this.#previousParts === undefined) {
|
|
34
|
+
this.#previousParts = new Set();
|
|
35
|
+
if (part.strings !== undefined) {
|
|
36
|
+
this.#staticParts = new Set(
|
|
37
|
+
part.strings
|
|
38
|
+
.join(' ')
|
|
39
|
+
.split(/\s/u)
|
|
40
|
+
.filter((s) => s !== ''),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
Object.keys(partNameInfo).forEach((name) => {
|
|
44
|
+
if (partNameInfo[name] && !this.#staticParts?.has(name)) {
|
|
45
|
+
this.#previousParts.add(name);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
return this.render(partNameInfo);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const partList = part.element.part;
|
|
52
|
+
|
|
53
|
+
// Remove old parts that no longer apply
|
|
54
|
+
this.#previousParts.forEach((name) => {
|
|
55
|
+
if (!(name in partNameInfo)) {
|
|
56
|
+
partList.remove(name);
|
|
57
|
+
this.#previousParts.delete(name);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Add or remove parts based on their partMap value
|
|
62
|
+
Object.keys(partNameInfo).forEach((name) => {
|
|
63
|
+
const value = !!partNameInfo[name];
|
|
64
|
+
if (value !== this.#previousParts.has(name) && !this.#staticParts?.has(name)) {
|
|
65
|
+
if (value) {
|
|
66
|
+
partList.add(name);
|
|
67
|
+
this.#previousParts.add(name);
|
|
68
|
+
} else {
|
|
69
|
+
partList.remove(name);
|
|
70
|
+
this.#previousParts.delete(name);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return noChange;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A directive that applies dynamic shadow DOM part names.
|
|
81
|
+
*
|
|
82
|
+
* This must be used in the `part` attribute and must be the only binding in it.
|
|
83
|
+
* Each property name in `partNameInfo` is added to the element's `part` list
|
|
84
|
+
* if the property value is truthy, and removed if the value is falsy.
|
|
85
|
+
*/
|
|
86
|
+
export const partMap = directive(PartMapDirective);
|