base-ui icon indicating copy to clipboard operation
base-ui copied to clipboard

[menu] Motion with Menu

Open daonham opened this issue 10 months ago • 4 comments

Bug report

Current behavior

Bug when using Motion with Menu: when using the render component in Menu.Popup, the initial side value is always “bottom” and only switches to “top” after requestAnimationFrame. As a result, if you set the initial animation with side === “top”, it won’t run; in practice it only recognizes side === “bottom”.

 <Menu.Popup
              render={(props, state) => {
                console.log("state", state); // allway bottom
                return (
                  <motion.div
                    {...props}
                    animate={{
                      opacity: 1,
                      y: 0,
                      x: 0,
                      scale: 1,
                    }}
                    exit={{
                      opacity: 0,
                      y:
                        state.side === "bottom"
                          ? -8
                          : state.side === "top"
                            ? 8
                            : 0,
                      scale: 0.95,
                    }}
                    initial={{
                      opacity: 0,
                      y:
                        state.side === "bottom"
                          ? -8
                          : state.side === "top"
                            ? 8
                            : 0,
                      scale: 0.95,
                    }}
                    onAnimationComplete={() => {
                      if (!open) {
                        actionsRef?.current.unmount();
                      }
                    }}
                    transition={{
                      duration: 0.2,
                      ease: "easeOut",
                    }}
                  />
                );
              }}
            >
              {children}
            </Menu.Popup>

Base UI version

1.0.0

Which browser are you using?

Chrome

Which OS are you using?

Mac OS.

daonham avatar Jan 07 '26 09:01 daonham

@daonham Would you mind providing a repro?

mj12albert avatar Jan 07 '26 11:01 mj12albert

https://codesandbox.io/p/devbox/suspicious-sky-wlw93h

https://github.com/user-attachments/assets/8e27d8bd-e542-4d81-ab63-8f1137a781bc

daonham avatar Jan 08 '26 02:01 daonham

As far as I know, this is a missing feature in Motion: https://github.com/motiondivision/motion/issues/1878 (the maintainer closed the issue without understanding what I was demonstrating 😅)

It has a hacky workaround, but you might be better off using plain CSS and linear(...) if you want a spring animation

atomiks avatar Jan 09 '26 04:01 atomiks

Closing here as an upstream Motion limitation: the initial animation values are read before positioning has resolved, as discussed in motiondivision/motion#1878. CSS transitions with linear() easing are an alternative for spring-like motion.

atomiks avatar Oct 20 '26 07:10 atomiks