forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement <ScrollRestoration /> (twentyhq#5086)
### Description Implement <ScrollRestoration /> ### Refs [twentyhq#4357 ### Demo https://github.com/twentyhq/twenty/assets/140154534/321242e1-4751-4204-8c86-e9b921c1733e Fixes twentyhq#4357 --------- Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com> Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com> Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: RubensRafael <rubensrafael2@live.com>
- Loading branch information
1 parent
992602b
commit 0e525ca
Showing
8 changed files
with
161 additions
and
71 deletions.
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,34 @@ | ||
import { useEffect } from 'react'; | ||
import { useLocation, useNavigation } from 'react-router-dom'; | ||
import { useRecoilState, useRecoilValue } from 'recoil'; | ||
|
||
import { overlayScrollbarsState } from '@/ui/utilities/scroll/states/overlayScrollbarsState'; | ||
import { scrollPositionState } from '@/ui/utilities/scroll/states/scrollPositionState'; | ||
import { isDefined } from '~/utils/isDefined'; | ||
|
||
/** | ||
* Note that `location.key` is used in the cache key, not `location.pathname`, | ||
* so the same path navigated to at different points in the history stack will | ||
* not share the same scroll position. | ||
*/ | ||
export const useScrollRestoration = (viewportHeight?: number) => { | ||
const key = `scroll-position-${useLocation().key}`; | ||
const { state } = useNavigation(); | ||
|
||
const [scrollPosition, setScrollPosition] = useRecoilState( | ||
scrollPositionState(key), | ||
); | ||
|
||
const overlayScrollbars = useRecoilValue(overlayScrollbarsState); | ||
|
||
const scrollWrapper = overlayScrollbars?.elements().viewport; | ||
const skip = isDefined(viewportHeight) && scrollPosition > viewportHeight; | ||
|
||
useEffect(() => { | ||
if (state === 'loading') { | ||
setScrollPosition(scrollWrapper?.scrollTop ?? 0); | ||
} else if (state === 'idle' && isDefined(scrollWrapper) && !skip) { | ||
scrollWrapper.scrollTo({ top: scrollPosition }); | ||
} | ||
}, [key, state, scrollWrapper, skip, scrollPosition, setScrollPosition]); | ||
}; |
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
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
7 changes: 7 additions & 0 deletions
7
packages/twenty-front/src/modules/ui/utilities/scroll/states/overlayScrollbarsState.ts
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,7 @@ | ||
import { OverlayScrollbars } from 'overlayscrollbars'; | ||
import { createState } from 'twenty-ui'; | ||
|
||
export const overlayScrollbarsState = createState<OverlayScrollbars | null>({ | ||
key: 'scroll/overlayScrollbarsState', | ||
defaultValue: null, | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/twenty-front/src/modules/ui/utilities/scroll/states/scrollPositionState.ts
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,6 @@ | ||
import { createFamilyState } from '@/ui/utilities/state/utils/createFamilyState'; | ||
|
||
export const scrollPositionState = createFamilyState({ | ||
key: 'scroll/scrollPositionState', | ||
defaultValue: 0, | ||
}); |