Ark Logo
Closed components

Closed Components

Learn how to create reusable components using the example of an avatar


Writing a few lines of code every time you need a simple Avatar is tedious. Creating a dedicated component encapsulates logic, simplifies the API, ensures consistent usage, and maintains clean code. This approach enhances reusability, making the component easier to maintain and test.

Here's an example of an Avatar component that can be used consistently across your application:

<script setup lang="ts">
import { computed } from 'vue'
import { Avatar, type AvatarRootEmits, type AvatarRootProps, useForwardPropsEmits } from '@ark-ui/vue'

export interface AvatarProps extends AvatarRootProps {
  src?: string
  name: string

const props = defineProps<AvatarProps>()
const emits = defineEmits<AvatarRootEmits>()

const forwarded = useForwardPropsEmits(props, emits)

const getInitials = computed(() =>
    .split(' ')
    .map((part) => part[0])
    .slice(0, 2)

  <Avatar.Root v-bind="forwarded">
    <Avatar.Fallback>{{ getInitials }}</Avatar.Fallback>
    <Avatar.Image :src="props.src" :alt="" />


To use the Avatar component, pass the name and src props as shown below:

<Avatar name="Christian" src="" />