Skip to content

Commit

Permalink
fix: on trigger click (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
codemers authored Oct 4, 2022
1 parent bb1e0ec commit d2f3a8a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-zoos-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@feedbackfarm/js": patch
---

fix: on trigger click
70 changes: 37 additions & 33 deletions packages/js/src/widget.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,16 @@
const triggers = document.querySelectorAll("[data-feedback-farm]");

function initializeTriggers() {
// Load floatingUI
const popperScript = document.createElement("script");
popperScript.src = "https://unpkg.com/@popperjs/core@2";
popperScript.defer = true;
document.head.appendChild(popperScript);

triggers.forEach((trigger) => {
trigger.addEventListener("click", (event) => {
event.preventDefault();

const feedbackFarmIFrame = document.getElementById(
"feedback-farm-iframe"
);
if (!feedbackFarmIFrame) {
return;
}
feedbackFarmIFrame.style.display = "block";

setupOutsideClickListener();

// Position widget
// @ts-expect-error
window.Popper.createPopper(trigger, feedbackFarmIFrame, {
placement: "bottom",
modifiers: [
{
name: "offset",
options: {
offset: [0, 10],
},
},
],
});
});
});
document.body.addEventListener("click", detectTriggerClick);
setupOutsideClickListener();
}

function setupIFrame() {
const [trigger] = triggers;
const [trigger] = document.querySelectorAll("[data-feedback-farm]");
if (!trigger) return;

const params = getDataParameters(trigger);
Expand Down Expand Up @@ -74,7 +45,40 @@ function setupOutsideClickListener() {
document.addEventListener("click", detectOutsideClick);
}

function detectTriggerClick(e) {
const triggers = document.querySelectorAll("[data-feedback-farm]");
for (let i = 0; i < triggers.length; i++) {
if (triggers[i] === e.target) {
e.preventDefault();
const feedbackFarmIFrame = document.getElementById(
"feedback-farm-iframe"
);

if (!feedbackFarmIFrame) {
return;
}
feedbackFarmIFrame.style.display = "block";

// Position widget
// @ts-expect-error
window.Popper.createPopper(triggers[i], feedbackFarmIFrame, {
placement: "bottom",
modifiers: [
{
name: "offset",
options: {
offset: [0, 10],
},
},
],
});
return true;
}
}
}

function detectOutsideClick(e) {
const triggers = document.querySelectorAll("[data-feedback-farm]");
let clickOutside = true;

for (let i = 0; i < triggers.length; i++) {
Expand Down Expand Up @@ -129,6 +133,6 @@ function getDataParameters(trigger) {
};
}

initializeTriggers();
setupIFrame();
initializeTriggers();
setupMessageListener();

1 comment on commit d2f3a8a

@vercel
Copy link

@vercel vercel bot commented on d2f3a8a Oct 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.