Menu

Change language

GitHub repo

Avatar

avatarui

Round or rounded-square user image with a fallback that shows initials or an icon when the image is missing or fails to load. Sizes from xs to 2xl, optional status dot, and a group composition for stacked avatar lists.

Installation

$npx shadcn-svelte@latest add https://uipkge.dev/r/svelte/avatar.json
Manifest:https://uipkge.dev/r/svelte/avatar.jsonInstalls to:src/lib/components/ui/avatar/

Examples

Loading interactive previews…

Props

NameType / ValuesDefaultRequired
size
'xs''sm''default''lg''xl''2xl'
defaultoptional
rounded
'none''sm''default''md''lg''xl''2xl''3xl''full'
defaultoptional
color
'default''primary''secondary''destructive''success''warning''info''error''muted'
defaultoptional
variant
'default''outlined''soft'
—optional
tilebooleanfalseoptional
disabledbooleanfalseoptional
loadingbooleanfalseoptional
refHTMLSpanElement | null—optional

npm dependencies

Used by

Files installed (7)

  • src/lib/components/ui/avatar/Avatar.svelte1.7 kB
    <script lang="ts" module>
      import type { HTMLAttributes } from 'svelte/elements'
    
      export interface AvatarProps extends HTMLAttributes<HTMLSpanElement> {
        size?: 'xs' | 'sm' | 'default' | 'lg' | 'xl' | '2xl'
        rounded?: 'none' | 'sm' | 'default' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' | 'full'
        color?: 'default' | 'primary' | 'secondary' | 'destructive' | 'success' | 'warning' | 'info' | 'error' | 'muted'
        variant?: 'default' | 'outlined' | 'soft'
        tile?: boolean
        disabled?: boolean
        loading?: boolean
        ref?: HTMLSpanElement | null
      }
    </script>
    
    <script lang="ts">
      import type { MouseEventHandler } from 'svelte/elements'
      import { setContext } from 'svelte'
      import { cn } from '$lib/utils'
      import { avatarVariants } from './avatar.variants'
      import { AVATAR_CONTEXT_KEY, AvatarContextState } from './context.svelte'
    
      let {
        class: className,
        size,
        rounded,
        color,
        variant,
        tile = false,
        disabled = false,
        loading = false,
        children,
        ref = $bindable(null),
        onclick,
        ...restProps
      }: AvatarProps = $props()
    
      // Sibling AvatarFallback shows until a sibling AvatarImage reports loaded.
      setContext(AVATAR_CONTEXT_KEY, new AvatarContextState())
    
      const handleClick: MouseEventHandler<HTMLSpanElement> = (event) => {
        if (!disabled) {
          onclick?.(event)
        }
      }
    
      const rootClasses = $derived(
        cn(
          avatarVariants({ size, rounded, color, variant }),
          tile ? 'rounded-none' : '',
          disabled ? 'cursor-not-allowed opacity-50' : '',
          loading ? 'animate-pulse' : '',
          className,
        ),
      )
    </script>
    
    <span bind:this={ref} class={rootClasses} data-uipkge="" data-slot="avatar" onclick={handleClick} {...restProps}>
      {@render children?.()}
    </span>
    
  • src/lib/components/ui/avatar/AvatarFallback.svelte1.4 kB
  • src/lib/components/ui/avatar/AvatarGroup.svelte2.1 kB
  • src/lib/components/ui/avatar/AvatarImage.svelte2.1 kB
  • src/lib/components/ui/avatar/avatar.variants.ts3.3 kB
  • src/lib/components/ui/avatar/context.svelte.ts0.5 kB
  • src/lib/components/ui/avatar/index.ts0.6 kB

Raw manifest:https://uipkge.dev/r/svelte/avatar.json