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.

fix: the arrival press of Show me is also skipped after the visitor pressed Show me themselves or left focus in the stage before the script ran

matin52729511fad7
diff --git a/assets/js/demo-page.js b/assets/js/demo-page.js
index 3b36156e..cccd88c4 100644
--- a/assets/js/demo-page.js
+++ b/assets/js/demo-page.js
@@ -358,7 +358,9 @@
// The same input, or focus arriving in the stage (a Tab into a field), also means the visitor got there before the Show me
// press on arrival, and that press is then skipped, so it cannot wipe or replace what they are doing. Focus counts whoever
// asks for it: a page's own focus() call makes a trusted focusin in Chrome too, so a do-it page must not move focus into
- // its stage before that press (none does).
+ // its stage before that press (none does). A press on Show me itself, which sits outside the stage, counts too: the press
+ // on arrival would start the run over. Input made before this script ran was not heard; focus it left in the stage is still
+ // there, so the press on arrival is skipped for that as well.
var visitorActed = false;
function setUpVisitorInput() {
['pointerdown', 'keydown', 'wheel', 'touchstart', 'click'].forEach(function (type) {
@@ -369,6 +371,7 @@
}, { capture: true, passive: true });
});
stage.addEventListener('focusin', function (e) { if (e.isTrusted) visitorActed = true; }, { capture: true, passive: true });
+ demoCtl.addEventListener('click', function (e) { if (e.isTrusted) visitorActed = true; });
}
// Scroll pages: Play scrolls the box from where it is to its end at a steady speed (the whole box in about six
@@ -474,7 +477,10 @@
else if (reduce && reduce.addListener) reduce.addListener(followReducedMotion);
if (doc.body.hasAttribute('data-hb-autoplay')) {
win.setTimeout(function () {
- if (demoCtl) { if (!(reduce && reduce.matches) && !visitorActed) demoCtl.click(); return; }
+ if (demoCtl) {
+ if (!(reduce && reduce.matches) && !visitorActed && !(stage && stage.contains(doc.activeElement))) demoCtl.click();
+ return;
+ }
if (scrollCtl) { if (!(reduce && reduce.matches)) scrollCtl.click(); return; }
if (loopCtl && !(reduce && reduce.matches)) {
// After Back or a reload a browser can bring Loop back already on; press Replay so the demo still starts.
diff --git a/tests/demo-page.test.js b/tests/demo-page.test.js
index 20bb516e..f016fb79 100644
--- a/tests/demo-page.test.js
+++ b/tests/demo-page.test.js
@@ -135,7 +135,7 @@ function standInPage(kind, reduced, bodyAttributes) {
CustomEvent: class { constructor(type, init) { this.type = type; this.detail = init && init.detail; } }
};
DP.boot(doc, win);
- return { stage, player, demo, sent, runTimers: () => timers.forEach(t => t.fn()), timerDelays: () => timers.map(t => t.ms) };
+ return { doc, stage, player, demo, sent, runTimers: () => timers.forEach(t => t.fn()), timerDelays: () => timers.map(t => t.ms) };
}
const visitor = { isTrusted: true }, script = { isTrusted: false };
@@ -173,6 +173,29 @@ test('events dispatched by script (isTrusted false) do not count as the visitor
assert.equal(page.sent.length, 0);
});
+test('the Show me press on arrival is skipped after the visitor pressed Show me themselves, which sits outside the stage', () => {
+ const page = standInPage('do', false);
+ page.demo.emit('click', visitor);
+ page.runTimers();
+ assert.equal(page.demo.clicks, 0);
+ assert.equal(page.sent.length, 0);
+ const other = standInPage('do', false);
+ other.demo.emit('click', script);
+ other.runTimers();
+ assert.equal(other.demo.clicks, 1);
+});
+
+test('the Show me press on arrival is skipped when focus is already in the stage, as when the visitor got there before the script ran', () => {
+ const page = standInPage('do', false);
+ page.doc.activeElement = page.stage;
+ page.runTimers();
+ assert.equal(page.demo.clicks, 0);
+ const elsewhere = standInPage('do', false);
+ elsewhere.doc.activeElement = elsewhere.doc.body;
+ elsewhere.runTimers();
+ assert.equal(elsewhere.demo.clicks, 1);
+});
+
test('a do-it page presses nothing on arrival under reduced motion', () => {
const page = standInPage('do', true);
page.runTimers();