All files / src/Button Button.tsx

100% Statements 15/15
87.5% Branches 7/8
100% Functions 3/3
100% Lines 15/15

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                                                                    32x                         323x 323x       323x   323x             323x             323x             323x             323x                 323x                     8x               323x                                   32x   283x 283x                  
import * as React from 'react';
import { Button as ReakitButton, ButtonProps as ReakitButtonProps, useButton as useReakitButton } from 'reakit';
import ConditionalWrap from 'conditional-wrap';
 
import { useClassName, createComponent, createElement, createHook } from '../utils';
import { ButtonKind, ButtonType, Omit, Size, Palette } from '../types';
import { Box, BoxProps } from '../Box';
import { Flex } from '../Flex';
import { Icon, IconProps } from '../Icon';
import { Text } from '../Text';
import { Spinner, SpinnerProps } from '../Spinner';
 
import * as styles from './styles';
 
export type LocalButtonProps = {
  /** Icon that appears on the right side of the button. */
  iconAfter?: IconProps['icon'];
  iconAfterProps?: Omit<IconProps, 'icon'>;
  /** Icon that appears on the left side of the button. */
  iconBefore?: IconProps['icon'];
  iconBeforeProps?: Omit<IconProps, 'icon'>;
  /** Adds a loading indicator to the button. */
  isLoading?: boolean;
  /** Makes the button not interactable. */
  isStatic?: boolean;
  kind?: ButtonKind;
  palette?: Palette;
  size?: Size;
  /** Custom props for the isLoading spinner. */
  spinnerProps?: SpinnerProps;
  type?: ButtonType;
};
export type ButtonProps = BoxProps & ReakitButtonProps & LocalButtonProps;
 
const useProps = createHook<ButtonProps>(
  (props, { themeKey, themeKeyOverride }) => {
    let {
      disabled,
      focusable,
      iconAfter,
      iconAfterProps,
      iconBefore,
      iconBeforeProps,
      spinnerProps,
      unstable_clickOnEnter,
      unstable_clickOnSpace,
      ...htmlProps
    } = props;
    const buttonProps = useReakitButton(
      { disabled, focusable, unstable_clickOnEnter, unstable_clickOnSpace },
      htmlProps
    );
    htmlProps = Box.useProps({ ...htmlProps, ...buttonProps });
 
    const className = useClassName({
      style: styles.Button,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: htmlProps.className
    });
    const iconBeforeClassName = useClassName({
      style: styles.ButtonIcon,
      styleProps: { ...props, isBefore: true },
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Icon'
    });
    const iconAfterClassName = useClassName({
      style: styles.ButtonIcon,
      styleProps: { ...props, isAfter: true },
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Icon'
    });
    const spinnerWrapperClassName = useClassName({
      style: styles.ButtonSpinnerWrapper,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'SpinnerWrapper'
    });
    const spinnerClassName = useClassName({
      style: styles.ButtonSpinner,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'Spinner'
    });
 
    const children = (
      <React.Fragment>
        {props.isLoading && (
          <Box className={spinnerWrapperClassName}>
            <Spinner
              use={Flex}
              className={spinnerClassName}
              color={props.kind === 'default' ? `${props.palette}Inverted` : props.palette}
              {...spinnerProps}
            />
          </Box>
        )}
        <ConditionalWrap condition={props.isLoading} wrap={children => <Text>{children}</Text>}>
          {iconBefore && <Icon className={iconBeforeClassName} icon={iconBefore} {...iconBeforeProps} />}
          {htmlProps.children}
          {iconAfter && <Icon className={iconAfterClassName} icon={iconAfter} {...iconAfterProps} />}
        </ConditionalWrap>
      </React.Fragment>
    );
 
    return { ...htmlProps, className, children };
  },
  {
    defaultProps: {
      disabled: false,
      iconAfter: undefined,
      iconBefore: undefined,
      isLoading: false,
      isStatic: false,
      kind: 'default',
      palette: 'default',
      size: 'default',
      type: 'button'
    },
    themeKey: 'Button'
  }
);
 
export const Button = createComponent<ButtonProps>(
  (props: ButtonProps) => {
    const buttonProps = useProps(props);
    return createElement({ children: props.children, component: ReakitButton, use: props.use, htmlProps: buttonProps });
  },
  {
    attach: {
      useProps
    },
    themeKey: 'Button'
  }
);