ldc-bien-etre-animal/resources/ts/front/Components/Shared/SmartLink.tsx
2026-09-04 19:40:48 +02:00

53 lines
997 B
TypeScript

import { Link } from "@inertiajs/react";
import { type ReactNode } from "react";
import useIsExternalLink from "@/hooks/use-is-external-link";
interface SmartLinkProps {
href?: string;
onClick?: () => void;
type?: "button" | "submit" | "reset";
className?: string;
children: ReactNode;
}
export default function SmartLink({
href,
onClick,
type = "button",
className = "",
children,
}: Readonly<SmartLinkProps>) {
const isExternal = useIsExternalLink(href);
if (href) {
if (isExternal) {
return (
<a
href={href}
className={className}
target="_blank"
rel="noopener noreferrer"
>
{children}
</a>
);
}
return (
<Link href={href} className={className}>
{children}
</Link>
);
}
const handleClick = () => {
onClick?.();
};
return (
<button type={type} onClick={handleClick} className={className}>
{children}
</button>
);
}