All files / src/material-toolbox/tab-indicator index.js

85.71% Statements 18/21
70% Branches 7/10
57.14% Functions 4/7
85.71% Lines 18/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 81 82 83 84 85 86 87 88 89 90 91 92 93                                          67x               447x 447x 447x 447x         447x 225x   225x 182x     225x   96x                 225x   225x   225x 225x     225x 225x       447x                                                
// @flow strict
import React, { useEffect, useRef, useReducer } from 'react';
import cx from 'classnames';
 
import MDCSlidingTabIndicatorFoundation from '@material/tab-indicator/sliding-foundation';
import MDCFadingTabIndicatorFoundation from '@material/tab-indicator/fading-foundation';
 
import type { Node } from 'react';
 
import { mtClassNamesReducer, mtStylesReducer } from '../reducers';
 
import type { Style, HTMLTabIndicatorElement } from '../mt-types';
 
type Props = {
  fade?: boolean,
  icon?: boolean,
  children?: Node,
  className?: string,
  style?: Style,
};
 
const TabIndicator = ({
  className,
  children,
  fade = false,
  icon = false,
  style,
  ...rest
}: Props) => {
  const rootRef = useRef<?HTMLSpanElement>();
  const contentRef = useRef<?HTMLSpanElement>();
  const [mtStyle, mtStyleDispatch] = useReducer(mtStylesReducer, {});
  const [mtClassName, mtClassNameDispatch] = useReducer(
    mtClassNamesReducer,
    new Set<string>(),
  );
 
  useEffect(() => {
    let Foundation = MDCSlidingTabIndicatorFoundation;
 
    if (fade) {
      Foundation = MDCFadingTabIndicatorFoundation;
    }
 
    const foundation = new Foundation({
      addClass: mClassName =>
        mtClassNameDispatch({ type: 'add', className: mClassName }),
      removeClass: mClassName =>
        mtClassNameDispatch({ type: 'remove', className: mClassName }),
      computeContentClientRect: () =>
        contentRef.current && contentRef.current.getBoundingClientRect(),
      setContentStyleProperty: (prop, value) =>
        mtStyleDispatch({ type: 'set', name: prop, value }),
    });
 
    foundation.init();
 
    Eif (rootRef.current) {
      // flowlint-next-line unclear-type:off
      const root: HTMLTabIndicatorElement = (rootRef.current: any);
      root.foundation = foundation;
    }
 
    return () => {
      foundation.destroy();
    };
  }, [fade]);
 
  return (
    <span
      {...rest}
      ref={rootRef}
      className={cx('mdc-tab-indicator', ...Array.from(mtClassName), {
        'mdc-tab-indicator--fade': fade,
      })}
    >
      <span
        aria-hidden={icon ? 'true' : undefined}
        style={mtStyle}
        ref={contentRef}
        className={cx('mdc-tab-indicator__content', {
          'mdc-tab-indicator__content--underline': !icon,
          'mdc-tab-indicator__content--icon': icon,
        })}
      >
        {children}
      </span>
    </span>
  );
};
 
export default TabIndicator;