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
Restore List Scroll Position on Edit and Create Views side effects #9774
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8a31bc9
Restore List Scroll Position on Edit and Create Views side effects
djhi c4d8039
Apply review suggestions
djhi 670ad49
Ensure scroll restoration does not conflict with scrollToTop
djhi 7c08d83
Remove debug code
djhi f25ca10
Move scroll restoration to Resource
djhi 3e46163
Revert unnecessary changes
djhi 2466c7d
Export RestoreScrollPosition
djhi 15d11e8
Apply review suggestions
djhi f442870
Fix missing example
djhi 072daad
Apply suggestions from code review
djhi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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,30 @@ | ||
import { ReactNode } from 'react'; | ||
import { useRestoreScrollPosition } from './useRestoreScrollPosition'; | ||
|
||
/** | ||
* A component that tracks the scroll position and restores it when the component mounts. | ||
* @param children The content to render | ||
* @param key The key under which to store the scroll position in the store | ||
* @param debounceMs The debounce time in milliseconds | ||
* | ||
* @example | ||
* import { ListBase, RestoreScrollPosition } from 'ra-core'; | ||
* | ||
* const MyCustomList = (props) => ( | ||
* <RestoreScrollPosition key="my-list> | ||
djhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* <ListBase {...props} /> | ||
* </RestoreScrollPosition> | ||
* }; | ||
fzaninotto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
export const RestoreScrollPosition = ({ | ||
fzaninotto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
children, | ||
storeKey, | ||
debounce = 250, | ||
}: { | ||
storeKey: string; | ||
debounce?: number; | ||
children: ReactNode; | ||
}) => { | ||
useRestoreScrollPosition(storeKey, debounce); | ||
return children; | ||
}; |
This file contains 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 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 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,71 @@ | ||
import { useEffect } from 'react'; | ||
import { useStore } from '../store'; | ||
import { debounce } from 'lodash'; | ||
import { useLocation } from 'react-router'; | ||
|
||
/** | ||
* A hook that tracks the scroll position and restores it when the component mounts. | ||
* @param storeKey The key under which to store the scroll position in the store | ||
* @param debounceMs The debounce time in milliseconds | ||
* | ||
* @example | ||
* import { ListBase, useRestoreScrollPosition } from 'ra-core'; | ||
* | ||
* const MyCustomList = (props) => { | ||
* useRestoreScrollPosition('my-list'); | ||
* return <ListBase {...props} />; | ||
* }; | ||
fzaninotto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
export const useRestoreScrollPosition = ( | ||
storeKey: string, | ||
debounceMs = 250 | ||
) => { | ||
const [position, setPosition] = useTrackScrollPosition( | ||
storeKey, | ||
debounceMs | ||
); | ||
const location = useLocation(); | ||
|
||
useEffect(() => { | ||
if (position != null && location.state?._scrollToTop !== true) { | ||
setPosition(undefined); | ||
window.scrollTo(0, position); | ||
} | ||
// We only want to run this effect on mount | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
}; | ||
|
||
/** | ||
* A hook that tracks the scroll position and stores it. | ||
* @param storeKey The key under which to store the scroll position in the store | ||
* @param debounceMs The debounce time in milliseconds | ||
* | ||
* @example | ||
* import { ListBase, useTrackScrollPosition } from 'ra-core'; | ||
* | ||
* const MyCustomList = (props) => { | ||
* useTrackScrollPosition('my-list'); | ||
* return <ListBase {...props} />; | ||
* }; | ||
*/ | ||
export const useTrackScrollPosition = (storeKey: string, debounceMs = 250) => { | ||
const [position, setPosition] = useStore(storeKey); | ||
|
||
useEffect(() => { | ||
if (typeof window === 'undefined') { | ||
return; | ||
} | ||
const handleScroll = debounce(() => { | ||
setPosition(window.scrollY); | ||
}, debounceMs); | ||
|
||
window.addEventListener('scroll', handleScroll); | ||
|
||
return () => { | ||
window.removeEventListener('scroll', handleScroll); | ||
}; | ||
}, [debounceMs, setPosition]); | ||
|
||
return [position, setPosition]; | ||
}; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
syntax error, missing quote