Skip to content

Commit fe6f9b4

Browse files
committed
fix(web-components): remove empty text nodes from avatar
1 parent bfadc50 commit fe6f9b4

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "fix: remove empty text nodes from avatar",
4+
"packageName": "@fluentui/web-components",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/web-components/src/avatar/avatar.base.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import { type AvatarActive } from './avatar.options.js';
66
* @public
77
*/
88
export class BaseAvatar extends FASTElement {
9+
/**
10+
* Reference to the default slot element.
11+
* @internal
12+
*/
13+
public defaultSlot!: HTMLSlotElement;
14+
915
/**
1016
* The internal {@link https://developer.mozilla.org/docs/Web/API/ElementInternals | `ElementInternals`} instance for the component.
1117
*
@@ -51,4 +57,14 @@ export class BaseAvatar extends FASTElement {
5157

5258
this.elementInternals.role = 'img';
5359
}
60+
61+
public slotchangeHandler(e: Event): void {
62+
if (!this.defaultSlot.innerText.trim()) {
63+
this.defaultSlot.assignedNodes().forEach(node => {
64+
if (node.nodeType === Node.TEXT_NODE) {
65+
(node as Element).remove();
66+
}
67+
});
68+
}
69+
}
5470
}

packages/web-components/src/avatar/avatar.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ test.describe('Avatar', () => {
1212
await expect(element).toHaveJSProperty('elementInternals.role', 'img');
1313
});
1414

15+
test('should render an icon when no name or initials are provided', async ({ fastPage }) => {
16+
const { element } = fastPage;
17+
const icon = element.locator('svg');
18+
19+
await expect(icon).toBeVisible();
20+
});
21+
1522
test('When no name value is set, should render with custom initials based on the provided initials value', async ({
1623
fastPage,
1724
}) => {
@@ -20,6 +27,12 @@ test.describe('Avatar', () => {
2027
await fastPage.setTemplate({ attributes: { initials: 'JD' } });
2128

2229
await expect(element).toContainText('JD');
30+
31+
await test.step('should not render the default icon', async () => {
32+
const icon = element.locator('svg');
33+
34+
await expect(icon).toBeHidden();
35+
});
2336
});
2437

2538
test('When name value is set, should generate initials based on the provided name value', async ({ fastPage }) => {

packages/web-components/src/avatar/avatar.template.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ElementViewTemplate, html } from '@microsoft/fast-element';
1+
import { type ElementViewTemplate, html, ref } from '@microsoft/fast-element';
22
import type { Avatar } from './avatar.js';
33

44
const defaultIconTemplate = html`<svg
@@ -21,7 +21,9 @@ const defaultIconTemplate = html`<svg
2121
*/
2222
export function avatarTemplate<T extends Avatar>(): ElementViewTemplate<T> {
2323
return html<T>`
24-
<slot>${x => (x.name || x.initials ? x.generateInitials() : defaultIconTemplate)}</slot>
24+
<slot @slotchange="${(x, c) => x.slotchangeHandler(c.event)}" ${ref('defaultSlot')}>
25+
${x => (x.name || x.initials ? x.generateInitials() : defaultIconTemplate)}
26+
</slot>
2527
<slot name="badge"></slot>
2628
`;
2729
}

packages/web-components/src/avatar/avatar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ export class Avatar extends BaseAvatar {
110110
const size = this.size ?? 32;
111111

112112
return (
113-
this.initials ??
114-
getInitials(this.name, window.getComputedStyle(this as unknown as HTMLElement).direction === 'rtl', {
113+
this.initials ||
114+
getInitials(this.name, window.getComputedStyle(this).direction === 'rtl', {
115115
firstInitialOnly: size <= 16,
116116
})
117117
);

0 commit comments

Comments
 (0)