All files / src/material-toolbox/icon-button index.js

91.67% Statements 33/36
76% Branches 19/25
76.92% Functions 10/13
91.43% Lines 32/35

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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134                                                5x                     36x 36x   36x         36x         36x   36x 18x 11x     7x   21x       7x   14x     7x 7x   7x     7x   7x 7x     7x 7x 7x     7x 7x       36x 18x 7x       36x 18x 7x         36x         36x                                                      
// @flow strict
import React, { useRef, useMemo, useEffect, useReducer } from 'react';
import cx from 'classnames';
 
import MDCIconButtonToggleFoundation from '@material/icon-button/foundation';
 
import type { ElementType } from 'react';
 
import { useRipple } from '../ripple';
import { mtAttrsReducer, mtClassNamesReducer } from '../reducers';
 
import type { Style } from '../mt-types';
 
type Props = {
  as?: ElementType,
  on?: boolean,
  offLabel?: string,
  onLabel?: string,
  toggle?: boolean,
  onChange?: ({ isOn: boolean }) => void,
  className?: string,
  style?: Style,
};
 
const IconButton = ({
  as: Component = 'button',
  on = false,
  toggle = false,
  offLabel,
  onLabel,
  onChange,
  className,
  style,
  ...rest
}: Props) => {
  const rootRef = useRef();
  const foundationRef = useRef<?MDCIconButtonToggleFoundation>();
 
  const { rippleStyle, rippleClassName } = useRipple({
    activator: rootRef,
    unbounded: true,
  });
 
  const [mtClassName, mtClassNameDispatch] = useReducer(
    mtClassNamesReducer,
    new Set<string>(),
  );
 
  const [mtAttrs, mtAttrsDispatch] = useReducer(mtAttrsReducer, {});
 
  useEffect(() => {
    if (!toggle) {
      return () => {};
    }
 
    const foundation = new MDCIconButtonToggleFoundation({
      hasClass: mClassName =>
        rootRef.current && rootRef.current.classList.contains(mClassName),
      addClass: mClassName =>
        mtClassNameDispatch({ type: 'add', className: mClassName }),
      removeClass: mClassName =>
        mtClassNameDispatch({ type: 'remove', className: mClassName }),
      setAttr: (attrName, attrValue) =>
        mtAttrsDispatch({ type: 'set', name: attrName, value: attrValue }),
    });
 
    foundationRef.current = foundation;
    foundation.init();
 
    const handleClick = (...handleClickRest) =>
      foundation.handleClick(...handleClickRest);
 
    const root = rootRef.current;
 
    Eif (root) {
      root.addEventListener('click', handleClick);
    }
 
    return () => {
      Eif (root) {
        root.removeEventListener('click', handleClick);
      }
 
      foundation.destroy();
      foundationRef.current = null;
    };
  }, [toggle]);
 
  useEffect(() => {
    if (foundationRef.current) {
      foundationRef.current.toggle(on);
    }
  }, [on]);
 
  useEffect(() => {
    if (foundationRef.current) {
      foundationRef.current.adapter_.notifyChange = (...notifyChangeRest) =>
        onChange && onChange(...notifyChangeRest);
    }
  }, [onChange]);
 
  const memoizedStyle = useMemo(() => ({ ...style, ...rippleStyle }), [
    style,
    rippleStyle,
  ]);
 
  return (
    <Component
      {...(rest: mixed)}
      ref={rootRef}
      style={memoizedStyle}
      aria-label={
        foundationRef.current && foundationRef.current.isOn()
          ? onLabel
          : offLabel
      }
      aria-hidden={
        foundationRef.current && foundationRef.current.isOn()
          ? 'true'
          : undefined
      }
      {...mtAttrs}
      className={cx(
        'mdc-icon-button',
        className,
        ...Array.from(rippleClassName),
        ...Array.from(mtClassName),
      )}
    />
  );
};
 
export default IconButton;