Skip to content
Open
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
6 changes: 5 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1149,14 +1149,18 @@ <h2>Usage with Javascript</h2>

// When the animation ends, we clean the classes and resolve the Promise
function handleAnimationEnd(event) {
if (event.target !== node) return;

event.stopPropagation();
node.classList.remove(`${prefix}animated`, animationName);
node.removeEventListener('animationend', handleAnimationEnd);
resolve('Animation ended');
}

node.addEventListener('animationend', handleAnimationEnd, {once: true});
node.addEventListener('animationend', handleAnimationEnd);
});
</code></pre>
<p>The handler ignores animation events that bubble up from children. It removes itself after the selected element finishes; using <code>{once: true}</code> would remove it on the first child event instead.</p>
<p>And use it like this:</p>
<pre><code class="language-javascript">animateCSS('.my-element', 'bounce');

Expand Down
7 changes: 6 additions & 1 deletion docsSource/sections/04-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ const animateCSS = (element, animation, prefix = 'animate__') =>

// When the animation ends, we clean the classes and resolve the Promise
function handleAnimationEnd(event) {
if (event.target !== node) return;

event.stopPropagation();
node.classList.remove(`${prefix}animated`, animationName);
node.removeEventListener('animationend', handleAnimationEnd);
resolve('Animation ended');
}

node.addEventListener('animationend', handleAnimationEnd, {once: true});
node.addEventListener('animationend', handleAnimationEnd);
});
```

The handler ignores animation events that bubble up from children. It removes itself after the selected element finishes; using `{once: true}` would remove it on the first child event instead.

And use it like this:

```javascript
Expand Down
65 changes: 65 additions & 0 deletions tests/javascript-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Run with Node after installing @playwright/test and its browsers.
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const {chromium, firefox, webkit} = require('@playwright/test');
const markdown = fs.readFileSync(
path.join(__dirname, '../docsSource/sections/04-javascript.md'),
'utf8',
);
const snippet = [...markdown.matchAll(/```javascript\n([\s\S]*?)```/g)]
.map((match) => match[1])
.find((code) => code.includes('const animateCSS ='));
assert(snippet, 'Expected the documented animateCSS helper');

(async () => {
for (const engine of [chromium, firefox, webkit]) {
const browser = await engine.launch();
try {
const page = await browser.newPage();
await page.setContent('<div id="parent"><span id="child">Child</span></div>');
await page.addScriptTag({content: snippet + '\nwindow.animateCSS = animateCSS;'});
const result = await page.evaluate(async () => {
const parent = document.querySelector('#parent');
const child = document.querySelector('#child');
let resolved = false;
let bubbled = 0;
document.body.addEventListener('animationend', () => bubbled++);
const completion = window.animateCSS('#parent', 'bounce').then(() => {
resolved = true;
});
child.dispatchEvent(
new AnimationEvent('animationend', {bubbles: true, animationName: 'fadeIn'}),
);
await Promise.resolve();
const afterChild = {
resolved,
animated: parent.classList.contains('animate__animated'),
bubbled,
};
parent.dispatchEvent(
new AnimationEvent('animationend', {bubbles: true, animationName: 'bounce'}),
);
await completion;
parent.classList.add('animate__animated', 'animate__bounce');
parent.dispatchEvent(
new AnimationEvent('animationend', {bubbles: true, animationName: 'bounce'}),
);
return {
afterChild,
resolved,
listenerRemoved: parent.classList.contains('animate__bounce'),
};
});
assert.deepStrictEqual(result.afterChild, {resolved: false, animated: true, bubbled: 1});
assert(result.resolved, 'The parent event should resolve the Promise');
assert(result.listenerRemoved, 'The listener should be removed after the parent finishes');
console.log(`PASS: ${engine.name()} child bubbling, parent completion and cleanup`);
} finally {
await browser.close();
}
}
})().catch((error) => {
console.error(error);
process.exitCode = 1;
});