Trigger a mouse event every time the ReactFlow component is clicked #724
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.
This PR fixes #713 .
Issue Recap
Initially, I tried updating the ReactFlow library to version 12, hoping that would solve the issue, but it didn’t work. After further investigation, I found this discussion detailing our exact use case. The response from the maintainer identified the underlying cause: the problem stems from the d3 zoom library that ReactFlow uses under the hood.
When disabling pointer events (as suggested), it allowed me to detect clicks, but this also disabled the drag functionality, which isn’t ideal for our use case.
My Solution
To resolve this, I implemented a workaround that triggers a
mousedown
event programmatically whenever a click occurs inside the ReactFlow component, ensuring that the dropdown closes as expected without disabling any essential functionality like dragging.Here’s what I did:
Added a function to programmatically trigger the
mousedown
event:This function triggers a
mousedown
event every time it's called. The frontend library we’re using (Ant Design) automatically closes the dropdown whenever amousedown
event is detected, so this is the key to solving the problem.Triggered the function on relevant events in the ReactFlow component:
By calling the
dispatchMouseEvent
function insideonPaneClick
andonPointerDown
, we ensure that amousedown
event is triggered whenever someone clicks or presses inside the ReactFlow component, resulting in the dropdown closing. This way, no library update is required, and we preserve the drag functionality.I’ve also recorded a video demonstrating the fix in action. It works perfectly now, and I believe this is the best way to resolve the issue.
Screen.Recording.2024-10-04.at.9.01.16.AM.mov
Let me know your thoughts, and feel free to merge this PR if you’re satisfied with the solution.