Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Library: drag a horizontal chip strip to scroll it on a pointer device

Henryfc555f57cedd
diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx
index 0433c5580..c387d4a95 100644
--- a/frontend/src/App.jsx
+++ b/frontend/src/App.jsx
@@ -9,6 +9,7 @@ import { setNav } from './lib/nav.js'
import { initBackButton } from './lib/back.js'
import { useWakeLock } from './lib/wakelock.js'
import { installViewportGuard } from './lib/viewport-guard.js'
+import { installChipDrag } from './lib/hchips.js'
import { startFlow } from './sheets.jsx'
import Icon from './components/Icon.jsx'
import TabBar from './components/TabBar.jsx'
@@ -80,6 +81,9 @@ function Shell() {
const pathRef = useRef(loc.pathname)
// iOS leaves the page displaced after the keyboard goes away (see lib/viewport-guard.js).
useEffect(() => installViewportGuard(), [])
+ // Click-drag a horizontal chip strip to scroll it sideways (lib/hchips.js) — on a desktop
+ // browser there's otherwise no way to reach the filters past the edge.
+ useEffect(() => installChipDrag(), [])
useEffect(() => {
const onScroll = () => {
// Modals pins the body while a sheet is open; scrollY is 0 then, not a position.
diff --git a/frontend/src/index.css b/frontend/src/index.css
index 206733377..2946d8b15 100644
--- a/frontend/src/index.css
+++ b/frontend/src/index.css
@@ -444,6 +444,16 @@ h4.sec{
.chip:active{background:var(--surface-2)}
.chip.on{background:var(--acc);color:var(--on-acc);font-weight:500}
.chip.on .dim{color:color-mix(in srgb,var(--on-acc) 62%,transparent)}
+/* On a phone these rows scroll by swipe. On a pointer device lib/hchips.js lets you
+ click-drag anywhere on the row to pull it along; a grab cursor on rows that actually
+ overflow (data-xscroll, set by hchips.js on hover) is the hint. */
+@media (hover:hover) and (pointer:fine){
+ .chips[data-xscroll]{cursor:grab}
+}
+/* while a drag is in flight: no text selection, grabbing cursor everywhere (the `*` rule
+ outweighs button{cursor:pointer} so it holds even over a chip) */
+body.chips-dragging{cursor:grabbing;user-select:none;-webkit-user-select:none}
+body.chips-dragging *{cursor:inherit}
/* =========================== CONTROLS ==================================== */
diff --git a/frontend/src/lib/hchips.js b/frontend/src/lib/hchips.js
new file mode 100644
index 000000000..75c060fbb
--- /dev/null
+++ b/frontend/src/lib/hchips.js
@@ -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')
+ }
+}
diff --git a/frontend/src/lib/hchips.test.js b/frontend/src/lib/hchips.test.js
new file mode 100644
index 000000000..914dcf2b5
--- /dev/null
+++ b/frontend/src/lib/hchips.test.js
@@ -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)
+ })
+})