11import { defineElement } from '@umbraco-ui/uui-base/lib/registration' ;
22import { 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 }
0 commit comments