-
Notifications
You must be signed in to change notification settings - Fork 75
feat: add sidebar scroll position persistence #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ubay1
wants to merge
4
commits into
nodejs:main
Choose a base branch
from
ubay1:fix/persist-sidebar-scroll-position
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
70776cb
feat: add sidebar scroll position persistence
ubay1 ced00da
fix: remove console.log, scope localStorage key by id, and mount Navi…
ubay1 305c7d0
fix: remove ref.current from useEffect deps to prevent scroll jump
ubay1 93020dd
fix: wrap localStorage.setItem in try/catch in scroll handler
ubay1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { useEffect, useRef } from 'react'; | ||
|
|
||
| // Custom hook to handle scroll events with optional debouncing | ||
| const useScroll = (ref, { debounceTime = 300, onScroll }) => { | ||
| const timeoutRef = useRef(undefined); | ||
| const onScrollRef = useRef(onScroll); | ||
|
|
||
| // Keep onScrollRef updated with the latest callback | ||
| useEffect(() => { | ||
| onScrollRef.current = onScroll; | ||
| }, [onScroll]); | ||
| useEffect(() => { | ||
| // Get the current element | ||
| const element = ref.current; | ||
|
|
||
| // Return early if no element or onScroll callback is provided | ||
| if (!element || !onScrollRef.current) { | ||
| return; | ||
| } | ||
|
|
||
| // Debounced scroll handler | ||
| const handleScroll = () => { | ||
| // Clear existing timeout | ||
| if (timeoutRef.current) { | ||
| clearTimeout(timeoutRef.current); | ||
| } | ||
|
|
||
| // Set new timeout to call onScroll after debounceTime | ||
| timeoutRef.current = setTimeout(() => { | ||
| if (element && onScrollRef.current) { | ||
| onScrollRef.current({ | ||
| x: element.scrollLeft, | ||
| y: element.scrollTop, | ||
| }); | ||
| } | ||
| }, debounceTime); | ||
| }; | ||
|
|
||
| element.addEventListener('scroll', handleScroll, { passive: true }); | ||
|
|
||
| return () => { | ||
| element.removeEventListener('scroll', handleScroll); | ||
| // Clear any pending debounced calls | ||
| if (timeoutRef.current) { | ||
| clearTimeout(timeoutRef.current); | ||
| } | ||
| }; | ||
| }, [debounceTime]); | ||
| }; | ||
|
|
||
| export default useScroll; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { useContext, useEffect } from 'react'; | ||
|
|
||
| import { NavigationStateContext } from '../providers/navigationStateProvider'; | ||
|
|
||
| import useScroll from './useScroll'; | ||
|
|
||
| const useScrollToElement = (id, ref, debounceTime = 300) => { | ||
| const navigationState = useContext(NavigationStateContext); | ||
|
|
||
| // Restore scroll position on mount | ||
| useEffect(() => { | ||
| const element = ref.current; | ||
| if (!element) { | ||
| return; | ||
| } | ||
|
|
||
| // Prefer in-memory context state (set during same session/SPA navigation). | ||
| // Fall back to localStorage so position is restored after a full page refresh. | ||
| let savedState = navigationState[id]; | ||
|
|
||
| if (!savedState) { | ||
| try { | ||
| const raw = localStorage.getItem(`navigationState:${id}`); | ||
| if (raw) { | ||
| savedState = JSON.parse(raw); | ||
| // Hydrate context so it's available for the rest of the session | ||
| navigationState[id] = savedState; | ||
| } | ||
| } catch { | ||
| localStorage.removeItem(`navigationState:${id}`); | ||
| } | ||
| } | ||
|
|
||
| // Scroll only if the saved position differs from current | ||
| if (savedState && savedState.y !== element.scrollTop) { | ||
| element.scroll({ top: savedState.y, behavior: 'auto' }); | ||
| } | ||
| }, [id]); | ||
|
|
||
| // Save scroll position on scroll | ||
| const handleScroll = position => { | ||
| try { | ||
| localStorage.setItem(`navigationState:${id}`, JSON.stringify(position)); | ||
| } catch { | ||
| // localStorage may be unavailable (e.g. Safari private browsing) | ||
| // or the quota may be exceeded — fall through so in-memory state | ||
| // is still updated below. | ||
| } | ||
| // Always update in-memory state regardless of localStorage availability | ||
| navigationState[id] = position; | ||
| }; | ||
|
|
||
| // Use the useScroll hook to handle scroll events with debouncing | ||
| useScroll(ref, { debounceTime, onScroll: handleScroll }); | ||
| }; | ||
|
|
||
| export default useScrollToElement; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| 'use client'; | ||
|
|
||
| import { createContext, useRef } from 'react'; | ||
|
|
||
| export const NavigationStateContext = createContext({}); | ||
|
|
||
| export const NavigationStateProvider = ({children}) => { | ||
| const navigationStateRef = useRef({}); | ||
|
|
||
| return ( | ||
| <NavigationStateContext.Provider value={navigationStateRef.current}> | ||
| {children} | ||
| </NavigationStateContext.Provider> | ||
| ); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.