1.1.2 • Published 5 years ago

@agney/react-avatar v1.1.2

Weekly downloads
8
License
MIT
Repository
github
Last release
5 years ago

React Avatar

All Contributors

Create Image Avatars with text based fallbacks.

Demo Storybook

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

DescriptionDefault Value
srcSource of image to be displayed
textText to be displayed as fallback
imageAltAlt tag for image'' - decorative
shapeshape of avatar. options: 'circle''square'circle
htmlWidthwidth of image element and text fallback.100%
htmlHeightheight of image element and text fallbacksame as htmlWidth
backgroundsArray of background colors'#3c40c6','#ffa801','#485460','#0be881','#f53b57'
bgColorbackground color for specific text fallback.picks random from backgrounds, this changes according to text prop passed in.
textColortext color for specific text fallbackreadable dark or light color depending on background.
textProcessorfunction 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
classNameclassName 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

ArgumentDescriptionDefault
emailEmail of the person for which gravatar is to be fetched
configconfiguration object. following fields are part of this object
config.sizenumber representing height & width of image. 1px up to 2048px80
config.defaultImageIf 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.forceDefaultAlways returns the default image if truefalse
config.ratingGravatar 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

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

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

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2020 Agney Menon <agney@outlook.in>. This project is MIT licensed.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

1.1.2

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.1.1

5 years ago