@vaadin/field-base 23.0.0-alpha2 → 23.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/LICENSE +1 -1
- package/package.json +4 -3
- package/src/checked-mixin.d.ts +1 -1
- package/src/checked-mixin.js +1 -1
- package/src/delegate-focus-mixin.d.ts +1 -1
- package/src/delegate-focus-mixin.js +1 -1
- package/src/delegate-state-mixin.d.ts +1 -1
- package/src/delegate-state-mixin.js +1 -1
- package/src/field-aria-controller.d.ts +1 -1
- package/src/field-aria-controller.js +1 -1
- package/src/field-mixin.d.ts +3 -1
- package/src/field-mixin.js +35 -185
- package/src/helper-controller.d.ts +23 -0
- package/src/helper-controller.js +185 -0
- package/src/input-constraints-mixin.d.ts +1 -1
- package/src/input-constraints-mixin.js +1 -1
- package/src/input-control-mixin.d.ts +1 -1
- package/src/input-control-mixin.js +1 -1
- package/src/input-controller.d.ts +1 -1
- package/src/input-controller.js +1 -1
- package/src/input-field-mixin.d.ts +1 -1
- package/src/input-field-mixin.js +16 -1
- package/src/input-mixin.d.ts +1 -1
- package/src/input-mixin.js +1 -1
- package/src/label-controller.d.ts +26 -0
- package/src/label-controller.js +186 -0
- package/src/label-mixin.d.ts +4 -3
- package/src/label-mixin.js +10 -49
- package/src/labelled-input-controller.d.ts +1 -1
- package/src/labelled-input-controller.js +17 -4
- package/src/pattern-mixin.d.ts +1 -1
- package/src/pattern-mixin.js +1 -1
- package/src/shadow-focus-mixin.d.ts +1 -1
- package/src/shadow-focus-mixin.js +1 -1
- package/src/slot-styles-mixin.d.ts +1 -1
- package/src/slot-styles-mixin.js +1 -1
- package/src/slot-target-controller.d.ts +31 -0
- package/src/slot-target-controller.js +119 -0
- package/src/styles/clear-button-styles.d.ts +1 -1
- package/src/styles/clear-button-styles.js +1 -1
- package/src/styles/field-shared-styles.d.ts +1 -1
- package/src/styles/field-shared-styles.js +1 -1
- package/src/styles/input-field-container-styles.d.ts +1 -1
- package/src/styles/input-field-container-styles.js +1 -1
- package/src/styles/input-field-shared-styles.d.ts +1 -1
- package/src/styles/input-field-shared-styles.js +1 -1
- package/src/text-area-controller.d.ts +1 -1
- package/src/text-area-controller.js +1 -1
- package/src/utils.d.ts +1 -1
- package/src/utils.js +1 -1
- package/src/validate-mixin.d.ts +1 -1
- package/src/validate-mixin.js +1 -1
- package/src/virtual-keyboard-controller.d.ts +14 -0
- package/src/virtual-keyboard-controller.js +38 -0
- package/src/slot-label-mixin.d.ts +0 -15
- package/src/slot-label-mixin.js +0 -38
- package/src/slot-target-mixin.d.ts +0 -25
- package/src/slot-target-mixin.js +0 -110
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2021 Vaadin Ltd.
|
|
3
|
+
* Copyright (c) 2021 - 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 { SlotController } from '@vaadin/component-base/src/slot-controller.js';
|
package/src/input-controller.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2021 Vaadin Ltd.
|
|
3
|
+
* Copyright (c) 2021 - 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 { SlotController } from '@vaadin/component-base/src/slot-controller.js';
|
package/src/input-field-mixin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2021 Vaadin Ltd.
|
|
3
|
+
* Copyright (c) 2021 - 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 { InputControlMixin } from './input-control-mixin.js';
|
|
@@ -120,6 +120,21 @@ export const InputFieldMixin = (superclass) =>
|
|
|
120
120
|
this.validate();
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Override an event listener from `InputMixin`
|
|
125
|
+
* to mark as valid after user started typing.
|
|
126
|
+
* @param {Event} event
|
|
127
|
+
* @protected
|
|
128
|
+
* @override
|
|
129
|
+
*/
|
|
130
|
+
_onInput(event) {
|
|
131
|
+
super._onInput(event);
|
|
132
|
+
|
|
133
|
+
if (this.invalid) {
|
|
134
|
+
this.validate();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
123
138
|
/**
|
|
124
139
|
* Override a method from `InputMixin` to validate the field
|
|
125
140
|
* when a new value is set programmatically.
|
package/src/input-mixin.d.ts
CHANGED
package/src/input-mixin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2021 Vaadin Ltd.
|
|
3
|
+
* Copyright (c) 2021 - 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 { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 - 2022 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A controller to manage the label element.
|
|
10
|
+
*/
|
|
11
|
+
export class LabelController extends SlotController {
|
|
12
|
+
/**
|
|
13
|
+
* String used for the label.
|
|
14
|
+
*/
|
|
15
|
+
protected label: string | null | undefined;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Set label based on corresponding host property.
|
|
19
|
+
*/
|
|
20
|
+
setLabel(label: string | null | undefined): void;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* ID attribute value set on the label element.
|
|
24
|
+
*/
|
|
25
|
+
labelId: string;
|
|
26
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 - 2022 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A controller to manage the label element.
|
|
10
|
+
*/
|
|
11
|
+
export class LabelController extends SlotController {
|
|
12
|
+
constructor(host) {
|
|
13
|
+
super(
|
|
14
|
+
host,
|
|
15
|
+
'label',
|
|
16
|
+
() => document.createElement('label'),
|
|
17
|
+
(_host, node) => {
|
|
18
|
+
// Set ID attribute or use the existing one.
|
|
19
|
+
this.__updateLabelId(node);
|
|
20
|
+
|
|
21
|
+
// Set text content for the default label.
|
|
22
|
+
this.__updateDefaultLabel(this.label);
|
|
23
|
+
|
|
24
|
+
this.__observeLabel(node);
|
|
25
|
+
}
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @return {string}
|
|
31
|
+
*/
|
|
32
|
+
get labelId() {
|
|
33
|
+
return this.node.id;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Override to initialize the newly added custom label.
|
|
38
|
+
*
|
|
39
|
+
* @param {Node} labelNode
|
|
40
|
+
* @protected
|
|
41
|
+
* @override
|
|
42
|
+
*/
|
|
43
|
+
initCustomNode(labelNode) {
|
|
44
|
+
const hasLabel = this.__hasLabel(labelNode);
|
|
45
|
+
this.__toggleHasLabel(hasLabel);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Override to cleanup label node when it's removed.
|
|
50
|
+
*
|
|
51
|
+
* @param {Node} node
|
|
52
|
+
* @protected
|
|
53
|
+
* @override
|
|
54
|
+
*/
|
|
55
|
+
teardownNode(node) {
|
|
56
|
+
if (this.__labelObserver) {
|
|
57
|
+
this.__labelObserver.disconnect();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let labelNode = this.getSlotChild();
|
|
61
|
+
|
|
62
|
+
// If custom label was removed, restore the default one.
|
|
63
|
+
if (!labelNode && node !== this.defaultNode) {
|
|
64
|
+
labelNode = this.attachDefaultNode();
|
|
65
|
+
|
|
66
|
+
// Run initializer to update default label and ID.
|
|
67
|
+
this.initNode(labelNode);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const hasLabel = this.__hasLabel(labelNode);
|
|
71
|
+
this.__toggleHasLabel(hasLabel);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Set label based on corresponding host property.
|
|
76
|
+
*
|
|
77
|
+
* @param {string} label
|
|
78
|
+
*/
|
|
79
|
+
setLabel(label) {
|
|
80
|
+
this.label = label;
|
|
81
|
+
|
|
82
|
+
this.__updateDefaultLabel(label);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @param {HTMLElement} labelNode
|
|
87
|
+
* @return {boolean}
|
|
88
|
+
* @private
|
|
89
|
+
*/
|
|
90
|
+
__hasLabel(labelNode) {
|
|
91
|
+
if (!labelNode) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return labelNode.children.length > 0 || this.__isNotEmpty(labelNode.textContent);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @param {string} label
|
|
100
|
+
* @private
|
|
101
|
+
*/
|
|
102
|
+
__isNotEmpty(label) {
|
|
103
|
+
return Boolean(label && label.trim() !== '');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @param {HTMLElement} labelNode
|
|
108
|
+
* @private
|
|
109
|
+
*/
|
|
110
|
+
__observeLabel(labelNode) {
|
|
111
|
+
this.__labelObserver = new MutationObserver((mutations) => {
|
|
112
|
+
mutations.forEach((mutation) => {
|
|
113
|
+
const target = mutation.target;
|
|
114
|
+
|
|
115
|
+
// Ensure the mutation target is the currently connected label
|
|
116
|
+
// to ignore async mutations dispatched for removed element.
|
|
117
|
+
const isLabelMutation = target === this.node;
|
|
118
|
+
|
|
119
|
+
if (mutation.type === 'attributes') {
|
|
120
|
+
// We use attributeFilter to only observe ID mutation,
|
|
121
|
+
// no need to check for attribute name separately.
|
|
122
|
+
if (isLabelMutation && target.id !== this.defaultId) {
|
|
123
|
+
this.__updateLabelId(target);
|
|
124
|
+
}
|
|
125
|
+
} else if (isLabelMutation || target.parentElement === this.node) {
|
|
126
|
+
// Update has-label when textContent changes
|
|
127
|
+
const hasLabel = this.__hasLabel(this.node);
|
|
128
|
+
this.__toggleHasLabel(hasLabel);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// Observe changes to label ID attribute, text content and children.
|
|
134
|
+
this.__labelObserver.observe(labelNode, {
|
|
135
|
+
attributes: true,
|
|
136
|
+
attributeFilter: ['id'],
|
|
137
|
+
childList: true,
|
|
138
|
+
subtree: true,
|
|
139
|
+
characterData: true
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @param {boolean} hasLabel
|
|
145
|
+
* @private
|
|
146
|
+
*/
|
|
147
|
+
__toggleHasLabel(hasLabel) {
|
|
148
|
+
this.host.toggleAttribute('has-label', hasLabel);
|
|
149
|
+
|
|
150
|
+
// Make it possible for other mixins to observe change
|
|
151
|
+
this.dispatchEvent(
|
|
152
|
+
new CustomEvent('label-changed', {
|
|
153
|
+
detail: {
|
|
154
|
+
hasLabel,
|
|
155
|
+
node: this.node
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @param {string} label
|
|
163
|
+
* @private
|
|
164
|
+
*/
|
|
165
|
+
__updateDefaultLabel(label) {
|
|
166
|
+
if (this.defaultNode) {
|
|
167
|
+
this.defaultNode.textContent = label;
|
|
168
|
+
|
|
169
|
+
// Update has-label if default label is used
|
|
170
|
+
if (this.defaultNode === this.node) {
|
|
171
|
+
const hasLabel = this.__isNotEmpty(label);
|
|
172
|
+
this.__toggleHasLabel(hasLabel);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* @param {HTMLElement} labelNode
|
|
179
|
+
* @private
|
|
180
|
+
*/
|
|
181
|
+
__updateLabelId(labelNode) {
|
|
182
|
+
if (!labelNode.id) {
|
|
183
|
+
labelNode.id = this.defaultId;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
package/src/label-mixin.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2021 Vaadin Ltd.
|
|
3
|
+
* Copyright (c) 2021 - 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';
|
|
7
7
|
import { SlotMixinClass } from '@vaadin/component-base/src/slot-mixin.js';
|
|
8
|
+
import { LabelController } from './label-controller.js';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* A mixin to provide label via corresponding property or named slot.
|
|
@@ -21,7 +22,7 @@ export declare class LabelMixinClass {
|
|
|
21
22
|
|
|
22
23
|
protected readonly _labelNode: HTMLLabelElement;
|
|
23
24
|
|
|
24
|
-
protected
|
|
25
|
+
protected _labelController: LabelController;
|
|
25
26
|
|
|
26
|
-
protected
|
|
27
|
+
protected _labelChanged(label: string | null | undefined): void;
|
|
27
28
|
}
|
package/src/label-mixin.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2021 Vaadin Ltd.
|
|
3
|
+
* Copyright (c) 2021 - 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 { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
|
|
7
|
-
import {
|
|
7
|
+
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
|
|
8
|
+
import { LabelController } from './label-controller.js';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* A mixin to provide label via corresponding property or named slot.
|
|
@@ -14,7 +15,7 @@ import { SlotMixin } from '@vaadin/component-base/src/slot-mixin.js';
|
|
|
14
15
|
*/
|
|
15
16
|
export const LabelMixin = dedupingMixin(
|
|
16
17
|
(superclass) =>
|
|
17
|
-
class LabelMixinClass extends
|
|
18
|
+
class LabelMixinClass extends ControllerMixin(superclass) {
|
|
18
19
|
static get properties() {
|
|
19
20
|
return {
|
|
20
21
|
/**
|
|
@@ -29,65 +30,25 @@ export const LabelMixin = dedupingMixin(
|
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
/** @protected */
|
|
32
|
-
get
|
|
33
|
-
return
|
|
34
|
-
...super.slots,
|
|
35
|
-
label: () => {
|
|
36
|
-
const label = document.createElement('label');
|
|
37
|
-
label.textContent = this.label;
|
|
38
|
-
return label;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
33
|
+
get _labelId() {
|
|
34
|
+
return this._labelController.labelId;
|
|
41
35
|
}
|
|
42
36
|
|
|
43
37
|
/** @protected */
|
|
44
38
|
get _labelNode() {
|
|
45
|
-
return this.
|
|
39
|
+
return this._labelController.node;
|
|
46
40
|
}
|
|
47
41
|
|
|
48
42
|
constructor() {
|
|
49
43
|
super();
|
|
50
44
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this._labelId = `label-${this.localName}-${uniqueId}`;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @type {MutationObserver}
|
|
57
|
-
* @private
|
|
58
|
-
*/
|
|
59
|
-
this.__labelNodeObserver = new MutationObserver(() => {
|
|
60
|
-
this._toggleHasLabelAttribute();
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** @protected */
|
|
65
|
-
ready() {
|
|
66
|
-
super.ready();
|
|
67
|
-
|
|
68
|
-
if (this._labelNode) {
|
|
69
|
-
this._labelNode.id = this._labelId;
|
|
70
|
-
this._toggleHasLabelAttribute();
|
|
71
|
-
|
|
72
|
-
this.__labelNodeObserver.observe(this._labelNode, { childList: true, subtree: true, characterData: true });
|
|
73
|
-
}
|
|
45
|
+
this._labelController = new LabelController(this);
|
|
46
|
+
this.addController(this._labelController);
|
|
74
47
|
}
|
|
75
48
|
|
|
76
49
|
/** @protected */
|
|
77
50
|
_labelChanged(label) {
|
|
78
|
-
|
|
79
|
-
this._labelNode.textContent = label;
|
|
80
|
-
this._toggleHasLabelAttribute();
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** @protected */
|
|
85
|
-
_toggleHasLabelAttribute() {
|
|
86
|
-
if (this._labelNode) {
|
|
87
|
-
const hasLabel = this._labelNode.children.length > 0 || this._labelNode.textContent.trim() !== '';
|
|
88
|
-
|
|
89
|
-
this.toggleAttribute('has-label', hasLabel);
|
|
90
|
-
}
|
|
51
|
+
this._labelController.setLabel(label);
|
|
91
52
|
}
|
|
92
53
|
}
|
|
93
54
|
);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2021 Vaadin Ltd.
|
|
3
|
+
* Copyright (c) 2021 - 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
|
|
|
@@ -8,15 +8,28 @@
|
|
|
8
8
|
* A controller for linking a `<label>` element with an `<input>` element.
|
|
9
9
|
*/
|
|
10
10
|
export class LabelledInputController {
|
|
11
|
-
constructor(input,
|
|
11
|
+
constructor(input, labelController) {
|
|
12
12
|
this.input = input;
|
|
13
13
|
this.__preventDuplicateLabelClick = this.__preventDuplicateLabelClick.bind(this);
|
|
14
14
|
|
|
15
|
+
labelController.addEventListener('label-changed', (event) => {
|
|
16
|
+
this.__initLabel(event.detail.node);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// Initialize the default label element
|
|
20
|
+
this.__initLabel(labelController.node);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {HTMLElement} label
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
27
|
+
__initLabel(label) {
|
|
15
28
|
if (label) {
|
|
16
29
|
label.addEventListener('click', this.__preventDuplicateLabelClick);
|
|
17
30
|
|
|
18
|
-
if (input) {
|
|
19
|
-
label.setAttribute('for', input.id);
|
|
31
|
+
if (this.input) {
|
|
32
|
+
label.setAttribute('for', this.input.id);
|
|
20
33
|
}
|
|
21
34
|
}
|
|
22
35
|
}
|
package/src/pattern-mixin.d.ts
CHANGED
package/src/pattern-mixin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2021 Vaadin Ltd.
|
|
3
|
+
* Copyright (c) 2021 - 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 { timeOut } from '@vaadin/component-base/src/async.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2021 Vaadin Ltd.
|
|
3
|
+
* Copyright (c) 2021 - 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 { KeyboardMixin } from '@vaadin/component-base/src/keyboard-mixin.js';
|
package/src/slot-styles-mixin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2021 Vaadin Ltd.
|
|
3
|
+
* Copyright (c) 2021 - 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 { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 - 2022 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import { ReactiveController } from 'lit';
|
|
7
|
+
|
|
8
|
+
export class SlotTargetController implements ReactiveController {
|
|
9
|
+
constructor(
|
|
10
|
+
sourceSlot: HTMLSlotElement,
|
|
11
|
+
targetFactory: () => HTMLElement,
|
|
12
|
+
copyCallback?: (nodes: HTMLElement[]) => void
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
hostConnected(): void;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The source `<slot>` element to copy nodes from.
|
|
19
|
+
*/
|
|
20
|
+
protected sourceSlot: HTMLSlotElement;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Function used to get a reference to slot target.
|
|
24
|
+
*/
|
|
25
|
+
protected targetFactory: () => HTMLElement;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Function called after copying nodes to target.
|
|
29
|
+
*/
|
|
30
|
+
protected copyCallback?: (nodes: HTMLElement[]) => void;
|
|
31
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 - 2022 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A controller to copy the content from a source slot to a target element.
|
|
9
|
+
*/
|
|
10
|
+
export class SlotTargetController {
|
|
11
|
+
constructor(sourceSlot, targetFactory, callback) {
|
|
12
|
+
/**
|
|
13
|
+
* The source `<slot>` element to copy nodes from.
|
|
14
|
+
*/
|
|
15
|
+
this.sourceSlot = sourceSlot;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Function used to get a reference to slot target.
|
|
19
|
+
*/
|
|
20
|
+
this.targetFactory = targetFactory;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Function called after copying nodes to target.
|
|
24
|
+
*/
|
|
25
|
+
this.copyCallback = callback;
|
|
26
|
+
|
|
27
|
+
if (sourceSlot) {
|
|
28
|
+
sourceSlot.addEventListener('slotchange', () => {
|
|
29
|
+
// Copy in progress, ignore this event.
|
|
30
|
+
if (this.__copying) {
|
|
31
|
+
this.__copying = false;
|
|
32
|
+
} else {
|
|
33
|
+
this.__checkAndCopyNodesToSlotTarget();
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
hostConnected() {
|
|
40
|
+
this.__sourceSlotObserver = new MutationObserver(() => this.__checkAndCopyNodesToSlotTarget());
|
|
41
|
+
|
|
42
|
+
// Ensure the content is up to date when host is connected
|
|
43
|
+
// to handle e.g. mutating text content while disconnected.
|
|
44
|
+
this.__checkAndCopyNodesToSlotTarget();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Copies every node from the source slot to the target element
|
|
49
|
+
* once the source slot' content is changed.
|
|
50
|
+
*
|
|
51
|
+
* @private
|
|
52
|
+
*/
|
|
53
|
+
__checkAndCopyNodesToSlotTarget() {
|
|
54
|
+
this.__sourceSlotObserver.disconnect();
|
|
55
|
+
|
|
56
|
+
// Ensure slot target element is up to date.
|
|
57
|
+
const slotTarget = this.targetFactory();
|
|
58
|
+
|
|
59
|
+
if (!slotTarget) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Remove any existing clones from the slot target
|
|
64
|
+
if (this.__slotTargetClones) {
|
|
65
|
+
this.__slotTargetClones.forEach((node) => {
|
|
66
|
+
if (node.parentElement === slotTarget) {
|
|
67
|
+
slotTarget.removeChild(node);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
delete this.__slotTargetClones;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Exclude whitespace text nodes
|
|
74
|
+
const nodes = this.sourceSlot
|
|
75
|
+
.assignedNodes({ flatten: true })
|
|
76
|
+
.filter((node) => !(node.nodeType == Node.TEXT_NODE && node.textContent.trim() === ''));
|
|
77
|
+
|
|
78
|
+
if (nodes.length > 0) {
|
|
79
|
+
slotTarget.innerHTML = '';
|
|
80
|
+
|
|
81
|
+
// Ignore next slotchange
|
|
82
|
+
this.__copying = true;
|
|
83
|
+
|
|
84
|
+
this.__copyNodesToSlotTarget(nodes, slotTarget);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Copies the nodes to the target element.
|
|
90
|
+
*
|
|
91
|
+
* @param {!Array<!Node>} nodes
|
|
92
|
+
* @param {HTMLElement} slotTarget
|
|
93
|
+
* @private
|
|
94
|
+
*/
|
|
95
|
+
__copyNodesToSlotTarget(nodes, slotTarget) {
|
|
96
|
+
this.__slotTargetClones = this.__slotTargetClones || [];
|
|
97
|
+
|
|
98
|
+
nodes.forEach((node) => {
|
|
99
|
+
// Clone the nodes and append the clones to the target
|
|
100
|
+
const clone = node.cloneNode(true);
|
|
101
|
+
this.__slotTargetClones.push(clone);
|
|
102
|
+
|
|
103
|
+
slotTarget.appendChild(clone);
|
|
104
|
+
|
|
105
|
+
// Observe all changes to the source node to have the clones updated
|
|
106
|
+
this.__sourceSlotObserver.observe(node, {
|
|
107
|
+
attributes: true,
|
|
108
|
+
childList: true,
|
|
109
|
+
subtree: true,
|
|
110
|
+
characterData: true
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// Run callback e.g. to show a deprecation warning
|
|
115
|
+
if (typeof this.copyCallback === 'function') {
|
|
116
|
+
this.copyCallback(nodes);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|