Skip to content

MazAvatar ​

MazAvatar is a standalone component that displays an image or an icon with a caption. It can be used to display a user's profile picture, a placeholder image, or an icon.

Before use it, you should install the MazUi plugin in your project, follow instructions in Getting Started

TIP

This component uses vLazyImg directive

Basic usage ​

avatar image
vue
<script lang="ts" setup>
import MazAvatar from 'maz-ui/components/MazAvatar'
</script>

<template>
  <MazAvatar src="https://api.dicebear.com/7.x/big-smile/svg?backgroundColor=1d90ff&scale=80" />
</template>

Options ​

TIP

See all the options props here

LM
avatar image
avatar image
avatar image
vue
<script lang="ts" setup>
import MazAvatar from 'maz-ui/components/MazAvatar'
function clicked() { console.log('clicked') }
</script>

<template>
  <MazAvatar
    caption="Louis Mazel"
    size="1.5rem"
  />
  <MazAvatar
    src="https://api.dicebear.com/7.x/big-smile/svg?backgroundColor=1d90ff&scale=80&seed=maz-ui"
    size="2rem"
    href="https://api.dicebear.com/7.x/big-smile/svg?backgroundColor=1d90ff&scale=80&seed=maz-ui"
    target="_blank"
    rounded-size="none"
    clickable
  >
    <template #icon>
      <MazIcon icon="/eye.svg" />
    </template>
  </MazAvatar>
  <MazAvatar
    src="https://api.dicebear.com/7.x/big-smile/svg?backgroundColor=1d90ff&scale=80&seed=200"
    size="2.5rem"
    clickable
    rounded-size="xl"
    @click="clicked"
  />
  <MazAvatar
    src="https://api.dicebear.com/7.x/big-smile/svg?backgroundColor=1d90ff&scale=80&seed=600"
    size="3rem"
    bordered
  />
</template>

Initials from caption ​

When no src is provided, MazAvatar displays initials generated from the caption. The letter-count prop (default 2) controls how many letters are displayed:

  • Single word: the first letter-count characters of the word are used.
  • Multiple words: the first letter of each of the first letter-count words is used.

Initials are always uppercased.

AD
ADM
LM
JCD
vue
<template>
  <!-- "AD" -->
  <MazAvatar caption="admin" />
  <!-- "ADM" -->
  <MazAvatar caption="admin" :letter-count="3" />
  <!-- "LM" -->
  <MazAvatar caption="Louis Mazel" />
  <!-- "JCD" -->
  <MazAvatar caption="Jean Claude Dus" :letter-count="3" />
</template>

On Error ​

When no src (and no caption) is provided, or when the image fails to load, MazAvatar displays the default no-image placeholder.

avatar image
html
<MazAvatar @error="error" src="https://broken-link-image-src.example" />

Fallback image loaded on error ​

avatar image
html
<MazAvatar
  src="https://broken-link-image-src.com"
  fallback-src="https://api.dicebear.com/7.x/big-smile/svg?backgroundColor=1d90ff&scale=80&seed=100"
/>

Loading ​

The props loading has 3 possible values: intersecting, lazy, or eager.

By default, the value is intersecting which means the image will be loaded when it's intersecting with the IntersectionObserver browser API. This mode uses the MazLazyImg component with vLazyImg directive to handle the lazy loading.

Native modes:

  • lazy: The image will be loaded only when it's in the viewport
  • eager: The image will be loaded immediately

These modes are native use an HTMLImageElement with the loading attribute. (see MDN). Useful for SSR (Server Side Rendering) or when you want to load the image immediately.

avatar image
avatar image
avatar image
html
<MazAvatar
  loading="intersecting"
  src="https://api.dicebear.com/7.x/big-smile/svg?backgroundColor=1d90ff&scale=80&seed=123"
/>
<MazAvatar loading="lazy" src="https://api.dicebear.com/7.x/big-smile/svg?backgroundColor=1d90ff&scale=80&seed=123" />
<MazAvatar loading="eager" src="https://api.dicebear.com/7.x/big-smile/svg?backgroundColor=1d90ff&scale=80&seed=123" />

Image height full ​

By default the image fills the avatar width (width: 100%). Enable image-height-full to make the image fill the height instead: it keeps its aspect ratio, fills 100% of the height and overflows/crops horizontally. This is ideal for wide images or SVGs (like flags) that must cover the whole avatar.

avatar image
avatar image
vue
<template>
  <!-- default: width 100% -->
  <MazAvatar src="https://placedog.net/300/200" />
  <!-- height 100%, cropped on the sides -->
  <MazAvatar image-height-full src="https://placedog.net/300/200" />
</template>

Props ​

NameDescriptionTypeRequiredDefaultPossible values
srcThe source of the imagestring | nullNo--
captionThe caption of the avatarstring | nullNo--
hrefThe link of the avatarstringNo--
toThe link (router-link or nuxt-link) of the avatarstring | Record<string, unknown>No--
altThe alt of the imagestringNo"avatar image"-
targetThe target of the linkstringNo"_self"-
sizeThe size of the avatar - a keyword from MazSize or any CSS size unit``${number}rem|${number}px|${number}em|${number}vw|${number}vh|${number}vmin|${number}vmax|${number}%|${number}ch|${number}cm|${number}mm|${number}in|${number}ex|${number}pc|${number...`No-'mini' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2rem' | '48px'
borderedAdd a border to the avatarbooleanNo--
clickableMake the avatar clickablebooleanNo--
squareMake the avatar squarebooleanNo--
no-elevationRemove the shadowbooleanNo--
show-captionShow the captionbooleanNo--
image-height-fullMake the image height fullbooleanNo--
hide-loaderRemove the loaderbooleanNo--
button-colorThe color of the clickable button"primary" | "secondary" | "accent" | "info" | "success" | "warning" | "destructive" | "contrast" | "transparent"No"info"'primary' | 'secondary' | 'accent' | 'info' | 'success' | 'warning' | 'destructive' | 'contrast' | 'transparent'
hide-clickable-iconRemove the icon on hover when component is clickablebooleanNo--
letter-countNumber of letters to display in the round text initials. - For a single-word caption, the first letterCount characters are used ('admin' + 3 -> 'ADM'). - For a multi-word caption, the first letter of the first letterCount words is used ('Louis Mazel' -> 'LM').numberNo2-
rounded-sizeSize of the roundedMazRoundedSizeNo-'none' | 'sm' | 'md' | 'lg' | 'xl' | 'full'
fallback-srcThe fallback src to replace the src on loading errorstringNo--
loadingThe loading strategy of the image - lazy, eager or intersecting"intersecting" | "lazy" | "eager"No"intersecting"'intersecting' | 'lazy' | 'eager'
colorThe color of the avatar"primary" | "secondary" | "accent" | "info" | "success" | "warning" | "destructive" | "contrast" | "transparent"No"primary"'primary' | 'secondary' | 'accent' | 'info' | 'success' | 'warning' | 'destructive' | 'contrast' | 'transparent'

Events ​

Event namePayloadDescription
loadingel: ElementEmitted when the image is loading
clickevent: MouseEventEmitted when the avatar is clicked
intersectingel: ElementEmitted when the image is intersecting
loadedel: ElementEmitted when the image is loaded
errorel: ElementEmitted when the image is in error

Slots ​

NameDescriptionBindings
round-textReplace the initials displayed when a caption is set and no image source is provided-
iconThe icon displayed on the clickable button overlay (shown on hover/focus)-
captionReplace the caption displayed below the avatar (requires showCaption)-