Files
4a38322a-5f8b-4fdb-b207-55b…/src/components/button/useButtonClick.ts
2025-12-21 20:56:21 +02:00

32 lines
745 B
TypeScript

import { useLenis } from "lenis/react";
export const useButtonClick = (href?: string, onClick?: () => void) => {
const lenis = useLenis();
const handleClick = () => {
if (href) {
const isExternalLink = /^(https?:\/\/|www\.)/.test(href);
if (isExternalLink) {
window.open(
href.startsWith("www.") ? `https://${href}` : href,
"_blank",
"noopener,noreferrer"
);
} else {
if (lenis) {
lenis.scrollTo(`#${href}`);
} else {
const element = document.getElementById(href);
if (element) {
element.scrollIntoView({ behavior: "smooth" });
}
}
}
}
onClick?.();
};
return handleClick;
};