import { AnchorHTMLAttributes, ReactNode } from "react";
import { ArrowUpRight } from "lucide-react";
import { cn, isExternalUrl } from "../node/utils";
export interface AnchorProps extends AnchorHTMLAttributes {
href?: string;
activeClassName?: string;
activeWhen?: string | RegExp | ((pathname: string) => boolean);
disabled?: boolean;
children: ReactNode;
className?: string;
}
export default function Anchor({
href = "",
className = "",
activeClassName = "",
activeWhen,
disabled = false,
children,
...props
}: AnchorProps) {
const isActive = (() => {
if (!activeWhen || typeof window === "undefined") return false;
const pathname = window.location.pathname;
if (typeof activeWhen === "string")
return pathname === activeWhen || pathname.endsWith(activeWhen);
if (activeWhen instanceof RegExp) return activeWhen.test(pathname);
if (typeof activeWhen === "function") return activeWhen(pathname);
return false;
})();
const isExternal = isExternalUrl(href);
const activeClass = isActive ? activeClassName : "";
const baseClasses = cn(
"hover:underline transition-colors",
className,
activeClass,
disabled && "cursor-not-allowed opacity-50"
);
if (disabled) {
return {children};
}
if (isExternal) {
return (
{children}
);
}
return (
{children}
);
}