Add src/lib/hooks/useButtonClick.ts

This commit is contained in:
2026-06-10 16:51:02 +00:00
parent c5fb7640bc
commit acef064147

View File

@@ -0,0 +1,36 @@
import { useCallback } from 'react';
import { useLenis } from 'lenis/react';
import { useRouter } from 'next/navigation';
interface UseButtonClickProps {
href?: string;
onClick?: (event: React.MouseEvent) => void;
}
export const useButtonClick = ({ href, onClick }: UseButtonClickProps) => {
const lenis = useLenis();
const router = useRouter();
const handleClick = useCallback((event: React.MouseEvent) => {
if (onClick) {
onClick(event);
}
if (href) {
if (href.startsWith('#')) {
event.preventDefault();
const normalizedSectionId = href.replace(/^#/, '');
if (lenis) {
lenis.scrollTo(`#${normalizedSectionId}`);
} else {
document.getElementById(normalizedSectionId)?.scrollIntoView({ behavior: 'smooth' });
}
} else if (href.startsWith('/') || href.startsWith('.')) {
event.preventDefault();
router.push(href);
}
}
}, [href, onClick, lenis, router]);
return handleClick;
};