All files / src/material-toolbox/line-ripple index.js

81.82% Statements 18/22
62.5% Branches 5/8
60% Functions 6/10
80.95% Lines 17/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                                      10x   79x 79x 79x         79x 40x                           40x   40x       40x     40x   40x 40x     40x 40x         79x   79x                   10x      
// @flow strict
import React, {
  useRef,
  useReducer,
  useImperativeHandle,
  useEffect,
} from 'react';
import cx from 'classnames';
 
import MDCLineRippleFoundation from '@material/line-ripple/foundation';
 
import { normalizePropToReactStyle } from '../utils';
 
import { mtClassNamesReducer, mtStylesReducer } from '../reducers';
 
import type { HTMLLineRippleElement } from '../mt-types';
 
type Prop = {};
 
const LineRipple = React.forwardRef<Prop, HTMLLineRippleElement>(
  (props, ref) => {
    const rootRef = useRef<?HTMLDivElement>();
    const [mtStyle, mtStyleDispatch] = useReducer(mtStylesReducer, {});
    const [mtClassName, mtClassNameDispatch] = useReducer(
      mtClassNamesReducer,
      new Set<string>(),
    );
 
    useEffect(() => {
      const foundation = new MDCLineRippleFoundation({
        addClass: mClassName =>
          mtClassNameDispatch({ type: 'add', className: mClassName }),
        removeClass: mClassName =>
          mtClassNameDispatch({ type: 'remove', className: mClassName }),
        hasClass: className =>
          rootRef.current && rootRef.current.classList.contains(className),
        setStyle: (propertyName, value) =>
          mtStyleDispatch({
            type: 'set',
            name: normalizePropToReactStyle(propertyName),
            value,
          }),
        registerEventHandler: (evtType, handler) =>
          rootRef.current && rootRef.current.addEventListener(evtType, handler),
        deregisterEventHandler: (evtType, handler) =>
          rootRef.current &&
          rootRef.current.removeEventListener(evtType, handler),
      });
 
      foundation.init();
 
      // flowlint-next-line unclear-type:off
      const root: HTMLLineRippleElement = (rootRef.current: any);
 
      Eif (root) {
        root.foundation = foundation;
      }
 
      return () => {
        foundation.destroy();
      };
    }, []);
 
    // flowlint-next-line unclear-type:off
    useImperativeHandle(ref, () => (rootRef.current: any), []);
 
    return (
      <div
        style={mtStyle}
        ref={rootRef}
        className={cx('mdc-line-ripple', ...Array.from(mtClassName))}
      />
    );
  },
);
 
LineRipple.displayName = 'LineRipple';
 
export default LineRipple;