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
cbui-cli
globally and authenticated your account. This will ensure you can access all the necessary features.Import
1import { Avatar, AvatarGroup, AvatarIcon } from '@/crossbuildui/avatar';
2// import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; // If using custom icons
AvatarIcon
uses react-native-vector-icons/MaterialIcons
. Ensure this library is installed and linked in your Expo project if you rely on the default icon or plan to use custom icons from this library.Avatar Component
Basic Usage
Provide an image src
and/or a name
for initials.
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.
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.
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).
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.
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.
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
.
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
.
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.
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.
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
PROP | TYPE | DESCRIPTION |
---|---|---|
src | ImageSourcePropType | string | Image source URI or local asset. |
name | string | User's name for initials fallback. |
icon | ReactElement<AvatarIconProps> | Custom icon for fallback. |
fallback | ReactElement | Custom component for fallback (overrides icon/initials). |
color | AvatarColor | Semantic color for fallback/border. Default: 'default' . |
size | AvatarSize | Size of the avatar. Default: 'md' . |
radius | AvatarRadius | Border radius. Default: 'full' . |
isBordered | boolean | Show border. Default: false (true in group). |
isDisabled | boolean | Disabled appearance. Default: false . |
showFallback | boolean | Show fallback if src fails. Default: true . |
imgProps | Omit<ImageProps, 'source' | 'style'> | Props for the underlying Image component. |
ImgComponent | ComponentType<ImageProps> | Custom Image component. |
styles | AvatarSlotsStyles | Custom styles for avatar slots (base, img, fallback, initials, icon). |
style | StyleProp<ViewStyle> | Style for the avatar's outer container. |
onError | ImageProps['onError'] | Callback on image load error. |
AvatarGroup Props
PROP | TYPE | DEFAULT | DESCRIPTION |
---|---|---|---|
children | ReactElement<AvatarProps> | ReactElement<AvatarProps>[] | - | Avatar components. |
max | number | 5 | Max visible avatars. |
total | number | - | Total avatars for count display. |
size | AvatarSize | 'md' | Default size for avatars in group. |
color | AvatarColor | 'default' | Default color for avatars. |
radius | AvatarRadius | 'full' | Default radius for avatars. |
isBordered | boolean | true | Default border state for avatars. |
isDisabled | boolean | false | Default disabled state for avatars. |
isGrid | boolean | false | Grid layout (affects overlap). |
renderCount | (count: number) => ReactElement | - | Custom renderer for the count indicator. |
style | StyleProp<ViewStyle> | - | Style for the group container. |
countStyles | base?: 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.
PROP | TYPE | DEFAULT | DESCRIPTION |
---|---|---|---|
size | number | 24 | Icon size. |
color | string | '#FFFFFF' | Icon color. |
style | StyleProp<ViewStyle> | - | Style for the icon container. |
path | string | - | Path for SVG icons (if adapted). |
Styling
Avatar Styling
Customize the Avatar
appearance using:
style
prop onAvatar
for its outermost container.styles
prop onAvatar
to provide styles for internal slots:base
: The main containerView
.img
: TheImage
component itself.fallback
: The container for fallback content (initials/icon).initials
: TheText
component for initials.icon
: The containerView
for the icon within fallback.
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 onAvatarGroup
for its main container.countStyles
prop onAvatarGroup
to style the count indicator:base
: The containerView
for the count.text
: TheText
component for the count number.
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 thename
prop or a default "User avatar". - If interactive (e.g., wrapped in a Pressable by the user), ensure appropriate roles and states are added.
- The image has an
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.
- The group container can be given an