Skip to content

Commit 601797f

Browse files
committed
feat: adds an "initials" property
1 parent d725691 commit 601797f

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

packages/uui-avatar/lib/uui-avatar.element.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
22
import { css, html, LitElement } from 'lit';
3-
import { property, state } from 'lit/decorators.js';
3+
import { property } from 'lit/decorators.js';
44

55
/**
66
* Avatar for displaying users
@@ -43,19 +43,20 @@ export class UUIAvatarElement extends LitElement {
4343
* @default ''
4444
*/
4545
@property({ type: String, reflect: true })
46-
get name() {
47-
return this._name;
48-
}
49-
set name(newVal) {
50-
const oldValue = this._name;
51-
this._name = newVal;
52-
this.initials = this.createInitials(this._name);
53-
this.requestUpdate('title', oldValue);
54-
}
55-
private _name = '';
46+
name = '';
47+
48+
/**
49+
* Use this to override the initials generated from the name.
50+
* @type {string}
51+
* @attr
52+
* @default undefined
53+
*/
54+
@property({ type: String })
55+
initials?: string;
5656

57-
@state()
58-
private initials = '';
57+
private get _initials() {
58+
return this.initials?.substring(0, 3) || this.createInitials(this.name);
59+
}
5960

6061
connectedCallback() {
6162
super.connectedCallback();
@@ -77,10 +78,10 @@ export class UUIAvatarElement extends LitElement {
7778
return initials;
7879
}
7980

80-
initials = words[0].substring(0, 1);
81+
initials = words[0].charAt(0);
8182

8283
if (words.length > 1) {
83-
initials += words[words.length - 1].substring(0, 1);
84+
initials += words[words.length - 1].charAt(0);
8485
}
8586

8687
return initials.toUpperCase();
@@ -90,14 +91,14 @@ export class UUIAvatarElement extends LitElement {
9091
return html` <img
9192
src="${this.imgSrc}"
9293
srcset="${this.imgSrcset}"
93-
alt="${this.initials}"
94+
alt="${this._initials}"
9495
title="${this.name}" />`;
9596
}
9697

9798
render() {
9899
return html`
99100
${this.imgSrc ? this.renderImage() : ''}
100-
${!this.imgSrc ? this.initials : ''}
101+
${!this.imgSrc ? this._initials : ''}
101102
<slot></slot>
102103
`;
103104
}

packages/uui-avatar/lib/uui-avatar.story.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ export const Colors: Story = {
5959
},
6060
};
6161

62+
export const Initials: Story = {
63+
args: {
64+
name: 'Umbraco HQ',
65+
initials: 'AB',
66+
},
67+
};
68+
6269
/**
6370
* Slotted content might overflow, use the `overflow` attribute to hide overflow.
6471
*/

0 commit comments

Comments
 (0)