Skip to content

Comments

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

Closed
teamharmony wants to merge 1 commit intomotiondivision:mainfrom
teamharmony:pr-shantesh-memoryleak
Closed

fork: fix memory leak in prefers-reduced-motion listener and one othe…#3304
teamharmony wants to merge 1 commit intomotiondivision:mainfrom
teamharmony:pr-shantesh-memoryleak

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

mattgperry added a commit that referenced this pull request Jan 6, 2026
Only initialize the matchMedia listener for prefers-reduced-motion
when the reducedMotionConfig is not explicitly set to "never" or
"always". This avoids adding unnecessary event listeners when the
motion preference is statically configured.

Related to: #3304, #2444

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
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