Create Image Avatars with text based fallbacks for react

React Avatar

Create Image Avatars with text based fallbacks.

Features ✨

  1. Ability to render image avatar or text avatar as circle or square.
  2. Ability to render text based fallbacks for images for the time they take to load or error.
  3. Renders text based avatars and backgrounds based on text passed in.
  4. Autoscales text if it doesn't fit the container width.
  5. Automatically determines readable text color depending on background.
  6. Exposes a hook to use/generate gravatar for a user.
  7. Exposes a context so you can configure avatar components across your application.

Install

npm install @agney/react-avatar
# OR
yarn add @agney/react-avatar

Requires React v16.8 or above and Styled Components v4 or above.

Usage

import React from "react";
import { Avatar } from "@agney/react-avatar";

const App = () => {
  return (
    <Avatar
      src="https://gravatar.com/avatar/7c4ff521986b4ff8d29440beec01972d?s=400&d=robohash&r=x"
      text="CM"
    />
  );
}

export default App;

API

Description Default Value
src Source of image to be displayed
text Text to be displayed as fallback
imageAlt Alt tag for image '' - decorative
shape shape of avatar. options: 'circle' 'square'
htmlWidth width of image element and text fallback. 100%
htmlHeight height of image element and text fallback same as htmlWidth
backgrounds Array of background colors ['#3c40c6','#ffa801','#485460','#0be881','#f53b57']
bgColor background color for specific text fallback. picks random from backgrounds, this changes according to text prop passed in.
textColor text color for specific text fallback readable dark or light color depending on background.
textProcessor function to process visible text inside the avatar. Original text will be used for hashing so even ones with same initials inside avatar can have different backgrounds. (text) => text
className className on the wrapper. can be used for wrapping with CSS-in-JS frameworks

See Storybook for more examples and code snippets.

Context

Package exposes a context in the form of AvatarContext.

import { Avatar, AvatarContext } from '../.';

function App() {
  const contextValue = React.useMemo(() => ({
    backgrounds: ['#000000', '#DD2C00', '#6200EA', '#3F51B5'], // Any props used by Avatar can be used here.
  }), []);
  return (
    <AvatarContext.Provider>
      <div style={{ display: 'flex' }}>
        <Avatar
          htmlWidth='150px'
          text="Fallback"
          backgrounds={['red']}
          textColor='white'
        />
        <Avatar
          htmlWidth='150px'
          text="AJ"
        />
      </div>
    </AvatarContext.Provider>
  )
}

Values on the Context Provider are overridden by any props that are on the individual component. Context Provider is not compulsory for usage of Avatar component. Read more about Context API on docs.

useGravatar hook

Gravatars or Globally Recognized Avatars is a free service that allows you to share profile pictures/avatars across different sites and services. react-avatar exposes a hook that makes it easier for you to request and use these gravatars in your application.

Usage:

import React from "react";
import { Avatar, useGravatar } from "@agney/react-avatar";

const App = () => {
  const url = useGravatar('person@email.com');
  return (
    <Avatar
      src={url}
      text="CM"
    />
  );
}

export default App;

API

Argument Description Default
email Email of the person for which gravatar is to be fetched
config configuration object. following fields are part of this object
config.size number representing height & width of image. 1px up to 2048px 80
config.defaultImage If the image is not available, gravatar defaults to this property. You can provide a custom URL image to default to or one of the alternatives that Gravatar supports.
config.forceDefault Always returns the default image if true false
config.rating Gravatar allows users to self rate their images. If required pass in a higher rating. g

For a complete list of available options, view Gravatar docs

Development

We use yarn v1 for development.

yarn
yarn start

# To run example
cd example
yarn start

# Running storybook
yarn run storybook

Run tests

yarn test