New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

smart-avatar

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smart-avatar

Render avatars with dynamic, defined fallback

  • 0.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
decreased by-60.71%
Maintainers
1
Weekly downloads
 
Created
Source

smart-avatar

Render avatars with dynamic, defined fallback


Table of Contents


Live Demo

A live demo is hosted at the following link. This demo allows for experimentation with all of the configuration settings smart-avatar provides. This demo may help you to determine whether this package is right for your needs. Demo will be updated with major changes to functionality.


Usage

Basic Example

import smartAvatar from 'smart-avatar';

const targetElement = document.querySelector(.topbar-user);

smartAvatar(targetElement, {
  "email": "jared.gorski@liferay.com",
  "initials": "JG",
  "src": "./user-imgs/jared.jpg",
  "icon": "identicon"
  "round": true,
  "size": 48,
  "color": "#00AE55",
  "textColor": "#FFF",
  "cssClass": "we-user",
});

Detailed Example

import smartAvatar from 'smart-avatar';

// define target element to append with smart-avatar
const targetElement = document.querySelector(.topbar-user);

// run smartAvatar function, passing target element and desired configuration
smartAvatar(targetElement, {
  
  // fallback priority definition
  "priority": [
    "gravatar",
    "src",
    "smart"
  ],
  
  // general settings
  "cssClass": "site-avatar",                        // user-defined CSS classes
  "round": true,                                    // shape of asset (square if false)
  "size": 80,                                       // size of asset (width in px)
  "timestamp": true                                 // adds timestamp attribute (for unique id)
  "alt": "User jaredgorski profile image"           // adds alt attribute for accessibility (if not initials or smartfox)
  
  // gravatar settings
  "email": "jared.gorski@liferay.com",              // gravatar email address
  "format": "png",                                  // gravatar image format (jpg, png, or tiff)
  "hash": "93a5c828f0da09f0af10d2ac238724b5",       // md5 hash of gravatar email address
  "protocol": "secure",                             // protocol of gravatar request (secure, unsecure, or agnostic)
  "resolution": 120,                                // resolution of gravatar (width in px)
  
  // user-defined src
  "src": "./img/src.jpg",                           // path to custom fallback asset, relative or absolute
  
  // initials settings
  "initials": "J",                                  // 1 or 2 letters
  "color": "#777777",                               // color of background (affects both initials and built-in icon)
  "textColor": "#FFFFFF",                           // color of initials font
  
  // built-in icon
  "icon": "smartfox",                               // see below for available icon styles
});

Guide to Settings

Priority Setting

KeyDefinitionDefault
priorityArray - Defines fallback order.['gravatar', 'src', 'smart']

The priority setting consists of up to 3 items:

  • gravatar: Attempts to render a gravatar asset if email address and/or hash is provided.
  • src: Attempts to render a user-defined asset.
  • smart: Attempts to render an initials avatar if initials is provided, or an icon if not. If no valid icon option is provided, the smartfox icon will be rendered.

In the case of gravatar and src, these options will fall back to the next option in the priority array. So, gravatar will fall back to src (and vice-versa) and/or will finally fall back to smart of no valid data was provided to render a gravatar or src asset.

Once smart is reached, the smart-avatar will look for initials. If none are provided, it will fall back to any defined icon, and finally to the smartfox. This means that, if smart is included before gravatar or src, any data for the latter option will not be reached in the priority queue and will therefore not be rendered. Also, even if smart is not included in the array, the smartfox icon will serve as a final fallback.

General Settings

KeyDefinitionDefault
altString - Adds alt attribute to img element output.N/A
cssClassString - Adds custom CSS classes.N/A
roundBoolean - Generates output with border-radius of 50%.false
sizeNumber or String - Defines output element width in px.80
timestampBoolean - Adds sa_timestamp attribute with milliseconds elapsed since the UNIX epoch.false

These settings apply to the rendered element in general. One can add CSS classes for use by custom stylesheets, define the size and shape of the rendered element, add a timestamp for debugging or use as a unique id (obviously this value is not permanent), and add an alt attribute for accessibility purposes.

Gravatar Settings

KeyDefinitionDefault
emailString - Relative or absolute path to desired asset.N/A
formatString - Image format to request from Gravatar: jpg, png, or tiff.jpg
hashString - md5 hash of Gravatar email address.N/A
protocolString - Protocol to use for Gravatar request: secure (https), unsecure (http), or agnostic (//).secure
resolutionNumber or String - Defines gravatar resolution (width) in px. Does not affect size.120, or size * 1.5

Defining these settings enables rendering a Gravatar user image by requesting a resource from Gravatar for a given user's email address. Many aspects of this request and output can be customized, including the request protocol, resource format, and image resolution. Keep in mind that higher resolutions result in more data transfer, meaning larger requests.

User-defined Source

KeyDefinitionDefault
srcString - Relative or absolute path to desired asset.N/A

Providing a path to a custom resource allows for proprietary icons and user images to be used as primary avatars or fallback resources.

Smart Resource Settings

KeyDefinitionDefault
colorString - Background color of smart icon as Hex, RGB, or RGBA value.#777
iconString - Defines smart icon style.smartfox
initialsString - 1 or 2 letters to use as initials.N/A
textColorString - Font color of initials icon as Hex, RGB, or RGBA value.#FFF

The "smart" resource is so-called because it provides an easy API for quality fallback in the worst-case scenario. If the given gravatar data doesn't return a valid image and the src path is broken, an asset with a given user's initials or a general, non-specific icon makes for a great avatar. If an icon is preferred, there are plenty of options to choose from, with more to come. See the list of available icons below.

Available Icon Options

SmartAvatar Icons

smartfox - the best default fox ever (uses color as background)

Gravatar-hosted Icons (from their website):

mp - (mystery-person) a simple, cartoon-style silhouetted outline of a person (does not vary by email hash)

identicon - a geometric pattern based on an email hash

monsterid - a generated 'monster' with different colors, faces, etc

wavatar - generated faces with differing features and backgrounds

retro - awesome generated, 8-bit arcade-style pixelated faces

robohash - a generated robot with different colors, faces, etc


Develop and Test:

npm i

(write code)

npm run test

navigate to http://127.0.0.1:7878/ to manually test changes

Keywords

FAQs

Package last updated on 10 Feb 2019

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