All files / src/Spinner Spinner.tsx

100% Statements 11/11
100% Branches 2/2
100% Functions 2/2
100% Lines 11/11

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                                                      33x   36x   36x             36x             36x             36x                 36x               36x         33x   35x 35x                  
import * as React from 'react';
import { Box as ReakitBox } from 'reakit';
 
import { Size } from '../types';
import { useClassName, createComponent, createElement, createHook } from '../utils';
import { Box, BoxProps } from '../Box';
 
import * as styles from './styles';
 
export type LocalSpinnerProps = {
  /** Color of the spinner. */
  color?: string;
  /** Duration (or speed) of the spinner animation. */
  duration?: string;
  /** Adds a background 'track' to the spinner. */
  hasTrack?: boolean;
  /** The color of the track. */
  trackColor?: string;
  /** Size of the spinner. */
  size?: Size;
  /** Length of the spinner's perimeter as a percentage. */
  perimeter?: string;
  /** Progress value of the spinner. */
  value?: number;
};
export type SpinnerProps = BoxProps & LocalSpinnerProps;
 
const useProps = createHook<SpinnerProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const boxProps = Box.useProps(props);
 
    const className = useClassName({
      style: styles.SpinnerWrapper,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
    const spinnerClassName = useClassName({
      style: styles.Spinner,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'vector'
    });
    const trackCircleClassName = useClassName({
      style: styles.TrackCircle,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'trackCircle'
    });
    const loaderCircleClassName = useClassName({
      style: styles.LoaderCircle,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'loaderCircle'
    });
 
    const children = (
      <svg viewBox="22 22 44 44" className={spinnerClassName}>
        {props.hasTrack && (
          <circle cx="44" cy="44" r="20" fill="none" strokeWidth="4" className={trackCircleClassName} />
        )}
        <circle cx="44" cy="44" r="20" fill="none" strokeWidth="4" className={loaderCircleClassName} />
      </svg>
    );
 
    return { ...boxProps, className, children };
  },
  { defaultProps: { color: 'primary', duration: '0.6s', perimeter: '60%' }, themeKey: 'Spinner' }
);
 
export const Spinner = createComponent<SpinnerProps>(
  props => {
    const spinnerProps = useProps(props);
    return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: spinnerProps });
  },
  {
    attach: {
      useProps
    },
    themeKey: 'Spinner'
  }
);