Skip to content

fork: fix memory leak in prefers-reduced-motion listener and one othe… #3304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

teamharmony
Copy link

Fix: Potential Memory Leaks with Framer Motion + React Portal (Dialog)
We've observed memory leaks when using Framer Motion inside React Portals — specifically with modal dialogs.

On the first few renders, detached DOM nodes from the dialog content appear in memory snapshots. These leaks are more concerning when DOM nodes have event listeners that close over large data objects (e.g., closures referencing refs or component state).

Investigation
Using memory profiling tools, I traced two potential sources of retained memory:

window.matchMedia listeners – particularly for the "prefers-reduced-motion" query

The render queue in render-step

These seem to hold references beyond their intended lifecycle, contributing to retention of DOM nodes and React refs (e.g., VisualElement instances).

Note
These fixes may not be optimal or complete — the goal of this PR is to start a discussion and validate the approach with the Framer team. If more appropriate internal handling is possible, I’m happy to revise accordingly.

Related Issue
See discussion and example in: motiondivision/motion#2444

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant