1- import { html , LitElement , nothing } from 'lit' ;
1+ import { html , LitElement , nothing , type PropertyValues } from 'lit' ;
22import { property , state } from 'lit/decorators.js' ;
33import { ifDefined } from 'lit/directives/if-defined.js' ;
4-
54import { themes } from '../../theming/theming-decorator.js' ;
6- import { watch } from '../common/decorators/watch .js' ;
5+ import { addInternalsController } from '../common/controllers/internals .js' ;
76import { registerComponent } from '../common/definitions/register.js' ;
87import type { AvatarShape } from '../types.js' ;
98import { styles } from './themes/avatar.base.css.js' ;
@@ -29,35 +28,40 @@ export default class IgcAvatarComponent extends LitElement {
2928 public static override styles = [ styles , shared ] ;
3029
3130 /* blazorSuppress */
32- public static register ( ) {
31+ public static register ( ) : void {
3332 registerComponent ( IgcAvatarComponent ) ;
3433 }
3534
36- private __internals : ElementInternals ;
35+ private readonly _internals = addInternalsController ( this , {
36+ initialARIA : {
37+ role : 'image' ,
38+ ariaLabel : 'avatar' ,
39+ } ,
40+ } ) ;
3741
3842 @state ( )
39- private hasError = false ;
43+ private _hasError = false ;
4044
4145 /**
4246 * The image source to use.
4347 * @attr
4448 */
4549 @property ( )
46- public src ! : string ;
50+ public src ? : string ;
4751
4852 /**
4953 * Alternative text for the image.
5054 * @attr
5155 */
5256 @property ( )
53- public alt ! : string ;
57+ public alt ? : string ;
5458
5559 /**
5660 * Initials to use as a fallback when no image is available.
5761 * @attr
5862 */
5963 @property ( )
60- public initials ! : string ;
64+ public initials ? : string ;
6165
6266 /**
6367 * The shape of the avatar.
@@ -66,27 +70,20 @@ export default class IgcAvatarComponent extends LitElement {
6670 @property ( { reflect : true } )
6771 public shape : AvatarShape = 'square' ;
6872
69- constructor ( ) {
70- super ( ) ;
71-
72- this . __internals = this . attachInternals ( ) ;
73- this . __internals . role = 'img' ;
74- this . __internals . ariaLabel = 'avatar' ;
75- }
76-
77- @watch ( 'initials' )
78- @watch ( 'alt' )
79- protected roleDescriptionChange ( ) {
80- this . __internals . ariaRoleDescription = this . alt ?? this . initials ;
81- }
73+ protected override willUpdate ( changedProperties : PropertyValues < this> ) : void {
74+ if ( changedProperties . has ( 'initials' ) || changedProperties . has ( 'alt' ) ) {
75+ this . _internals . setARIA ( {
76+ ariaRoleDescription : this . alt ?? this . initials ,
77+ } ) ;
78+ }
8279
83- @ watch ( 'src' )
84- protected handleErrorState ( ) {
85- this . hasError = false ;
80+ if ( changedProperties . has ( 'src' ) ) {
81+ this . _hasError = false ;
82+ }
8683 }
8784
88- protected handleError ( ) {
89- this . hasError = true ;
85+ protected _handleError ( ) : void {
86+ this . _hasError = true ;
9087 }
9188
9289 protected override render ( ) {
@@ -95,13 +92,13 @@ export default class IgcAvatarComponent extends LitElement {
9592 ${ this . initials
9693 ? html `< span part ="initials "> ${ this . initials } </ span > `
9794 : html `< slot > </ slot > ` }
98- ${ this . src && ! this . hasError
95+ ${ this . src && ! this . _hasError
9996 ? html `
10097 < img
10198 part ="image "
10299 alt =${ ifDefined ( this . alt ) }
103100 src =${ ifDefined ( this . src ) }
104- @error=${ this . handleError }
101+ @error=${ this . _handleError }
105102 />
106103 `
107104 : nothing }
0 commit comments