Avatar

Use the Avatar component to display user/profile picture or initials.

Props

source

Use this to set the image source.

TypeDefaultDescription
ImageSourcePropType/Regular React Native Image source.

Example

Source code
<UIAvatar
  source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
/>

children (fallback)

Use this to set the fallback text, icon (for example default avatar) or any other element when you don't have an image.

TypeDefaultDescription
ReactNode/Fallback text, icon (for example default avatar) or any other element when you don't have an image.

Example

NN
+5
Source code
<UIHStack xGap={16}>
  <UIAvatar>
     <Text style={{fontSize: 22, color: '#ffffff'}}>NN</Text>
  </UIAvatar>
  <UIAvatar>
    <MaterialIcons name="person" size={24} color="#ffffff" />
  </UIAvatar>
  <UIAvatar>
    <Text style={{fontSize: 22, color: '#ffffff'}}>+5</Text>
  </UIAvatar>
</UIHStack>

overlay

A placeholder for any element. This layer is placed on top of the image. You can use this to display some text, icon, button etc.

TypeDefaultDescription
ReactNode/This layer is placed on top of the image.

Example

Source code
<UIHStack xGap={16}>
  <UIAvatar
    source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
    overlay={
      <>
        <View style={{backgroundColor: '#67ab5b', width: '100%', height: '100%', position: 'absolute', opacity: 0.5}} />
        <MaterialIcons name="check" size={24} color="#ffffff" />
      </>
    }
  />
  <UIAvatar
    source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
    overlay={
      <>
        <View style={{backgroundColor: '#f29c38', width: '100%', height: '100%', position: 'absolute', opacity: 0.5}} />
        <MaterialIcons name="notifications-off" size={24} color="#ffffff" />
      </>
    }
  />
  <UIAvatar
    source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
    overlay={
      <>
        <View style={{backgroundColor: '#169f94', width: '100%', height: '100%', position: 'absolute', opacity: 0.5}} />
        <MaterialIcons name="camera-alt" size={24} color="#ffffff" />
      </>
    }
  />
</UIHStack>

badge

A placeholder for any element. You can use this to display a status, icon, button etc.

TypeDefaultDescription
ReactNode/This layer is placed on top of the image and overlay. You must set the width and height of your badge element, because badge wrapper is set to 0 width and height in order to be able to position it on the right place, without any additional calculations.

Example

3
Source code
<UIHStack xGap={16}>
  <UIAvatar
    source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
    badge={<View style={{width: 18, height: 18, borderRadius: 9, backgroundColor: '#67ab5b', borderWidth: 2, borderColor: '#ffffff'}} />}
  />
  <UIAvatar
    source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
    badge={
      <View style={{width: 20, height: 20, borderRadius: 4, alignItems: 'center', justifyContent: 'center', backgroundColor: '#a8bac1'}}>
        <MaterialIcons name="notifications-off" size={20} color={'#ffffff'} />
      </View>
    }
  />
  <UIAvatar
    source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
    badge={
      <View style={{height: 24, width: 24, borderRadius: 12, alignItems: 'center', justifyContent: 'center', backgroundColor: '#e15240'}}>
        <Text style={{fontSize: 14, color: '#ffffff'}}>3</Text>
      </View>
    }
  />
</UIHStack>

badgePosition

Use this to set the position of a badge.

TypeDefaultDescription
'tl' 
| 't'
| 'tr'
| 'r'
| 'br'
| 'b'
| 'bl'
| 'l'
'br'It's very likely that you will not need all of these positions, so feel free to remove the one that you don't need (and all the code related to it).

Example

BR
B
BL
L
TL
T
TR
R
Source code
<UIWrapStack 
  xGap={16} 
  yGap={16}
>
  <UIAvatar badge={<View style={{width: 18, height: 18, borderRadius: 9, backgroundColor: '#67ab5b', borderWidth: 2, borderColor: '#ffffff'}} />}>
    <Text style={{fontSize: 22, color: '#ffffff'}}>BR</Text>
  </UIAvatar>
  <UIAvatar
    badge={<View style={{width: 18, height: 18, borderRadius: 9, backgroundColor: '#67ab5b', borderWidth: 2, borderColor: '#ffffff'}} />}
    badgePosition="b"
  >
    <Text style={{fontSize: 22, color: '#ffffff'}}>B</Text>
  </UIAvatar>
  <UIAvatar
    badge={<View style={{width: 18, height: 18, borderRadius: 9, backgroundColor: '#67ab5b', borderWidth: 2, borderColor: '#ffffff'}} />}
    badgePosition="bl"
  >
    <Text style={{fontSize: 22, color: '#ffffff'}}>BL</Text>
  </UIAvatar>
  <UIAvatar
    badge={<View style={{width: 18, height: 18, borderRadius: 9, backgroundColor: '#67ab5b', borderWidth: 2, borderColor: '#ffffff'}} />}
    badgePosition="l"
  >
    <Text style={{fontSize: 22, color: '#ffffff'}}>L</Text>
  </UIAvatar>
  <UIAvatar
    badge={<View style={{width: 18, height: 18, borderRadius: 9, backgroundColor: '#67ab5b', borderWidth: 2, borderColor: '#ffffff'}} />}
    badgePosition="tl"
  >
    <Text style={{fontSize: 22, color: '#ffffff'}}>TL</Text>
  </UIAvatar>
  <UIAvatar
    badge={<View style={{width: 18, height: 18, borderRadius: 9, backgroundColor: '#67ab5b', borderWidth: 2, borderColor: '#ffffff'}} />}
    badgePosition="t"
  >
    <Text style={{fontSize: 22, color: '#ffffff'}}>T</Text>
  </UIAvatar>
  <UIAvatar
    badge={<View style={{width: 18, height: 18, borderRadius: 9, backgroundColor: '#67ab5b', borderWidth: 2, borderColor: '#ffffff'}} />}
    badgePosition="tr"
  >
    <Text style={{fontSize: 22, color: '#ffffff'}}>TR</Text>
  </UIAvatar>
  <UIAvatar
    badge={<View style={{width: 18, height: 18, borderRadius: 9, backgroundColor: '#67ab5b', borderWidth: 2, borderColor: '#ffffff'}} />}
    badgePosition="r"
  >
    <Text style={{fontSize: 22, color: '#ffffff'}}>R</Text>
  </UIAvatar>
</UIWrapStack>

badgeInset

Use this to set the offset of a badge.

TypeDefaultDescription
number
0By default, the badge is positioned at the edge of the avatar. You can use this to place the badge in the middle of the border (if you set the border width), or in any other case where you want to move the badge away from the edge of the avatar.

Example

3
0
-2
Source code
<UIHStack xGap={16}>
  <UIAvatar
    badge={<View style={{width: 18, height: 18, borderRadius: 9, backgroundColor: '#67ab5b', borderWidth: 2, borderColor: '#ffffff'}} />}
    badgeInset={3}
  >
    <Text style={{fontSize: 22, color: '#ffffff'}}>3</Text>
  </UIAvatar>
  <UIAvatar
    badge={<View style={{width: 18, height: 18, borderRadius: 9, backgroundColor: '#67ab5b', borderWidth: 2, borderColor: '#ffffff'}} />}
  >
    <Text style={{fontSize: 22, color: '#ffffff'}}>0</Text>
  </UIAvatar>
  <UIAvatar
    badge={<View style={{width: 18, height: 18, borderRadius: 9, backgroundColor: '#67ab5b', borderWidth: 2, borderColor: '#ffffff'}} />}
    badgeInset={-2}
  >
    <Text style={{fontSize: 22, color: '#ffffff'}}>-2</Text>
  </UIAvatar>
</UIHStack>

rounded

Use this to set the border radius of the avatar.

TypeDefaultDescription
'none' 
| 'xs'
| 'sm'
| 'md'
| 'lg'
| 'xl'
| '2xl'
| 'full'
'full'The position of the badge will follow the edges of the avatar, regardless of whether you set it to be rounded or not.

Example

NONE
XS
SM
MD
LG
XL
2XL
FULL
Source code
<UIWrapStack xGap={16} yGap={16}>
  <UIAvatar rounded="none">
    <Text style={{fontSize: 14, color: '#ffffff'}}>NONE</Text>
  </UIAvatar>
  <UIAvatar rounded="xs">
    <Text style={{fontSize: 14, color: '#ffffff'}}>XS</Text>
  </UIAvatar>
  <UIAvatar rounded="sm">
    <Text style={{fontSize: 14, color: '#ffffff'}}>SM</Text>
  </UIAvatar>
  <UIAvatar rounded="md">
    <Text style={{fontSize: 14, color: '#ffffff'}}>MD</Text>
  </UIAvatar>
  <UIAvatar rounded="lg">
    <Text style={{fontSize: 14, color: '#ffffff'}}>LG</Text>
  </UIAvatar>
  <UIAvatar rounded="xl">
    <Text style={{fontSize: 14, color: '#ffffff'}}>XL</Text>
  </UIAvatar>
  <UIAvatar rounded="2xl">
    <Text style={{fontSize: 14, color: '#ffffff'}}>2XL</Text>
  </UIAvatar>
  <UIAvatar>
    <Text style={{fontSize: 14, color: '#ffffff'}}>FULL</Text>
  </UIAvatar>
</UIWrapStack>

size

Use this to set the size of the avatar.

TypeDefaultDescription
'xs' 
| 'sm' 
| 'md' 
| 'lg' 
| 'xl' 
| '2xl'
'md'It applies mapped value to width and height properties.

Example

XS
SM
MD
LG
XL
2XL
Source code
<UIWrapStack 
  xGap={16}
  yGap={16}
>
  <UIAvatar size="xs">
    <Text style={{fontSize: 12, color: '#ffffff'}}>XS</Text>
  </UIAvatar>
  <UIAvatar size="sm">
    <Text style={{fontSize: 16, color: '#ffffff'}}>SM</Text>
  </UIAvatar>
  <UIAvatar>
    <Text style={{fontSize: 22, color: '#ffffff'}}>MD</Text>
  </UIAvatar>
  <UIAvatar size="lg">
    <Text style={{fontSize: 26, color: '#ffffff'}}>LG</Text>
  </UIAvatar>
  <UIAvatar size="xl">
    <Text style={{fontSize: 30, color: '#ffffff'}}>XL</Text>
  </UIAvatar>
  <UIAvatar size="2xl">
    <Text style={{fontSize: 30, color: '#ffffff'}}>2XL</Text>
  </UIAvatar>
</UIWrapStack>

bg

A background of an element when you don't have an image.

TypeDefaultDescription
string
#a8bac1Background color of the avatar when there is no source value. It's a regular React Native View backgroundColor .

Example

NN
NN
NN
Source code
<UIHStack xGap={16}>
  <UIAvatar bg="#a8bac1">
    <Text style={{fontSize: 22, color: '#ffffff'}}>NN</Text>
  </UIAvatar>
  <UIAvatar bg="#1bbeb0">
    <Text style={{fontSize: 22, color: '#ffffff'}}>NN</Text>
  </UIAvatar>
  <UIAvatar bg="#3b84a5">
    <Text style={{fontSize: 22, color: '#ffffff'}}>NN</Text>
  </UIAvatar>
</UIHStack>

border

Use this to set the border width.

TypeDefaultDescription
number
0It applies a value to borderWidth property.

Example

Source code
<View>
  <UIHStack xGap={-16}>
    <UIAvatar
      source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
      border={2}
    />
    <UIAvatar
      source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
      border={2}
    />
    <UIAvatar
      source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
      border={2}
    />
  </UIHStack>
  <UIHStack xGap={16}>
    <UIAvatar
      source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
    />
    <UIAvatar
      source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
      border={1}
    />
    <UIAvatar
      source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
      border={3}
    />
  </UIHStack>
</View>

borderColor

Use this to set the border color.

TypeDefaultDescription
string
#ffffffIt's a regular React Native View borderColor .

Example

NN
NN
NN
NN
NN
NN
Source code
<View>
  <UIHStack xGap={16}>
    <UIAvatar border={3} borderColor="#67ab5b" bg="#a8bac1">
      <Text style={{fontSize: 22, color: '#ffffff'}}>NN</Text>
    </UIAvatar>
    <UIAvatar border={3} borderColor="#f29c38" bg="#a8bac1">
      <Text style={{fontSize: 22, color: '#ffffff'}}>NN</Text>
    </UIAvatar>
    <UIAvatar border={3} borderColor="#e15240" bg="#a8bac1">
      <Text style={{fontSize: 22, color: '#ffffff'}}>NN</Text>
    </UIAvatar>
  </UIHStack>
  <UIHStack xGap={-16}>
    <UIAvatar border={3} borderColor="#1bbeb0" bg="#a8bac1">
      <Text style={{fontSize: 22, color: '#ffffff'}}>NN</Text>
    </UIAvatar>
    <UIAvatar border={3} borderColor="#1bbeb0" bg="#a8bac1">
      <Text style={{fontSize: 22, color: '#ffffff'}}>NN</Text>
    </UIAvatar>
    <UIAvatar border={3} borderColor="#1bbeb0" bg="#a8bac1">
      <Text style={{fontSize: 22, color: '#ffffff'}}>NN</Text>
    </UIAvatar>
  </UIHStack>
</View>

Preview

previewBg

Use this to set the background color of the preview container. It's useful when you want to see how the component looks on different backgrounds, especially when combined with borders.

Editor

Source code
import React from 'react';
import {View, Image} from 'react-native';

const UIAvatar = ({
  source,
  children,
  overlay,
  badge,
  badgePosition = 'br',
  badgeInset = 0,
  rounded = 'full',
  size = 'md',
  bg = '#a8bac1',
  border = 0,
  borderColor = '#ffffff',
}) => {
  const sizeMappedValue = {
    'xs': 24,
    'sm': 32,
    'md': 48,
    'lg': 64,
    'xl': 96,
    '2xl': 128,
  }[size];

  const fullRadius = sizeMappedValue / 2;

  const roundedMappedValue = {
    'none': 0,
    'xs': 2,
    'sm': 4,
    'md': 6,
    'lg': 8,
    'xl': 12,
    '2xl': 16,
    'full': fullRadius,
  }[rounded];

  // Determine the bounded radius, ensuring it does not exceed the full radius
  const boundedRadius = Math.min(roundedMappedValue, fullRadius);

  // Calculate the side length of the outer square surrounding the circle formed by the radius
  const radiusOuterSquareSide = boundedRadius * 2;
  // Calculate the hypotenuse of the outer square
  const hypotenuse = Math.sqrt(Math.pow(radiusOuterSquareSide, 2) + Math.pow(radiusOuterSquareSide, 2));
  // Calculate the distance between the corner of the outer square and the inner circle
  const distanceBetweenCornerAndInnerCircle = hypotenuse / 2 - boundedRadius;
  // Define the side length of the smaller square between the outer square and the inner circle, serving as the reference for positioning the badge (top/bottom and left/right)
  const referenceSquareSide = Math.sqrt(Math.pow(distanceBetweenCornerAndInnerCircle, 2) / 2);
  // This distance is used for determining top/bottom and left/right positions of the badge
  const distance = referenceSquareSide + badgeInset;
  
  const badgePositionMappedStyles = {
    'tl': {top: distance, left: distance},
    't': {top: badgeInset, left: '50%'},
    'tr': {top: distance, right: distance},
    'r': {top: '50%', right: badgeInset},
    'br': {bottom: distance, right: distance},
    'b': {bottom: badgeInset, left: '50%'},
    'bl': {bottom: distance, left: distance},
    'l': {top: '50%', left: badgeInset},
  }[badgePosition];

  const styles = {
    avatar: {
      flexShrink: 0,
      width: sizeMappedValue,
      height: sizeMappedValue,
    },
    avatarContainer: {
      position: 'absolute',
      top: 0,
      left: 0,
      width: '100%',
      height: '100%',
      alignItems: 'center',
      justifyContent: 'center',
      borderWidth: border,
      borderColor: borderColor,
      borderRadius: boundedRadius,
      backgroundColor: bg,
      overflow: 'hidden',
    },
    avatarImg: {
      position: 'absolute',
      top: 0,
      left: 0,
      width: '100%',
      height: '100%',
    },
    avatarOverlay: {
      position: 'absolute',
      top: 0,
      left: 0,
      width: '100%',
      height: '100%',
      alignItems: 'center',
      justifyContent: 'center',
    },
    avatarBadge: {
      ...badgePositionMappedStyles,
      position: 'absolute',
      width: 0,
      height: 0,
      alignItems: 'center',
      justifyContent: 'center',
    }
  };

  return (
    <View style={styles.avatar}>
      <View style={styles.avatarContainer}>
        {children}
        {source ? <Image source={source} style={styles.avatarImg} /> : null}
        {overlay ? <View style={styles.avatarOverlay}>{overlay}</View> : null}
      </View>
      {badge ? <View style={styles.avatarBadge}>{badge}</View> : null}
    </View>
  );
};

export default UIAvatar;
Usage example
<UIAvatar
  source={{uri: 'https://images.unsplash.com/photo-1620508115467-aa36a8dcf82d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=640&q=80'}}
  badge={<View style={{borderWidth: 3, borderColor: '#ffffff', width: 24, height: 24, borderRadius: 12, backgroundColor: '#67ab5b'}} />}
  badgeInset={2}
  size="2xl"
  border={3}
>
  <Text style={{fontSize: 30, color: '#ffffff'}}>NN</Text>
</UIAvatar>