All files / src/material-toolbox/top-app-bar index.js

93.02% Statements 40/43
100% Branches 29/29
78.57% Functions 11/14
92.86% Lines 39/42

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                                                6x                     25x 25x 25x 25x         25x 19x       19x   4x   4x       15x   20x       15x   4x       19x 4x 15x 4x   11x         19x       19x   19x   19x 11x       11x     19x   19x 19x   19x   19x 11x     19x 19x       25x 16x   16x 3x       25x         25x                                          
// @flow strict
import React, { useEffect, useMemo, useRef, useReducer } from 'react';
import cx from 'classnames';
 
import { strings } from '@material/top-app-bar/constants';
import MDCTopAppBarFoundation from '@material/top-app-bar/standard/foundation';
import MDCShortTopAppBarFoundation from '@material/top-app-bar/short/foundation';
import MDCFixedTopAppBarFoundation from '@material/top-app-bar/fixed/foundation';
 
import { mtStylesReducer, mtClassNamesReducer } from '../reducers';
 
import type { Style } from '../mt-types';
 
type Props = {
  style?: Style,
  className?: string,
  fixed?: boolean,
  short?: boolean,
  collapsed?: boolean,
  prominent?: boolean,
  dense?: boolean,
  scrollTarget?: ?HTMLElement,
};
 
const TopAppBar = ({
  dense = false,
  short = false,
  collapsed,
  fixed = false,
  prominent = false,
  style,
  className,
  scrollTarget,
  ...rest
}: Props) => {
  const rootEl = useRef<?HTMLElement>();
  const foundationRef = useRef();
  const [mtStyle, distpachMTStyle] = useReducer(mtStylesReducer, {});
  const [mtClassName, dispatchClassName] = React.useReducer(
    mtClassNamesReducer,
    new Set<string>(),
  );
 
  useEffect(() => {
    const scrollTargetEl = scrollTarget || window;
 
    let mdcFoundation;
 
    const currentAdapter = {
      hasClass: mClassName =>
        rootEl.current && rootEl.current.classList.contains(mClassName),
      addClass: (mClassName: string): void =>
        dispatchClassName({ type: 'add', className: mClassName }),
      removeClass: (mClassName: string): void =>
        dispatchClassName({ type: 'remove', className: mClassName }),
      setStyle: (property, value) =>
        distpachMTStyle({ type: 'set', name: property, value }),
      getViewportScrollY: () => {
        return scrollTargetEl.pageYOffset !== undefined
          ? scrollTargetEl.pageYOffset
          : scrollTargetEl.scrollTop;
      },
      getTopAppBarHeight: () => rootEl.current && rootEl.current.clientHeight,
      getTotalActionItems: () =>
        rootEl.current &&
        rootEl.current.querySelectorAll(strings.ACTION_ITEM_SELECTOR).length,
    };
 
    if (short) {
      mdcFoundation = new MDCShortTopAppBarFoundation(currentAdapter);
    } else if (fixed) {
      mdcFoundation = new MDCFixedTopAppBarFoundation(currentAdapter);
    } else {
      mdcFoundation = new MDCTopAppBarFoundation(currentAdapter);
    }
 
    let handleWindowResize_;
 
    const handleTargetScroll_ = (...handlerRest) => {
      mdcFoundation.handleTargetScroll(...handlerRest);
    };
 
    scrollTargetEl.addEventListener('scroll', handleTargetScroll_);
 
    mdcFoundation.init();
 
    if (!short && !fixed) {
      handleWindowResize_ = (...handlerRest) => {
        mdcFoundation.handleWindowResize(...handlerRest);
      };
 
      window.addEventListener('resize', handleWindowResize_);
    }
 
    foundationRef.current = mdcFoundation;
 
    return () => {
      dispatchClassName({ type: 'reset', className: '' });
 
      scrollTargetEl.removeEventListener('scroll', handleTargetScroll_);
 
      if (handleWindowResize_) {
        window.removeEventListener('resize', handleWindowResize_);
      }
 
      mdcFoundation.destroy();
      foundationRef.current = null;
    };
  }, [fixed, short, prominent, dense, scrollTarget]);
 
  useEffect(() => {
    const foundation = foundationRef.current;
 
    if (foundation && collapsed != null && foundation.setAlwaysCollapsed) {
      foundation.setAlwaysCollapsed(collapsed);
    }
  }, [collapsed]);
 
  const memoizedStyle = useMemo(() => ({ ...style, ...mtStyle }), [
    mtStyle,
    style,
  ]);
 
  return (
    <header
      {...rest}
      ref={rootEl}
      style={memoizedStyle}
      className={cx(
        'mdc-top-app-bar',
        {
          'mdc-top-app-bar--dense': dense,
          'mdc-top-app-bar--short': short,
          'mdc-top-app-bar--fixed': fixed,
          'mdc-top-app-bar--prominent': prominent,
        },
        ...Array.from(mtClassName),
        className,
      )}
    />
  );
};
 
export default TopAppBar;