All files / src/components/buttons/SectionLink SectionLink.tsx

100% Statements 5/5
100% Branches 7/7
100% Functions 1/1
100% Lines 5/5

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                2x 8x     8x       8x   8x                                        
import { Link, useLocation } from "react-router-dom";
 
export interface SectionLinkProps {
  href: string;
  children: React.ReactNode | string;
  indentLevel?: number;
}
 
const SectionLink: React.FC<SectionLinkProps> = (props) => {
  const currLocation = useLocation();
 
  const selected: boolean =
    props.href === currLocation.pathname ||
    (props.href.slice(-1) !== "/" &&
      currLocation.pathname.startsWith(props.href));
 
  const indent = props.indentLevel ? props.indentLevel * 16 : 0; // px
 
  return (
    <Link
      to={props.href}
      style={{ marginLeft: indent }}
      className={`hover:pl-5 block w-full pl-4 py-2 border-l transition-all duration-200
        ${
          selected
            ? // Keep the light (white-ish) highlighted cell in dark mode too, in
              // line with fe-core: light bg with readable blue text.
              "border-l-4 border-blue-500 text-blue-700 font-medium"
            : "border-l border-transparent text-gray-600 hover:border-gray-300 hover:bg-gray-50 dark:text-gray-300 dark:hover:border-gray-600 dark:hover:bg-white/5"
        }
      `}
    >
      {props.children}
    </Link>
  );
};
 
export default SectionLink;