Avatar

v0.0.10

v0.0.10StableMobStatelessApache-2.0
Installation mix mishka.ui.gen.mob avatar
Source View Source

The Mishka Chelekom Mob Avatar shows a person or an account on iOS and Android as an image with a text fallback. It is the native port of the headless Avatar, built for Mob, which drives Jetpack Compose and SwiftUI from Elixir.

Overview of the Mob Avatar component for iOS and Android

MishkaAvatar draws a square that holds a fallback, usually your initials on a colored background. Set src to an image URL or an on-device file path and the avatar stacks the image on top of the fallback.

The initials show immediately, and the image covers them once it has loaded. The screen shows no empty space while the image downloads, and you wire no loading callback. Mob's Image fetches and caches the file, using Coil on Android and AsyncImage on iOS.

The avatar has no state and sends no events. Children replace initials as the fallback content, so you can place an icon or any other node inside it. MishkaAvatar.radius/2 returns the corner radius a shape resolves to at a given size. For example, MishkaAvatar.radius(:circle, 44) is 22.0.

Generate the avatar component

Run this from your Mob app's root to add the component to lib/<app>/components/:

bash
mix mishka.ui.gen.mob avatar

The generator registers the component's tag, so you can use it in ~MOB markup. The examples on this page use the mishka_ prefix of the showcase app (--module-prefix mishka_), so the tag is <MishkaAvatar>. Without a prefix the tag is <Avatar>.

Avatar with initials

With no src, the fallback is the whole avatar. This one draws the initials SH on the default :surface_raised background. The card in the screenshot also shows two more avatars with their own background and color.

Avatar with initials

Card titled Initials, with three circular avatars in a row: SH on white, MK on violet and AB on near black, in the MishkaMob showcase app on Android
heex
<MishkaAvatar initials="SH" />

Avatar shapes

shape takes :circle, :rounded or :square. A circle uses a corner radius of exactly half of size, so it stays round at any size. :rounded uses a radius of 10, and :square has no rounding.

Avatar shapes

Card titled Shapes, with three near-black avatars in a row: a circle labeled CI, a rounded square labeled RO and a sharp-cornered square labeled SQ
heex
<MishkaAvatar initials="SH" shape={:circle} />
<MishkaAvatar initials="SH" shape={:rounded} />
<MishkaAvatar initials="SH" shape={:square} />

Avatar sizes

size sets both width and height, and the circle radius follows it. Use text_size to scale the initials with a larger avatar.

Avatar sizes

Card titled Sizes, with three white circular avatars lined up along the bottom and growing in size: a small S, a medium SH and a large SH
heex
<MishkaAvatar initials="SH" size={28} />
<MishkaAvatar initials="SH" size={64} text_size={:xl} />

Avatar in a row

An avatar usually sits beside a name. This row puts a violet avatar next to a Column that holds a name and an email address. Both avatar colors are ARGB integers.

Avatar in a row

Card titled In a row, with a violet circular avatar showing SH beside the name Shahryar and the gray email shahryar@mishka.tools, in the MishkaMob showcase app on Android
heex
<Row>
  <MishkaAvatar initials="SH" background={0xFF7C3AED} color={0xFFFFFFFF} />
  <Column>
    <Text text="Shahryar" />
    <Text text="[email protected]" />
  </Column>
</Row>

Props

PropTypeDefaultDescription
srcstringnilAn image URL (http:// or https://) or an on-device file path. Without it, the avatar draws the fallback alone.
initialsstringnilThe fallback text.
sizenumber44Width and height. An avatar is always square.
shape:circle, :rounded or :square:circleThe corner shape. A circle uses a radius of exactly size / 2, so it stays round at any size.
backgroundcolor token or ARGB integer:surface_raisedFallback background.
colorcolor token or ARGB integer:on_surfaceInitials color.
text_sizesize token:lgInitials size.
idstringNoneA native test tag. An avatar that shows only an image has no text, so the tag is the only handle a device test has on it.

Children replace initials as the fallback content.

Differences from the web component

These attributes of the web component are not ported:

  • delay and on_loading_status_change. They manage the swap from fallback to image in the browser. The native avatar stacks the image over the fallback instead.
  • alt. A Mob image has no screen reader text.
  • width and height. An avatar is square, so use size.
  • referrer_policy, crossorigin and the *_class attributes.