-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Navigation state provider and hook (#6709)
* feat: Navigation state provider and hook Temporary workaround for #6409 Get more info here - #6675 Signed-off-by: abizek <abishekilango@protonmail.com> * refactor: Navigation state hook effect Signed-off-by: abizek <abishekilango@protonmail.com> * docs: Add comment explaining eslint-disable-next-line Signed-off-by: abizek <abishekilango@protonmail.com> --------- Signed-off-by: abizek <abishekilango@protonmail.com>
- Loading branch information
Showing
6 changed files
with
76 additions
and
3 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
'use client'; | ||
|
||
import type { RichTranslationValues } from 'next-intl'; | ||
import type { FC } from 'react'; | ||
|
||
|
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,41 @@ | ||
'use client'; | ||
|
||
import type { RefObject } from 'react'; | ||
import { useContext, useEffect } from 'react'; | ||
|
||
import { NavigationStateContext } from '@/providers/navigationStateProvider'; | ||
import { debounce } from '@/util/debounce'; | ||
|
||
const useNavigationState = <T extends HTMLElement>( | ||
id: string, | ||
ref: RefObject<T>, | ||
debounceTime = 300 | ||
) => { | ||
const navigationState = useContext(NavigationStateContext); | ||
|
||
const handleScroll = debounce(() => { | ||
if (ref.current) { | ||
navigationState[id] = { | ||
x: ref.current.scrollLeft, | ||
y: ref.current.scrollTop, | ||
}; | ||
} | ||
}, debounceTime); | ||
|
||
useEffect(() => { | ||
const element = ref.current; | ||
if (element) { | ||
if (navigationState[id] && navigationState[id].y !== element.scrollTop) { | ||
element.scroll({ top: navigationState[id].y, behavior: 'instant' }); | ||
} | ||
|
||
element.addEventListener('scroll', handleScroll, { passive: true }); | ||
|
||
return () => element.removeEventListener('scroll', handleScroll); | ||
} | ||
// We need this effect to run only on mount | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
}; | ||
|
||
export default useNavigationState; |
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,22 @@ | ||
'use client'; | ||
|
||
import { createContext, useRef } from 'react'; | ||
import type { FC, PropsWithChildren } from 'react'; | ||
|
||
type NavigationStateContextType = Record<string, { x: number; y: number }>; | ||
|
||
export const NavigationStateContext = createContext<NavigationStateContextType>( | ||
{} | ||
); | ||
|
||
export const NavigationStateProvider: FC<PropsWithChildren> = ({ | ||
children, | ||
}) => { | ||
const navigationState = useRef<NavigationStateContextType>({}); | ||
|
||
return ( | ||
<NavigationStateContext.Provider value={navigationState.current}> | ||
{children} | ||
</NavigationStateContext.Provider> | ||
); | ||
}; |