Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 110 additions & 30 deletions assets/js/animations-interactivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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`;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion inc/animations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down
Loading