import { Component, Prop, State, h } from '@stencil/core'; /** * @since 1.0 * @status stable * * @slot icon - The default icon to use when no image or initials are present. * * @part base - The component's base wrapper. * @part icon - The container that wraps the avatar icon. * @part initials - The container that wraps the avatar initials. * @part image - The avatar image. */ @Component({ tag: 'sl-avatar', styleUrl: 'avatar.scss', shadow: true }) export class Avatar { constructor() { this.handleImageError = this.handleImageError.bind(this); } @State() hasError = false; /** The image source to use for the avatar. */ @Prop() image = ''; /** Alternative text for the image. */ @Prop() alt = ''; /** Initials to use as a fallback when no image is available (1-2 characters max recommended). */ @Prop() initials = ''; /** Initials to use as a fallback when no image is available (1-2 characters max recommended). */ @Prop() shape: 'circle' | 'square' | 'rounded' = 'circle'; handleImageError() { this.hasError = true; } render() { return (
{!this.initials && (
)} {this.initials && (
{this.initials}
)} {this.image && !this.hasError && ( )}
); } }