fork: fix memory leak in prefers-reduced-motion listener and one othe…#3304
Closed
teamharmony wants to merge 1 commit intomotiondivision:mainfrom
Closed
fork: fix memory leak in prefers-reduced-motion listener and one othe…#3304teamharmony wants to merge 1 commit intomotiondivision:mainfrom
teamharmony wants to merge 1 commit intomotiondivision:mainfrom
Conversation
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]>
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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