From 05c6af9cf4f55f25bc3b40dfa94e7c3a052087ac Mon Sep 17 00:00:00 2001 From: Simon Li Date: Sun, 14 Jul 2024 23:11:55 +0100 Subject: [PATCH] Close clipboard if clicked anywhere outside clipboard --- js/clipboard.js | 5 +++-- js/index.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/js/clipboard.js b/js/clipboard.js index 6827590a..78acd923 100644 --- a/js/clipboard.js +++ b/js/clipboard.js @@ -11,7 +11,7 @@ import "./clipboard.css"; * @param {Element} trigger * @param {Element} clipboard */ -export function setupClipboard(trigger, clipboard, canvas) { +export function setupClipboard(trigger, clipboard, parent) { const arrowElement = clipboard.querySelector(".arrow"); function updatePosition() { computePosition(trigger, clipboard, { @@ -53,6 +53,7 @@ export function setupClipboard(trigger, clipboard, canvas) { trigger.classList.toggle("active"); updatePosition(); e.preventDefault(); + e.stopPropagation(); }); // If the clipboard is clicked this should not be passed to the desktop @@ -60,7 +61,7 @@ export function setupClipboard(trigger, clipboard, canvas) { e.stopPropagation(); }); // Close the popup if we click outside it - canvas.addEventListener("click", () => { + parent.addEventListener("click", () => { if (trigger.classList.contains("active")) { clipboard.classList.toggle("hidden"); trigger.classList.toggle("active"); diff --git a/js/index.js b/js/index.js index e661ba60..c11a3e34 100644 --- a/js/index.js +++ b/js/index.js @@ -85,7 +85,7 @@ function connect() { setupClipboard( document.getElementById("clipboard-button"), document.getElementById("clipboard-container"), - document.getElementsByTagName("canvas")[0], + document.body, ); }