All files / src HashLoader.tsx

100% Statements 56/56
100% Branches 15/15
100% Functions 10/10
100% Lines 43/43

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  1x 1x   1x   1x                 4x 1x   4x 24x   24x     4x 12x   12x     6x   4x 6x   6x     4x 3x   3x 3x 3x 3x   3x               4x 3x   3x 3x 3x 3x   3x               4x 6x   6x                             4x 3x   3x               1x 4x   4x             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 { calculateRgba, sizeDefaults, sizeKeys } from "./helpers/index";
import {
  StyleFunction,
  PrecompiledCss,
  LoaderSizeProps,
  CalcFunction,
  StyleFunctionWithIndex
} from "./interfaces";
 
class Loader extends React.PureComponent<LoaderSizeProps> {
  public static defaultProps: LoaderSizeProps = sizeDefaults(50);
 
  public thickness: CalcFunction<number> = (): number => {
    const { size } = this.props;
 
    return size! / 5;
  };
 
  public lat: CalcFunction<number> = (): number => {
    const { size } = this.props;
 
    return (size! - this.thickness()) / 2;
  };
 
  public offset: CalcFunction<number> = (): number => this.lat() - this.thickness();
 
  public color: CalcFunction<string> = (): string => {
    const { color } = this.props;
 
    return calculateRgba(color!, 0.75);
  };
 
  public before: CalcFunction<Keyframes> = (): Keyframes => {
    const { size, sizeUnit } = this.props;
 
    const color: string = this.color();
    const lat: number = this.lat();
    const thickness: number = this.thickness();
    const offset: number = this.offset();
 
    return keyframes`
      0% {width: ${thickness}px;box-shadow: ${lat}px ${-offset}px ${color}, ${-lat}px ${offset}px ${color}}
      35% {width: ${`${size}${sizeUnit}`};box-shadow: 0 ${-offset}px ${color}, 0 ${offset}px ${color}}
      70% {width: ${thickness}px;box-shadow: ${-lat}px ${-offset}px ${color}, ${lat}px ${offset}px ${color}}
      100% {box-shadow: ${lat}px ${-offset}px ${color}, ${-lat}px ${offset}px ${color}}
    `;
  };
 
  public after: CalcFunction<Keyframes> = (): Keyframes => {
    const { size, sizeUnit } = this.props;
 
    const color: string = this.color();
    const lat: number = this.lat();
    const thickness: number = this.thickness();
    const offset: number = this.offset();
 
    return keyframes`
      0% {height: ${thickness}px;box-shadow: ${offset}px ${lat}px ${color}, ${-offset}px ${-lat}px ${color}}
      35% {height: ${`${size}${sizeUnit}`};box-shadow: ${offset}px 0 ${color}, ${-offset}px 0 ${color}}
      70% {height: ${thickness}px;box-shadow: ${offset}px ${-lat}px ${color}, ${-offset}px ${lat}px ${color}}
      100% {box-shadow: ${offset}px ${lat}px ${color}, ${-offset}px ${-lat}px ${color}}
    `;
  };
 
  public style: StyleFunctionWithIndex = (i: number): PrecompiledCss => {
    const { size, sizeUnit } = this.props;
 
    return css`
      position: absolute;
      content: "";
      top: 50%;
      left: 50%;
      display: block;
      width: ${`${size! / 5}${sizeUnit}`};
      height: ${`${size! / 5}${sizeUnit}`};
      border-radius: ${`${size! / 10}${sizeUnit}`};
      transform: translate(-50%, -50%);
      animation-fill-mode: none;
      animation: ${i === 1 ? this.before() : this.after()} 2s infinite;
    `;
  };
 
  public wrapper: StyleFunction = (): PrecompiledCss => {
    const { size, sizeUnit } = this.props;
 
    return css`
      position: relative;
      width: ${`${size}${sizeUnit}`};
      height: ${`${size}${sizeUnit}`};
      transform: rotate(165deg);
    `;
  };
 
  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;