Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e435fdf

Browse files
committedFeb 8, 2025·
Correctly assign click event target in shadow dom
and remove obsolete API for creating new synthetic mouse events
1 parent 046d0ec commit e435fdf

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed
 

‎src/components/dragelement/index.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -221,30 +221,38 @@ dragElement.init = function init(options) {
221221
if(gd._dragged) {
222222
if(options.doneFn) options.doneFn();
223223
} else {
224-
if(options.clickFn) options.clickFn(numClicks, initialEvent);
224+
// If you're in a shadow DOM the target here gets pushed
225+
// up to the container in the main DOM. (why only here? IDK)
226+
// Don't make an event at all, just an object that looks like one,
227+
// since the shadow DOM puts restrictions on what can go in the event,
228+
// but copy as much as possible since it will be passed on to
229+
// plotly_click handlers
230+
var clickEvent;
231+
if (initialEvent.target === initialTarget) {
232+
clickEvent = initialEvent;
233+
} else {
234+
clickEvent = {
235+
target: initialTarget,
236+
srcElement: initialTarget,
237+
toElement: initialTarget
238+
};
239+
Object.keys(initialEvent)
240+
.concat(Object.keys(initialEvent.__proto__))
241+
.forEach(k => {
242+
var v = initialEvent[k];
243+
if (!clickEvent[k] && (typeof v !== 'function')) {
244+
clickEvent[k] = v;
245+
}
246+
});
247+
}
248+
if(options.clickFn) options.clickFn(numClicks, clickEvent);
225249

226250
// If we haven't dragged, this should be a click. But because of the
227251
// coverSlip changing the element, the natural system might not generate one,
228252
// so we need to make our own. But right clicks don't normally generate
229253
// click events, only contextmenu events, which happen on mousedown.
230254
if(!rightClick) {
231-
var e2;
232-
233-
try {
234-
e2 = new MouseEvent('click', e);
235-
} catch(err) {
236-
var offset = pointerOffset(e);
237-
e2 = document.createEvent('MouseEvents');
238-
e2.initMouseEvent('click',
239-
e.bubbles, e.cancelable,
240-
e.view, e.detail,
241-
e.screenX, e.screenY,
242-
offset[0], offset[1],
243-
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
244-
e.button, e.relatedTarget);
245-
}
246-
247-
initialTarget.dispatchEvent(e2);
255+
initialTarget.dispatchEvent(new MouseEvent('click', e));
248256
}
249257
}
250258

0 commit comments

Comments
 (0)
Please sign in to comment.