Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-avatar

Package Overview
Dependencies
Maintainers
2
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-avatar

Universal React avatar component makes it possible to generate avatars based on user information.

  • 3.4.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
61K
increased by3.35%
Maintainers
2
Weekly downloads
 
Created
Source

<Avatar>Build Status

Universal avatar makes it possible to fetch/generate an avatar based on the information you have about that user. We use a fallback system that if for example an invalid Facebook ID is used it will try Google, and so on.

React Avatar component preview

For the moment we support following types:

  • Facebook
  • Google
  • Twitter (using Avatar Redirect)
  • Instagram (using Avatar Redirect)
  • Vkontakte
  • Skype
  • Gravatar
  • Custom image
  • Name initials

The fallbacks are in the same order as the list above were Facebook has the highest priority.

Demo

Check it live!

Install

Install the component using NPM:

$ npm install react-avatar --save

# besides React, react-avatar also has react-addons-shallow-compare and prop-types
# as peer dependencies, make sure to install the correct version
# for your version of react
$ npm install react-addons-shallow-compare@^0.14 --save
# OR
$ npm install react-addons-shallow-compare@^15 --save

Or download as ZIP.

Usage

  1. Import Custom Element:

    import Avatar from 'react-avatar';
    
  2. Start using it!

    <Avatar name="Foo Bar" />
    

Some examples:

<Avatar googleId="118096717852922241760" size="100" round={true} />
<Avatar facebookId="100008343750912" size="150" />
<Avatar vkontakteId="1" size="150" />
<Avatar skypeId="sitebase" size="200" />
<Avatar twitterHandle="sitebase" size="40" />
<Avatar name="Wim Mostmans" size="150" />
<Avatar name="Wim Mostmans" size="150" textSizeRatio="1.75" />
<Avatar value="86%" size="40" />
<Avatar size="100" facebook-id="invalidfacebookusername" src="http://www.gravatar.com/avatar/a16a38cdfe8b2cbd38e8a56ab93238d3" />
<Avatar name="Wim Mostmans" unstyled="true" />

Manually generating a color:

import Avatar from 'react-avatar';

<Avatar color={Avatar.getRandomColor('sitebase', ['red', 'green', 'blue'])} name="Wim Mostmans" />

Configuring React Avatar globally

import Avatar, { ConfigProvider } from 'react-avatar';

<ConfigProvider colors={['red', 'green', 'blue']}>
    <YourApp>
        ...
        <Avatar name="Wim Mostmans" />
        ...
    </YourApp>
</ConfigProvider>

Options

Avatar

AttributeOptionsDefaultDescription
classNamestringName of the CSS class you want to add to this component alongside the default sb-avatar.
emailstringString of the email address of the user.
md5EmailstringString of the MD5 hash of email address of the user.
facebookIdstring
twitterHandlestring
instagramIdstring
googleIdstring
skypeIdstring
namestringWill be used to generate avatar based on the initials of the person
maxInitialsnumberSet max nr of characters used for the initials. If maxInitials=2 and the name is Foo Bar Var the initials will be FB
initialsstring or functiondefaultInitialsSet the initials to show or a function that derives them from the component props, the method should have the signature fn(name, props)
valuestringShow a value as avatar
colorstringrandomUsed in combination with name and value. Give the background a fixed color with a hex like for example #FF0000
fgColorstring#FFFUsed in combination with name and value. Give the text a fixed color with a hex like for example #FF0000
sizelength50pxSize of the avatar
textSizeRationumber3For text based avatars the size of the text as a fragment of size (size / textSizeRatio)
roundbool or lengthfalseThe amount of border-radius to apply to the avatar corners, true shows the avatar in a circle.
srcstringFallback image to use
styleobjectStyle that will be applied on the root element
unstyledboolfalseDisable all styles
onClickfunctionMouse click event

ConfigProvider

AttributeOptionsDefaultDescription
colorsarray(string)default colorsA list of color values as strings from which the getRandomColor picks one at random.
cachecacheinternal cacheCache implementation used to track broken img URLs
initialsfunctiondefaultInitialsA function that derives the initials from the component props, the method should have the signature fn(name, props)
avatarRedirectUrlURLundefinedBase URL to a Avatar Redirect instance

Avatar Redirect

Avatar Redirect adds support for social networks which require a server-side service to find the correct avatar URL.

Examples of this are:

  • Twitter
  • Instagram

An open Avatar Redirect endpoint is provided at https://avatar-redirect.appspot.com. However this endpoint is provided for free and as such an explicit opt-in is required as no guarantees can be made about uptime of this endpoint.

Avatar Redirect is enabled by setting the avatarRedirectUrl property on the ConfigProvider context

Development

In order to run it locally you'll need to fetch some dependencies and a basic server setup.

  • Install local dependencies:

    $ npm install
    
  • To test your react-avatar and your changes, start the development server and open http://localhost:8000/index.html.

    $ npm run dev
    
  • To create a local production build into the lib and es folders.

    $ npm run build
    

Implementing a custom cache

cache as provided to the ConfigProvider should be an object implementing the methods below. The default cache implementation can be found here

MethodDescription
set(key, value)Save value at key, such that it can be retrieved using get(key). Returns undefined
get(key)Retrieve the value stored at key, if the cache does not contain a value for key return null
sourceFailed(source)Mark the image URL specified in source as failed. Returns undefined
hasSourceFailedBefore(source)Returns true if the source has been tagged as failed using sourceFailed(source), otherwise false.

Products using React Avatar

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

History

For detailed changelog, check Releases.

Maintainers

License

MIT License

Keywords

FAQs

Package last updated on 16 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc