Created
August 3, 2025 21:41
-
-
Save Atlessc/2a4a540e3a848252c1446c1cc7e7bd5d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use client'; | |
import React, { ReactNode } from 'react'; | |
import { useEffect } from 'react'; | |
function useScrollHighlight() { | |
useEffect(() => { | |
const runHighlight = () => { | |
const params = new URLSearchParams(window.location.search); | |
const section = params.get('highlight'); | |
if (!section) return; | |
const el = document.getElementById(section); | |
if (!el) return; | |
el.scrollIntoView({ behavior: 'smooth', block: 'start' }); | |
el.classList.add('highlighted'); | |
setTimeout(() => el.classList.remove('highlighted'), 2000); | |
}; | |
// on initial load | |
runHighlight(); | |
// if user navigates via back/forward | |
window.addEventListener('popstate', runHighlight); | |
return () => window.removeEventListener('popstate', runHighlight); | |
}, []); | |
} | |
export function ScrollClient({ children }: { children: ReactNode }) { | |
useScrollHighlight(); | |
return <>{children}</>; | |
} | |
// how to use | |
// import { useScrollHighlight } from '@/hooks/useScrollHighlight"; | |
// add 'useScrollHighlight();' to the layout or top level parent container to your main scrollable content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment