# All files / src/Avatar Avatar.tsx

100% Statements 42/42
95.23% Branches 20/21
100% Functions 4/4
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    1939x
    9x
    9x

    9x

    236x
    233x
    233x

    233x
    233x
    233x
    233x
    233x

    233x

    233x

    233x
    233x

    233x

    233x

    233x

    233x

    233x

    9x

    9x

    9x

    73x
    73x

    9x

    68x

    68x

    9x

    65x
    65x

    9x

    9x

    9x

    9x

    9x

    9x

    9x

     import { useButton } from '@react-aria/button'
    import React from 'react'
    import { addSubcomponents } from '../_utils/addSubcomponents'
    import { DivAttributes } from '../_utils/types'
    import {
      StyledAvatarContainer,
      StyledAvatarOverlay,
      StyledIconContainer,
      StyledLabelContainer,
      StyledPortraitContainer,
    } from './Avatar.styles'
    import {
      AvatarIconProps,
      AvatarLabelProps,
      AvatarPortraitProps,
      AvatarProps,
      AvatarRef,
      AvatarRoles,
      AvatarRoleType,
      BaseAvatarProps,
    } from './Avatar.types'

    /**

     We use avatars to visually represent our users, places, and things in the app.
     These can be in the form of rich media or representative illustrations.

     @since 10.19.0

     @see [Storybook](https://procore.github.io/core/core-react/latest/?path=/story/demos-avatar--demo)

     @see [Design Guidelines](https://design.procore.com/avatar)

     @a11y Avatar's need a `aria-label` and can be enhanced with `role` for interaction. If decorative only, add `aria-hidden`. The inaccessible `clickable` prop is for UI only

     */
    function AvatarInner(
      {
        as,
        clickable,
        disabled,
        // @ts-ignore link only
        href,
        size,
        children,
        onBlur,
        onFocus,
        // @ts-ignore link only
        rel,
        // @ts-ignore not on legacy
        role,
        // @ts-ignore link only
        target,
        ...props
      }: AvatarProps<AvatarRoleType>,
      ref: AvatarRef<AvatarRoleType>
    ) {
      const { onPress } = props as AvatarProps<'button'>

      let { buttonProps } = useButton(
        {
          ...props,
          onBlur: onBlur as (e: React.FocusEvent) => void,
          onFocus: onFocus as (e: React.FocusEvent) => void,
          elementType: (as as any as string) === 'a' ? 'a' : 'div',
          href,
          isDisabled: disabled,
          onPress,
          rel,
          target,
          type: 'button',
        },
        ref as any // TODO fix
      )

      const linkDisabled = disabled || (role === 'link' && !href)

      const a11yProps =
        role === 'button'
          ? { ...buttonProps, as: as || 'div', $clickable: !disabled }
          : role === 'link'
          ? {
              ['aria-disabled']: linkDisabled ? true : undefined,
              as: as || 'a',
              $clickable: !linkDisabled,
              href: linkDisabled ? undefined : href,
              onBlur,
              onFocus,
              rel,
              role: 'link',
              target,
            }
          : role === 'img'
          ? {
              ['aria-disabled']: disabled ? true : undefined,
              role: 'img',
              onBlur,
              onFocus,
            }
          : { onBlur, onFocus }

      return (
        <StyledAvatarContainer
          ref={ref as any}
          $clickable={clickable}
          $disabled={disabled}
          $size={size}
          data-qa="core-avatar"
          {...a11yProps}
          {...(props as any)} // TODO fix
        >
          {React.Children.only(children)}
          <StyledAvatarOverlay aria-hidden />
        </StyledAvatarContainer>
      )
    }

    const Avatar_ = React.forwardRef(AvatarInner) as <Role extends AvatarRoles>(
      props:
        | AvatarProps<Role>
        // Legacy
        | (BaseAvatarProps & { ref?: React.ComponentPropsWithRef<'div'>['ref'] })
    ) => ReturnType<typeof AvatarInner>

    const NextAvatar_ = React.forwardRef(AvatarInner) as <Role extends AvatarRoles>(
      props: AvatarProps<Role>
    ) => ReturnType<typeof AvatarInner>

    export const Icon = React.forwardRef<
      HTMLDivElement,
      DivAttributes & AvatarIconProps
    >(function Icon({ icon, ...props }, ref) {
      return (
        <StyledIconContainer ref={ref} {...props}>
          {icon}
        </StyledIconContainer>
      )
    })

    export const Label = React.forwardRef<
      HTMLDivElement,
      DivAttributes & AvatarLabelProps
    >(function Label({ children, ...props }, ref) {
      // Breaking change: Option to make aria-hidden since parent requires a label.
      // Can add in major once labelling is enforced
      return (
        <StyledLabelContainer ref={ref} {...props}>
          {children}
        </StyledLabelContainer>
      )
    })

    export const Portrait = React.forwardRef<
      HTMLDivElement,
      DivAttributes & AvatarPortraitProps
    >(function Portrait({ imageUrl, ...props }, ref) {
      return (
        <StyledPortraitContainer ref={ref} $imageUrl={imageUrl} {...props}>
          <StyledAvatarOverlay />
        </StyledPortraitContainer>
      )
    })

    // @ts-ignore
    Avatar_.displayName = 'Avatar'

    Icon.displayName = 'Avatar.Icon'

    Label.displayName = 'Avatar.Label'

    Portrait.displayName = 'Avatar.Portrait'

    // @ts-ignore
    NextAvatar_.displayName = 'Avatar'

    export const NextAvatar = addSubcomponents(
      {
        Icon,
        Label,
        Portrait,
      },
      NextAvatar_
    )

    export const Avatar = addSubcomponents(
      {
        Icon,
        Label,
        Portrait,
      },
      Avatar_
    )