Add scoped mouse wheel navigation for desktop carousels
This commit is contained in:
+38
-3
@@ -3,8 +3,9 @@ import assert from 'node:assert/strict';
|
||||
import vm from 'node:vm';
|
||||
import fs from 'node:fs';
|
||||
const source=fs.readFileSync(process.env.CAROUSEL_SOURCE || new URL('../assets/custom/js/carousel.js',import.meta.url),'utf8');
|
||||
function fixture({width=390,size=330,length=5}={}) {
|
||||
function fixture({width=390,size=330,length=5,platform='Win32',fine=true,reduced=false}={}) {
|
||||
const timers=new Map();let timer=0, resize;
|
||||
let now=1000;
|
||||
const target=()=>({listeners:{},dataset:{},classList:{toggle(){},add(){},remove(){}},addEventListener(n,f){(this.listeners[n]??=[]).push(f);},emit(n,e={}){for(const f of this.listeners[n]||[])f(e);}});
|
||||
const images=Array.from({length},()=>({...target(),tagName:'IMG'}));
|
||||
const root={...target(),clientWidth:width};
|
||||
@@ -14,9 +15,43 @@ function fixture({width=390,size=330,length=5}={}) {
|
||||
const prev=target(),next=target(),count={},controls={};
|
||||
root.querySelector=s=>({'[data-carousel-track]':track,'[data-carousel-prev]':prev,'[data-carousel-next]':next,'[data-carousel-count]':count,'[data-carousel-controls]':controls}[s]);
|
||||
root.querySelectorAll=()=>[];
|
||||
vm.runInNewContext(source,{matchMedia:()=>({matches:false}),getComputedStyle:()=>({columnGap:'24'}),document:{readyState:'complete',querySelectorAll:()=>[root]},window:target(),setTimeout:f=>{timers.set(++timer,f);return timer;},clearTimeout:id=>timers.delete(id),ResizeObserver:class{constructor(f){resize=f;}observe(){}},IntersectionObserver:class{observe(){}}});
|
||||
return {root,track,images,prev,next,count,calls,resize:()=>resize(),settle(){const pending=[...timers.values()];timers.clear();pending.forEach(f=>f());},swipe(left){track.scrollLeft=left;track.emit('scroll');}};
|
||||
vm.runInNewContext(source,{navigator:{platform},performance:{now:()=>now},matchMedia:q=>({matches:q.includes('reduced-motion')?reduced:fine}),getComputedStyle:()=>({columnGap:'24'}),document:{readyState:'complete',querySelectorAll:()=>[root]},window:target(),setTimeout:f=>{timers.set(++timer,f);return timer;},clearTimeout:id=>timers.delete(id),ResizeObserver:class{constructor(f){resize=f;}observe(){}},IntersectionObserver:class{observe(){}}});
|
||||
return {root,track,images,prev,next,count,calls,resize:()=>resize(),advance(ms=300){now+=ms;},wheel(props={}){const e={deltaX:0,deltaY:100,deltaMode:0,cancelable:true,defaultPrevented:false,stopped:false,target:{closest(){return null;}},preventDefault(){this.defaultPrevented=true;},stopPropagation(){this.stopped=true;},...props};track.emit('wheel',e);return e;},settle(){const pending=[...timers.values()];timers.clear();pending.forEach(f=>f());},swipe(left){track.scrollLeft=left;track.emit('scroll');}};
|
||||
}
|
||||
|
||||
test('mouse wheel steps once per burst, reverses, and releases page at both ends',()=>{
|
||||
const f=fixture({length:3});
|
||||
assert.equal(f.wheel({deltaY:-100}).defaultPrevented,false);
|
||||
assert.equal(f.wheel().defaultPrevented,true);assert.equal(f.count.textContent,'2 / 3');
|
||||
f.wheel();assert.equal(f.count.textContent,'2 / 3');
|
||||
f.advance();f.wheel();assert.equal(f.count.textContent,'3 / 3');
|
||||
f.advance();assert.equal(f.wheel().defaultPrevented,false);
|
||||
f.wheel({deltaY:-120});assert.equal(f.count.textContent,'2 / 3');
|
||||
f.wheel({deltaY:120});assert.equal(f.count.textContent,'3 / 3');
|
||||
});
|
||||
test('line/page wheels work and reduced motion uses instant scrolling',()=>{
|
||||
const f=fixture({reduced:true});
|
||||
f.wheel({deltaMode:1,deltaY:3});assert.equal(f.count.textContent,'2 / 5');assert.equal(f.calls.at(-1).behavior,'instant');
|
||||
f.advance();f.wheel({deltaMode:2,deltaY:1});assert.equal(f.count.textContent,'3 / 5');
|
||||
f.advance();f.wheel({deltaY:98.53334554036458});assert.equal(f.count.textContent,'4 / 5','scaled mouse deltas need not be integers');
|
||||
});
|
||||
test('macOS, touch devices, fitting galleries, zoom and form controls retain native input',()=>{
|
||||
for(const settings of [{platform:'MacIntel'},{fine:false},{width:1920,size:560,length:2}]){
|
||||
const f=fixture(settings),n=f.calls.length;assert.equal(f.wheel().defaultPrevented,false);assert.equal(f.calls.length,n);
|
||||
}
|
||||
for(const props of [{ctrlKey:true},{metaKey:true},{shiftKey:true},{altKey:true},{cancelable:false},{target:{closest:()=>({})}}]){
|
||||
const f=fixture(),n=f.calls.length;f.wheel(props);assert.equal(f.calls.length,n);
|
||||
}
|
||||
});
|
||||
test('horizontal, diagonal and fine trackpad gestures are not converted into slide steps',()=>{
|
||||
const f=fixture(),n=f.calls.length;
|
||||
for(const props of [{deltaX:90,deltaY:0},{deltaX:12,deltaY:100},{deltaY:2.5},{deltaY:17}]){
|
||||
assert.equal(f.wheel(props).defaultPrevented,false);
|
||||
}
|
||||
assert.equal(f.wheel().defaultPrevented,false,'a coarse sample inside a continuous gesture stays native');
|
||||
assert.equal(f.calls.length,n);
|
||||
f.advance();assert.equal(f.wheel().defaultPrevented,true);
|
||||
});
|
||||
test('late image loads do not cancel smooth desktop navigation',()=>{
|
||||
const f=fixture({width:1663,size:1280});f.next.emit('click');const n=f.calls.length;
|
||||
f.images.forEach(i=>i.emit('load'));
|
||||
|
||||
Reference in New Issue
Block a user