Skip to content
ui8kit

component / components

Avatar

Avatar component

Install

npx ui8kit add avatar

Pulls in: compose, image, inline, kit-image, kit-text, presets, utils

Preview

size.xs
size.sm
size.default
size.lg
size.xl

Code

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework svelte --dir src --registry-url https://registry.buildy.tw/svelte --strict-cdn
npx ui8kit add avatar
components/avatar.svelte
<!-- Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT. -->
<!-- An image in a round frame (recipe: size xs to xl, 24 to 64 px). Avatars are always round. -->
<script lang="ts" module>
  import type { Snippet } from "svelte";
  import { variants } from "../utils";

  /** Avatar: the classes of each value (shadcn/cva style). */
  export const avatarVariants = variants({
    base: "relative inline-flex shrink-0 overflow-hidden bg-muted rounded-full",
    variants: {
      size: {
        xs: "size-6",
        sm: "size-8",
        default: "size-10",
        lg: "size-12",
        xl: "size-16",
      },
    },
    defaultVariants: { size: "default" },
  });

  export type AvatarProps = {
    src: string;
    alt?: string;
    size?: "xs" | "sm" | "default" | "lg" | "xl";
    class?: string;
  };
</script>

<script lang="ts">
  import Image from "../compose/Image.svelte";
  import Inline from "../compose/Inline.svelte";
  import { cn, urlOf } from "../utils";

  let { src, alt = "", size = "default", class: className }: AvatarProps = $props();
</script>

<Inline class={cn(avatarVariants({ size: size }), className)} data-slot="avatar">
  <Image
    src={urlOf(src, true)}
    alt={alt}
    class="size-full object-cover"
    fit="cover"
    size="auto"
    loading="eager"
    decoding="auto"
    draggable="false"
  />
</Inline>

Svelte registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework react --dir src --registry-url https://registry.buildy.tw/react --strict-cdn
npx ui8kit add avatar
components/avatar.tsx
// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
/** An image in a round frame (recipe: size xs to xl, 24 to 64 px). Avatars are always round. */
import { type ReactNode } from "react";
import { Image, Inline } from "../compose";
import { cn, urlOf, variants } from "../utils";

/** Avatar: the classes of each value (shadcn/cva style). */
export const avatarVariants = variants({
  base: "relative inline-flex shrink-0 overflow-hidden bg-muted rounded-full",
  variants: {
    size: {
      xs: "size-6",
      sm: "size-8",
      default: "size-10",
      lg: "size-12",
      xl: "size-16",
    },
  },
  defaultVariants: { size: "default" },
});

export type AvatarProps = {
  src: string;
  alt?: string;
  size?: "xs" | "sm" | "default" | "lg" | "xl";
  className?: string;
};

export function Avatar({ src, alt = "", size = "default", className }: AvatarProps) {
  return (
    <Inline className={cn(avatarVariants({ size: size }), className)} data-slot="avatar">
      <Image
        src={urlOf(src, true)}
        alt={alt}
        className="size-full object-cover"
        fit="cover"
        size="auto"
        loading="eager"
        decoding="auto"
        draggable="false"
      />
    </Inline>
  );
}

React registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework vue --dir src --registry-url https://registry.buildy.tw/vue --strict-cdn
npx ui8kit add avatar
components/avatar.vue
<!-- Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT. -->
<!-- An image in a round frame (recipe: size xs to xl, 24 to 64 px). Avatars are always round. -->
<script lang="ts">
import { variants } from "../utils";

/** Avatar: the classes of each value (shadcn/cva style). */
export const avatarVariants = variants({
  base: "relative inline-flex shrink-0 overflow-hidden bg-muted rounded-full",
  variants: {
    size: {
      xs: "size-6",
      sm: "size-8",
      default: "size-10",
      lg: "size-12",
      xl: "size-16",
    },
  },
  defaultVariants: { size: "default" },
});

export type AvatarProps = {
  src: string;
  alt?: string;
  size?: "xs" | "sm" | "default" | "lg" | "xl";
  class?: string;
};
</script>

<script setup lang="ts">
import Image from "../compose/Image.vue";
import Inline from "../compose/Inline.vue";
import { cn, urlOf } from "../utils";

const { src, alt = "", size = "default", class: className } = defineProps<AvatarProps>();
</script>

<template>
  <Inline :class="cn(avatarVariants({ size: size }), className)" data-slot="avatar">
    <Image
      :src="urlOf(src, true)"
      :alt="alt"
      class="size-full object-cover"
      fit="cover"
      size="auto"
      loading="eager"
      decoding="auto"
      draggable="false"
    />
  </Inline>
</template>

Vue registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework solid --dir src --registry-url https://registry.buildy.tw/solid --strict-cdn
npx ui8kit add avatar
components/avatar.tsx
// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
/** An image in a round frame (recipe: size xs to xl, 24 to 64 px). Avatars are always round. */
import { type JSX } from "solid-js";
import { Image, Inline } from "../compose";
import { cn, urlOf, variants } from "../utils";

/** Avatar: the classes of each value (shadcn/cva style). */
export const avatarVariants = variants({
  base: "relative inline-flex shrink-0 overflow-hidden bg-muted rounded-full",
  variants: {
    size: {
      xs: "size-6",
      sm: "size-8",
      default: "size-10",
      lg: "size-12",
      xl: "size-16",
    },
  },
  defaultVariants: { size: "default" },
});

export type AvatarProps = {
  src: string;
  alt?: string;
  size?: "xs" | "sm" | "default" | "lg" | "xl";
  class?: string;
};

export function Avatar({ src, alt = "", size = "default", class: className }: AvatarProps) {
  return (
    <Inline class={cn(avatarVariants({ size: size }), className)} data-slot="avatar">
      <Image
        src={urlOf(src, true)}
        alt={alt}
        class="size-full object-cover"
        fit="cover"
        size="auto"
        loading="eager"
        decoding="auto"
        draggable="false"
      />
    </Inline>
  );
}

Solid registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework templ --dir src --registry-url https://registry.buildy.tw/templ --strict-cdn
npx ui8kit add avatar
blocks/avatar.templ
// Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
// Source: packages/compose-gen/specs/parts/avatar.json

package blocks

import (
	"github.com/ui8kit/codegen/generated/templ/compose"
	"github.com/ui8kit/codegen/generated/templ/ui/image"
	"github.com/ui8kit/codegen/generated/templ/ui/text"
	uiutils "github.com/ui8kit/codegen/generated/templ/orgutils"
)

// AvatarProps are the props of Avatar; the values are the JSON shapes of the TypeScript props.
type AvatarProps struct {
	Src any `json:"src"`
	Alt any `json:"alt"`
	Size any `json:"size"`
	Class string `json:"className"`
}

// An image in a round frame (recipe: size xs to xl, 24 to 64 px). Avatars are always round.
templ Avatar(p AvatarProps) {
	{{ vSrc := p.Src }}
	{{ vAlt := uiutils.Def(p.Alt, "") }}
	{{ vSize := uiutils.Def(p.Size, "default") }}
	@compose.Inline(compose.InlineProps{TextProps: text.TextProps{Class: uiutils.S(uiutils.Cnv(recipeAvatar.Of(map[string]any{"size": vSize}), p.Class)), Attrs: uiutils.Attrs("data-slot", "avatar")}}) {
		@compose.Image(compose.ImageProps{Fit: "cover", Size: "auto", ImageProps: image.ImageProps{Src: uiutils.URL(vSrc, true), Alt: uiutils.S(vAlt), Class: "size-full object-cover", Loading: "eager", Decoding: "auto", Attrs: uiutils.Attrs("draggable", "false")}})
	}
}

templ registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework twig --dir src --registry-url https://registry.buildy.tw/twig --strict-cdn
npx ui8kit add avatar
blocks/avatar.html.twig
{#- Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
    Source: packages/compose-gen/specs/parts/avatar.json
    An image in a round frame (recipe: size xs to xl, 24 to 64 px). Avatars are always round.
    Props (p): src, alt, size, className. -#}
{%- set v_src = (p.src ?? null) -%}
{%- set v_alt = (p.alt ?? '') -%}
{%- set v_size = (p.size ?? 'default') -%}
{%- set c_1 -%}
  {{ include('compose/Image.html.twig', {'src': og_url(og_s(v_src), true), 'alt': og_s(v_alt), 'class': 'size-full object-cover', 'fit': 'cover', 'size': 'auto', 'loading': 'eager', 'decoding': 'auto', 'attrs': og_attrs(['draggable', 'false'])}, with_context = false) }}
{%- endset -%}
{{ include('compose/Inline.html.twig', {'class': og_s(og_cnv([og_recipe('blocks/recipes/avatar.variants.json', {'size': v_size}), (p.className ?? '')])), 'attrs': og_attrs(['data-slot', 'avatar']), 'children': c_1}, with_context = false) }}

Twig registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework latte --dir src --registry-url https://registry.buildy.tw/latte --strict-cdn
npx ui8kit add avatar
blocks/avatar.latte
{* Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
    Source: packages/compose-gen/specs/parts/avatar.json
    An image in a round frame (recipe: size xs to xl, 24 to 64 px). Avatars are always round.
    Props (p): src, alt, size, className. *}
{var $v_src = ($p['src'] ?? null)}
{var $v_alt = ($p['alt'] ?? '')}
{var $v_size = ($p['size'] ?? 'default')}
{capture $c_1}
  {=og_include('compose/Image.latte', ['src' => og_url(og_s($v_src), true), 'alt' => og_s($v_alt), 'class' => 'size-full object-cover', 'fit' => 'cover', 'size' => 'auto', 'loading' => 'eager', 'decoding' => 'auto', 'attrs' => og_attrs(['draggable', 'false'])])|noescape}
{/capture}
{=og_include('compose/Inline.latte', ['class' => og_s(og_cnv([og_recipe('blocks/recipes/avatar.variants.json', ['size' => $v_size]), ($p['className'] ?? '')])), 'attrs' => og_attrs(['data-slot', 'avatar']), 'children' => (string) $c_1])|noescape}

Latte registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework jinja2 --dir src --registry-url https://registry.buildy.tw/jinja2 --strict-cdn
npx ui8kit add avatar
blocks/avatar.html.j2
{#- Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
    Source: packages/compose-gen/specs/parts/avatar.json
    An image in a round frame (recipe: size xs to xl, 24 to 64 px). Avatars are always round.
    Props (p): src, alt, size, className. -#}
{%- set v_src = og_def(p['src'], none) -%}
{%- set v_alt = og_def(p['alt'], '') -%}
{%- set v_size = og_def(p['size'], 'default') -%}
{%- set c_1 -%}
  {{ og_include('compose/image.html.j2', {'p': {'Src': og_url(og_s(v_src), true), 'Alt': og_s(v_alt), 'Class': 'size-full object-cover', 'Fit': 'cover', 'Size': 'auto', 'Loading': 'eager', 'Decoding': 'auto', 'Attrs': og_attrs(['draggable', 'false'])}}) }}
{%- endset -%}
{{ og_include('compose/inline.html.j2', {'p': {'Class': og_s(og_cnv([og_recipe('blocks/recipes/avatar.variants.json', {'size': v_size}), og_def(p['className'], '')])), 'Attrs': og_attrs(['data-slot', 'avatar'])}, 'children': c_1}) }}

Jinja2 registry item (JSON)

Props

Names and types come from the xgen specs of the source. Standard HTML attributes (id, role, tabIndex, aria-*) pass through to the root element; the class prop is className in React.

An image in a round frame (recipe: size xs to xl, 24 to 64 px). Avatars are always round.

Props of Avatar
PropTypeDefault Description
src (required)string-
alt string""empty for a decorative picture
size "xs" | "sm" | "default" | "lg" | "xl""default"frame size in px