Skip to content
LIBRARYBaseUpdated

Avatar ​

MAvatar represents a person or team. It center-crops an image and keeps initials visible while loading or after a failure. Without a name, it uses the user-round icon.

Next alpha

Available in 0.1.0-alpha.4.

Web preview
Open example ↗

Usage ​

vue
<m-avatar :src="person.image" :name="person.name" size="lg" />
<m-avatar name="Mavis Studio" shape="square" />
<m-avatar accessibilityLabel="Guest" />

The app supplies person. Sources follow platform conventions: NativeScript accepts a ~/ path, file, res:// resource, HTTPS URL or image data URI; Web accepts a browser-accessible URL or data URI. A native path is not converted into a Web URL. Map assets at the application boundary. The demos use an original PNG illustration as a data URI to work offline on both renderers.

Props ​

PropTypeDefaultBehavior
srcstring''Image source. Missing or failed sources keep the fallback.
namestring''Name for initials and the default accessible name.
sizesm | md | lg | xlmd32, 48, 64 or 96 DIP/px.
shapecircle | squarecircleCircle or square with rounded corners.
accessibilityLabelstringnameOverrides the announced name.
accessibilityHiddenbooleanfalseHides the avatar when nearby text already identifies it.
idstring—Optional identifier.

Initials use the first letter of the first and last words; a single word produces one letter. This is not a cultural surname parser. Background and initials use the theme's primarySoft and primary. Dimensions are fixed and do not shrink in Row. Initials and the icon act as a visual mark and do not follow text scaling; provide the name in Text beside the avatar.

Loading and composition ​

A photo appears only once it can paint: on Web the component waits for the image to decode, then fades it in while it eases from a slight zoom into its frame. NativeScript plays the same settle when the decoded image replaces the initials. Reduced motion keeps the fade and drops the zoom.

Clearing src fades the current photo back to the initials or icon, then releases the old image. Stale results are ignored, including native results after unmount. Native requests already in flight may finish in the background: the component does not cancel the connection, implement its own cache or resize files before download. Supply appropriately sized images. There is no automatic retry; change src or remount to retry.

Without a name or accessibilityLabel, the avatar is decorative. Inner images, initials and icons are not separately announced. There are no slots, events, image picker, upload or built-in action. Compose an appropriate, clearly named control for an action.

Profile combines Avatar, Badge and editable fields; the name remains app state.

Full example ​

View source
vue
<script setup lang="ts">
import { useCatalogLocale } from '../locale'
import { computed, ref } from 'vue'
import { useMavisTheme } from '@getcortexlabs/mavis-core/vue'
import { avatarImage } from '../assets/avatar-image'

const { t, locale } = useCatalogLocale(() => props.lang)

const props = withDefaults(defineProps<{ lang?: 'en' | 'pt' }>(), { lang: undefined })

const pt = computed(() => (props.lang ?? locale.value) === 'pt')
const { theme } = useMavisTheme()
const mode = ref<'image' | 'initials' | 'error'>('image')
const src = computed(() =>
  mode.value === 'image'
    ? avatarImage
    : mode.value === 'error'
      ? 'data:image/png;base64,broken'
      : '',
)
</script>

<template>
  <m-column gap="lg">
    <m-column padding="lg" gap="lg" radius="lg" :backgroundColor="theme.colors.accentSoft">
      <m-row justify="between" gap="sm">
        <m-text variant="overline" tone="accent">{{
          t(pt ? 'PESSOAS PRIMEIRO' : 'PEOPLE FIRST')
        }}</m-text>
        <m-badge :text="t(pt ? 'Criadora' : 'Creator')" tone="accent" size="sm" />
      </m-row>
      <m-avatar
        id="avatar-preview"
        :src="src"
        name="Marina Costa"
        size="xl"
        :accessibilityHidden="true"
      />
      <m-column gap="xs">
        <m-text variant="title">{{ t('Marina Costa') }}</m-text>
        <m-text tone="accent">{{
          t(pt ? 'Um rosto. Muitas histórias.' : 'One face. Many stories.')
        }}</m-text>
      </m-column>
    </m-column>
    <m-row gap="sm" wrap>
      <m-button
        size="sm"
        :variant="mode === 'image' ? 'primary' : 'secondary'"
        @click="mode = 'image'"
        >{{ t(pt ? 'Imagem' : 'Image') }}</m-button
      >
      <m-button
        size="sm"
        :variant="mode === 'initials' ? 'primary' : 'secondary'"
        @click="mode = 'initials'"
        >{{ t(pt ? 'Iniciais' : 'Initials') }}</m-button
      >
      <m-button
        size="sm"
        :variant="mode === 'error' ? 'primary' : 'secondary'"
        @click="mode = 'error'"
        >{{ t(pt ? 'Imagem indisponível' : 'Unavailable image') }}</m-button
      >
    </m-row>
    <m-text variant="caption" tone="muted">{{
      t(
        pt
          ? 'A identidade permanece, mesmo quando a imagem não chega.'
          : 'Identity stays, even when the image does not arrive.',
      )
    }}</m-text>
    <m-card gap="lg">
      <m-text variant="overline" tone="muted">{{
        t(pt ? 'EM QUALQUER ESCALA' : 'AT EVERY SCALE')
      }}</m-text>
      <m-row gap="md" wrap>
        <m-avatar
          v-for="size in ['sm', 'md', 'lg', 'xl'] as const"
          :key="size"
          name="Marina Costa"
          :size="size"
          :accessibilityLabel="t(`Marina Costa, ${size}`)"
        />
      </m-row>
      <m-row gap="md">
        <m-avatar name="Mavis Studio" shape="square" size="lg" />
        <m-avatar shape="square" size="lg" :accessibilityLabel="t(pt ? 'Visitante' : 'Guest')" />
        <m-column :width="0" :grow="1" gap="xs">
          <m-text variant="heading">{{
            t(pt ? 'Para cada identidade.' : 'For every identity.')
          }}</m-text>
          <m-text variant="caption" tone="muted">{{
            t(pt ? 'Pessoas, equipes e quem está chegando.' : 'People, teams and new arrivals.')
          }}</m-text>
        </m-column>
      </m-row>
    </m-card>
  </m-column>
</template>

The initials remain visible until the photo loads. The photo fades over them, and fades away to reveal them again if its source is cleared. The avatar keeps one accessible name throughout.

Desktop ​

On the Mac and on Windows the initials stay until the photo has loaded, and a photo that fails leaves them in place. Every prop works the same way as on the other platforms. See Desktop components.

Components for NativeScript and Web. Starting with Vue.