Avatar
An image element with a fallback for representing the user.
T
Props
Features
- 🎮 Automatic & manual control over image rendering
- 🗼 Fallback supports any children elements
- ⌛ Optionally delay fallback rendering to avoid flashes
Usage
<script lang="ts"> import { Avatar } from "melt";
const avatar = new Avatar({src: '...'});</script>
<img {...avatar.image} alt="Avatar" /><span {...avatar.fallback}>RH</span>
<script lang="ts"> import { Avatar } from "melt/components";</script>
<Avatar src="..."> {#snippet children(avatar)} <img {...avatar.image} alt="Avatar" /> <span {...avatar.fallback}>Fallback</span> {/snippet}</PinInput>
API Reference
Constructor Props
The props that are passed when calling
new Avatar()
new Avatar()
export type AvatarProps = { /** * The source of the image to display. */ src?: MaybeGetter<string | undefined>;
/** * The amount of time in milliseconds to wait before displaying the image. * * @default 0 */ delayMs?: MaybeGetter<number | undefined>;
/** * A callback invoked when the loading status store of the avatar changes. */ onLoadingStatusChange?: (value: ImageLoadingStatus) => void | undefined;};
export type AvatarProps = { /** * The source of the image to display. */ src?: MaybeGetter<string | undefined>;
/** * The amount of time in milliseconds to wait before displaying the image. * * @default 0 */ delayMs?: MaybeGetter<number | undefined>;
/** * A callback invoked when the loading status store of the avatar changes. */ onLoadingStatusChange?: (value: ImageLoadingStatus) => void | undefined;};
Properties
The properties returned from
new Avatar()
new Avatar()
-
src
stringstring -
delayMs
numbernumber -
loadingStatus
ImageLoadingStatusImageLoadingStatus -
image
{readonly "data-melt-avatar-image": ""readonly src: stringreadonly style: `display: ${string}`readonly onload: () => (() => void) | undefinedreadonly onerror: () => void}{readonly "data-melt-avatar-image": ""readonly src: stringreadonly style: `display: ${string}`readonly onload: () => (() => void) | undefinedreadonly onerror: () => void} -
fallback
{readonly "data-melt-avatar-fallback": ""readonly style: `display: ${string}` | undefinedreadonly hidden: true | undefined}{readonly "data-melt-avatar-fallback": ""readonly style: `display: ${string}` | undefinedreadonly hidden: true | undefined}