Sha256: e2bc25d6283a606709997309972d28ee1156b28e3575df341d3c5d54189f927b

Contents?: true

Size: 1.83 KB

Versions: 67

Compression:

Stored size: 1.83 KB

Contents

/* @flow */

import React, { useState } from 'react'
import classnames from 'classnames'
import { map } from 'lodash'

import { buildAriaProps, buildCss, buildDataProps } from '../utilities/props'
import { globalProps } from '../utilities/globalProps.js'

import { Image, OnlineStatus } from '../'

type AvatarProps = {
  aria?: object,
  className?: string,
  data?: object,
  dark?: boolean,
  id?: string,
  imageAlt?: string,
  imageUrl: string,
  name: string,
  size?: "md" | "lg" | "sm" | "xl" | "xs",
  status: "away" | "offline" | "online",
}

const firstTwoInitials = (name) =>
  map(name.split(/\s/), (name) => name[0])
    .join('')
    .substring(0, 2)

const Avatar = (props: AvatarProps) => {
  const {
    aria = {},
    className,
    data = {},
    name = null,
    id = '',
    imageAlt = '',
    imageUrl,
    size = 'md',
    status = null,
    dark = false,
  } = props
  const dataProps = buildDataProps(data)
  const ariaProps = buildAriaProps(aria)
  const classes = classnames(
    buildCss('pb_avatar_kit', size),
    globalProps(props),
    className
  )

  const initials = name && firstTwoInitials(name)
  dataProps['data-initials'] = initials

  const [error, setError] = useState(false)
  const handleError = () => setError(true)

  return (
    <div
        {...ariaProps}
        {...dataProps}
        className={classes}
        id={id}
    >
      <div
          className="avatar_wrapper"
          data-initials={initials}
      >
        <If condition={imageUrl && !error}>
          <Image
              alt={imageAlt}
              onError={handleError}
              url={imageUrl}
          />
        </If>
      </div>
      <If condition={status}>
        <OnlineStatus
            className={`size_${size}`}
            dark={dark}
            status={status}
        />
      </If>
    </div>
  )
}

export default Avatar

Version data entries

67 entries across 67 versions & 1 rubygems

Version Path
playbook_ui-9.9.0.alpha.inline1 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.9.0 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.8.0 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.7.0.pre.alphawidth1 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.7.0.pre.alpha.a11y.btn app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.7.0 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.6.1 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.6.1.pre.deps1 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.6.0 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.5.0.alpha.rs.downgrade app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.5.0 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.4.0.alpha.sisense app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.4.0.alpha.user.kit.bug app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.4.0 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.3.1.pre.alpha2 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.4.0.pre.alpha2 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.4.0.pre.alpha1 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.3.1.pre.alpha1 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.3.0.alpha.inline3 app/pb_kits/playbook/pb_avatar/_avatar.jsx
playbook_ui-9.3.0.pre.alpha.password.strength.2 app/pb_kits/playbook/pb_avatar/_avatar.jsx