Skip to content

Commit b8de26d

Browse files
authored
refactor: make slot-target-mixin clone instead of moving (#2735)
1 parent c30b28b commit b8de26d

14 files changed

Lines changed: 187 additions & 31 deletions

File tree

‎dev/checkbox.html‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
</head>
1313

1414
<body>
15-
<vaadin-checkbox>I accept <a href="http://vaadin.com">the terms and conditions</a></vaadin-checkbox>
15+
<vaadin-checkbox>
16+
<label slot="label">
17+
I accept <a href="http://vaadin.com">the terms and conditions</a>
18+
</label>
19+
</vaadin-checkbox>
1620
</body>
1721
</html>

‎dev/radio-group.html‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
<body>
1515
<vaadin-radio-group label="Travel class">
16-
<vaadin-radio-button value="economy">Economy</vaadin-radio-button>
17-
<vaadin-radio-button value="business">Business</vaadin-radio-button>
18-
<vaadin-radio-button value="firstClass">First Class</vaadin-radio-button>
16+
<vaadin-radio-button value="economy" label="Economy"></vaadin-radio-button>
17+
<vaadin-radio-button value="business" label="Business"></vaadin-radio-button>
18+
<vaadin-radio-button value="firstClass" label="First Class"></vaadin-radio-button>
1919
</vaadin-radio-group>
2020
</body>
2121
</html>

‎packages/checkbox/src/vaadin-checkbox.js‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class Checkbox extends SlotLabelMixin(
174174
}
175175

176176
/**
177-
* A reference to the default slot from which nodes are forwarded to the label node.
177+
* A reference to the default slot from which nodes are copied to the label node.
178178
*
179179
* @override
180180
* @protected
@@ -184,6 +184,21 @@ class Checkbox extends SlotLabelMixin(
184184
return this.$.noop;
185185
}
186186

187+
/**
188+
* Override __copyNodesToSlotTarget from SlotTargetMixin to show a warning.
189+
* @override
190+
* @protected
191+
* @param {!Array<!Node>} nodes
192+
**/
193+
__copyNodesToSlotTarget(nodes) {
194+
super.__copyNodesToSlotTarget(nodes);
195+
196+
console.warn(
197+
`WARNING: Since Vaadin 22, placing the label as a direct child of a <vaadin-checkbox> is deprecated.
198+
Please use <label slot="label"> wrapper or the label property instead.`
199+
);
200+
}
201+
187202
/**
188203
* Extends the method from `ActiveMixin` in order to
189204
* prevent setting the `active` attribute when interacting with a link inside the label.

‎packages/checkbox/test/checkbox.test.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,23 @@ describe('checkbox', () => {
281281
});
282282
});
283283
});
284+
285+
describe('warnings', () => {
286+
beforeEach(() => {
287+
sinon.stub(console, 'warn');
288+
});
289+
290+
afterEach(() => {
291+
console.warn.restore();
292+
});
293+
294+
it('should warn about using default slot label', () => {
295+
fixtureSync('<vaadin-checkbox>label</vaadin-checkbox>');
296+
297+
expect(console.warn.calledOnce).to.be.true;
298+
expect(console.warn.args[0][0]).to.include(
299+
'WARNING: Since Vaadin 22, placing the label as a direct child of a <vaadin-checkbox> is deprecated.'
300+
);
301+
});
302+
});
284303
});

‎packages/checkbox/test/visual/lumo/checkbox.test.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('checkbox', () => {
1010
div = document.createElement('div');
1111
div.style.display = 'inline-block';
1212
div.style.padding = '10px';
13-
element = fixtureSync('<vaadin-checkbox>Checkbox</vaadin-checkbox>', div);
13+
element = fixtureSync('<vaadin-checkbox label="Checkbox"></vaadin-checkbox>', div);
1414
});
1515

1616
it('basic', async () => {

‎packages/checkbox/test/visual/material/checkbox.test.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('checkbox', () => {
1010
div = document.createElement('div');
1111
div.style.display = 'inline-block';
1212
div.style.padding = '10px';
13-
element = fixtureSync('<vaadin-checkbox>Checkbox</vaadin-checkbox>', div);
13+
element = fixtureSync('<vaadin-checkbox label="Checkbox"></vaadin-checkbox>', div);
1414
});
1515

1616
it('basic', async () => {

‎packages/field-base/src/slot-target-mixin.d.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
/**
8-
* A mixin to forward the content from a source slot to a target element.
8+
* A mixin to copy the content from a source slot to a target element.
99
*/
1010
declare function SlotTargetMixin<T extends new (...args: any[]) => {}>(base: T): T & SlotTargetMixinConstructor;
1111

@@ -15,14 +15,14 @@ interface SlotTargetMixinConstructor {
1515

1616
interface SlotTargetMixin {
1717
/**
18-
* A reference to the source slot from which the content is forwarded to the target element.
18+
* A reference to the source slot from which the content is copied to the target element.
1919
*
2020
* @protected
2121
*/
2222
_sourceSlot: HTMLSlotElement;
2323

2424
/**
25-
* A reference to the target element to which the content is forwarded from the source slot.
25+
* A reference to the target element to which the content is copied from the source slot.
2626
*
2727
* @protected
2828
*/

‎packages/field-base/src/slot-target-mixin.js‎

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ const SlotTargetMixinImplementation = (superclass) =>
77
super.ready();
88

99
if (this._sourceSlot) {
10-
this.__forwardNodesToSlotTarget();
10+
this.__sourceSlotObserver = new MutationObserver(() => this.__checkAndCopyNodesToSlotTarget());
11+
12+
this.__checkAndCopyNodesToSlotTarget();
1113

1214
this._sourceSlot.addEventListener('slotchange', () => {
13-
this.__forwardNodesToSlotTarget();
15+
this.__checkAndCopyNodesToSlotTarget();
1416
});
1517
}
1618
}
1719

1820
/**
19-
* A reference to the source slot from which the nodes are forwarded to the target element.
21+
* A reference to the source slot from which the nodes are copied to the target element.
2022
*
2123
* @type {HTMLSlotElement | null}
2224
* @protected
@@ -27,7 +29,7 @@ const SlotTargetMixinImplementation = (superclass) =>
2729
}
2830

2931
/**
30-
* A reference to the target element to which the nodes are forwarded from the source slot.
32+
* A reference to the target element to which the nodes are copied from the source slot.
3133
*
3234
* @type {HTMLElement | null}
3335
* @protected
@@ -38,28 +40,64 @@ const SlotTargetMixinImplementation = (superclass) =>
3840
}
3941

4042
/**
41-
* Forwards every node from the source slot to the target element
43+
* Copies every node from the source slot to the target element
4244
* once the source slot' content is changed.
4345
*
4446
* @private
4547
*/
46-
__forwardNodesToSlotTarget() {
48+
__checkAndCopyNodesToSlotTarget() {
49+
this.__sourceSlotObserver.disconnect();
50+
4751
if (!this._slotTarget) {
4852
return;
4953
}
5054

55+
// Remove any existing clones from the slot target
56+
if (this.__slotTargetClones) {
57+
this.__slotTargetClones.forEach((node) => {
58+
if (node.parentElement === this._slotTarget) {
59+
this._slotTarget.removeChild(node);
60+
}
61+
});
62+
delete this.__slotTargetClones;
63+
}
64+
5165
// Exclude whitespace text nodes
5266
const nodes = this._sourceSlot
5367
.assignedNodes({ flatten: true })
5468
.filter((node) => !(node.nodeType == Node.TEXT_NODE && node.textContent.trim() === ''));
5569

5670
if (nodes.length > 0) {
57-
this._slotTarget.replaceChildren(...nodes);
71+
this._slotTarget.innerHTML = '';
72+
this.__copyNodesToSlotTarget(nodes);
5873
}
5974
}
75+
76+
/**
77+
* Copies the nodes to the target element.
78+
*
79+
* @protected
80+
* @param {!Array<!Node>} nodes
81+
*/
82+
__copyNodesToSlotTarget(nodes) {
83+
this.__slotTargetClones = this.__slotTargetClones || [];
84+
nodes.forEach((node) => {
85+
// Clone the nodes and append the clones to the target slot
86+
const clone = node.cloneNode(true);
87+
this.__slotTargetClones.push(clone);
88+
this._slotTarget.appendChild(clone);
89+
// Observe all changes to the source node to have the clones updated
90+
this.__sourceSlotObserver.observe(node, {
91+
attributes: true,
92+
childList: true,
93+
subtree: true,
94+
characterData: true
95+
});
96+
});
97+
}
6098
};
6199

62100
/**
63-
* A mixin to forward the content from a source slot to a target element.
101+
* A mixin to copy the content from a source slot to a target element.
64102
*/
65103
export const SlotTargetMixin = dedupingMixin(SlotTargetMixinImplementation);

‎packages/field-base/test/slot-target-mixin.test.js‎

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ describe('slot-target-mixin', () => {
6161
beforeEach(() => {
6262
element = document.createElement('slot-target-mixin-element');
6363
node1 = document.createElement('div');
64+
node1.textContent = 'node1';
6465
node2 = document.createTextNode('');
6566
element.append(node1, node2);
6667

@@ -72,13 +73,12 @@ describe('slot-target-mixin', () => {
7273
});
7374

7475
it('should populate the target element with non-empty nodes from the source slot', () => {
75-
expect(element._slotTarget.childNodes).to.have.lengthOf(1);
76-
expect(element._slotTarget.firstChild).to.equal(node1);
76+
expect(element._slotTarget.firstChild.textContent).to.equal('node1');
7777
});
7878

79-
it('should keep whitespace text nodes in the source slot', () => {
80-
expect(element.childNodes).to.have.lengthOf(1);
81-
expect(element.firstChild).to.equal(node2);
79+
it('should not clone whitespace text nodes', () => {
80+
expect(element.childNodes).to.have.lengthOf(2);
81+
expect(element._slotTarget.childNodes).to.have.lengthOf(1);
8282
});
8383
});
8484

@@ -87,22 +87,68 @@ describe('slot-target-mixin', () => {
8787
element = fixtureSync(`<slot-target-mixin-element></slot-target-mixin-element>`);
8888

8989
const node = document.createElement('div');
90+
node.textContent = 'node';
9091
element.appendChild(node);
9192
await nextFrame();
9293

9394
expect(element._slotTarget.childNodes).to.have.lengthOf(1);
94-
expect(element._slotTarget.firstChild).to.equal(node);
95+
expect(element._slotTarget.firstChild.textContent).to.equal('node');
9596
});
9697

97-
it('should re-populate the target element when adding new nodes to the source slot', async () => {
98+
it('should add new nodes to the target slot', async () => {
9899
element = fixtureSync(`<slot-target-mixin-element><div>Content</div></slot-target-mixin-element>`);
99100

100101
const node = document.createElement('div');
102+
node.textContent = 'New content';
101103
element.appendChild(node);
102104
await nextFrame();
103105

104-
expect(element._slotTarget.childNodes).to.have.lengthOf(1);
105-
expect(element._slotTarget.firstChild).to.equal(node);
106+
expect(element._slotTarget.childNodes).to.have.lengthOf(2);
107+
expect(element._slotTarget.children[0].textContent).to.equal('Content');
108+
expect(element._slotTarget.children[1].textContent).to.equal('New content');
109+
});
110+
111+
it('should reflect content mutations to the target slot content', async () => {
112+
element = fixtureSync(`<slot-target-mixin-element><div>Content</div></slot-target-mixin-element>`);
113+
expect(element._slotTarget.textContent).to.equal('Content');
114+
await nextFrame();
115+
element.firstChild.textContent = 'New content';
116+
await nextFrame();
117+
expect(element._slotTarget.textContent).to.equal('New content');
118+
});
119+
120+
it('should reflect content attribute mutations to the target slot content', async () => {
121+
element = fixtureSync(`<slot-target-mixin-element><div>Content</div></slot-target-mixin-element>`);
122+
await nextFrame();
123+
element.firstChild.setAttribute('data-name', 'content');
124+
await nextFrame();
125+
expect(element._slotTarget.firstElementChild.dataset.name).to.equal('content');
126+
});
127+
128+
it('should reflect content subtree mutations to the target slot content', async () => {
129+
element = fixtureSync(`<slot-target-mixin-element><div>Content</div></slot-target-mixin-element>`);
130+
const subtree = document.createElement('div');
131+
element.firstChild.appendChild(subtree);
132+
await nextFrame();
133+
subtree.textContent = 'New content';
134+
await nextFrame();
135+
expect(element._slotTarget.firstElementChild.firstElementChild.textContent).to.equal('New content');
136+
});
137+
138+
it('should reflect content character data mutations to the target slot content', async () => {
139+
element = fixtureSync(`<slot-target-mixin-element><div>Content</div></slot-target-mixin-element>`);
140+
await nextFrame();
141+
element.firstChild.firstChild.textContent = 'New content';
142+
await nextFrame();
143+
expect(element._slotTarget.firstElementChild.textContent).to.equal('New content');
144+
});
145+
146+
it('should reflect cleared content to the target slot content', async () => {
147+
element = fixtureSync(`<slot-target-mixin-element><div>Content</div></slot-target-mixin-element>`);
148+
await nextFrame();
149+
element.firstChild.remove();
150+
await nextFrame();
151+
expect(element._slotTarget.children).to.be.empty;
106152
});
107153
});
108154

‎packages/radio-group/src/vaadin-radio-button.js‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class RadioButton extends SlotLabelMixin(
141141
}
142142

143143
/**
144-
* A reference to the default slot from which nodes are forwarded to the label node.
144+
* A reference to the default slot from which nodes are copied to the label node.
145145
*
146146
* @override
147147
* @protected
@@ -151,6 +151,21 @@ class RadioButton extends SlotLabelMixin(
151151
return this.$.noop;
152152
}
153153

154+
/**
155+
* Override __copyNodesToSlotTarget from SlotTargetMixin to show a warning.
156+
* @override
157+
* @protected
158+
* @param {!Array<!Node>} nodes
159+
**/
160+
__copyNodesToSlotTarget(nodes) {
161+
super.__copyNodesToSlotTarget(nodes);
162+
163+
console.warn(
164+
`WARNING: Since Vaadin 22, placing the label as a direct child of a <vaadin-radio-button> is deprecated.
165+
Please use <label slot="label"> wrapper or the label property instead.`
166+
);
167+
}
168+
154169
/** @protected */
155170
ready() {
156171
super.ready();

0 commit comments

Comments
 (0)