Sha256: 870d8cb0a81b8b98333243f9fdf2122b081d07db5c3c6153a5384b53f965e251

Contents?: true

Size: 2 KB

Versions: 20

Compression:

Stored size: 2 KB

Contents

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

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

import Image from '../pb_image/_image'
import OnlineStatus from '../pb_online_status/_online_status'

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

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

const Avatar = (props: AvatarProps): React.ReactElement => {
  const {
    aria = {},
    className,
    data = {},
    name = null,
    id = '',
    imageAlt = '',
    imageUrl,
    size = 'md',
    status = null,
    dark = false,
  } = props
  const dataProps: {[key: string]: any} = buildDataProps(data)
  const ariaProps: {[key: string]: any} = buildAriaProps(aria)
  const classes = classnames(
    buildCss('pb_avatar_kit', `size_${size}`),
    globalProps(props),
    className
  )

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

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

  const canShowImage = imageUrl && !error

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

export default Avatar

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
playbook_ui-11.2.2 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-11.2.1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-11.2.0 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-11.1.2 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-11.1.2.pre.alpha1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-11.1.1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-11.1.1.pre.alpha1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-11.1.0 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-11.0.1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-11.0.0 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-11.0.0.pre.alpha.2 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-11.0.0.pre.alpha.1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.26.1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.27.0.pre.lazysizes1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.27.0.pre.datepicker1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.26.0 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.26.0.pre.alpha5 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.26.0.pre.rc1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.26.0.pre.alpha.display1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.26.0.pre.alpha3 app/pb_kits/playbook/pb_avatar/_avatar.tsx