Merge !119: drag a horizontal chip strip to scroll it on a pointer device (HenryByte, #147)

This commit is contained in:
Duarte Santos committed 2026-09-12 13:05:21 +02:00
commit 413bce5bf8
4 files changed
+217

No files matched your search

+91
View File
@@ -0,0 +1,91 @@
/* Horizontal chip strips (`.chips` — the body-part and equipment filters in the Library
* and the exercise picker, the muscle explorer) scroll by touch-swipe on a phone and by a
* two-finger swipe on a trackpad. A plain mouse and a click do nothing sideways, so on a
* desktop browser the chips that overflow the row to the right are unreachable — index.css
* hides the scrollbar (`scrollbar-width:none`) so there is no handle either.
*
* `installChipDrag` adds the missing way in: press and drag anywhere on a strip to pull it
* along, the way a swipe does on a phone. index.css shows a grab cursor as the hint.
*/
const DRAG_SLOP = 6 // px of travel before a press counts as a drag, not a click
const doc = () => (typeof document !== 'undefined' ? document : null)
export function installChipDrag(root = doc()) {
if (!root?.addEventListener) return () => {}
const win = root.defaultView || (typeof window !== 'undefined' ? window : null)
const body = root.body || root.documentElement
if (!win) return () => {}
let strip = null, startX = 0, startLeft = 0, dragging = false
// cursor hint on hover: only rows that actually overflow are draggable
const onOver = e => {
const s = e.target?.closest?.('.chips')
if (!s) return
if (s.scrollWidth > s.clientWidth) s.dataset.xscroll = ''
else delete s.dataset.xscroll
}
const swallowClick = e => {
win.removeEventListener('click', swallowClick, true)
e.stopPropagation()
e.preventDefault()
}
const onMove = e => {
if (!strip) return
const dx = e.clientX - startX
if (!dragging) {
if (Math.abs(dx) < DRAG_SLOP) return
dragging = true
body?.classList.add('chips-dragging')
win.getSelection?.()?.removeAllRanges?.()
try { strip.setPointerCapture?.(e.pointerId) } catch { /* pointer already gone */ }
}
strip.scrollLeft = startLeft - dx
if (e.cancelable) e.preventDefault()
}
const onUp = () => {
win.removeEventListener('pointermove', onMove)
win.removeEventListener('pointerup', onUp, true)
win.removeEventListener('pointercancel', onUp, true)
if (dragging) {
body?.classList.remove('chips-dragging')
// The browser fires one click after the drag; drop it so releasing over a chip
// doesn't toggle that filter. Self-removes on that click, 0ms fallback if none comes.
win.addEventListener('click', swallowClick, true)
win.setTimeout(() => win.removeEventListener('click', swallowClick, true), 0)
}
strip = null
dragging = false
}
const onDown = e => {
if (e.button != null && e.button !== 0) return // primary button only
if (e.pointerType && e.pointerType !== 'mouse') return // touch / pen already scroll
const s = e.target?.closest?.('.chips')
if (!s || s.scrollWidth <= s.clientWidth) return
strip = s
startX = e.clientX
startLeft = s.scrollLeft
dragging = false
win.addEventListener('pointermove', onMove)
win.addEventListener('pointerup', onUp, true)
win.addEventListener('pointercancel', onUp, true)
}
root.addEventListener('pointerover', onOver, true)
root.addEventListener('pointerdown', onDown, true)
return () => {
root.removeEventListener('pointerover', onOver, true)
root.removeEventListener('pointerdown', onDown, true)
win.removeEventListener('pointermove', onMove)
win.removeEventListener('pointerup', onUp, true)
win.removeEventListener('pointercancel', onUp, true)
win.removeEventListener('click', swallowClick, true)
body?.classList.remove('chips-dragging')
}
}
+112
View File
@@ -0,0 +1,112 @@
// @vitest-environment happy-dom
import { afterEach, describe, expect, it } from 'vitest'
import { installChipDrag } from './hchips.js'
// A real .chips element with overridable scroll geometry and a clamping scrollLeft.
const mountStrip = ({ scrollWidth = 800, clientWidth = 300 } = {}) => {
document.body.innerHTML = '<div class="chips"><button class="chip">a</button></div>'
const row = document.querySelector('.chips')
Object.defineProperties(row, {
scrollWidth: { value: scrollWidth, configurable: true },
clientWidth: { value: clientWidth, configurable: true },
})
let left = 0
Object.defineProperty(row, 'scrollLeft', {
get() { return left },
set(v) { left = Math.max(0, Math.min(v, scrollWidth - clientWidth)) },
configurable: true,
})
return row
}
const pointer = (type, opts = {}) => new PointerEvent(type, {
bubbles: true, cancelable: true, pointerId: 1, pointerType: 'mouse', button: 0, ...opts,
})
let stop = () => {}
afterEach(() => { stop(); stop = () => {}; document.body.className = ''; document.body.innerHTML = '' })
describe('installChipDrag', () => {
const drag = (chip, ...xs) => {
chip.dispatchEvent(pointer('pointerdown', { clientX: xs[0] }))
for (const x of xs.slice(1)) window.dispatchEvent(pointer('pointermove', { clientX: x }))
}
it('pulls the strip along once the press passes the slop threshold', () => {
const row = mountStrip()
stop = installChipDrag(document)
const chip = document.querySelector('.chip')
drag(chip, 200, 197) // 3px — still a click, nothing moves
expect(row.scrollLeft).toBe(0)
expect(document.body.classList.contains('chips-dragging')).toBe(false)
window.dispatchEvent(pointer('pointermove', { clientX: 140 })) // now 60px left
expect(row.scrollLeft).toBe(60)
expect(document.body.classList.contains('chips-dragging')).toBe(true)
window.dispatchEvent(pointer('pointerup', { clientX: 140 }))
expect(document.body.classList.contains('chips-dragging')).toBe(false)
})
it('swallows the click that follows a real drag, but not a plain click', () => {
const row = mountStrip()
stop = installChipDrag(document)
const chip = document.querySelector('.chip')
// plain click through (tiny move, released)
drag(chip, 100, 102)
window.dispatchEvent(pointer('pointerup', { clientX: 102 }))
const plain = pointer('click')
chip.dispatchEvent(plain)
expect(plain.defaultPrevented).toBe(false)
// real drag, then its trailing click is eaten
drag(chip, 300, 260, 220)
expect(row.scrollLeft).toBe(80)
window.dispatchEvent(pointer('pointerup', { clientX: 220 }))
const ghost = pointer('click')
chip.dispatchEvent(ghost)
expect(ghost.defaultPrevented).toBe(true)
// and only that one click
const next = pointer('click')
chip.dispatchEvent(next)
expect(next.defaultPrevented).toBe(false)
})
it('leaves touch and non-overflowing rows alone', () => {
const row = mountStrip()
stop = installChipDrag(document)
const chip = document.querySelector('.chip')
chip.dispatchEvent(pointer('pointerdown', { clientX: 200, pointerType: 'touch' }))
window.dispatchEvent(pointer('pointermove', { clientX: 120, pointerType: 'touch' }))
expect(row.scrollLeft).toBe(0)
const tight = mountStrip({ scrollWidth: 280, clientWidth: 300 })
drag(document.querySelector('.chip'), 200, 120)
expect(tight.scrollLeft).toBe(0)
})
it('marks an overflowing row with data-xscroll on hover, clears it otherwise', () => {
const row = mountStrip()
stop = installChipDrag(document)
document.querySelector('.chip').dispatchEvent(pointer('pointerover'))
expect(row.hasAttribute('data-xscroll')).toBe(true)
const tight = mountStrip({ scrollWidth: 200, clientWidth: 300 })
document.querySelector('.chip').dispatchEvent(pointer('pointerover'))
expect(tight.hasAttribute('data-xscroll')).toBe(false)
})
it('teardown removes the listeners', () => {
const row = mountStrip()
const off = installChipDrag(document)
off()
const chip = document.querySelector('.chip')
chip.dispatchEvent(pointer('pointerdown', { clientX: 300 }))
window.dispatchEvent(pointer('pointermove', { clientX: 200 }))
expect(row.scrollLeft).toBe(0)
})
})