40 lines
1003 B
JavaScript
40 lines
1003 B
JavaScript
// hooks/useScroll.js
|
|
import { useCallback } from 'react';
|
|
|
|
export const useScroll = () => {
|
|
const scrollToSection = useCallback((sectionId, offset = 0) => {
|
|
if (typeof window === 'undefined') return;
|
|
|
|
const element = document.getElementById(sectionId);
|
|
|
|
if (element) {
|
|
const elementPosition = element.getBoundingClientRect().top;
|
|
const offsetPosition = elementPosition + window.pageYOffset - offset;
|
|
|
|
window.scrollTo({
|
|
top: offsetPosition,
|
|
behavior: 'smooth'
|
|
});
|
|
}
|
|
}, []);
|
|
|
|
const scrollToSectionWithHeader = useCallback((sectionId) => {
|
|
const headerOffset = 80; // Adjust based on your header height
|
|
scrollToSection(sectionId, headerOffset);
|
|
}, [scrollToSection]);
|
|
|
|
const scrollToTop = useCallback(() => {
|
|
if (typeof window === 'undefined') return;
|
|
|
|
window.scrollTo({
|
|
top: 0,
|
|
behavior: 'smooth'
|
|
});
|
|
}, []);
|
|
|
|
return {
|
|
scrollToSection,
|
|
scrollToSectionWithHeader,
|
|
scrollToTop
|
|
};
|
|
}; |