All files / src/shared/components/Card Card.tsx

95.5% Statements 85/89
80% Branches 16/20
100% Functions 1/1
95.5% Lines 85/89

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 1251x 1x 1x 1x 1x 1x 1x                               1x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x   47x 47x 47x 47x   47x 39x 1x 1x 1x 47x   47x 47x 47x 47x   47x 47x 47x 47x 47x 47x 47x 47x   47x 47x             47x 47x     47x 23x 23x 23x   23x 23x     47x 47x 47x 47x 47x     47x 47x 47x 47x 47x 47x 47x   47x 47x 47x 47x 47x 47x 47x 47x 47x 47x   47x 21x 21x 21x   26x   47x   47x   1x  
import { useEffect, useState, ReactNode, CSSProperties } from 'react';
import { motion } from 'framer-motion';
import LazyImage from '../LazyImage/LazyImage';
import useResizeObserver from '@/hooks/useResizeObserver';
import classNames from 'classnames';
import './Card.scss';
import { NavLink } from 'react-router-dom';
 
interface CardProps {
  name: string;
  isEffect?: boolean;
  className?: string;
  image?: string | null;
  onResize?: (width: number, height: number) => void;
  link?: string;
  linkTarget?: '_blank' | '_self' | '_parent' | '_top' | '';
  clickable?: boolean;
  showFooterLink?: boolean;
  children?: ReactNode;
  style?: CSSProperties;
}
 
const Card = ({
  name = '',
  isEffect = false,
  className = '',
  image = '',
  onResize,
  showFooterLink = false,
  clickable = false,
  link = '',
  linkTarget = '',
  children,
  style = {},
  ...rest
}: CardProps) => {
  const anchorObserver = useResizeObserver<HTMLAnchorElement>();
  const divObserver = useResizeObserver<HTMLDivElement>();
 
  const isLink = clickable && !!link;
  const { size } = clickable && isLink ? anchorObserver : divObserver;
  const resizeRef = clickable && isLink ? anchorObserver : divObserver;
  const [prevSize, setPrevSize] = useState(resizeRef.size);
 
  useEffect(() => {
    if (size.width !== prevSize.width || size.height !== prevSize.height) {
      setPrevSize(size);
      onResize?.(size.width, size.height);
    }
  }, [size.width, size.height]);
 
  const variants = {
    initial: { opacity: 0, y: 10 },
    animate: { opacity: 1, y: 0 },
  };
 
  const content = (
    <>
      {image && <LazyImage src={image} alt={name} />}
      <div className="card-body">
        {name && (
          <div className="card-title" id={`${name}-title`}>
            {name}
          </div>
        )}
        {children}
        {showFooterLink && link && !isEffect && (
          <div className="d-block mt-2">
            <NavLink to={link} target={linkTarget || undefined} className="card-link">
              View more
            </NavLink>
          </div>
        )}
      </div>
    </>
  );
 
  const overlay = isEffect && (
    <>
      <div className="card-overlay-box" aria-hidden="true" />
      <div className="card-overlay-box-text" aria-hidden="false" aria-label="Details">
        Details
      </div>
    </>
  );
 
  const innerContent = (
    <>
      {content}
      {overlay}
    </>
  );
 
  const id = `card-title-${name.replace(/\s+/g, '-').toLowerCase()}`;
  const isExternal = linkTarget === '_blank';
  const linkProps = {
    to: link,
    target: linkTarget || undefined,
    rel: isExternal ? 'noopener noreferrer' : undefined,
  };
 
  return (
    <motion.div
      ref={divObserver.ref}
      role="group"
      aria-labelledby={id}
      className={classNames('card', className, { hoverable: isEffect })}
      style={style}
      variants={variants}
      transition={{ duration: 0.3 }}
      {...rest}
    >
      {clickable && link ? (
        <NavLink className="card-container-link flex-column space-between" {...linkProps} {...rest}>
          {innerContent}
        </NavLink>
      ) : (
        innerContent
      )}
    </motion.div>
  );
};
 
export default Card;