Skip to content

Commit a5f5bc5

Browse files
authored
fix: prevent scroll to top when switching tabs (#283)
Preserve scroll position when changing tabs by capturing scrollY before navigation and restoring it after. This prevents the jarring scroll-to-top behavior on both desktop and mobile when clicking tabs in slot/epoch detail pages. The fix uses requestAnimationFrame to ensure the scroll position is restored after the browser has processed the URL change.
1 parent f843c96 commit a5f5bc5

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/hooks/useTabState/useTabState.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ export function useTabState(tabs: TabConfig[]): UseTabStateReturn {
168168
// Check if current hash is an anchor within the NEW tab
169169
const hashBelongsToNewTab = newTab.anchors?.includes(currentHash);
170170

171+
// Store current scroll position before navigation
172+
const scrollY = window.scrollY;
173+
171174
if (hashBelongsToNewTab) {
172175
// Keep the anchor and scroll to it
173176
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-restricted-syntax
@@ -187,6 +190,12 @@ export function useTabState(tabs: TabConfig[]): UseTabStateReturn {
187190
replace: true,
188191
hash: undefined,
189192
});
193+
194+
// Restore scroll position after navigation to prevent scroll to top
195+
// This prevents the jarring scroll behavior on both desktop and mobile
196+
requestAnimationFrame(() => {
197+
window.scrollTo(0, scrollY);
198+
});
190199
}
191200
};
192201

0 commit comments

Comments
 (0)