Skip to content

176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.

feat: pressing See inside or Compare plays a plays-once or do-it demo once (never under reduced motion), and Copy prompt is set up only on a page with a prompt

matin2c52f4b4af2f
diff --git a/assets/js/demo-page.js b/assets/js/demo-page.js
index 5ed9939d..03230bee 100644
--- a/assets/js/demo-page.js
+++ b/assets/js/demo-page.js
@@ -7,7 +7,8 @@
* loops start paused).
* On scroll pages Play scrolls the box, Back to top jumps to its top and the visitor's own input stops it.
* The mode switch (Normal, See inside, Compare) marks the stage with data-hb-mode, shows that mode's note and sends
- * "hb:mode", so the page draws the mode. Pages without a prompt get everything but the prompt parts.
+ * "hb:mode", so the page draws the mode; after See inside or Compare it plays the demo once (Replay or Show me), except under
+ * reduced motion. Pages without a prompt get everything but the prompt parts.
* Opened from disk (file:), the page's links to folders open each folder's index.html instead of its listing.
* The pure helpers are exported for tests/demo-page.test.js. */
(function (root, factory) {
@@ -486,12 +487,23 @@
}
// See inside and Compare: the mode switch above the stage. Pressing a mode presses its button (one at a time), marks the
- // stage with data-hb-mode, shows the note for that mode (Normal has none) and sends "hb:mode", so the page draws the mode.
- // Nothing scrolls and nothing takes focus, and a running Play goes on: the switch sits outside the stage, whose input
- // stops Play.
+ // stage with data-hb-mode and shows the note for that mode (Normal has none), and only then sends "hb:mode", so the page
+ // finds the buttons, the notes and the mark already changed when it draws the mode. A press on the mode already pressed
+ // sends "hb:mode" again. Nothing scrolls and nothing takes focus, and a running Play goes on: the switch sits outside the
+ // stage, whose input stops Play.
+ // Once the page has drawn See inside or Compare, the demo plays once by itself so the view is seen working: Replay on a
+ // plays-once page, Show me on a do-it page (a press of a mode is the visitor acting, so the Show me press on arrival is then
+ // skipped). Never under reduced motion, never after Normal, never on a scroll page (Play is there for that) or a loop (it is
+ // already moving). The page's own hb:mode listener presses nothing.
function setUpModes() {
var buttons = Array.prototype.slice.call(modesCtl.querySelectorAll('[data-hb-mode]'));
var notes = Array.prototype.slice.call(page.querySelectorAll('[data-hb-for]'));
+ var kind = doc.body.getAttribute('data-hb-kind');
+ function playOnce() {
+ if (reduce && reduce.matches) return;
+ if (kind === 'once' && replayCtl) replayCtl.click();
+ else if (kind === 'do' && demoCtl) { visitorActed = true; demoCtl.click(); }
+ }
function choose(mode) {
buttons.forEach(function (b) {
var on = b.getAttribute('data-hb-mode') === mode;
@@ -501,6 +513,7 @@
notes.forEach(function (n) { n.hidden = n.getAttribute('data-hb-for') !== mode; });
if (stage) stage.setAttribute('data-hb-mode', mode);
doc.dispatchEvent(new win.CustomEvent('hb:mode', { detail: { mode: mode } }));
+ if (mode !== 'normal') playOnce();
}
buttons.forEach(function (b) {
b.addEventListener('click', function () { choose(b.getAttribute('data-hb-mode')); });
@@ -540,7 +553,7 @@
if (win.location.protocol === 'file:') openFoldersAsPages();
if (promptEl) setUpPrompt();
- if (copyBtn) setUpCopy();
+ if (promptEl && copyBtn) setUpCopy();
if (tryStep) ['input', 'change', 'click'].forEach(function (type) { tryStep.addEventListener(type, onSettingsChange); });
if (slowCtl) slowCtl.addEventListener('change', replay);
if (pauseCtl) pauseCtl.addEventListener('click', function () { setPaused(!paused); });
diff --git a/tests/demo-page.test.js b/tests/demo-page.test.js
index 65ea0148..9bdce19f 100644
--- a/tests/demo-page.test.js
+++ b/tests/demo-page.test.js
@@ -130,9 +130,11 @@ test('motionNote says what reduced motion changes in the player bar', () => {
// served over that protocol (http: otherwise) and holds one link for each href.
// `extra` ({ modes, noPrompt }): modes adds the mode switch (Normal, See inside, Compare) and one note for See inside and one
// for Compare; noPrompt leaves the prompt out.
+// A plays-once page also has Replay and a loop has Pause; `log` keeps, in order, every hb:mode sent and every press of Replay
+// or Show me, and each hb:mode event carries `seen`: the stage's mark, the buttons' aria-pressed and the notes on show then.
function standInPage(kind, reduced, bodyAttributes, copy, links, extra) {
const opts = extra || {};
- const timers = [], sent = [];
+ const timers = [], sent = [], log = [];
const node = () => ({
attrs: {}, handlers: {}, children: [], clicks: 0, textContent: '', innerHTML: '', classList: { add() {}, remove() {}, toggle() {} },
addEventListener(type, fn) { (this.handlers[type] = this.handlers[type] || []).push(fn); },
@@ -142,9 +144,13 @@ function standInPage(kind, reduced, bodyAttributes, copy, links, extra) {
hasAttribute(name) { return name in this.attrs; },
removeAttribute(name) { delete this.attrs[name]; },
appendChild(child) { this.children.push(child); return child; },
- insertAdjacentElement() {}, contains(other) { return other === this; }, click() { this.clicks++; }
+ insertAdjacentElement() {}, contains(other) { return other === this; }, click() { this.clicks++; if (this.onClick) this.onClick(); }
});
- const stage = node(), player = node(), prompt = node(), demo = node(), play = node(), top = node();
+ const stage = node(), player = node(), prompt = node(), demo = node(), play = node(), top = node(), replay = node();
+ // A loop's Pause button: the shared script looks for its label inside it
+ const pause = Object.assign(node(), { querySelector: () => null });
+ replay.onClick = () => log.push('Replay');
+ demo.onClick = () => log.push('Show me');
const modeButtons = opts.modes ? ['normal', 'inside', 'compare'].map(mode => {
const b = node();
b.setAttribute('data-hb-mode', mode);
@@ -162,6 +168,7 @@ function standInPage(kind, reduced, bodyAttributes, copy, links, extra) {
const button = copy && Object.assign(node(), { querySelector: () => null });
if (copy) prompt.textContent = copy.prompt;
const wanted = { '.hb-prompt': !opts.noPrompt && prompt, '.stage': stage, '.hb-player': player, '[data-hb-demo]': kind === 'do' && demo,
+ '[data-hb-replay]': kind === 'once' && replay, '[data-hb-pause]': kind === 'loop' && pause,
'[data-hb-autoscroll]': kind === 'scroll' && play, '[data-hb-top]': kind === 'scroll' && top, '.hb-copy': button,
'.hb-try': copy && copy.controls && Object.assign(node(), { querySelectorAll: () => copy.controls }),
'[data-hb-modes]': modes };
@@ -172,7 +179,14 @@ function standInPage(kind, reduced, bodyAttributes, copy, links, extra) {
body.setAttribute('data-hb-autoplay', '');
Object.keys(bodyAttributes || {}).forEach(name => body.setAttribute(name, bodyAttributes[name]));
const anchors = ((links && links.hrefs) || []).map(href => { const a = node(); a.setAttribute('href', href); return a; });
- const doc = { body, querySelector: selector => (selector === '.hb-page' ? page : null), createElement: node, dispatchEvent: event => sent.push(event),
+ const doc = { body, querySelector: selector => (selector === '.hb-page' ? page : null), createElement: node, dispatchEvent: event => {
+ // What the page could see when hb:mode reached it: the stage's mark, the pressed buttons and the notes on show
+ if (event.type === 'hb:mode') {
+ event.seen = { mark: stage.getAttribute('data-hb-mode'), pressed: modeButtons.map(b => b.getAttribute('aria-pressed')), shown: notes.map(n => !n.hidden) };
+ log.push('hb:mode ' + event.detail.mode);
+ }
+ sent.push(event);
+ },
querySelectorAll: selector => (selector === 'a[href]' ? anchors : []) };
const win = {
document: doc, // the page's own document, as in a browser
@@ -183,7 +197,7 @@ function standInPage(kind, reduced, bodyAttributes, copy, links, extra) {
};
if (copy) Object.assign(win, { navigator: { clipboard: copy.clipboard }, getComputedStyle: () => ({ display: '' }) });
DP.boot(doc, win);
- return { doc, win, stage, player, demo, play, button, anchors, sent, modeButtons, notes,
+ return { doc, win, stage, player, demo, play, replay, pause, button, anchors, sent, log, modeButtons, notes,
runTimers: () => timers.forEach(t => t.fn()), timerDelays: () => timers.map(t => t.ms) };
}
const visitor = { isTrusted: true }, script = { isTrusted: false };
@@ -330,6 +344,58 @@ test('the mode switch gives the class on to the pressed mode only', () => {
assert.deepEqual(marked(), [true, false, false]);
});
+test('a mode press changes the buttons, the notes and the stage mark before hb:mode is sent', () => {
+ const page = standInPage('scroll', false, {}, null, null, { modes: true });
+ page.modeButtons[1].emit('click', visitor);
+ page.modeButtons[2].emit('click', visitor);
+ assert.deepEqual(page.sent.filter(e => e.type === 'hb:mode').map(e => e.seen), [
+ { mark: 'inside', pressed: ['false', 'true', 'false'], shown: [true, false] },
+ { mark: 'compare', pressed: ['false', 'false', 'true'], shown: [false, true] }
+ ]);
+});
+
+test('See inside and Compare play a plays-once page once, pressing Replay after hb:mode; the pressed mode again plays again; Normal plays nothing', () => {
+ const page = standInPage('once', false, {}, null, null, { modes: true });
+ const [normal, inside, compare] = page.modeButtons;
+ inside.emit('click', visitor);
+ compare.emit('click', visitor);
+ compare.emit('click', visitor);
+ normal.emit('click', visitor);
+ assert.deepEqual(page.log, ['hb:mode inside', 'Replay', 'hb:mode compare', 'Replay', 'hb:mode compare', 'Replay', 'hb:mode normal']);
+});
+
+test('See inside and Compare press Show me on a do-it page after hb:mode, and the Show me press on arrival is then skipped', () => {
+ const page = standInPage('do', false, {}, null, null, { modes: true });
+ page.modeButtons[2].emit('click', visitor);
+ assert.deepEqual(page.log, ['hb:mode compare', 'Show me']);
+ page.runTimers();
+ assert.equal(page.demo.clicks, 1, 'the press on arrival is skipped: the visitor acted first');
+});
+
+test('under reduced motion a mode press plays nothing', () => {
+ for (const kind of ['once', 'do']) {
+ const page = standInPage(kind, true, {}, null, null, { modes: true });
+ page.modeButtons[1].emit('click', visitor);
+ page.modeButtons[2].emit('click', visitor);
+ assert.deepEqual(page.log, ['hb:mode inside', 'hb:mode compare'], kind);
+ }
+});
+
+test('a mode press plays nothing on a scroll page or a loop', () => {
+ const scroll = standInPage('scroll', false, {}, null, null, { modes: true });
+ scroll.modeButtons[1].emit('click', visitor);
+ assert.equal(scroll.play.clicks, 0, 'Play is not pressed');
+ const loop = standInPage('loop', false, {}, null, null, { modes: true });
+ loop.modeButtons[2].emit('click', visitor);
+ assert.equal(loop.pause.clicks, 0, 'Pause is not pressed');
+ assert.deepEqual(loop.log, ['hb:mode compare']);
+});
+
+test('a page without a prompt does not set up Copy prompt, even when it has the button', () => {
+ const page = standInPage('once', false, {}, { prompt: '', controls: null, clipboard: { writeText: () => Promise.resolve() } }, null, { noPrompt: true });
+ assert.equal((page.button.handlers.click || []).length, 0);
+});
+
// The home page loads the shared script too and has no .hb-page: boot() returns before it touches anything else.
test('a document with no .hb-page, such as the home page, is skipped without touching anything else', () => {
const doc = { querySelector: () => null };