All files / src MoonLoader.tsx

100% Statements 40/40
100% Branches 15/15
100% Functions 7/7
100% Lines 29/29

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  1x 1x   1x   1x         1x       5x 1x   5x 20x   20x     5x 8x   8x             5x 4x   4x                 5x 4x   4x                     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, CalcFunction } from "./interfaces";
 
type BallStyleFunction = (size: number) => PrecompiledCss;
 
const moon: Keyframes = keyframes`
  100% {transform: rotate(360deg)}
`;
 
class Loader extends React.PureComponent<LoaderSizeProps> {
  public static defaultProps: LoaderSizeProps = sizeDefaults(60);
 
  public moonSize: CalcFunction<number> = (): number => {
    const { size } = this.props;
 
    return size! / 7;
  };
 
  public ballStyle: BallStyleFunction = (size: number): PrecompiledCss => {
    const { sizeUnit } = this.props;
 
    return css`
      width: ${`${size}${sizeUnit}`};
      height: ${`${size}${sizeUnit}`};
      border-radius: 100%;
    `;
  };
 
  public wrapper: StyleFunction = (): PrecompiledCss => {
    const { size, sizeUnit } = this.props;
 
    return css`
      position: relative;
      width: ${`${size! + this.moonSize() * 2}${sizeUnit}`};
      height: ${`${size! + this.moonSize() * 2}${sizeUnit}`};
      animation: ${moon} 0.6s 0s infinite linear;
      animation-fill-mode: forwards;
    `;
  };
 
  public ball: CalcFunction<PrecompiledCss> = (): PrecompiledCss => {
    const { color, size, sizeUnit } = this.props;
 
    return css`
      ${this.ballStyle(this.moonSize())};
      background-color: ${color};
      opacity: 0.8;
      position: absolute;
      top: ${`${size! / 2 - this.moonSize() / 2}${sizeUnit}`};
      animation: ${moon} 0.6s 0s infinite linear;
      animation-fill-mode: forwards;
    `;
  };
 
  public circle: CalcFunction<PrecompiledCss> = (): PrecompiledCss => {
    const { size, color } = this.props;
 
    return css`
      ${this.ballStyle(size!)};
      border: ${this.moonSize()}px solid ${color};
      opacity: 0.1;
    `;
  };
 
  public render(): JSX.Element | null {
    const { loading, css } = this.props;
 
    return loading ? (
      <div css={[this.wrapper(), css]}>
        <div css={this.ball()} />
        <div css={this.circle()} />
      </div>
    ) : null;
  }
}
 
const Component: React.ComponentClass<LoaderSizeProps> = onlyUpdateForKeys(sizeKeys)(Loader);
Component.defaultProps = Loader.defaultProps;
export default Component;