All files / src/material-toolbox/snackbar index.js

89.36% Statements 42/47
78.38% Branches 29/37
78.57% Functions 11/14
89.13% Lines 41/46

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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184                  3x                                       3x                                 10x 10x 10x 10x         10x 9x   1x   28x     9x   9x   9x   9x       9x 9x     9x 9x 9x     9x 9x       10x 9x   9x 9x 9x 9x 9x 9x       10x 9x 1x       10x 9x 9x       10x 9x 1x       10x                 10x                 10x                                                                                                  
// @flow strict
import React, { useEffect, useRef, useReducer, useCallback } from 'react';
import cx from 'classnames';
 
import MDCSnackbarFoundation from '@material/snackbar/foundation';
import { announce } from '@material/snackbar/util';
 
import { mtClassNamesReducer } from '../reducers';
 
const noOp = () => {};
 
type Props = {
  buttonText?: string,
  className?: string,
  closeOnEscape?: boolean,
  dismissClassName?: string,
  dismissText?: string,
  label: string,
  leading?: boolean,
  onClosed?: (reason: string) => void,
  onClosing?: (reason: string) => void,
  onOpened?: () => void,
  onOpenning?: () => void,
  open?: boolean,
  stacked?: boolean,
  timeout?: number,
  withDismiss?: boolean,
};
 
const Snackbar = ({
  buttonText,
  className,
  closeOnEscape = true,
  dismissClassName,
  dismissText,
  label,
  leading = false,
  onClosed = noOp,
  onClosing = noOp,
  onOpened = noOp,
  onOpenning = noOp,
  open = false,
  stacked = false,
  timeout,
  withDismiss = false,
}: Props) => {
  const rootRef = useRef<?HTMLDivElement>();
  const labelRef = useRef<?HTMLDivElement>();
  const foundationRef = useRef<?MDCSnackbarFoundation>();
  const [mtClassName, mtClassNameDispatch] = useReducer(
    mtClassNamesReducer,
    new Set<string>(),
  );
 
  useEffect(() => {
    const foundation = new MDCSnackbarFoundation({
      addClass: mClassName =>
        mtClassNameDispatch({ type: 'add', className: mClassName }),
      removeClass: mClassName =>
        mtClassNameDispatch({ type: 'remove', className: mClassName }),
    });
 
    foundation.init();
 
    foundationRef.current = foundation;
 
    const root = rootRef.current;
 
    const handleKeyDown = (evt: Event) => {
      foundation.handleKeyDown(evt);
    };
 
    Eif (root) {
      root.addEventListener('keydown', handleKeyDown);
    }
 
    return () => {
      Eif (root) {
        root.removeEventListener('keydown', handleKeyDown);
      }
 
      foundation.destroy();
      foundationRef.current = null;
    };
  }, []);
 
  useEffect(() => {
    const foundation = foundationRef.current;
 
    Eif (foundation) {
      foundation.adapter_.announce = () => announce(labelRef.current);
      foundation.adapter_.notifyClosed = onClosed;
      foundation.adapter_.notifyClosing = onClosing;
      foundation.adapter_.notifyOpened = onOpened;
      foundation.adapter_.notifyOpening = onOpenning;
    }
  }, [label, onClosed, onClosing, onOpened, onOpenning]);
 
  useEffect(() => {
    if (foundationRef.current && timeout != null && timeout > 0) {
      foundationRef.current.setTimeoutMs(timeout);
    }
  }, [timeout]);
 
  useEffect(() => {
    Eif (foundationRef.current) {
      foundationRef.current.setCloseOnEscape(closeOnEscape);
    }
  }, [closeOnEscape]);
 
  useEffect(() => {
    if (foundationRef.current && open) {
      foundationRef.current.open();
    }
  }, [open]);
 
  const handleActionButtonClick = useCallback(
    (evt: SyntheticEvent<HTMLDivElement>) => {
      if (foundationRef.current) {
        foundationRef.current.handleActionButtonClick(evt);
      }
    },
    [],
  );
 
  const handleActionIconClick = useCallback(
    (evt: SyntheticEvent<HTMLDivElement>) => {
      if (foundationRef.current) {
        foundationRef.current.handleActionIconClick(evt);
      }
    },
    [],
  );
 
  return (
    <div
      ref={rootRef}
      className={cx('mdc-snackbar', className, ...Array.from(mtClassName), {
        'mdc-snackbar--stacked': stacked,
        'mdc-snackbar--leading': leading,
      })}
    >
      <div className="mdc-snackbar__surface">
        <div
          ref={labelRef}
          className="mdc-snackbar__label"
          role="status"
          aria-live="polite"
        >
          {label}
        </div>
        {(buttonText !== undefined || dismissText !== undefined) && (
          <div className="mdc-snackbar__actions">
            {buttonText !== undefined && (
              <button
                onClick={handleActionButtonClick}
                type="button"
                className="mdc-button mdc-snackbar__action"
              >
                {buttonText}
              </button>
            )}
            {withDismiss && (
              <button
                onClick={handleActionIconClick}
                type="button"
                className={cx(
                  'mdc-icon-button mdc-snackbar__dismiss',
                  dismissClassName,
                )}
                title="Dismiss"
              >
                {dismissText}
              </button>
            )}
          </div>
        )}
      </div>
    </div>
  );
};
 
export default Snackbar;