Skip to content

Feature: User Card Avatar image #690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/uui-base/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './demandCustomElement';
export * from './drag';
export * from './math';
export * from './findAncestorByAttributeValue';
export * from './slotHasContent';
5 changes: 5 additions & 0 deletions packages/uui-base/lib/utils/slotHasContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function slotHasContent(target: EventTarget | null): boolean {
return target
? (target as HTMLSlotElement).assignedNodes({ flatten: true }).length > 0
: false;
}
24 changes: 21 additions & 3 deletions packages/uui-card-user/lib/uui-card-user.element.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
import { demandCustomElement } from '@umbraco-ui/uui-base/lib/utils';
import {
demandCustomElement,
slotHasContent,
} from '@umbraco-ui/uui-base/lib/utils';
import { UUICardElement } from '@umbraco-ui/uui-card/lib';
import { css, html, nothing } from 'lit';
import { property } from 'lit/decorators.js';
import { property, state } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';

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

@state()
private _avatarSlotHasContent = false;
private _avatarSlotChanged = (e: Event) => {
this._avatarSlotHasContent = slotHasContent(e.target);
};

connectedCallback(): void {
super.connectedCallback();

Expand Down Expand Up @@ -54,7 +63,16 @@ export class UUICardUserElement extends UUICardElement {

public render() {
return html`
<uui-avatar id="avatar" name=${this.name} size="m"></uui-avatar>
${this._avatarSlotHasContent
? nothing
: html`<uui-avatar
id="avatar"
name=${this.name}
size="m"></uui-avatar>`}
<slot
name="avatar"
id="avatar"
@slotchange=${this._avatarSlotChanged}></slot>
${this.href ? this.#renderLink() : this.#renderButton()}
<slot></slot>
<slot name="tag"></slot>
Expand Down
24 changes: 24 additions & 0 deletions packages/uui-card-user/lib/uui-card-user.story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,27 @@ Tags.parameters = {
},
},
};

export const Avatar: StoryFn = () => html`
<uui-card-user name="John Rabbit">
<uui-avatar
slot="avatar"
size="m"
name="John Rabbit"
img-src="https://placedog.net/120/?random"></uui-avatar>
${cardContent}
</uui-card-user>
`;

Tags.parameters = {
docs: {
source: {
code: `
<uui-card-user name="John Rabbit">
<uui-avatar slot="avatar" size="m" name="John Rabbit" src="https://placedog.net/120/?random"></uui-avatar>

<!-- Content -->
</uui-card-user>`,
},
},
};
5 changes: 5 additions & 0 deletions packages/uui-card-user/lib/uui-card-user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ describe('UUICardUserElement', () => {
const slot = element.shadowRoot!.querySelector('slot[name=actions]')!;
expect(slot).to.exist;
});

it('renders an avatar slot', () => {
const slot = element.shadowRoot!.querySelector('slot[name=avatar]')!;
expect(slot).to.exist;
});
});

describe('events', () => {
Expand Down