diff --git a/assets/js/animations-interactivity.js b/assets/js/animations-interactivity.js index 661e4bf..34177b4 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):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 14b2296..2c1931c 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):not([data-ext-animated]) { animation: ' . $hold . ' 3s; } } '; } if ( ! empty( $animation_css ) ) {