# All files / src/Avatar Avatar.coverage.stories.tsx

100% Statements 39/39
84.61% Branches 11/13
100% Functions 9/9
100% Lines 34/34

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
    193
    194
    195
    1961x
    1x
    1x
    1x
    1x

    1x

    1x

    1x
    2x

    1x
    1x

    1x

    9x
    9x

    9x

    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x

    1x
    1x

    3x

    24x

    24x

    24x
    12x

    12x

     import { Cog, Help, Person } from '@procore/core-icons'
    import React from 'react'
    import { FlexList } from '../FlexList/FlexList'
    import { avatarImageUrl } from '../_storyHelpers_/constants'
    import { Avatar } from './Avatar'

    const title = 'Coverage/Avatar'

    export default {
      component: Avatar,
      title,
    }

    export const Basics = () => {
      const ref = React.useRef<HTMLAnchorElement>(null)

      React.useEffect(() => {
        ref?.current?.focus()
      }, [])

      return (
        <FlexList direction="column">
          <Avatar aria-hidden>
            <Avatar.Label>md</Avatar.Label>
          </Avatar>
          <Avatar aria-hidden>
            <Avatar.Icon icon={<Person />} />
          </Avatar>
          <Avatar aria-hidden>
            <Avatar.Portrait imageUrl={avatarImageUrl} />
          </Avatar>
          <Avatar role="button" onPress={console.log} aria-label="Example button">
            <Avatar.Label>btn</Avatar.Label>
          </Avatar>
          <Avatar role="link" href="#" aria-label="Example link">
            <Avatar.Label>link</Avatar.Label>
          </Avatar>
          <a href="#" aria-label="Example link wrap">
            <Avatar aria-hidden>
              <Avatar.Label>a</Avatar.Label>
            </Avatar>
          </a>
          <a href="#" aria-label="Example link wrap focused" ref={ref}>
            <Avatar aria-hidden>
              <Avatar.Label>aaf</Avatar.Label>
            </Avatar>
          </a>
        </FlexList>
      )
    }

    function CoverageBase({ autoFocus, ...p }: any) {
      const ref = React.useRef(null)

      return (
        <div>
          <h2>size: {p.size}</h2>
          Base {autoFocus ? 'autoFocus ' : ''}
          <Avatar
            ref={ref}
            {...p}
            autoFocus={autoFocus}
            aria-label={autoFocus ? 'Load as focused' : 'Example'}
          >
            <Avatar.Label>A</Avatar.Label>
          </Avatar>
          <br />
          Default:
          <FlexList space="md">
            <Avatar {...p} aria-label="Default Portrait">
              <Avatar.Portrait imageUrl={avatarImageUrl} />
            </Avatar>
            <Avatar {...p} aria-label="Default Icon">
              <Avatar.Icon icon={<Cog />} />
            </Avatar>
            <Avatar {...p} aria-label="Default Label">
              <Avatar.Label>BB</Avatar.Label>
            </Avatar>
          </FlexList>
          Disabled:
          <FlexList space="md">
            <Avatar disabled aria-label="Disabled Portrait" {...p}>
              <Avatar.Portrait imageUrl={avatarImageUrl} />
            </Avatar>
            <Avatar disabled aria-label="Default Icon" {...p}>
              <Avatar.Icon icon={<Cog />} />
            </Avatar>
            <Avatar disabled aria-label="Default Label" {...p}>
              <Avatar.Label>CCC</Avatar.Label>
            </Avatar>
          </FlexList>
          <br />
        </div>
      )
    }

    export function ButtonAvatar() {
      const base = { role: 'button', onPress: console.log }
      return (
        <>
          <CoverageBase {...base} size="md" autoFocus />
          <CoverageBase {...base} size="lg" />
          <CoverageBase {...base} size="xl" />
        </>
      )
    }

    export function LinkAvatar() {
      const base = { role: 'link', href: '#' }
      return (
        <>
          <CoverageBase {...base} size="md" autoFocus />
          <CoverageBase {...base} size="lg" />
          <CoverageBase {...base} size="xl" />
        </>
      )
    }

    export function ImageAvatar() {
      const base = { role: 'img' }
      return (
        <>
          <CoverageBase {...base} size="md" autoFocus />
          <CoverageBase {...base} size="lg" />
          <CoverageBase {...base} size="xl" />
        </>
      )
    }

    export const Variations = () => {
      const variations = [
        { disabled: true, clickable: true, icon: true },
        { disabled: false, clickable: true, icon: true },
        { disabled: true, clickable: false, icon: true },
        { disabled: true, clickable: true, icon: false },
        { disabled: false, clickable: false, icon: true },
        { disabled: false, clickable: true, icon: false },
        { disabled: true, clickable: false, icon: false },
        { disabled: false, clickable: false, icon: false },
      ]
      const sizes = ['md', 'lg', 'xl'] as const
      return (
        <FlexList direction="column">
          {sizes.map((size) => {
            return (
              <FlexList direction="column" key={size}>
                {variations.map(({ disabled, clickable, icon }, index) => {
                  const debug = (
                    <pre>{JSON.stringify({ disabled, clickable, size })}</pre>
                  )

                  if (icon) {
                    return (
                      <React.Fragment key={index}>
                        {debug}
                        <FlexList>
                          <Avatar
                            disabled={disabled}
                            clickable={clickable}
                            size={size}
                          >
                            <Avatar.Icon icon={<Help />} />
                          </Avatar>
                          <Avatar
                            disabled={disabled}
                            clickable={clickable}
                            size={size}
                          >
                            <Avatar.Icon icon={<Cog />} />
                          </Avatar>
                        </FlexList>
                      </React.Fragment>
                    )
                  }

                  return (
                    <React.Fragment key={index}>
                      {debug}
                      <Avatar
                        aria-hidden
                        disabled={disabled}
                        clickable={clickable}
                        size={size}
                      >
                        <Avatar.Portrait imageUrl={avatarImageUrl} />
                      </Avatar>
                    </React.Fragment>
                  )
                })}
              </FlexList>
            )
          })}
        </FlexList>
      )
    }