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: 6 additions & 0 deletions docs/modules/playground.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const playground = (
const containerEl = document.querySelector(container);
const items = [...containerEl.querySelectorAll(item)];
const targetEl = document.querySelector(target);
const animationClasses = items.map(
(item) => `animate__${item.getAttribute('data-animation')}`,
);

setEndListener(targetEl, target.replace('.', ''));

Expand All @@ -31,6 +34,9 @@ const playground = (
clearAll(items);
const animation = `animate__${el.parentElement.getAttribute('data-animation')}`;

targetEl.classList.remove(...animationClasses);
// Flush removal so selecting the same animation starts a new run.
void targetEl.offsetWidth;
targetEl.classList.add('animate__animated', animation);
document.documentElement.classList.add('isPlaying');
document.documentElement.classList.remove('animationList-active');
Expand Down
65 changes: 65 additions & 0 deletions tests/playground-restart.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 compileAnimationList = require('../docsSource/compileAnimationList');
const css = fs.readFileSync(path.join(__dirname, '../animate.css'), 'utf8');
const playground = fs
.readFileSync(path.join(__dirname, '../docs/modules/playground.mjs'), 'utf8')
.replace('export default playground;', 'playground();');

(async () => {
for (const engine of [chromium, firefox, webkit]) {
const browser = await engine.launch();
try {
const page = await browser.newPage({reducedMotion: 'no-preference'});
await page.setContent(`<style>${css}:root {--animate-duration: 30s}</style>
<h1 class="callout-title animate__animated animate__zoomInDown">Animate.css</h1>
<div class="animation-list">${compileAnimationList()}</div>`);
await page.addScriptTag({content: playground});
// Use real CSS animations; slow durations make consecutive clicks deterministic.
for (const name of ['pulse', 'bounce']) {
await page.locator(`[data-animation="${name}"] .animation-item--title`).click();
const state = await page.locator('.callout-title').evaluate((el) => ({
animation: getComputedStyle(el).animationName,
classes: [...el.classList].filter(
(c) => c.startsWith('animate__') && c !== 'animate__animated',
),
}));
assert.strictEqual(
state.animation,
name,
'The most recent choice must win, regardless of CSS source order',
);
assert.deepStrictEqual(state.classes, ['animate__' + name]);
}
await page.evaluate(() => {
window.previousAnimation = document.querySelector('.callout-title').getAnimations()[0];
});
await page.locator('[data-animation="bounce"] .animation-item--title').click();
assert(
await page.evaluate(
() =>
document.querySelector('.callout-title').getAnimations()[0] !==
window.previousAnimation,
),
'Selecting the same animation must restart it',
);
await page.locator('.callout-title').evaluate((el) => el.getAnimations()[0].finish());
await page.waitForFunction(() => !document.documentElement.classList.contains('isPlaying'));
assert.strictEqual(
await page.locator('.callout-title').getAttribute('class'),
'callout-title',
);
console.log(
`PASS: ${engine.name()} intro interruption, switching, replay, completion cleanup`,
);
} finally {
await browser.close();
}
}
})().catch((error) => {
console.error(error);
process.exitCode = 1;
});