import React, { DetailedHTMLProps, HTMLAttributes } from "react"; import { twMerge } from "tailwind-merge"; /** * # General Card * @className twui-card * @className twui-card-link * * @info use the classname `nested-link` to prevent the card from being clickable when * a link (or the target element with this calss) inside the card is clicked. */ export default function Card({ href, variant, linkProps, ...props }: DetailedHTMLProps, HTMLDivElement> & { variant?: "normal"; href?: string; linkProps?: DetailedHTMLProps< React.AnchorHTMLAttributes, HTMLAnchorElement >; }) { const component = (
{props.children}
); if (href) { return ( { const targetEl = e.target as HTMLElement; if (targetEl.closest(".nested-link")) { e.preventDefault(); } else if (e.ctrlKey) { window.open(href, "_blank"); } else { window.location.href = href; } linkProps?.onClick?.(e); }} className={twMerge( "cursor-pointer", "twui-card-link", linkProps?.className )} > {component} ); } return component; }