From 2444c0a66fb291be844c8451d5a1c8660302faf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CAmjad?= Date: Tue, 24 Nov 2026 03:37:30 +0500 Subject: [PATCH 1/2] Animate elements added after load, and never leave one hidden Pagination, sorting and filters on a Woo product collection, and on a core Query Loop with "Force page reload" off, swap results in through the Interactivity API router without a page load. The new cards still carry .ext-animate--on, so the head CSS hid them, but the script only set elements up once on DOMContentLoaded. Page 2 of the shop rendered blank until a reload. A MutationObserver now runs the existing per-element setup on newly added .ext-animate--on elements only, the way WOW.js handles content added later. The router has no public "region swapped" event, so an observer is the only hook a plain script has. A full re-scan, as AOS does, was rejected because it replays cards the visitor already saw. The hide rule now applies only to elements the script has set up. One it never sets up is held hidden by a 3s animation and then shows, so a missed element, a blocked script or a script error can no longer keep content invisible. On a load where the footer script runs later than 3s, an element can show, hide and then animate in; visible content was preferred over the risk of content that never appears. The fix lives in the animation code every site loads, so sites with a saved copy of the shop template are covered without re-saving it. --- assets/js/animations-interactivity.js | 140 ++++++++++++++++++++------ inc/animations.php | 8 +- 2 files changed, 117 insertions(+), 31 deletions(-) diff --git a/assets/js/animations-interactivity.js b/assets/js/animations-interactivity.js index 661e4bf..10946e7 100644 --- a/assets/js/animations-interactivity.js +++ b/assets/js/animations-interactivity.js @@ -11,7 +11,10 @@ parseFloat(config.defaults && config.defaults.stagger) || 0.08; const maxStagger = 0.6; let observer = null; + let mutationObserver = null; let trackedElements = []; + let addedNodes = []; + let flushScheduled = false; const speeds = Object.freeze({ slow: 1, @@ -72,41 +75,60 @@ const pending = []; Object.keys(currentMap).forEach((selector) => { try { - const elements = document.querySelectorAll(selector); - const animationType = currentMap[selector]; - const elementsByParent = new Map(); - elements.forEach((element) => { - if (element.classList.contains('ext-animate--off')) { - return; - } - const parent = element.parentElement; - if (!elementsByParent.has(parent)) { - elementsByParent.set(parent, []); - } - elementsByParent.get(parent).push(element); - }); + collect( + document.querySelectorAll(selector), + currentMap[selector], + pending + ); + } catch {} + }); + apply(pending); - elementsByParent.forEach((siblingElements) => { - siblingElements.forEach((element, index) => { - const rect = element.getBoundingClientRect(); - pending.push({ - element, - animationType, - delay: Math.min(index * staggerDelay, maxStagger), - // Elements already in viewport (like headers) - reveal immediately - inViewport: - rect.top < window.innerHeight && - rect.bottom > 0, - }); - }); + if (!mutationObserver) { + mutationObserver = new MutationObserver(onMutations); + mutationObserver.observe(document.body, { + childList: true, + subtree: true, + }); + } + } + + function collect(elements, animationType, pending) { + const elementsByParent = new Map(); + elements.forEach((element) => { + if (element.classList.contains('ext-animate--off')) { + return; + } + const parent = element.parentElement; + if (!elementsByParent.has(parent)) { + elementsByParent.set(parent, []); + } + elementsByParent.get(parent).push(element); + }); + + elementsByParent.forEach((siblingElements) => { + siblingElements.forEach((element, index) => { + const rect = element.getBoundingClientRect(); + pending.push({ + element, + animationType, + delay: Math.min(index * staggerDelay, maxStagger), + // Elements already in viewport (like headers) - reveal immediately + inViewport: + rect.top < window.innerHeight && rect.bottom > 0, }); - } catch {} + }); }); + } - // Batch all writes into one frame so layout happens once, not once per element. + // Batch all writes into one frame so layout happens once, not once per element. + function apply(pending) { const duration = speeds[currentSpeed]; requestAnimationFrame(() => { pending.forEach(({ element, animationType, delay, inViewport }) => { + if (element.classList.contains('ext-animate')) { + return; + } element.classList.add('ext-animate'); element.dataset.extAnimate = animationType; element.style.animationDuration = `${duration}s`; @@ -131,6 +153,64 @@ }); } + function onMutations(records) { + records.forEach((record) => { + record.addedNodes.forEach((node) => { + if (node.nodeType === Node.ELEMENT_NODE) { + addedNodes.push(node); + } + }); + }); + + if (addedNodes.length && !flushScheduled) { + flushScheduled = true; + requestAnimationFrame(setUpAddedNodes); + } + } + + // Content swapped in after load, e.g. by the Interactivity API router. + function setUpAddedNodes() { + flushScheduled = false; + const added = addedNodes; + addedNodes = []; + + if (currentType === 'none' || prefersReducedMotion.matches) { + return; + } + + const nodes = added.filter((node) => node.isConnected); + trackedElements = trackedElements.filter((element) => { + if (element.isConnected) { + return true; + } + observer.unobserve(element); + return false; + }); + + const pending = []; + Object.keys(currentMap).forEach((selector) => { + try { + const found = new Set(); + nodes.forEach((node) => { + if (node.matches(selector)) { + found.add(node); + } + node.querySelectorAll(selector).forEach((element) => + found.add(element) + ); + }); + collect( + [...found].filter( + (element) => !element.classList.contains('ext-animate') + ), + currentMap[selector], + pending + ); + } catch {} + }); + apply(pending); + } + function resetAnimations() { // Early return for reduced motion - nothing to reset if (prefersReducedMotion.matches) { @@ -251,9 +331,9 @@ if (initialType === 'none') { const styleEl = document.createElement('style'); styleEl.id = 'ext-fouc-styles'; - let css = ''; + let css = '@keyframes ext-hold { from, to { opacity: 0; } } '; Object.keys(currentMap).forEach((selector) => { - css += `@media (prefers-reduced-motion: no-preference) { ${selector}:not(.ext-animate--off) { opacity: 0; } } `; + css += `@media (prefers-reduced-motion: no-preference) { ${selector}.ext-animate:not(.ext-animate--off) { opacity: 0; } ${selector}:not(.ext-animate--off):not(.ext-animate) { animation: ext-hold 3s; } } `; }); styleEl.textContent = css; document.head.appendChild(styleEl); diff --git a/inc/animations.php b/inc/animations.php index 14b2296..110ed59 100644 --- a/inc/animations.php +++ b/inc/animations.php @@ -203,8 +203,14 @@ function extendable_enqueue_animations() { ? $css_config[ $animation ] : 'opacity: 0;'; + // Shows an element after 3s if the script never sets it up. + $hold = 'ext-hold-' . $animation; + $animation_css .= '@keyframes ' . $hold . ' { from, to { ' . $css_rule . ' } } '; + // Only hide elements if reduced motion is not preferred. - $animation_css .= '@media (prefers-reduced-motion: no-preference) { ' . $selector . ':not(.ext-animate--off):not([data-ext-animated]) { ' . $css_rule . ' } } '; + $animation_css .= '@media (prefers-reduced-motion: no-preference) { ' + . $selector . '.ext-animate:not(.ext-animate--off):not([data-ext-animated]) { ' . $css_rule . ' } ' + . $selector . ':not(.ext-animate--off):not(.ext-animate) { animation: ' . $hold . ' 3s; } } '; } if ( ! empty( $animation_css ) ) { From 80573ab6a2d22671631c6185f6eb0769ab1dc1e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CAmjad?= Date: Tue, 24 Nov 2026 04:00:13 +0500 Subject: [PATCH 2/2] Keep the fail-safe hold off blocks that already animated The hold rule matched any .ext-animate--on element without the ext-animate class. Blocks with Interactivity API class directives, such as accordion items, get their class attribute rebuilt when they change state, which drops ext-animate. Opening an accordion item that had already animated hid it again for up to 3s. data-ext-animated survives the rebuild, which is why the long-lived hide rule already checks it; the hold rule now checks it too. --- assets/js/animations-interactivity.js | 2 +- inc/animations.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/js/animations-interactivity.js b/assets/js/animations-interactivity.js index 10946e7..34177b4 100644 --- a/assets/js/animations-interactivity.js +++ b/assets/js/animations-interactivity.js @@ -333,7 +333,7 @@ styleEl.id = 'ext-fouc-styles'; let css = '@keyframes ext-hold { from, to { opacity: 0; } } '; Object.keys(currentMap).forEach((selector) => { - css += `@media (prefers-reduced-motion: no-preference) { ${selector}.ext-animate:not(.ext-animate--off) { opacity: 0; } ${selector}:not(.ext-animate--off):not(.ext-animate) { animation: ext-hold 3s; } } `; + css += `@media (prefers-reduced-motion: no-preference) { ${selector}.ext-animate:not(.ext-animate--off) { opacity: 0; } ${selector}:not(.ext-animate--off):not(.ext-animate):not([data-ext-animated]) { animation: ext-hold 3s; } } `; }); styleEl.textContent = css; document.head.appendChild(styleEl); diff --git a/inc/animations.php b/inc/animations.php index 110ed59..2c1931c 100644 --- a/inc/animations.php +++ b/inc/animations.php @@ -210,7 +210,7 @@ function extendable_enqueue_animations() { // Only hide elements if reduced motion is not preferred. $animation_css .= '@media (prefers-reduced-motion: no-preference) { ' . $selector . '.ext-animate:not(.ext-animate--off):not([data-ext-animated]) { ' . $css_rule . ' } ' - . $selector . ':not(.ext-animate--off):not(.ext-animate) { animation: ' . $hold . ' 3s; } } '; + . $selector . ':not(.ext-animate--off):not(.ext-animate):not([data-ext-animated]) { animation: ' . $hold . ' 3s; } } '; } if ( ! empty( $animation_css ) ) {