All files / src RingLoader.tsx

100% Statements 30/30
100% Branches 15/15
100% Functions 4/4
100% Lines 21/21

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  1x 1x   1x   1x               1x         1x         5x 1x   5x 8x   8x                             5x 4x   4x             1x 5x   5x             1x   1x 1x 1x  
/** @jsx jsx */
import * as React from "react";
import { keyframes, css, jsx } from "@emotion/core";
import { Keyframes } from "@emotion/serialize";
import onlyUpdateForKeys from "recompose/onlyUpdateForKeys";
 
import { sizeDefaults, sizeKeys } from "./helpers";
import {
  StyleFunction,
  PrecompiledCss,
  LoaderSizeProps,
  StyleFunctionWithIndex
} from "./interfaces";
 
const right: Keyframes = keyframes`
  0% {transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg)}
  100% {transform: rotateX(180deg) rotateY(360deg) rotateZ(360deg)}
`;
 
const left: Keyframes = keyframes`
  0% {transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg)}
  100% {transform: rotateX(360deg) rotateY(180deg) rotateZ(360deg)}
`;
 
class Loader extends React.PureComponent<LoaderSizeProps> {
  public static defaultProps: LoaderSizeProps = sizeDefaults(60);
 
  public style: StyleFunctionWithIndex = (i: number): PrecompiledCss => {
    const { size, sizeUnit, color } = this.props;
 
    return css`
      position: absolute;
      top: 0;
      left: 0;
      width: ${`${size}${sizeUnit}`};
      height: ${`${size}${sizeUnit}`};
      border: ${`${size! / 10}${sizeUnit}`} solid ${color};
      opacity: 0.4;
      border-radius: 100%;
      animation-fill-mode: forwards;
      perspective: 800px;
      animation: ${i === 1 ? right : left} 2s 0s infinite linear;
    `;
  };
 
  public wrapper: StyleFunction = (): PrecompiledCss => {
    const { size, sizeUnit } = this.props;
 
    return css`
      width: ${`${size}${sizeUnit}`};
      height: ${`${size}${sizeUnit}`};
      position: relative;
    `;
  };
 
  public render(): JSX.Element | null {
    const { loading, css } = this.props;
 
    return loading ? (
      <div css={[this.wrapper(), css]}>
        <div css={this.style(1)} />
        <div css={this.style(2)} />
      </div>
    ) : null;
  }
}
 
const Component: React.ComponentClass<LoaderSizeProps> = onlyUpdateForKeys(sizeKeys)(Loader);
Component.defaultProps = Loader.defaultProps;
export default Component;