Avatar

The Avatar component is used to represent a user, displaying an image, initials, or an icon. Avatars can be grouped using the AvatarGroup component.

Installation

$ cbui-cli install @crossbuildui/avatar

Import

index.tsx
1import { Avatar, AvatarGroup, AvatarIcon } from '@/crossbuildui/avatar'; 2// import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; // If using custom icons

Avatar Component

Basic Usage

Provide an image src and/or a name for initials.

index.tsx
1<Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026024d" name="Jane Doe" /> 2<Avatar name="John Smith" /> {/* Will show initials JS */}

Fallbacks

Avatars can display initials, a custom icon, or a completely custom fallback component if the image source is unavailable or fails to load. The showFallback prop (default true) controls this behavior.

index.tsx
1// Initials (default if no src or src fails, and name is provided) 2<Avatar name="Sarah Connor" color="primary" /> 3 4// Custom Icon 5<Avatar icon={<Icon name="account-circle" size={24} color="white" />} color="secondary" /> 6 7// Custom Fallback Component (overrides icon and initials) 8const CustomFallback = () => ( 9 <View style={{width: '100%', height: '100%', backgroundColor: 'lightcoral', justifyContent: 'center', alignItems: 'center'}}> 10 <Text style={{color: 'white', fontWeight: 'bold'}}>Error</Text> 11 </View> 12); 13<Avatar src="https://invalid-url.xyz/image.png" fallback={<CustomFallback />} /> 14 15// No fallback shown 16<Avatar src="https://invalid-url.xyz/image.png" name="No Fallback" showFallback={false} />

Sizes

Avatars come in sm, md (default), and lg sizes.

index.tsx
1<Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026704d" size="sm" /> 2<Avatar src="https://i.pravatar.cc/150?u=a04258114e29026702d" size="md" /> 3<Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026024d" size="lg" />

Radius

Control the border radius with none, sm, md, lg, or full (default).

index.tsx
1<Avatar name="NR" radius="none" color="success" /> 2<Avatar name="SM" radius="sm" color="warning" /> 3<Avatar name="MD" radius="md" color="danger" /> 4<Avatar name="LG" radius="lg" color="primary" /> 5<Avatar name="FL" radius="full" color="secondary" /> {/* Default */}

Colors

The color prop affects the background of fallbacks (initials/icon) and the border color if isBordered is true.

index.tsx
1<Avatar name="DF" color="default" /> 2<Avatar name="PR" color="primary" /> 3<Avatar name="SE" color="secondary" /> 4<Avatar name="SU" color="success" /> 5<Avatar name="WA" color="warning" /> 6<Avatar name="DA" color="danger" />

Bordered & Disabled

Use isBordered to add a border and isDisabled for a disabled appearance.

index.tsx
1// Bordered 2<Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026704d" isBordered color="primary" /> 3 4// Disabled 5<Avatar src="https://i.pravatar.cc/150?u=a04258114e29026702d" isDisabled /> 6<Avatar name="Disabled" isDisabled isBordered color="secondary" />

Custom Image Component

Use the ImgComponent prop to render the avatar image with a custom component, e.g., for image caching libraries like react-native-fast-image. Pass additional props to this custom component via imgProps.

index.tsx
1import FastImage from 'react-native-fast-image'; 2 3<Avatar 4 src="https://my-cdn.com/user-image.jpg" 5 name="Cached User" 6 ImgComponent={FastImage} 7 imgProps={{ 8 priority: FastImage.priority.high, 9 resizeMode: FastImage.resizeMode.contain, 10 }} 11/>

AvatarGroup Component

Basic Usage

Group multiple Avatar components. They will overlap by default. Props like size, color, radius, isBordered, and isDisabled can be set on AvatarGroup to apply to all children, unless overridden by an individual Avatar.

index.tsx
1<AvatarGroup> 2 <Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026024d" name="User 1" /> 3 <Avatar src="https://i.pravatar.cc/150?u=a04258114e29026702d" name="User 2" /> 4 <Avatar name="U3" color="primary" /> 5 <Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026704d" name="User 4" /> 6</AvatarGroup>

Max & Total Count

Use max to limit the number of visible avatars. Excess avatars are represented by a count. The total prop can override the count derived from children length.

index.tsx
1<AvatarGroup max={3} total={10}> 2 <Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026024d" /> 3 <Avatar src="https://i.pravatar.cc/150?u=a04258114e29026702d" /> 4 <Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026704d" /> 5 {/* These won't be rendered, count will be +7 */} 6 <Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026706d" /> 7 <Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026707d" /> 8</AvatarGroup> 9 10<AvatarGroup max={2} color="secondary" size="lg"> 11 <Avatar name="A" /> 12 <Avatar name="B" /> 13 <Avatar name="C" /> 14 <Avatar name="D" /> 15</AvatarGroup> {/* Shows A, B, and +2 count */}

Custom Count Renderer

Provide a custom component for the count indicator using the renderCount prop.

index.tsx
1const renderCustomCount = (count) => ( 2 <View style={{ 3 width: 32, height: 32, borderRadius: 16, backgroundColor: 'purple', 4 justifyContent: 'center', alignItems: 'center', marginLeft: -10, zIndex: 999, 5 borderWidth: 2, borderColor: 'white' 6 }}> 7 <Text style={{color: 'white', fontSize: 12}}>{count} more</Text> 8 </View> 9); 10 11<AvatarGroup max={2} renderCount={renderCustomCount}> 12 <Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026024d" /> 13 <Avatar src="https://i.pravatar.cc/150?u=a04258114e29026702d" /> 14 <Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026704d" /> 15 <Avatar src="https://i.pravatar.cc/150?u=a042581f4e29026706d" /> 16</AvatarGroup>

Props Overview

Avatar Props

PROPTYPEDESCRIPTION
srcImageSourcePropType | stringImage source URI or local asset.
namestringUser's name for initials fallback.
iconReactElement<AvatarIconProps>Custom icon for fallback.
fallbackReactElementCustom component for fallback (overrides icon/initials).
colorAvatarColorSemantic color for fallback/border. Default: 'default'.
sizeAvatarSizeSize of the avatar. Default: 'md'.
radiusAvatarRadiusBorder radius. Default: 'full'.
isBorderedbooleanShow border. Default: false (true in group).
isDisabledbooleanDisabled appearance. Default: false.
showFallbackbooleanShow fallback if src fails. Default: true.
imgPropsOmit<ImageProps, 'source' | 'style'>Props for the underlying Image component.
ImgComponentComponentType<ImageProps>Custom Image component.
stylesAvatarSlotsStylesCustom styles for avatar slots (base, img, fallback, initials, icon).
styleStyleProp<ViewStyle>Style for the avatar's outer container.
onErrorImageProps['onError']Callback on image load error.

AvatarGroup Props

PROPTYPEDEFAULTDESCRIPTION
childrenReactElement<AvatarProps> | ReactElement<AvatarProps>[]-Avatar components.
maxnumber5Max visible avatars.
totalnumber-Total avatars for count display.
sizeAvatarSize'md'Default size for avatars in group.
colorAvatarColor'default'Default color for avatars.
radiusAvatarRadius'full'Default radius for avatars.
isBorderedbooleantrueDefault border state for avatars.
isDisabledbooleanfalseDefault disabled state for avatars.
isGridbooleanfalseGrid layout (affects overlap).
renderCount(count: number) => ReactElement-Custom renderer for the count indicator.
styleStyleProp<ViewStyle>-Style for the group container.
countStylesbase?: ViewStyle; text?: TextStyle-Styles for the count indicator.

AvatarIcon Props

The AvatarIcon is a simple component used as the default fallback icon. It primarily accepts size, color, and style. It renders a MaterialIcons "person" icon by default.

PROPTYPEDEFAULTDESCRIPTION
sizenumber24Icon size.
colorstring'#FFFFFF'Icon color.
styleStyleProp<ViewStyle>-Style for the icon container.
pathstring-Path for SVG icons (if adapted).

Styling

Avatar Styling

Customize the Avatar appearance using:

  • style prop on Avatar for its outermost container.
  • styles prop on Avatar to provide styles for internal slots:
    • base: The main container View.
    • img: The Image component itself.
    • fallback: The container for fallback content (initials/icon).
    • initials: The Text component for initials.
    • icon: The container View for the icon within fallback.
index.tsx
1<Avatar 2 src="https://i.pravatar.cc/150?u=a042581f4e29026024d" 3 name="Styled Avatar" 4 size="lg" 5 isBordered 6 color="primary" 7 style={{ margin: 10, transform: [{ rotate: '5deg' }] }} // Styles the outer View container 8 styles={{ 9 base: { shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.25, shadowRadius: 3.84, elevation: 5 }, 10 img: { opacity: 0.8 }, 11 // fallback: { backgroundColor: 'darkslateblue' }, 12 // initials: { color: 'white', fontStyle: 'italic' }, 13 // icon: { padding: 4 } 14 }} 15/>

AvatarGroup Styling

Customize the AvatarGroup appearance using:

  • style prop on AvatarGroup for its main container.
  • countStyles prop on AvatarGroup to style the count indicator:
    • base: The container View for the count.
    • text: The Text component for the count number.
index.tsx
1<AvatarGroup 2 max={3} 3 style={{ paddingVertical: 10, backgroundColor: '#eee', borderRadius: 20 }} 4 countStyles={{ 5 base: { backgroundColor: 'teal', borderColor: 'white' }, 6 text: { color: 'white', fontWeight: 'normal' } 7 }} 8> 9 <Avatar name="S1" color="success" /> 10 <Avatar name="S2" color="success" /> 11 <Avatar name="S3" color="success" /> 12 <Avatar name="S4" color="success" /> 13</AvatarGroup>

Accessibility

The Avatar components include accessibility features:

  • Avatar:
    • The image has an accessibilityLabel derived from the name prop or a default "User avatar".
    • If interactive (e.g., wrapped in a Pressable by the user), ensure appropriate roles and states are added.
  • AvatarGroup:
    • The group container can be given an accessibilityLabel (e.g., "Group of users").
    • The count indicator has an implicit label (e.g., "+5"). Consider adding a more descriptive label if renderCount is used.