Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5932937239 | |||
| acef064147 |
36
src/lib/hooks/useButtonClick.ts
Normal file
36
src/lib/hooks/useButtonClick.ts
Normal 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;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user