All files / src/material-toolbox/linear-progress index.js

93.55% Statements 29/31
69.23% Branches 9/13
91.67% Functions 11/12
93.55% Lines 29/31

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                                  2x                 16x 16x 16x 16x   16x         16x 8x     6x 6x   16x   8x   12x     8x 8x   8x 8x 8x       16x 8x 8x       16x 8x 8x       16x 8x 8x     8x         16x                                                    
// @flow strict
import React, { useEffect, useRef, useReducer } from 'react';
import cx from 'classnames';
 
import MDCLinearProgressFoundation from '@material/linear-progress/foundation';
 
import { mtClassNamesReducer } from '../reducers';
 
type Props = {
  close?: boolean,
  progress?: number,
  buffer?: number,
  indeterminate?: boolean,
  reversed?: boolean,
  className?: string,
};
 
const LinearProgress = ({
  close = false,
  indeterminate = false,
  reversed = false,
  progress,
  buffer,
  className,
  ...rest
}: Props) => {
  const foundationRef = useRef<?MDCLinearProgressFoundation>();
  const rootRef = useRef<?HTMLDivElement>();
  const bufferRef = useRef<?HTMLDivElement>();
  const primaryBarRef = useRef<?HTMLDivElement>();
 
  const [mtClassName, mtClassNameDispatch] = useReducer(
    mtClassNamesReducer,
    new Set<string>(),
  );
 
  useEffect(() => {
    const foundation = new MDCLinearProgressFoundation({
      addClass: mClassName =>
        mtClassNameDispatch({ type: 'add', className: mClassName }),
      getBuffer: () => bufferRef.current,
      getPrimaryBar: () => primaryBarRef.current,
      hasClass: mClassName =>
        rootRef.current && rootRef.current.classList.contains(mClassName),
      removeClass: mClassName =>
        mtClassNameDispatch({ type: 'remove', className: mClassName }),
      setStyle: (el: HTMLElement, styleProperty: string, value: string) =>
        el.style.setProperty(styleProperty, value),
    });
 
    foundation.init();
    foundationRef.current = foundation;
 
    return () => {
      foundation.destroy();
      foundationRef.current = null;
    };
  }, [reversed, indeterminate]);
 
  useEffect(() => {
    Eif (foundationRef.current) {
      foundationRef.current.setBuffer(buffer);
    }
  }, [buffer]);
 
  useEffect(() => {
    Eif (foundationRef.current) {
      foundationRef.current.setProgress(progress);
    }
  }, [progress]);
 
  useEffect(() => {
    Eif (foundationRef.current) {
      Iif (close) {
        foundationRef.current.close();
      } else {
        foundationRef.current.open();
      }
    }
  }, [close]);
 
  return (
    <div
      {...rest}
      ref={rootRef}
      role="progressbar"
      className={cx('mdc-linear-progress', ...Array.from(mtClassName), {
        'mdc-linear-progress--indeterminate': indeterminate,
        'mdc-linear-progress--reversed': reversed,
      })}
    >
      <div className="mdc-linear-progress__buffering-dots" />
      <div ref={bufferRef} className="mdc-linear-progress__buffer" />
      <div
        ref={primaryBarRef}
        className="mdc-linear-progress__bar mdc-linear-progress__primary-bar"
      >
        <span className="mdc-linear-progress__bar-inner" />
      </div>
      <div className="mdc-linear-progress__bar mdc-linear-progress__secondary-bar">
        <span className="mdc-linear-progress__bar-inner" />
      </div>
    </div>
  );
};
 
export default LinearProgress;