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

100% Statements 8/8
100% Branches 14/14
100% Functions 4/4
100% Lines 8/8

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                                    33x           140x   83x                                     33x                 140x         140x 140x 140x          
// @flow strict
import React, { useMemo } from 'react';
import cx from 'classnames';
 
import type { Node } from 'react';
 
type useProps = {
  background?: 'primary' | 'secondary' | 'background' | 'surface',
  color?:
    | 'primary'
    | 'secondary'
    | 'on-primary'
    | 'on-secondary'
    | 'on-surface',
  on?: 'light' | 'dark',
  text?: 'primary' | 'secondary' | 'hint' | 'disabled' | 'icon',
};
 
const useTheme = ({
  background = '',
  color = '',
  on = '',
  text = '',
}: useProps) => {
  return useMemo(
    () =>
      cx({
        [`mdc-theme--${background}`]:
          background === 'background' || background === 'surface',
        [`mdc-theme--${background}-bg`]:
          background === 'primary' || background === 'secondary',
        [`mdc-theme--${color}`]: color !== '' && on === '' && text === '',
        [`mdc-theme--text-${text}-on-${on}`]:
          on !== '' && text !== '' && color === '',
      }),
    [background, color, on, text],
  );
};
 
type Props = {
  ...useProps,
  children?: Node,
  className?: string,
};
 
const Theme = ({
  background,
  children,
  className,
  color,
  on,
  text,
  ...rest
}: Props) => {
  const composedClassName = cx(
    className,
    useTheme({ background, color, on, text }),
  );
 
  return React.Children.map(children, child => {
    const newClassName = cx(composedClassName, child.props.className);
    return <child.type {...child.props} {...rest} className={newClassName} />;
  });
};
 
export default Theme;