Skip to content

Page Transitions/Animations Using Framer Motion #823

Answered by fauxparse
salemlf asked this question in Q&A
Discussion options

You must be logged in to vote

Okay, I think I've cracked this! The trick seems to be keeping hold of the old router context and using that when the component is on the way out. Thankfully, TSR exposes all the context stuff you need to make this work:

const AnimatedOutlet = forwardRef<HTMLDivElement>((_, ref) => {
  const RouterContext = getRouterContext();

  const routerContext = useContext(RouterContext);

  const renderedContext = useRef(routerContext);

  const isPresent = useIsPresent();

  if (isPresent) {
    renderedContext.current = cloneDeep(routerContext);
  }

  return (
    <motion.div ref={ref} {...transitionProps}>
      <RouterContext.Provider value={renderedContext.current}>
        <Outlet />
      </R…

Replies: 8 comments 27 replies

Comment options

You must be logged in to vote
3 replies
@egor-xyz
Comment options

@Ashilex
Comment options

@egor-xyz
Comment options

Comment options

You must be logged in to vote
20 replies
@VolDr
Comment options

@vladyoslav
Comment options

@trompx
Comment options

@rhinck
Comment options

@airtonix
Comment options

Answer selected by salemlf
Comment options

You must be logged in to vote
1 reply
@fauxparse
Comment options

Comment options

You must be logged in to vote
2 replies
@fauxparse
Comment options

@AndreiTS
Comment options

Comment options

You must be logged in to vote
1 reply
@mwkcoding
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet