Skip to content

Commit 47c9793

Browse files
authored
refactor!: do not remove slotted card title when string title is set (#13231)
1 parent 411c75f commit 47c9793

5 files changed

Lines changed: 96 additions & 28 deletions

File tree

‎packages/card/src/title-controller.js‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@ export class TitleController extends SlotController {
2727
this.title = title;
2828

2929
if (title && title.trim() !== '') {
30-
// The string title replaces a custom slotted title, if any.
30+
// A custom slotted title takes precedence over the string title.
3131
const child = this.getSlotChild();
32-
if (child && child !== this.defaultNode) {
33-
child.remove();
32+
if (!child) {
33+
this.restoreDefaultNode();
34+
} else if (child === this.defaultNode) {
35+
this.updateDefaultNode(child);
3436
}
35-
36-
// Create or restore the default title node.
37-
this.restoreDefaultNode();
38-
this.updateDefaultNode(this.node);
3937
} else if (this.node && this.node === this.defaultNode) {
4038
// Clearing the string title removes the generated node.
4139
this.node.remove();
@@ -81,8 +79,7 @@ export class TitleController extends SlotController {
8179

8280
/**
8381
* Override method inherited from `SlotController` to set a generated ID
84-
* on a custom title without one, and to clear the string title property,
85-
* as a custom title takes over.
82+
* on a custom title without one.
8683
*
8784
* @param {Node} node
8885
* @protected
@@ -92,8 +89,6 @@ export class TitleController extends SlotController {
9289
if (!node.id) {
9390
node.id = this.defaultId;
9491
}
95-
96-
this.host.cardTitle = '';
9792
}
9893

9994
/**
@@ -112,12 +107,11 @@ export class TitleController extends SlotController {
112107
this.#labelId = undefined;
113108

114109
this.restoreDefaultNode();
115-
this.updateDefaultNode(this.node);
116110
}
117111
}
118112

119113
/**
120-
* Create the default title element lazily as needed.
114+
* Create and update the default title element lazily as needed.
121115
*
122116
* @protected
123117
*/
@@ -131,19 +125,18 @@ export class TitleController extends SlotController {
131125
const node = this.attachDefaultNode();
132126
node.id = this.defaultId;
133127
this.initNode(node);
128+
this.updateDefaultNode(node);
134129
}
135130
}
136131

137132
/**
138133
* Update the default title element.
139134
*
140-
* @param {Node | undefined} node
135+
* @param {Node} node
141136
* @protected
142137
*/
143138
updateDefaultNode(node) {
144-
if (node) {
145-
node.textContent = this.title;
146-
node.setAttribute('aria-level', this.level || 2);
147-
}
139+
node.textContent = this.title;
140+
node.setAttribute('aria-level', this.level || 2);
148141
}
149142
}

‎packages/card/src/vaadin-card.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mix
5757
*/
5858
declare class Card extends ElementMixin(ThemableMixin(HTMLElement)) {
5959
/**
60-
* The title of the card. When set, any custom slotted title is removed and this string-based title is used instead. The card is labelled with `aria-labelledby` by the title element in the `title` slot, generated or custom.
60+
* The title of the card. A custom slotted title takes precedence over this property. The card is labelled with `aria-labelledby` by the title element in the `title` slot, generated or custom.
6161
* @attr {string} card-title
6262
*/
6363
cardTitle: string;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Card extends ElementMixin(ThemableMixin(PolylitMixin(LumoInjectionMixin(Li
8282
static get properties() {
8383
return {
8484
/**
85-
* The title of the card. When set, any custom slotted title is removed and this string-based title is used instead. The card is labelled with `aria-labelledby` by the title element in the `title` slot, generated or custom.
85+
* The title of the card. A custom slotted title takes precedence over this property. The card is labelled with `aria-labelledby` by the title element in the `title` slot, generated or custom.
8686
*
8787
* @attr {string} card-title
8888
*/

‎packages/card/test/accessibility.test.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ describe('accessibility', () => {
4141
await nextRender();
4242
expect(card.getAttribute('aria-labelledby')).to.equal(customTitleElement.id);
4343
});
44+
45+
it('should set aria-labelledby attribute to string title id when custom title is removed', async () => {
46+
const customTitleElement = fixtureSync('<span slot="title">Custom title element</span>');
47+
card.appendChild(customTitleElement);
48+
await nextRender();
49+
customTitleElement.remove();
50+
await nextRender();
51+
expect(card.getAttribute('aria-labelledby')).to.equal(getStringTitleElement().id);
52+
});
4453
});
4554

4655
describe('custom title', () => {
@@ -60,10 +69,10 @@ describe('accessibility', () => {
6069
expect(card.getAttribute('aria-labelledby')).to.equal(customTitleElement.id);
6170
});
6271

63-
it('should set aria-labelledby attribute to string title id when string title replaces custom title', async () => {
72+
it('should keep aria-labelledby attribute set to custom title id when string title is set', async () => {
6473
card.cardTitle = 'Some title';
6574
await nextRender();
66-
expect(card.getAttribute('aria-labelledby')).to.equal(getStringTitleElement().id);
75+
expect(card.getAttribute('aria-labelledby')).to.equal(customTitleElement.id);
6776
});
6877

6978
it('should set aria-labelledby attribute to new custom title id when custom title is replaced', async () => {

‎packages/card/test/card.test.ts‎

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@vaadin/chai-plugins';
2-
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
2+
import { fixtureSync, nextRender, nextUpdate } from '@vaadin/testing-helpers';
33
import '../vaadin-card.js';
44
import type { Card } from '../vaadin-card.js';
55

@@ -106,11 +106,21 @@ describe('vaadin-card', () => {
106106
});
107107
});
108108

109-
it('should clear string title when custom title element is used', async () => {
109+
it('should keep string title value when custom title element is used', async () => {
110110
card.appendChild(fixtureSync('<span slot="title">Custom title element</span>'));
111111
await nextRender();
112-
expect(card.cardTitle).to.be.not.ok;
112+
expect(card.cardTitle).to.equal('Some title');
113113
expect(getStringTitleElement()).to.not.exist;
114+
expect(getCustomTitleElement()).to.exist;
115+
});
116+
117+
it('should restore string title when custom title element is removed', async () => {
118+
const customTitleElement = fixtureSync('<span slot="title">Custom title element</span>');
119+
card.appendChild(customTitleElement);
120+
await nextRender();
121+
customTitleElement.remove();
122+
await nextRender();
123+
expect(getStringTitleElement().textContent).to.equal('Some title');
114124
});
115125

116126
it('should clear string title when empty string title is set', async () => {
@@ -128,6 +138,16 @@ describe('vaadin-card', () => {
128138
});
129139
});
130140

141+
it('should keep custom title element when string title is set in the same task', async () => {
142+
await nextRender();
143+
const customTitleElement = fixtureSync('<span slot="title">Custom title element</span>');
144+
card.appendChild(customTitleElement);
145+
card.cardTitle = 'Some title';
146+
await nextUpdate(card);
147+
expect(getCustomTitleElement()).to.equal(customTitleElement);
148+
expect(getStringTitleElement()).to.not.exist;
149+
});
150+
131151
describe('custom title', () => {
132152
let customTitleElement: HTMLElement;
133153

@@ -137,18 +157,64 @@ describe('vaadin-card', () => {
137157
await nextRender();
138158
});
139159

140-
it('should clear custom title element when string title is set', async () => {
160+
it('should keep custom title element when string title is set', async () => {
141161
card.cardTitle = 'Some title';
142162
await nextRender();
143-
expect(getCustomTitleElement()).to.not.exist;
163+
expect(getCustomTitleElement()).to.equal(customTitleElement);
164+
expect(getStringTitleElement()).to.not.exist;
144165
});
145166

146167
it('should not clear custom title element when blank string title is set', async () => {
147-
// A custom title already sets `cardTitle` to '', use a blank string to trigger an update
148168
card.cardTitle = ' ';
149169
await nextRender();
150170
expect(getCustomTitleElement()).to.equal(customTitleElement);
151171
});
172+
173+
it('should restore string title when custom title element is removed', async () => {
174+
card.cardTitle = 'Some title';
175+
await nextRender();
176+
customTitleElement.remove();
177+
await nextRender();
178+
const stringTitleElement = getStringTitleElement();
179+
expect(stringTitleElement.textContent).to.equal('Some title');
180+
expect(stringTitleElement.getAttribute('aria-level')).to.equal('2');
181+
expect(card.getAttribute('aria-labelledby')).to.equal(stringTitleElement.id);
182+
});
183+
184+
it('should restore string title when set in the same tick as custom title element is removed', async () => {
185+
card.cardTitle = 'Some title';
186+
customTitleElement.remove();
187+
await nextRender();
188+
expect(getStringTitleElement().textContent).to.equal('Some title');
189+
});
190+
191+
it('should not restore string title when it is cleared before custom title element is removed', async () => {
192+
card.cardTitle = 'Some title';
193+
await nextRender();
194+
card.cardTitle = '';
195+
await nextRender();
196+
customTitleElement.remove();
197+
await nextRender();
198+
expect(getStringTitleElement()).to.not.exist;
199+
expect(card.hasAttribute('aria-labelledby')).to.be.false;
200+
});
201+
});
202+
203+
describe('string and custom title initially', () => {
204+
beforeEach(async () => {
205+
card = fixtureSync(`
206+
<vaadin-card card-title="Some title">
207+
<span slot="title">Custom title element</span>
208+
</vaadin-card>
209+
`);
210+
await nextRender();
211+
});
212+
213+
it('should keep custom title element and string title value', () => {
214+
expect(getCustomTitleElement()).to.exist;
215+
expect(getStringTitleElement()).to.not.exist;
216+
expect(card.cardTitle).to.equal('Some title');
217+
});
152218
});
153219
});
154220

0 commit comments

Comments
 (0)