|
| 1 | +import { LitElement, html, type TemplateResult } from 'lit'; |
| 2 | +import { property } from 'lit/decorators/property.js'; |
| 3 | +import { customElement } from 'lit/decorators/custom-element.js'; |
| 4 | + |
| 5 | +import style from './pf-v6-avatar.css'; |
| 6 | + |
| 7 | +/** Size variants for the avatar. */ |
| 8 | +export type AvatarSize = 'sm' | 'md' | 'lg' | 'xl'; |
| 9 | + |
| 10 | +export class PfV6AvatarLoadEvent extends Event { |
| 11 | + constructor(public originalEvent: Event) { |
| 12 | + super('load', { bubbles: true }); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * An **avatar** is a visual used to represent a user. It may contain an image |
| 18 | + * or a placeholder graphic. |
| 19 | + * @summary Displays a user's avatar image |
| 20 | + * @fires {PfV6AvatarLoadEvent} load - when the avatar image loads |
| 21 | + * @cssprop {<length>} --pf-v6-c-avatar--Width - Width of the avatar |
| 22 | + * @cssprop {<length>} --pf-v6-c-avatar--Height - Height of the avatar |
| 23 | + * @cssprop {<length>} --pf-v6-c-avatar--BorderRadius - Border radius of the avatar |
| 24 | + * @cssprop {<color>} --pf-v6-c-avatar--BorderColor - Border color of the avatar |
| 25 | + * @cssprop {<length>} --pf-v6-c-avatar--BorderWidth - Border width of the avatar |
| 26 | + * @cssprop {<length>} --pf-v6-c-avatar--m-sm--Width - Width when size is `sm` |
| 27 | + * @cssprop {<length>} --pf-v6-c-avatar--m-sm--Height - Height when size is `sm` |
| 28 | + * @cssprop {<length>} --pf-v6-c-avatar--m-md--Width - Width when size is `md` |
| 29 | + * @cssprop {<length>} --pf-v6-c-avatar--m-md--Height - Height when size is `md` |
| 30 | + * @cssprop {<length>} --pf-v6-c-avatar--m-lg--Width - Width when size is `lg` |
| 31 | + * @cssprop {<length>} --pf-v6-c-avatar--m-lg--Height - Height when size is `lg` |
| 32 | + * @cssprop {<length>} --pf-v6-c-avatar--m-xl--Width - Width when size is `xl` |
| 33 | + * @cssprop {<length>} --pf-v6-c-avatar--m-xl--Height - Height when size is `xl` |
| 34 | + * @cssprop {<color>} --pf-v6-c-avatar--m-bordered--BorderColor - Border color when bordered |
| 35 | + * @cssprop {<length>} --pf-v6-c-avatar--m-bordered--BorderWidth - Border width when bordered |
| 36 | + */ |
| 37 | +@customElement('pf-v6-avatar') |
| 38 | +export class PfV6Avatar extends LitElement { |
| 39 | + static readonly styles: CSSStyleSheet[] = [style]; |
| 40 | + |
| 41 | + /** The URL to the user's custom avatar image. */ |
| 42 | + @property() src?: string; |
| 43 | + |
| 44 | + /** The alt text for the avatar image. */ |
| 45 | + @property({ reflect: true }) alt?: string; |
| 46 | + |
| 47 | + /** Size of the avatar */ |
| 48 | + @property({ reflect: true }) size?: AvatarSize; |
| 49 | + |
| 50 | + /** Whether to display a border around the avatar */ |
| 51 | + @property({ type: Boolean, reflect: true }) bordered = false; |
| 52 | + |
| 53 | + override render(): TemplateResult { |
| 54 | + return this.src != null ? html` |
| 55 | + <img id="img" |
| 56 | + alt="${this.alt ?? ''}" |
| 57 | + src=${this.src} |
| 58 | + @load="${this.#onLoad}"> |
| 59 | + ` : html` |
| 60 | + <svg id="placeholder" |
| 61 | + aria-hidden="true" |
| 62 | + xmlns="http://www.w3.org/2000/svg" |
| 63 | + viewBox="0 0 36 36"> |
| 64 | + <rect width="36" height="36" fill="var(--_placeholder-bg)"/> |
| 65 | + <path d="M30.5 36c-.4-3.9-1.3-9-2.9-11-1.1-1.4-2.3-2.2-3.5-2.6s-1.8-.6-6.3-.6-6.1.7-6.1.7c-1.2.4-2.4 1.2-3.4 2.6C6.7 27 5.8 32.2 5.4 36h25.1zM17.7 20.1c-3.5 0-6.4-2.9-6.4-6.4s2.9-6.4 6.4-6.4 6.4 2.9 6.4 6.4-2.8 6.4-6.4 6.4z" |
| 66 | + fill="var(--_placeholder-fg)"/> |
| 67 | + </svg> |
| 68 | + `; |
| 69 | + } |
| 70 | + |
| 71 | + #onLoad(event: Event) { |
| 72 | + this.dispatchEvent(new PfV6AvatarLoadEvent(event)); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +declare global { |
| 77 | + interface HTMLElementTagNameMap { |
| 78 | + 'pf-v6-avatar': PfV6Avatar; |
| 79 | + } |
| 80 | +} |
0 commit comments