motiondivision / motion

A modern animation library for React and JavaScript
https://motion.dev
MIT License
24.59k stars 824 forks source link

[FEATURE] Propagate `PresenceContext` Across Nested `AnimatePresence` #2848

Open custardcream98 opened 2 weeks ago

custardcream98 commented 2 weeks ago

Is your feature request related to a problem? Please describe.

When there are multiple layers of AnimatePresence, usePresence references the context of the nearest parent PresenceContext.

Because of this, even when the parent component unmounts and isPresent should be false, the isPresent of the child context remains true, making it difficult to achieve the desired behavior.

Describe the solution you'd like

In cases with multiple layers, the usePresence value in the child should be updated based on the context of the parent's. Alternatively, there should be a way to access the parent context directly.

Describe alternatives you've considered

Currently, this issue can only be resolved by defining a custom component or context that passes down the parent's isPresent value to its children.

Additional context

Checkout Codesandbox here (Try clicking the toggle button and observe the isPresent values of the Parent and Children components)

custardcream98 commented 2 weeks ago

This behavior might be considered a bug, but I'm not sure.