Skip to content

Commit

Permalink
fix content toggle performance issue
Browse files Browse the repository at this point in the history
  • Loading branch information
farhan-shafi committed Jul 6, 2024
1 parent 78b279c commit 7b41525
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 36 deletions.
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ That's it. You're done!

= 3.2.1 =

* FIX: Content Toggle performance issue.
* FIX: Post Excerpt null warning in post grid block.
* FIX: Post image preserve aspect ratio not working properly on frontend.
* IMPROVE: Run counter when it visible on the screen in the counter block.
Expand Down
34 changes: 24 additions & 10 deletions src/blocks/content-toggle/front.build.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ var togglePanel = function togglePanel(target) {
if (panelContent.classList.contains("ub-hiding")) {
var convertedTop = convertToPixels(topPadding, topPaddingUnit);
var convertedBottom = convertToPixels(bottomPadding, bottomPaddingUnit);
panelContent.style.height = "".concat(panelContent.scrollHeight + convertedTop + convertedBottom - (topPaddingUnit === "%" || bottomPaddingUnit === "%" ? panelContent.parentElement.scrollHeight : 0), "px");
Object.assign(panelContent.style, {
paddingTop: "".concat(convertedTop, "px"),
paddingBottom: "".concat(convertedBottom, "px")
});
panelContent.style.height = "".concat(panelContent.scrollHeight + convertedTop + convertedBottom, "px");
panelContent.style.paddingTop = "".concat(convertedTop, "px");
panelContent.style.paddingBottom = "".concat(convertedBottom, "px");
document.querySelectorAll(".ub_image_slider").forEach(function (slider) {
new Swiper("#".concat(slider.id), JSON.parse(slider.dataset.swiperData));
});
Expand All @@ -82,6 +80,24 @@ var togglePanel = function togglePanel(target) {
embeddedContent.style.removeProperty("height");
});
};
var handleKeyDown = function handleKeyDown(e, i, toggleHeads) {
if (e.key === "ArrowUp" && i > 0) {
e.preventDefault();
toggleHeads[i - 1].focus();
} else if (e.key === "ArrowDown" && i < toggleHeads.length - 1) {
e.preventDefault();
toggleHeads[i + 1].focus();
} else if ([" ", "Enter"].includes(e.key)) {
e.preventDefault();
togglePanel(e.currentTarget);
} else if (e.key === "Home" && i > 0) {
e.preventDefault();
toggleHeads[0].focus();
} else if (e.key === "End" && i < toggleHeads.length - 1) {
e.preventDefault();
toggleHeads[toggleHeads.length - 1].focus();
}
};
var attachTogglePanelEvents = function attachTogglePanelEvents() {
document.querySelectorAll(".wp-block-ub-content-toggle").forEach(function (toggleContainer) {
var toggleHeads = Array.from(toggleContainer.children).map(function (toggle) {
Expand All @@ -90,12 +106,9 @@ var attachTogglePanelEvents = function attachTogglePanelEvents() {
return toggle && toggle.classList.contains("wp-block-ub-content-toggle-accordion-title-wrap");
});
toggleHeads.forEach(function (toggleHead, i) {
toggleHead.removeEventListener("keydown", handleKeyDown);
toggleHead.addEventListener("keydown", function (e) {
if (e.key === "ArrowUp" && i > 0) e.preventDefault(), toggleHeads[i - 1].focus();
if (e.key === "ArrowDown" && i < toggleHeads.length - 1) e.preventDefault(), toggleHeads[i + 1].focus();
if ([" ", "Enter"].includes(e.key)) e.preventDefault(), togglePanel(toggleHead);
if (e.key === "Home" && i > 0) e.preventDefault(), toggleHeads[0].focus();
if (e.key === "End" && i < toggleHeads.length - 1) e.preventDefault(), toggleHeads[toggleHeads.length - 1].focus();
return handleKeyDown(e, i, toggleHeads);
});
});
if (!toggleContainer.hasAttribute("data-preventcollapse")) {
Expand All @@ -119,6 +132,7 @@ var attachTogglePanelEvents = function attachTogglePanelEvents() {
var panelId = instance.parentElement.getAttribute("id");
var hash = location.hash.substring(1);
if (panelId && panelId === hash) togglePanel(instance);
instance.removeEventListener("click", togglePanel);
instance.addEventListener("click", function (e) {
e.stopImmediatePropagation();
togglePanel(instance);
Expand Down
53 changes: 27 additions & 26 deletions src/blocks/content-toggle/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,9 @@ const togglePanel = (target) => {
const convertedTop = convertToPixels(topPadding, topPaddingUnit);
const convertedBottom = convertToPixels(bottomPadding, bottomPaddingUnit);

panelContent.style.height = `${
panelContent.scrollHeight +
convertedTop +
convertedBottom -
(topPaddingUnit === "%" || bottomPaddingUnit === "%"
? panelContent.parentElement.scrollHeight
: 0)
}px`;

Object.assign(panelContent.style, {
paddingTop: `${convertedTop}px`,
paddingBottom: `${convertedBottom}px`,
});
panelContent.style.height = `${panelContent.scrollHeight + convertedTop + convertedBottom}px`;
panelContent.style.paddingTop = `${convertedTop}px`;
panelContent.style.paddingBottom = `${convertedBottom}px`;

document.querySelectorAll(".ub_image_slider").forEach((slider) => {
new Swiper(`#${slider.id}`, JSON.parse(slider.dataset.swiperData));
Expand Down Expand Up @@ -110,6 +100,25 @@ const togglePanel = (target) => {
});
};

const handleKeyDown = (e, i, toggleHeads) => {
if (e.key === "ArrowUp" && i > 0) {
e.preventDefault();
toggleHeads[i - 1].focus();
} else if (e.key === "ArrowDown" && i < toggleHeads.length - 1) {
e.preventDefault();
toggleHeads[i + 1].focus();
} else if ([" ", "Enter"].includes(e.key)) {
e.preventDefault();
togglePanel(e.currentTarget);
} else if (e.key === "Home" && i > 0) {
e.preventDefault();
toggleHeads[0].focus();
} else if (e.key === "End" && i < toggleHeads.length - 1) {
e.preventDefault();
toggleHeads[toggleHeads.length - 1].focus();
}
};

const attachTogglePanelEvents = () => {
document
.querySelectorAll(".wp-block-ub-content-toggle")
Expand All @@ -125,18 +134,10 @@ const attachTogglePanelEvents = () => {
);

toggleHeads.forEach((toggleHead, i) => {
toggleHead.addEventListener("keydown", (e) => {
if (e.key === "ArrowUp" && i > 0)
e.preventDefault(), toggleHeads[i - 1].focus();
if (e.key === "ArrowDown" && i < toggleHeads.length - 1)
e.preventDefault(), toggleHeads[i + 1].focus();
if ([" ", "Enter"].includes(e.key))
e.preventDefault(), togglePanel(toggleHead);
if (e.key === "Home" && i > 0)
e.preventDefault(), toggleHeads[0].focus();
if (e.key === "End" && i < toggleHeads.length - 1)
e.preventDefault(), toggleHeads[toggleHeads.length - 1].focus();
});
toggleHead.removeEventListener("keydown", handleKeyDown);
toggleHead.addEventListener("keydown", (e) =>
handleKeyDown(e, i, toggleHeads),
);
});

if (!toggleContainer.hasAttribute("data-preventcollapse")) {
Expand Down Expand Up @@ -177,6 +178,7 @@ const attachTogglePanelEvents = () => {
const hash = location.hash.substring(1);
if (panelId && panelId === hash) togglePanel(instance);

instance.removeEventListener("click", togglePanel);
instance.addEventListener("click", (e) => {
e.stopImmediatePropagation();
togglePanel(instance);
Expand Down Expand Up @@ -219,7 +221,6 @@ document.addEventListener("DOMContentLoaded", () => {
) {
Array.from(toggleContainer.children).forEach((child) => {
const panel = child.children[0].nextElementSibling;

if (!panel.classList.contains("ub-hide")) {
togglePanel(child.children[0]);
}
Expand Down

0 comments on commit 7b41525

Please sign in to comment.