Skip to content

Commit

Permalink
Clos clipboard if canvas or other element is clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
manics committed Jul 15, 2024
1 parent 05c6af9 commit f88af7d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions js/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import "./clipboard.css";
* Setup trigger element to toggle showing / hiding clipboard element
* @param {Element} trigger
* @param {Element} clipboard
* @param {Array[Element]} closers array of elements that should close the clipboard if clicked
*/
export function setupClipboard(trigger, clipboard, parent) {
export function setupClipboard(trigger, clipboard, closers) {
const arrowElement = clipboard.querySelector(".arrow");
function updatePosition() {
computePosition(trigger, clipboard, {
Expand Down Expand Up @@ -61,10 +62,12 @@ export function setupClipboard(trigger, clipboard, parent) {
e.stopPropagation();
});
// Close the popup if we click outside it
parent.addEventListener("click", () => {
if (trigger.classList.contains("active")) {
clipboard.classList.toggle("hidden");
trigger.classList.toggle("active");
}
closers.forEach((el) => {
el.addEventListener("click", () => {
if (trigger.classList.contains("active")) {
clipboard.classList.toggle("hidden");
trigger.classList.toggle("active");
}
});
});
}
2 changes: 1 addition & 1 deletion js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function connect() {
setupClipboard(
document.getElementById("clipboard-button"),
document.getElementById("clipboard-container"),
document.body,
[document.body, document.getElementsByTagName("canvas")[0]],
);
}

Expand Down

0 comments on commit f88af7d

Please sign in to comment.