All files / src/Card Card.tsx

100% Statements 30/30
95.83% Branches 23/24
100% Functions 7/7
100% Lines 29/29

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                                    18x   18x   56x 56x           56x               56x 56x   56x     56x                                   56x                               18x   23x 23x                               22x 22x   22x             22x                         7x 7x   7x             7x                         7x 7x   7x             7x                         4x 4x   4x             4x            
import * as React from 'react';
import { Box as ReakitBox } from 'reakit';
 
import { useClassName, createComponent, createElement, createHook, useUniqueId } from '../utils';
import { Box, BoxProps } from '../Box';
 
import * as styles from './styles';
 
export type LocalCardProps = {
  kind?: 'border' | 'shadow';
  standalone?: boolean;
  title?: string | React.ReactElement<any>;
  footer?: string | React.ReactElement<any>;
  headerAddon?: React.ReactElement<any>;
};
export type CardProps = BoxProps & LocalCardProps;
 
export type CardContextOptions = CardProps & { descriptionId?: string; titleId?: string; themeKey?: string };
export const CardContext = React.createContext<CardContextOptions>({});
 
const useProps = createHook<CardProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const { footer, headerAddon, overrides, standalone, title, kind, ...restProps } = props;
    const boxProps = Box.useProps({
      altitude: kind === 'shadow' ? '100' : null,
      border: kind === 'border' ? 'default' : null,
      ...restProps
    });
 
    const className = useClassName({
      style: styles.Card,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
 
    const titleId = useUniqueId('cardTitle');
    const descriptionId = useUniqueId('cardDescription');
 
    const context = React.useMemo(() => ({ descriptionId, titleId, ...props }), [descriptionId, props, titleId]);
 
    const children = (
      <CardContext.Provider value={context}>
        {standalone ? (
          props.children
        ) : (
          <React.Fragment>
            {title && (
              <CardHeader overrides={overrides}>
                <CardTitle overrides={overrides}>{title}</CardTitle>
                {headerAddon && <Box>{headerAddon}</Box>}
              </CardHeader>
            )}
            <CardContent overrides={overrides}>{props.children}</CardContent>
            {footer && <CardFooter overrides={overrides}>{footer}</CardFooter>}
          </React.Fragment>
        )}
      </CardContext.Provider>
    );
 
    return {
      'aria-describedby': props.children ? descriptionId : undefined,
      'aria-labelledby': props.title ? titleId : undefined,
      ...boxProps,
      className,
      children
    };
  },
  {
    defaultProps: {
      kind: 'shadow'
    },
    themeKey: 'Card'
  }
);
 
export const Card = createComponent<CardProps>(
  props => {
    const cardProps = useProps(props);
    return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: cardProps });
  },
  {
    attach: {
      useProps
    },
    themeKey: 'Card'
  }
);
 
/////////////////////////////////////
 
export type LocalCardContentProps = {};
export type CardContentProps = BoxProps & LocalCardContentProps;
 
export function CardContent(props: CardContentProps) {
  const { children, ...restProps } = props;
  const context = React.useContext(CardContext);
 
  const cardContentClassName = useClassName({
    style: styles.CardContent,
    styleProps: { ...context, ...props },
    themeKey: context.themeKey || 'Card',
    themeKeySuffix: 'Content'
  });
 
  return (
    <Box className={cardContentClassName} id={context.descriptionId} {...restProps}>
      {children}
    </Box>
  );
}
 
/////////////////////////////////////
 
export type LocalCardTitleProps = {};
export type CardTitleProps = BoxProps & LocalCardTitleProps;
 
export function CardTitle(props: CardTitleProps) {
  const { children, ...restProps } = props;
  const context = React.useContext(CardContext);
 
  const cardTitleClassName = useClassName({
    style: styles.CardTitle,
    styleProps: { ...context, ...props },
    themeKey: context.themeKey || 'Card',
    themeKeySuffix: 'Title'
  });
 
  return (
    <Box className={cardTitleClassName} id={context.titleId} {...restProps}>
      {children}
    </Box>
  );
}
 
/////////////////////////////////////
 
export type LocalCardHeaderProps = {};
export type CardHeaderProps = BoxProps & LocalCardHeaderProps;
 
export function CardHeader(props: CardHeaderProps) {
  const { children, ...restProps } = props;
  const context = React.useContext(CardContext);
 
  const cardHeaderClassName = useClassName({
    style: styles.CardHeader,
    styleProps: { ...context, ...props },
    themeKey: context.themeKey || 'Card',
    themeKeySuffix: 'Header'
  });
 
  return (
    <Box className={cardHeaderClassName} {...restProps}>
      {children}
    </Box>
  );
}
 
/////////////////////////////////////
 
export type LocalCardFooterProps = {};
export type CardFooterProps = BoxProps & LocalCardFooterProps;
 
export function CardFooter(props: CardFooterProps) {
  const { children, ...restProps } = props;
  const context = React.useContext(CardContext);
 
  const cardFooterClassName = useClassName({
    style: styles.CardFooter,
    styleProps: { ...context, ...props },
    themeKey: context.themeKey || 'Card',
    themeKeySuffix: 'Footer'
  });
 
  return (
    <Box className={cardFooterClassName} {...restProps}>
      {children}
    </Box>
  );
}