# All files / src/Button Button.tsx

92.45% Statements 49/53
83.72% Branches 36/43
80% Functions 4/5
89.74% Lines 35/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
    15565x
    65x
    65x
    65x

    988x
    988x

    301x

    65x

    927x
    927x
    927x
    927x
    927x
    927x
    927x
    927x
    927x
    927x

    927x

    65x

    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x

    61x

    61x

    61x

    61x
    61x

    61x

     import React, { FunctionComponent, PropsWithChildren } from 'react'
    import { useEventListener } from '../_hooks/EventListener'
    import { mergeRefs } from '../_utils/mergeRefs'
    import {
      StyledButton,
      StyledContent,
      StyledLabel,
      StyledLinkButton,
      StyledSpinner,
    } from './Button.styles'
    import { ButtonProps, LinkButtonProps } from './Button.types'

    const ButtonContent: FunctionComponent<
      PropsWithChildren<
        Pick<ButtonProps, 'icon' | 'iconRight' | 'size' | 'variant'>
      >
    > = ({ children, icon, iconRight, size, variant }) => {
      return (
        <>
          <StyledContent>
            {React.isValidElement<{ ['data-icon-prop']: string }>(icon)
              ? React.cloneElement(icon, { ['data-icon-prop']: 'true' })
              : null}
            {React.Children.map(children, (child) => {
              return <StyledLabel>{child}</StyledLabel>
            })}
            {React.isValidElement<{ ['data-icon-prop']: string }>(iconRight)
              ? React.cloneElement(iconRight, { ['data-icon-prop']: 'true' })
              : null}
          </StyledContent>
          <StyledSpinner
            size={size === 'lg' ? 'md' : 'sm'}
            color={variant === 'primary' ? 'white' : 'gray15'}
          />
        </>
      )
    }

    /**

     We use buttons to link to other pages or to trigger an action.
     Buttons clearly define what happens when a user clicks it.

     @since 10.19.0

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

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

     */
    export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
      function Button(
        {
          block = false,
          children,
          disabled = false,
          icon,
          iconRight,
          loading = false,
          size = 'md',
          type = 'button',
          variant = 'primary',
          ...props
        },
        ref
      ) {
        return (
          <StyledButton
            aria-busy={loading || undefined}
            data-a11y-skip={variant === 'primary' ? 'color-contrast' : undefined}
            tabIndex={0}
            {...props}
            $block={block}
            disabled={disabled || loading}
            $loading={loading}
            $size={size}
            $variant={variant}
            type={type}
            ref={ref}
          >
            <ButtonContent
              icon={icon}
              iconRight={iconRight}
              size={size}
              variant={variant}
            >
              {children}
            </ButtonContent>
          </StyledButton>
        )
      }
    )

    export const LinkButton = React.forwardRef<HTMLAnchorElement, LinkButtonProps>(
      function LinkButton(
        {
          block = false,
          children,
          disabled: disabled_,
          href: href_,
          icon,
          iconRight,
          size = 'md',
          variant = 'primary',
          ...props
        },
        ref
      ) {
        const innerRef = React.useRef<HTMLAnchorElement>(null)

        const mimicButtonRole = (e: React.KeyboardEvent<HTMLAnchorElement>) => {
          const { key } = e

          Iif (['Enter', 'Spacebar', ' '].indexOf(key) >= 0) {
            // Prevents the page to scroll when "space" is pressed
            e.preventDefault()
            innerRef.current?.click()
          }
        }

        useEventListener({
          event: 'keydown',
          handler: mimicButtonRole,
          scope: innerRef,
        })

        const disabled = disabled_ || !href_
        const href = disabled ? undefined : href_

        return (
          <StyledLinkButton
            data-a11y-skip={variant === 'primary' ? 'color-contrast' : undefined}
            {...props}
            role="button"
            aria-disabled={disabled}
            href={href}
            $disabled={disabled}
            $block={block}
            $size={size}
            $variant={variant}
            ref={mergeRefs(ref, innerRef)}
          >
            <ButtonContent
              icon={icon}
              iconRight={iconRight}
              size={size}
              variant={variant}
            >
              {children}
            </ButtonContent>
          </StyledLinkButton>
        )
      }
    )