[FIX] Clipboard paste handler binding in dynamically rendered components#6037
Conversation
Greptile OverviewGreptile SummaryThis PR fixes a bug where the Key Changes:
How It Works: Testing: Confidence Score: 4/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant React
participant usePasteHandler
participant DOM
participant MutationObserver
participant EventListener
Note over React,usePasteHandler: Component renders with clipboard handler
React->>usePasteHandler: Call with target_ids, event_actions, on_paste
usePasteHandler->>usePasteHandler: Store on_paste & event_actions in refs
alt target_ids dependency changes
usePasteHandler->>usePasteHandler: Cleanup old listeners & observer
usePasteHandler->>DOM: Query for target elements by ID
alt All target elements exist
usePasteHandler->>EventListener: Attach paste listeners to targets
Note over usePasteHandler: Setup complete
else Some/all target elements missing (dynamic render)
usePasteHandler->>MutationObserver: Create observer for DOM changes
MutationObserver->>DOM: Watch document.body for childList changes
loop Until all targets found
DOM->>MutationObserver: DOM mutation detected
MutationObserver->>DOM: Query for target elements
alt Still missing targets
Note over MutationObserver: Continue observing
else All targets now exist
MutationObserver->>EventListener: Attach paste listeners
MutationObserver->>MutationObserver: Disconnect observer
end
end
end
end
Note over EventListener: User pastes content
EventListener->>usePasteHandler: Paste event fired
usePasteHandler->>usePasteHandler: Use current refs (latest handlers)
usePasteHandler->>React: Call on_paste with clipboard data
|
There was a problem hiding this comment.
Additional Comments (1)
-
reflex/.templates/web/utils/helpers/paste.js, line 85-90 (link)logic: the
MutationObservercallback can fire many times before elements appear, potentially creating duplicate listeners iftryAttach()succeeds while previous listeners aren't cleaned up
1 file reviewed, 1 comment
masenf
left a comment
There was a problem hiding this comment.
this is an excellent PR for a very annoying issue that's been around since day zero of the clipboard feature. thanks for your contribution!
CodSpeed Performance ReportMerging #6037 will not alter performanceComparing Summary
|
Thanks for the suggestions! Co-authored-by: Masen Furer <m_github@0x26.net>
Solution
Added a dependency array
[target_ids]to the seconduseEffectto ensure:MutationObserverwatches for DOM changes and attaches listeners once targets are available.Changes Made
[target_ids]dependency to the listener attachmentuseEffect.MutationObserverto handle elements that don't exist at initial render.All Submissions
Type of change
Please delete options that are not relevant.
Changes To Core Features
Screenshots