SOLVED: bricksToggle() fired multiple times causing multiple attached click events

Browser: Brave 1.63
OS: macOS
URL: Diary – sounddiary.me

I don’t remember when, but at some point my .brxe-toggle elements stopped working correctly (see the menu bar on the upper right – the burger as well as the filter and hashtag icons).

I am not sure if wpgridbuilder or bricks is the problem here, but the fact is, that when I load new content with the lazy load function of wpgridbuilder, bricksToggle() will fire again causing every .brxe-toggle element to have 2 of the same click events. Of course the functionality therefore stops working.

When I add a toggleCounter variable I can prevent the events from being attached twice. Of course this will be overridden in the next update and is far from ideal:

var toggleCounter = 0;
function bricksToggle() {
if (bricksIsFrontend) {
var e = bricksQuerySelectorAll(document, “.brxe-toggle”);
toggleCounter++;
if(toggleCounter === 1){
e.length && (bricksToggleDisplay(), e.forEach((function(e) {
e.addEventListener(“click”, (function(t) {
var n, r, i;
t.preventDefault();
var o = (null === (n = e.dataset) || void 0 === n ? void 0 : n.selector) || “.brxe-offcanvas”,
a = (null === (r = e.dataset) || void 0 === r ? void 0 : r.attribute) || “class”,
s = (null === (i = e.dataset) || void 0 === i ? void 0 : i.value) || “brx-open”,
c = !!o && document.querySelector(o);
if (c || (c = e.closest(“.brxe-nav-nested”)), c || (c = e.closest(“.brxe-offcanvas”)), c) {
document.querySelector(“.brx-has-megamenu”) && (t.target.closest(‘[data-effect=“offset”]’) || bricksSubmenuPosition(0)), e.dataset.scriptId && !c.dataset.toggleScriptId && (c.dataset.toggleScriptId = e.dataset.scriptId);
var l = “true” === e.getAttribute(“aria-expanded”);
e.setAttribute(“aria-expanded”, !l), e.classList.toggle(“is-active”), “class” === a ? e.closest(“.brxe-nav-nested”) && “brx-open” === s && c.classList.contains(“brx-open”) ? (c.classList.add(“brx-closing”), setTimeout((function() {
c.classList.remove(“brx-closing”), c.classList.remove(“brx-open”)
}), 200)) : c.classList.toggle(s) : c.getAttribute(a) ? c.removeAttribute(a) : c.setAttribute(a, s);
var d = bricksGetFocusables(c);
d.length && d[0].focus()
}
}))
})))
}
}
}

This is the answer from wpgridbuilder support:

Bricks provide helpers in order to reinitialize toggles, accordions, popups, etc. These helpers are executed every time content is appended by WP Grid Builder.
Otherwise, toggles, accordions, popups, etc. inside the filtered content will not work. Additionally, the toggle script does not take care of already instantiated toggles and creates this issue.
Unfortunately, we can do nothing on the WP Grid Builder side.
Thank you.

Hi @Jonathan ,

Thanks for reporting the issue.
We are aware of this issue and is currently working on it :slight_smile:
Will update this thread once fixed.

Regards,
Jenn

Hi Jonathan,

We’ve fixed this issue in Bricks 1.9.8 beta, now available as a manual download in your account (see changelog).

Please let us know if you are still experiencing issues.

As with any beta release, please do not use it on a production or live website. It is only meant for testing in a local or staging environment.