Skip to content

Instantly share code, notes, and snippets.

@Atlessc
Created August 3, 2025 21:41
Show Gist options
  • Save Atlessc/2a4a540e3a848252c1446c1cc7e7bd5d to your computer and use it in GitHub Desktop.
Save Atlessc/2a4a540e3a848252c1446c1cc7e7bd5d to your computer and use it in GitHub Desktop.
'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