fix(tools): ensure switching workspace or category resets scroll to top
This commit is contained in:
@@ -486,13 +486,34 @@ export function ToolGridSection({
|
||||
return () => window.removeEventListener('hashchange', syncFromHash);
|
||||
}, [categorizedTools]);
|
||||
|
||||
// Handle Category Selection with URL Hash reflection
|
||||
const isMountedRef = useRef<boolean>(false);
|
||||
|
||||
// Smoothly scroll window to top whenever active category or workspace changes
|
||||
useEffect(() => {
|
||||
if (!isMountedRef.current) {
|
||||
isMountedRef.current = true;
|
||||
return;
|
||||
}
|
||||
if (typeof window !== 'undefined') {
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' });
|
||||
const rafId = requestAnimationFrame(() => {
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' });
|
||||
});
|
||||
return () => cancelAnimationFrame(rafId);
|
||||
}
|
||||
}, [selectedCategory]);
|
||||
|
||||
// Handle Category Selection with URL Hash reflection and guaranteed scroll to top
|
||||
const handleSelectCategory = (catId: string) => {
|
||||
setSelectedCategory(catId);
|
||||
if (searchQuery) setSearchQuery('');
|
||||
if (typeof window !== 'undefined') {
|
||||
const newHash = catId === 'all' ? '' : `#category-${catId}`;
|
||||
window.history.replaceState(null, '', newHash || window.location.pathname);
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' });
|
||||
requestAnimationFrame(() => {
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' });
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user