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 avatarcbui-cli globally and authenticated your account. This will ensure you can access all the necessary features.Import
1import { Avatar, AvatarGroup, AvatarIcon, useAvatarGroup, AvatarGroupContext } from '@/crossbuildui/avatar';
2// import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; // If using custom iconsAvatarIcon 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" />Glass Variant
Apply a frosted glass effect to the avatar's fallback background using isGlass. Customize it with glassIntensity (0-100) and glassTint ('light', 'dark', 'default'). This effect is primarily for fallbacks (initials/icon) as images will cover it.
1<Avatar name="GL" color="primary" isGlass glassIntensity={70} glassTint="light" />
2<Avatar icon={<Icon name="face-man" size={24} color="black" />} isGlass glassIntensity={30} glassTint="dark" />
3<AvatarGroup isGlass glassTint="default" glassIntensity={60} color="success">
4 <Avatar name="G1" />
5 <Avatar name="G2" />
6</AvatarGroup>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, isDisabled, and glass props (isGlass, glassTint, glassIntensity) 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>Using AvatarGroup Context
For more advanced scenarios, or if you need to create custom components that react to the AvatarGroup's properties, you can use the useAvatarGroup hook. This hook provides access to the AvatarGroupContext, which includes properties like isGrid, size, color, radius, isBordered, isDisabled, max, and glass props.
Any component calling useAvatarGroup must be a descendant of an AvatarGroup component.
1import { Avatar, AvatarGroup, useAvatarGroup } from '@/crossbuildui/avatar';
2import { Text, View } from 'react-native';
3
4const GroupInfoDisplay = () => {
5 const context = useAvatarGroup();
6
7 if (!context) {
8 return <Text>This component must be used within an AvatarGroup.</Text>;
9 }
10
11 const { isGrid, size, color, max } = context;
12
13 return (
14 <View style={{ marginTop: 8, padding: 8, backgroundColor: '#f0f0f0', borderRadius: 4 }}>
15 <Text>Group Info:</Text>
16 <Text>- Grid Layout: {isGrid ? 'Yes' : 'No'}</Text>
17 <Text>- Avatar Size: {size}</Text>
18 <Text>- Default Color: {color}</Text>
19 <Text>- Max Displayed: {max}</Text>
20 </View>
21 );
22};
23
24<AvatarGroup max={2} size="sm" color="primary"><Avatar name="Ctx" /><Avatar name="Tst" /><GroupInfoDisplay /></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. |
isGlass | boolean | Apply glassmorphism to fallback. Default: false. |
glassTint | 'default' | 'light' | 'dark' | Tint for glass effect. Theme-derived if undefined. |
glassIntensity | number | Intensity (0-100) for glass. Default: 50. |
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. |
isGlass | boolean | false | Default glass state for avatars and count. |
glassTint | 'default' | 'light' | 'dark' | Theme-derived | Default glass tint. |
glassIntensity | number | 50 | Default glass intensity. |
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:
styleprop onAvatarfor its outermost container.stylesprop onAvatarto provide styles for internal slots:base: The main containerView.img: TheImagecomponent itself.fallback: The container for fallback content (initials/icon).initials: TheTextcomponent for initials.icon: The containerViewfor the icon within fallback.
- For the
isGlasseffect on fallbacks, the background is rendered byBlurViewfromexpo-blur, controlled byglassIntensityandglassTint.
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:
styleprop onAvatarGroupfor its main container.countStylesprop onAvatarGroupto style the count indicator:base: The containerViewfor the count.text: TheTextcomponent for the count number.
- If
isGlassis true onAvatarGroup, the count indicator will also adopt the glass effect.
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
accessibilityLabelderived from thenameprop 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
renderCountis used.
- The group container can be given an