All files / src BarLoader.tsx

100% Statements 34/34
100% Branches 19/19
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 72 73 74 75 76 77 78 79 80 81  1x 1x   1x   1x               1x           1x           8x 1x   8x 14x   14x                                   8x 7x   7x                   1x 8x   8x             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, heightWidthDefaults, heightWidthKeys } from "./helpers";
import {
  LoaderHeightWidthProps,
  StyleFunction,
  PrecompiledCss,
  StyleFunctionWithIndex
} from "./interfaces";
 
const long: Keyframes = keyframes`
  0% {left: -35%;right: 100%}
  60% {left: 100%;right: -90%}
  100% {left: 100%;right: -90%}
`;
 
const short: Keyframes = keyframes`
  0% {left: -200%;right: 100%}
  60% {left: 107%;right: -8%}
  100% {left: 107%;right: -8%}
`;
 
export class Loader extends React.PureComponent<LoaderHeightWidthProps> {
  public static defaultProps: LoaderHeightWidthProps = heightWidthDefaults(4, 100);
 
  public style: StyleFunctionWithIndex = (i: number): PrecompiledCss => {
    const { height, color, heightUnit } = this.props;
 
    return css`
      position: absolute;
      height: ${`${height}${heightUnit}`};
      overflow: hidden;
      background-color: ${color};
      background-clip: padding-box;
      display: block;
      border-radius: 2px;
      will-change: left, right;
      animation-fill-mode: forwards;
      animation: ${i === 1 ? long : short} 2.1s ${i === 2 ? "1.15s" : ""}
        ${i === 1
          ? "cubic-bezier(0.65, 0.815, 0.735, 0.395)"
          : "cubic-bezier(0.165, 0.84, 0.44, 1)"}
        infinite;
    `;
  };
 
  public wrapper: StyleFunction = (): PrecompiledCss => {
    const { width, height, color, heightUnit, widthUnit } = this.props;
 
    return css`
      position: relative;
      width: ${`${width}${widthUnit}`};
      height: ${`${height}${heightUnit}`};
      overflow: hidden;
      background-color: ${calculateRgba(color!, 0.2)};
      background-clip: padding-box;
    `;
  };
 
  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<LoaderHeightWidthProps> = onlyUpdateForKeys(heightWidthKeys)(
  Loader
);
Component.defaultProps = Loader.defaultProps;
export default Component;