Skip to content

Commit 4ca5066

Browse files
Feature: User Card Avatar image (#690)
Co-authored-by: Niels Lyngsø <[email protected]>
1 parent d3adda2 commit 4ca5066

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

packages/uui-base/lib/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './demandCustomElement';
33
export * from './drag';
44
export * from './math';
55
export * from './findAncestorByAttributeValue';
6+
export * from './slotHasContent';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function slotHasContent(target: EventTarget | null): boolean {
2+
return target
3+
? (target as HTMLSlotElement).assignedNodes({ flatten: true }).length > 0
4+
: false;
5+
}

packages/uui-card-user/lib/uui-card-user.element.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
2-
import { demandCustomElement } from '@umbraco-ui/uui-base/lib/utils';
2+
import {
3+
demandCustomElement,
4+
slotHasContent,
5+
} from '@umbraco-ui/uui-base/lib/utils';
36
import { UUICardElement } from '@umbraco-ui/uui-card/lib';
47
import { css, html, nothing } from 'lit';
5-
import { property } from 'lit/decorators.js';
8+
import { property, state } from 'lit/decorators.js';
69
import { ifDefined } from 'lit/directives/if-defined.js';
710

811
/**
@@ -23,6 +26,12 @@ export class UUICardUserElement extends UUICardElement {
2326
@property({ type: String })
2427
name = '';
2528

29+
@state()
30+
private _avatarSlotHasContent = false;
31+
private _avatarSlotChanged = (e: Event) => {
32+
this._avatarSlotHasContent = slotHasContent(e.target);
33+
};
34+
2635
connectedCallback(): void {
2736
super.connectedCallback();
2837

@@ -54,7 +63,16 @@ export class UUICardUserElement extends UUICardElement {
5463

5564
public render() {
5665
return html`
57-
<uui-avatar id="avatar" name=${this.name} size="m"></uui-avatar>
66+
${this._avatarSlotHasContent
67+
? nothing
68+
: html`<uui-avatar
69+
id="avatar"
70+
name=${this.name}
71+
size="m"></uui-avatar>`}
72+
<slot
73+
name="avatar"
74+
id="avatar"
75+
@slotchange=${this._avatarSlotChanged}></slot>
5876
${this.href ? this.#renderLink() : this.#renderButton()}
5977
<slot></slot>
6078
<slot name="tag"></slot>

packages/uui-card-user/lib/uui-card-user.story.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,27 @@ Tags.parameters = {
166166
},
167167
},
168168
};
169+
170+
export const Avatar: StoryFn = () => html`
171+
<uui-card-user name="John Rabbit">
172+
<uui-avatar
173+
slot="avatar"
174+
size="m"
175+
name="John Rabbit"
176+
img-src="https://placedog.net/120/?random"></uui-avatar>
177+
${cardContent}
178+
</uui-card-user>
179+
`;
180+
181+
Tags.parameters = {
182+
docs: {
183+
source: {
184+
code: `
185+
<uui-card-user name="John Rabbit">
186+
<uui-avatar slot="avatar" size="m" name="John Rabbit" src="https://placedog.net/120/?random"></uui-avatar>
187+
188+
<!-- Content -->
189+
</uui-card-user>`,
190+
},
191+
},
192+
};

packages/uui-card-user/lib/uui-card-user.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ describe('UUICardUserElement', () => {
6767
const slot = element.shadowRoot!.querySelector('slot[name=actions]')!;
6868
expect(slot).to.exist;
6969
});
70+
71+
it('renders an avatar slot', () => {
72+
const slot = element.shadowRoot!.querySelector('slot[name=avatar]')!;
73+
expect(slot).to.exist;
74+
});
7075
});
7176

7277
describe('events', () => {

0 commit comments

Comments
 (0)