Skip to content

Commit

Permalink
fix: External portals
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Nov 15, 2024
1 parent 35b47f3 commit bd7d1fe
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions editor.planx.uk/src/hooks/useScrollOnPreviousURLMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { useTheme } from "@mui/material/styles";
import { useStore } from "pages/FlowEditor/lib/store";
import { useEffect, useRef } from "react";

const useScrollOnPreviousURLMatch = <T extends HTMLElement>(id: string) => {
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(id);
const isReturningFromPortal = previousURL?.endsWith(urlMatcher);
if (!isReturningFromPortal) return;

// Center node
Expand All @@ -31,7 +33,7 @@ const useScrollOnPreviousURLMatch = <T extends HTMLElement>(id: string) => {
};

ref.current.animate(keyframes, animationOptions);
}, [previousURL, id, theme]);
}, [previousURL, urlMatcher, theme]);

return ref;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Question from "./Question";
const ExternalPortal: React.FC<any> = (props) => {
const [href, setHref] = useState("Loading...");

const ref = useScrollOnPreviousURLMatch<HTMLLIElement>(props.id);
const ref = useScrollOnPreviousURLMatch<HTMLLIElement>(href);

const addExternalPortal = useStore.getState().addExternalPortal;

Expand Down

0 comments on commit bd7d1fe

Please sign in to comment.