-
Notifications
You must be signed in to change notification settings - Fork 2
/
avatar.ts
119 lines (102 loc) · 2.83 KB
/
avatar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { LitElement, html, nothing } from 'lit';
import { property, state } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { themes } from '../../theming/theming-decorator.js';
import { watch } from '../common/decorators/watch.js';
import { registerComponent } from '../common/definitions/register.js';
import { SizableMixin } from '../common/mixins/sizable.js';
import { styles } from './themes/avatar.base.css.js';
import { styles as shared } from './themes/shared/avatar.common.css.js';
import { all } from './themes/themes.js';
/**
* An avatar component is used as a representation of a user identity
* typically in a user profile.
*
* @element igc-avatar
*
* @slot - Renders an icon inside the default slot.
*
* @csspart base - The base wrapper of the avatar.
* @csspart initials - The initials wrapper of the avatar.
* @csspart image - The image wrapper of the avatar.
* @csspart icon - The icon wrapper of the avatar.
*/
@themes(all)
export default class IgcAvatarComponent extends SizableMixin(LitElement) {
public static readonly tagName = 'igc-avatar';
public static override styles = [styles, shared];
/* blazorSuppress */
public static register() {
registerComponent(IgcAvatarComponent);
}
private __internals: ElementInternals;
@state()
private hasError = false;
/**
* The image source to use.
* @attr
*/
@property()
public src!: string;
/**
* Alternative text for the image.
* @attr
*/
@property()
public alt!: string;
/**
* Initials to use as a fallback when no image is available.
* @attr
*/
@property()
public initials!: string;
/**
* The shape of the avatar.
* @attr
*/
@property({ reflect: true })
public shape: 'circle' | 'rounded' | 'square' = 'square';
constructor() {
super();
this.__internals = this.attachInternals();
this.__internals.role = 'img';
this.__internals.ariaLabel = 'avatar';
this.size = 'small';
}
@watch('initials')
@watch('alt')
protected roleDescriptionChange() {
this.__internals.ariaRoleDescription = this.alt ?? this.initials;
}
@watch('src')
protected handleErrorState() {
this.hasError = false;
}
protected handleError() {
this.hasError = true;
}
protected override render() {
return html`
<div part="base">
${this.initials
? html`<span part="initials">${this.initials}</span>`
: html`<slot></slot>`}
${this.src && !this.hasError
? html`
<img
part="image"
alt=${ifDefined(this.alt)}
src=${ifDefined(this.src)}
@error=${this.handleError}
/>
`
: nothing}
</div>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'igc-avatar': IgcAvatarComponent;
}
}