-
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.
feat: Center previous portal when navigating back (#3955)
- Loading branch information
1 parent
e396735
commit be24a07
Showing
4 changed files
with
93 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useTheme } from "@mui/material/styles"; | ||
import { useStore } from "pages/FlowEditor/lib/store"; | ||
import { useEffect, useRef } from "react"; | ||
|
||
const useScrollOnPreviousURLMatch = <T extends HTMLElement>( | ||
urlMatcher: string, | ||
) => { | ||
const previousURL = useStore((state) => state.previousURL); | ||
const ref = useRef<T | null>(null); | ||
const theme = useTheme(); | ||
|
||
useEffect(() => { | ||
if (!ref.current) return; | ||
|
||
const isReturningFromPortal = previousURL?.endsWith(urlMatcher); | ||
if (!isReturningFromPortal) return; | ||
|
||
// Center node | ||
ref.current.scrollIntoView({ | ||
block: "center", | ||
inline: "center", | ||
}); | ||
|
||
// Visually highlight node | ||
const keyframes: Keyframe[] = [ | ||
{ outline: `4px solid ${theme.palette.action.focus}`, outlineOffset: 0 }, | ||
{ outline: `4px solid transparent`, outlineOffset: 0 }, | ||
]; | ||
|
||
const animationOptions: KeyframeAnimationOptions = { | ||
duration: 1500, | ||
easing: "ease-in", | ||
}; | ||
|
||
ref.current.animate(keyframes, animationOptions); | ||
}, [previousURL, urlMatcher, theme]); | ||
|
||
return ref; | ||
}; | ||
|
||
export default useScrollOnPreviousURLMatch; |
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