Sha256: a26ae32455a90110cf703b1af98128aedf67e75a7eaa1cd1c236c107b97710dc

Contents?: true

Size: 1.95 KB

Versions: 15

Compression:

Stored size: 1.95 KB

Contents

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

import { buildAriaProps, buildCss, buildDataProps } from '../utilities/props'
import { 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",
}

const firstTwoInitials = (name: string) =>
  name.split(/\s/).map((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: {[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

15 entries across 15 versions & 1 rubygems

Version Path
playbook_ui-10.26.0.pre.alpha.sticky1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.25.1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.26.0.pre.alpha1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.25.0 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.25.0.pre.alpha3 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.25.0.pre.treeshaking1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.24.0 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.24.0.pre.alpha1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.23.0 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.23.0.pre.alpha2 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.23.0.pre.alpha1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.22.1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.22.0 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.21.1.pre.alpha1 app/pb_kits/playbook/pb_avatar/_avatar.tsx
playbook_ui-10.21.0 app/pb_kits/playbook/pb_avatar/_avatar.tsx