Skip to content

Commit 5edbd34

Browse files
fix: show card content when passing text to default slot (#11209) (#11242)
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
1 parent 792acde commit 5edbd34

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

packages/card/src/vaadin-card.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
import { html, LitElement } from 'lit';
77
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
8+
import { isEmptyTextNode } from '@vaadin/component-base/src/dom-utils.js';
89
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
910
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
1011
import { generateUniqueId } from '@vaadin/component-base/src/unique-id-utils.js';
@@ -157,13 +158,19 @@ class Card extends ElementMixin(ThemableMixin(PolylitMixin(LumoInjectionMixin(Li
157158
);
158159
this.toggleAttribute('_hp', this.querySelector(':scope > [slot="header-prefix"]'));
159160
this.toggleAttribute('_hs', this.querySelector(':scope > [slot="header-suffix"]'));
160-
this.toggleAttribute('_c', this.querySelector(':scope > :not([slot])'));
161+
this.toggleAttribute('_c', this.__hasContent());
161162
this.toggleAttribute('_f', this.querySelector(':scope > [slot="footer"]'));
162163
if (this.__getCustomTitleElement()) {
163164
this.__clearStringTitle();
164165
}
165166
}
166167

168+
/** @private */
169+
__hasContent() {
170+
const slot = this.shadowRoot.querySelector('slot:not([name])');
171+
return slot.assignedNodes({ flatten: true }).filter((node) => !isEmptyTextNode(node)).length > 0;
172+
}
173+
167174
/** @private */
168175
__clearStringTitle() {
169176
const stringTitleElement = this.__getStringTitleElement();

packages/card/test/dom/__snapshots__/card.test.snap.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ snapshots["vaadin-card host content"] =
138138
`;
139139
/* end snapshot vaadin-card host content */
140140

141+
snapshots["vaadin-card host text content"] =
142+
`<vaadin-card
143+
_c=""
144+
role="region"
145+
>
146+
Text content
147+
</vaadin-card>
148+
`;
149+
/* end snapshot vaadin-card host text content */
150+
141151
snapshots["vaadin-card host footer"] =
142152
`<vaadin-card
143153
_f=""

packages/card/test/dom/card.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ describe('vaadin-card', () => {
109109
await expect(card).dom.to.equalSnapshot();
110110
});
111111

112+
it('text content', async () => {
113+
card.appendChild(document.createTextNode('Text content'));
114+
await nextUpdate(card);
115+
await expect(card).dom.to.equalSnapshot();
116+
});
117+
112118
it('footer', async () => {
113119
const footer = document.createElement('div');
114120
footer.setAttribute('slot', 'footer');

packages/card/test/visual/base/card.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('card', () => {
2222
div.style.padding = '20px';
2323
});
2424

25-
const cardFixture = (content) => fixtureSync(`<vaadin-card>${content}</vaadin-card>`, div);
25+
const cardFixture = (content = '') => fixtureSync(`<vaadin-card>${content}</vaadin-card>`, div);
2626

2727
const mediaFixture = (showImage, theme) => {
2828
const media = showImage

packages/card/test/visual/lumo/card.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('card', () => {
4747
div.style.padding = '20px';
4848
});
4949

50-
const cardFixture = (content) => fixtureSync(`<vaadin-card>${content}</vaadin-card>`, div);
50+
const cardFixture = (content = '') => fixtureSync(`<vaadin-card>${content}</vaadin-card>`, div);
5151

5252
describe('slot', () => {
5353
it('content', async () => {

0 commit comments

Comments
 (0)