Workday Canvas

Avatar

Avatar is a visual representation of a user's profile and identity.

v14.2.34
Install
yarn add @workday/canvas-kit-preview-react

Avatar (Main) vs. Avatar (Preview)

We recommend you use the Avatar in the Preview package (@workday/canvas-kit-preview-react) documented here on this page. The Avatar in the Main package (@workday/canvas-kit-react) will eventually be replaced with the Avatar in the Preview package.

Anatomy

The basic elements of an avatar

  1. Background: A randomly selected fill color for the initial-based variant.
  2. Initials: The first letters of a user’s first and last names in a darker gradient of fill color.
  3. Image: A user-uploaded image of themselves.

Usage Guidance

When to Use

  • Use Avatars to provide a user with a stronger identity or when an access point into their profile provides reasonable benefit.

When to Use Something Else

  • If the data is not a user, and a symbolic representation is needed, use an icon component instead.

Avatars vary on the basis of version, sizing, background, and default color.

Version

Avatars default to their initial-based version unless a user manually uploads a profile photo. The lettering inside the Avatar is based on the two letter English initials of the Western equivalent of a user name.

Avatar sizes

Sizing

Avatars should feel appropriately sized based on their importance and nearby content. They vary on a scale from XXS to XXL to accommodate an array of different possible use cases:

  • XXS: 24 x 24. Please note that it is recommended to use a larger size than XXS for all uses outside of pills.

Avatar sizes

Avatar sizes

  • XS: 32 x 32
  • S: 40 x 40
  • M: 48 x 48
  • L: 72 x 72
  • XL: 96 x 96
  • XXL: 120 x 120

Regardless of Avatar sizing, all tap targets are 48x48 or larger.

Avatar sizes

Color

Initial-based avatars are randomly assigned a gender neutral and contrast friendly color on a per-user basis.

Avatar colors

Examples

Basic Example

The most basic usage requires only a name prop. The component automatically extracts and displays the initials. If you want to display a different set of initials, you can use the preferredInitials prop.

JD
import {Avatar} from '@workday/canvas-kit-preview-react/avatar';

export default () => {
  return <Avatar name="John Doe" />;
};

Image Avatar

You can display a profile image by providing the url prop.

Note: The url and the name prop is required for the image avatar. The name is used for the alt attribute on the image.

Image Fallback Behavior

The Avatar component includes intelligent fallback handling:

  • While the image loads, the user’s initials are displayed using the name prop
  • If the image fails to load, initials remain visible
  • The name prop serves as both the alt text and fallback content
Happy DoggoHD
import {Avatar} from '@workday/canvas-kit-preview-react/avatar';

export default () => {
  return (
    <Avatar
      name="Happy Doggo"
      url={'https://picsum.photos/id/237/300/200'}
      objectFit="cover"
      size="medium"
    />
  );
};

Sizes

The Avatar component supports the following sizes:

  • extraExtraSmall is 24px x 24px
  • extraSmall is 32px x 32px
  • small is 40px x 40px
  • medium is 48px x 48px
  • large is 72px x 72px
  • extraLarge is 96px x 96px
  • extraExtraLarge is 120px x 120px
JD
LM
WW
IM
PP
BB
E
import {Avatar} from '@workday/canvas-kit-preview-react/avatar';
import {createStyles} from '@workday/canvas-kit-styling';
import {system} from '@workday/canvas-tokens-web';

const containerStyles = createStyles({
  display: 'inline-flex',
  gap: system.space.x2,
});

export default () => {
  return (
    <div className={containerStyles}>
      <Avatar name="John Doe" size="extraExtraSmall" />
      <Avatar name="Logan McNeil" size="extraSmall" />
      <Avatar name="Wonder Woman" size="small" />
      <Avatar name="Iron Man" size="medium" />
      <Avatar name="Peter Parker" size="large" />
      <Avatar name="Bruce Banner" size="extraLarge" />
      <Avatar name="Elektra" size="extraExtraLarge" />
    </div>
  );
};

Variants

Choose from four predefined color schemes:

JD
LM
WW
E
import {Avatar} from '@workday/canvas-kit-preview-react/avatar';
import {createStyles} from '@workday/canvas-kit-styling';
import {system} from '@workday/canvas-tokens-web';

const containerStyles = createStyles({
  display: 'inline-flex',
  gap: system.space.x2,
});

export default () => {
  return (
    <div className={containerStyles}>
      <Avatar name="John Doe" variant="blue" />
      <Avatar name="Logan McNeil" variant="amber" />
      <Avatar name="Wonder Woman" variant="teal" />
      <Avatar name="Elektra" variant="purple" />
    </div>
  );
};

Advanced Custom Component

For complete control over styling and behavior, use the BaseAvatar component:

import {BaseAvatar} from '@workday/canvas-kit-preview-react/avatar';
import {createStyles, px2rem} from '@workday/canvas-kit-styling';
import {base} from '@workday/canvas-tokens-web';

const customStyles = createStyles({
  cursor: 'pointer',
  backgroundColor: base.magenta300,
  color: base.magenta700,
  borderRadius: '50%',
  border: 'none',
  padding: '0',
  margin: '0',
  display: 'flex',
  alignItems: 'center',
  span: {
    cursor: 'pointer',
  },
});

export default () => {
  return (
    <BaseAvatar
      size={px2rem(56)}
      cs={customStyles}
      as="button"
      onClick={() => console.log('clicked')}
    >
      <BaseAvatar.Name name="John Doe Jane" />
    </BaseAvatar>
  );
};

Accessibility

If the Avatar is purely decorative, you can set the isDecorative prop to true to prevent the name prop from being forwarded to the alt attribute of the image.

NS
Nicholas Smith
import {Avatar} from '@workday/canvas-kit-preview-react/avatar';
// @ts-ignore
import nicholasAvatar from './nicholas-avatar.jpg';
import {createStyles} from '@workday/canvas-kit-styling';
import {Text} from '@workday/canvas-kit-react/text';
import {system} from '@workday/canvas-tokens-web';
const containerStyles = createStyles({
  display: 'inline-flex',
  gap: system.space.x2,
  alignItems: 'center',
});
export default () => {
  return (
    <div className={containerStyles}>
      <Avatar
        name="Nicholas Smith"
        isDecorative
        url={nicholasAvatar}
        objectFit="cover"
        size="small"
      />
      <Text>Nicholas Smith</Text>
    </div>
  );
};

Component API

Avatar

Props

Props extend from div. Changing the as prop will change the element interface.

NameTypeDescriptionDefault
urlstring

The URL of the user's photo. For best fit, use square images.

objectFitProperty.ObjectFit

An objectFit property that can customize how to resize your image to fit its container.

'contain'
isDecorativeboolean

If true, the Avatar won't forward the name prop to the alt attribute of the image. This is useful when the Avatar is purely decorative and is rendered next to a name or text.

childrenReactNode

Children of the BaseAvatar.

variant
  'blue'
  'amber'
  'teal'
  'purple'

The variant of the Avatar.

'blue'
size
  'extraExtraSmall'
  'extraSmall'
  'small'
  'medium'
  'large'
  'extraLarge'
  'extraExtraLarge'
  (string & {})

The size of the Avatar. extraExtraSmall is 24px x 24px extraSmall is 32px x 32px small is 40px x 40px medium is 48px x 48px large is 72px x 72px extraLarge is 96px x 96px extraExtraLarge is 120px x 120px

'medium'
cs

The cs prop takes in a single value or an array of values. You can pass the CSS class name returned by , or the result of and . If you're extending a component already using cs, you can merge that prop in as well. Any style that is passed to the cs prop will override style props. If you wish to have styles that are overridden by the css prop, or styles added via the styled API, use wherever elemProps is used. If your component needs to also handle style props, use {@link mergeStyles } instead.

import {handleCsProp} from '@workday/canvas-kit-styling';
import {mergeStyles} from '@workday/canvas-kit-react/layout';

// ...

// `handleCsProp` handles compat mode with Emotion's runtime APIs. `mergeStyles` has the same
// function signature, but adds support for style props.

return (
 <Element
   {...handleCsProp(elemProps, [
     myStyles,
     myModifiers({ size: 'medium' }),
     myVars({ backgroundColor: 'red' })
   ])}
 >
   {children}
 </Element>
)
namestring

The alt text of the Avatar image. This prop is also used for the initials. The first letter of the first name and the first letter of the second name are chosen for the initials.

preferredInitialsstring

If you want full control over the initials, use preferredInitials instead.

asReact.ElementType

Optional override of the default element used by the component. Any valid tag or Component. If you provided a Component, this component should forward the ref using React.forwardRefand spread extra props to a root element.

Note: Not all elements make sense and some elements may cause accessibility issues. Change this value with care.

div
refReact.Ref<R = div>

Optional ref. If the component represents an element, this ref will be a reference to the real DOM element of the component. If as is set to an element, it will be that element. If as is a component, the reference will be to that component (or element if the component uses React.forwardRef).

Can't Find What You Need?

Check out our FAQ section which may help you find the information you're looking for. For further information, contact the #ask-canvas-design or #ask-canvas-kitchannels on Slack.

On this Page: