From acef064147a6c747ce7f9c6fe08d18cce42bb7f6 Mon Sep 17 00:00:00 2001 From: bender Date: Wed, 10 Jun 2026 16:51:02 +0000 Subject: [PATCH] Add src/lib/hooks/useButtonClick.ts --- src/lib/hooks/useButtonClick.ts | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/lib/hooks/useButtonClick.ts diff --git a/src/lib/hooks/useButtonClick.ts b/src/lib/hooks/useButtonClick.ts new file mode 100644 index 0000000..58e4485 --- /dev/null +++ b/src/lib/hooks/useButtonClick.ts @@ -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; +};