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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
SwipeRow: the gesture core for swiping a set (axis lock, edge zones, reveal, full swipe, fling, RTL)
Duarte Santos2864bb379839
diff --git a/frontend/src/components/SwipeRow.jsx b/frontend/src/components/SwipeRow.jsxnew file mode 100644index 000000000..3f1816a31--- /dev/null+++ b/frontend/src/components/SwipeRow.jsx@@ -0,0 +1,49 @@+import { forwardRef, useEffect, useImperativeHandle } from 'react'+import { t } from '../lib/i18n.js'+import { useSwipeRow } from '../lib/use-swipe-row.js'+import Icon from './Icon.jsx'++// A set row you can swipe (v1.3.11): toward the start of the line deletes, toward the end copies.+// The gesture lives in lib/use-swipe-row.js; this draws it. Two panes sit behind the row, one per+// side, each growing with the swipe and holding a real button. While the row is shut they are+// zero wide (nothing shows through at the rounded corners, the SwipeToDelete lesson from QA+// 1.3.9), and while a pane is not the one showing it is inert, hidden from screen readers and out+// of the tab order: a set row already has four to six tab stops, and the set-number menu is the+// keyboard's and the screen reader's way to the same two actions.+//+// The row keeps an opaque background, so a done row's tint moves with it rather than letting the+// panes show through. `data-swipe-ignore` keeps the Cards layout's own swipe (SwipeCards) off it:+// on a set row a sideways swipe is about the set, everywhere else on the card it still turns it.+const SwipeRow = forwardRef(function SwipeRow({ children, canDelete = true, onDelete, onCopy, onBlocked, onStart, closeKey, flash, peek }, ref) {+ const sw = useSwipeRow({+ canDelete,+ onCommit: kind => (kind === 'delete' ? onDelete?.() : onCopy?.()),+ onBlocked, onStart, closeKey,+ })+ useImperativeHandle(ref, () => ({ peek: sw.peek }), [sw.peek])+ // A peek asked for by the owner (the first-use hint), once per new value.+ useEffect(() => { if (peek) sw.peek() }, [peek, sw.peek])+ // A copy or an undo lands with a short accent flash on the row it brought (`flash` changes).+ useEffect(() => {+ const el = sw.outerRef.current+ if (!flash || !el) return+ el.classList.remove('flash'); void el.offsetWidth; el.classList.add('flash')+ const tm = setTimeout(() => el.classList.remove('flash'), 600)+ return () => clearTimeout(tm)+ }, [flash, sw.outerRef])+ const shut = side => sw.openSide !== side+ return <div ref={sw.outerRef} className="swrow" data-swipe-ignore="" {...sw.handlers}>+ <div ref={sw.cpRef} className="swpane cp" aria-hidden={shut('copy') || undefined} inert={shut('copy') ? true : undefined}>+ <button type="button" tabIndex={shut('copy') ? -1 : 0} onClick={sw.pressCopy}>+ <span className="lab"><Icon name="copy" /><b>{t('Copy')}</b></span>+ </button>+ </div>+ <div ref={sw.delRef} className={'swpane del' + (canDelete ? '' : ' dis')} aria-hidden={shut('delete') || undefined} inert={shut('delete') ? true : undefined}>+ <button type="button" tabIndex={shut('delete') ? -1 : 0} onClick={sw.pressDelete}>+ <span className="lab"><Icon name="trash" /><b>{t('Delete')}</b></span>+ </button>+ </div>+ <div ref={sw.frontRef} className="swfront">{children}</div>+ </div>+})+export default SwipeRowdiff --git a/frontend/src/index.css b/frontend/src/index.cssindex a04ac677e..ffd9fa06d 100644--- a/frontend/src/index.css+++ b/frontend/src/index.css@@ -1080,6 +1080,42 @@ body.chips-dragging *{cursor:inherit}.setrow.done .n{background:var(--acc);color:var(--on-acc)}.setrow.done{opacity:.45}+/* Swipe on sets (v1.3.11, components/SwipeRow.jsx). The row slides over two panes: Copy at the+ start of the line, Delete at its end, each as wide as the swipe has gone and zero wide at rest.+ pan-y leaves vertical scroll to the browser and hands sideways moves to the row. */+.swrow{position:relative;overflow:hidden;border-radius:10px;touch-action:pan-y;-webkit-touch-callout:none}+.swrow.dragging{-webkit-user-select:none;user-select:none}+.swfront{position:relative;z-index:1;background:var(--surface);will-change:transform}+.swpane{position:absolute;top:0;bottom:0;width:0;overflow:hidden;z-index:0}+.swpane.del{inset-inline-end:0;background:color-mix(in srgb,var(--red) 22%,var(--surface))}+.swpane.cp{inset-inline-start:0;background:var(--acc-soft)}+.swpane button{position:absolute;top:0;bottom:0;width:76px;padding:0;color:#fff;transition:width .16s var(--ease)}+.swpane.del button{inset-inline-end:0;background:var(--red)}+.swpane.cp button{inset-inline-start:0;background:var(--acc);color:var(--on-acc)}+.swpane.armed button{width:100%}+.swpane .lab{position:absolute;top:0;bottom:0;width:76px;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:2px;font-size:12px}+.swpane.del .lab{inset-inline-start:0}+.swpane.cp .lab{inset-inline-end:0}+.swpane .lab .icn{font-size:21px;transform:scale(.8);transition:transform .16s var(--ease)}+.swpane .lab b{font-weight:500}+.swpane.armed .lab .icn{transform:scale(1)}+.swpane.armed .lab b{font-weight:800}+/* The last set cannot go: the delete side shows grey and never fills. */+.swpane.del.dis{background:var(--surface-2)}+.swpane.del.dis button{background:var(--surface-3);color:var(--label-3)}+.swpane.del.dis.armed button{width:76px}+.swrow.flash .swfront{animation:swflash .4s ease}+@keyframes swflash{0%,35%{background:color-mix(in srgb,var(--acc) 28%,var(--surface))}100%{background:var(--surface)}}+.swrow.flash-red .swfront{animation:swflashr .5s ease}+@keyframes swflashr{0%,35%{background:color-mix(in srgb,var(--red) 22%,var(--surface))}100%{background:var(--surface)}}+/* The first-use hint under the row it nudged. */+.swhint{display:flex;justify-content:center;padding:4px 0 6px}+.swhint button{display:inline-flex;align-items:center;gap:8px;background:var(--acc-soft);color:var(--acc);border-radius:99px;padding:7px 12px;font-size:13px;font-weight:600;animation:swhintin .3s var(--ease);text-align:start}+.swhint .icn{font-size:15px;flex:none}+.swhint .icn:last-child{opacity:.7}+@keyframes swhintin{from{opacity:0;transform:translateY(-4px)}}+.vh{position:absolute!important;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap;border:0}+/* one-sided (unilateral) sets (issue #60): the set number sits beside a two-row L/R stack, eachside its own weight/reps/effort + done tick. The two inner rows reuse .setrow so their columnsline up with the header exactly like a straight set — only the leading .n circle is replaced bydiff --git a/frontend/src/lib/use-swipe-row.js b/frontend/src/lib/use-swipe-row.jsnew file mode 100644index 000000000..c7d8f3c17--- /dev/null+++ b/frontend/src/lib/use-swipe-row.js@@ -0,0 +1,346 @@+import { useCallback, useEffect, useRef, useState } from 'react'+import { vibrate } from './sound.js'++// Swipe on sets (v1.3.11): the gesture core behind components/SwipeRow.jsx. A set row swiped+// toward the start of the line (left in a left-to-right language, right in Arabic) deletes, toward+// the end copies. A short swipe leaves the row open on a button; a long one, or a flick, acts on+// release. Everything below is in that logical sense: a negative offset is toward the start and+// is turned into pixels only when painted, the way SwipeToDelete and SwipeCards do it.+//+// The row is almost all controls (the set number, the steppers, the value fields, the tick), so a+// swipe may start on any of them, and a tap must still be a tap. Nothing is captured until the+// touch has gone 8 px and clearly sideways (1.5 times more x than y, the same lock SwipeCards+// uses); a touch that goes vertical first is the page's scroll for the rest of its life. Once+// locked, the pointer is captured, the next click is swallowed, so the ± or tick under the finger+// does not fire after the swipe, and a field that took focus on the way in lets it go again.+//+// Touches that start within 24 px of either screen edge are left alone: that is iOS Safari's back+// swipe and Android's gesture navigation. The page gutter and the card's padding are wider than+// that, so the zone costs almost no row.+//+// Mouse drags are not swipes: a desktop has the set-number menu and the keyboard.++export const EDGE = 24+export const SLOP = 8+export const RATIO = 1.5+export const REVEAL = 76+export const OPEN_MIN = 40+export const DELETE_AT = 0.6+export const DELETE_FLING_AT = 0.4+export const COPY_AT = 0.4+export const COPY_FLING_AT = 0.25+export const FLING = 0.5+export const RESIST = 0.35+const PEEK = 32++/** +1 in a left-to-right page, -1 in a right-to-left one: logical offset × this = pixels. */+export const lineDir = () => (typeof document !== 'undefined' && document.documentElement.dir === 'rtl' ? -1 : 1)++/** Whether a touch at clientX starts in the screen's edge zone (back gestures live there). */+export const inEdgeZone = (x, width) => x < EDGE || x > width - EDGE++/** The axis a move has settled on: null while under the slop, 'x' once clearly sideways, else 'y'. */+export function axisOf(dx, dy) {+ if (Math.max(Math.abs(dx), Math.abs(dy)) < SLOP) return null+ return Math.abs(dx) > RATIO * Math.abs(dy) ? 'x' : 'y'+}++/** Where the row sits for a raw logical drag `raw` on a row `w` wide: with the finger up to the+ * commit point, then at a third of the pace beyond it, never past the row's own width. */+export function shapeOffset(raw, w) {+ const at = (raw < 0 ? DELETE_AT : COPY_AT) * w+ const d = Math.abs(raw)+ const out = d <= at ? d : at + (d - at) * RESIST+ return Math.sign(raw) * Math.min(w, out)+}++/** Whether an offset is past the commit point, i.e. letting go now acts. */+export function armedFor(o, w) {+ if (o < 0) return -o >= DELETE_AT * w - 0.5 ? 'delete' : null+ if (o > 0) return o >= COPY_AT * w - 0.5 ? 'copy' : null+ return null+}++/** What letting go does, for a row resting at logical offset `o` on a row `w` wide, released at+ * logical velocity `v` (px/ms): 'delete' | 'copy' act; 'open-delete' | 'open-copy' leave the row+ * open on its button; 'close' snaps it shut. A flick back the other way always closes. */+export function commitFor(o, w, v = 0) {+ if (o < 0) {+ const d = -o+ if (d >= DELETE_AT * w || (d >= DELETE_FLING_AT * w && -v > FLING)) return 'delete'+ if (v > FLING) return 'close'+ return d >= OPEN_MIN ? 'open-delete' : 'close'+ }+ if (o > 0) {+ if (o >= COPY_AT * w || (o >= COPY_FLING_AT * w && v > FLING)) return 'copy'+ if (-v > FLING) return 'close'+ return o >= OPEN_MIN ? 'open-copy' : 'close'+ }+ return 'close'+}++/** Logical velocity over the last 100 ms of [time, offset] samples. */+export function velocityOf(samples) {+ if (samples.length < 2) return 0+ const a = samples[0], b = samples[samples.length - 1]+ const dt = b[0] - a[0]+ return dt > 0 ? (b[1] - a[1]) / dt : 0+}++export const reducedMotion = () => {+ try { return typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches } catch { return false }+}++// One open row at a time, and the click a swipe leaves behind. Shared by every row on the page.+const shared = { open: null, swallow: false, swallowTm: null, installed: false }+const swallowNextClick = () => {+ shared.swallow = true+ clearTimeout(shared.swallowTm)+ shared.swallowTm = setTimeout(() => { shared.swallow = false }, 600)+}+/** Closes whichever row is resting open, if any. */+export function closeOpenRow(instant = false) {+ const o = shared.open+ shared.open = null+ o?.close(instant)+}+function install() {+ if (shared.installed || typeof document === 'undefined') return+ shared.installed = true+ // Any touch elsewhere closes the open row. A tap that lands on another set only closes, so a+ // tick meant as "close that" does not tick (iOS Mail does the same).+ document.addEventListener('pointerdown', e => {+ shared.swallow = false+ const o = shared.open+ if (o && !o.el.contains(e.target)) {+ closeOpenRow()+ if (e.target.closest?.('.swrow')) swallowNextClick()+ }+ }, true)+ document.addEventListener('click', e => {+ if (shared.swallow) {+ shared.swallow = false+ e.stopPropagation(); e.preventDefault()+ return+ }+ // A tap on the open row itself (not its button) shuts it and does nothing else.+ const o = shared.open+ if (o && o.el.contains(e.target) && !e.target.closest?.('.swpane')) {+ e.stopPropagation(); e.preventDefault()+ closeOpenRow()+ }+ }, true)+ window.addEventListener('scroll', () => { if (shared.open && !shared.open.dragging()) closeOpenRow() }, { passive: true, capture: true })+}++/**+ * The gesture for one row. `onCommit('delete' | 'copy')` acts; `canDelete` false greys the delete+ * side and turns a delete into a rubber band and `onBlocked()`. `closeKey` closes the row whenever+ * it changes (the entry's rows were added or removed, a tick, a timer start: the row under the+ * finger may no longer be the set it was). `onStart` runs when a swipe locks (the hint chip goes).+ */+export function useSwipeRow({ canDelete = true, onCommit, onBlocked, onStart, closeKey }) {+ const outerRef = useRef(null)+ const frontRef = useRef(null)+ const delRef = useRef(null)+ const cpRef = useRef(null)+ const g = useRef(null) // the gesture in progress+ const o = useRef(0) // current logical offset+ const busy = useRef(false) // an act or a peek is animating+ const timers = useRef([])+ const anim = useRef(null)+ const [openSide, setOpenSide] = useState(null)+ const props = useRef({})+ props.current = { canDelete, onCommit, onBlocked, onStart }++ const later = (fn, ms) => { const id = setTimeout(fn, ms); timers.current.push(id); return id }+ const clearTimers = () => { timers.current.forEach(clearTimeout); timers.current = [] }++ const paint = useCallback((off, ms = 0) => {+ const outer = outerRef.current, front = frontRef.current+ if (!outer || !front) return+ const w = outer.clientWidth || 1+ const prevArmed = armedFor(o.current, w)+ o.current = off+ const tr = ms && !reducedMotion() ? `${ms}ms cubic-bezier(.32,.72,0,1)` : ''+ front.style.transition = tr ? `transform ${tr}` : 'none'+ front.style.transform = off ? `translateX(${off * lineDir()}px)` : ''+ const del = delRef.current, cp = cpRef.current+ for (const [pane, width] of [[del, Math.max(0, -off)], [cp, Math.max(0, off)]]) {+ if (!pane) continue+ pane.style.transition = tr ? `width ${tr}` : 'none'+ pane.style.width = width + 'px'+ }+ const armed = armedFor(off, w)+ del?.classList.toggle('armed', armed === 'delete')+ cp?.classList.toggle('armed', armed === 'copy')+ outer.classList.toggle('go-del', off < 0)+ outer.classList.toggle('go-cp', off > 0)+ // A tiny buzz crossing the commit point, either way (Android; Safari has no vibrate).+ if (!ms && armed !== prevArmed) {+ const deleting = armed === 'delete' || prevArmed === 'delete'+ if (!(deleting && props.current.canDelete === false)) vibrate(8)+ }+ }, [])++ const reset = useCallback(() => {+ clearTimers()+ anim.current?.cancel?.()+ anim.current = null+ busy.current = false+ g.current = null+ if (outerRef.current) outerRef.current.classList.remove('dragging', 'flash-red')+ paint(0)+ setOpenSide(null)+ if (shared.open?.el === outerRef.current) shared.open = null+ }, [paint])++ const close = useCallback((instant = false) => {+ if (shared.open?.el === outerRef.current) shared.open = null+ setOpenSide(null)+ paint(0, instant ? 0 : 260)+ }, [paint])++ const openTo = side => {+ if (shared.open && shared.open.el !== outerRef.current) closeOpenRow()+ paint(side === 'delete' ? -REVEAL : REVEAL, 260)+ setOpenSide(side)+ shared.open = { el: outerRef.current, close, dragging: () => !!g.current?.lock }+ }++ const doDelete = () => {+ const outer = outerRef.current+ if (!outer) return+ if (shared.open?.el === outer) shared.open = null+ setOpenSide(null)+ if (props.current.canDelete === false) {+ paint(0, 380)+ outer.classList.remove('flash-red'); void outer.offsetWidth; outer.classList.add('flash-red')+ later(() => outer.classList.remove('flash-red'), 520)+ props.current.onBlocked?.()+ return+ }+ vibrate(15)+ if (reducedMotion()) { paint(0); props.current.onCommit?.('delete'); return }+ busy.current = true+ paint(-(outer.clientWidth || 300), 170)+ later(() => {+ const h = outer.offsetHeight+ outer.style.overflow = 'hidden'+ const done = () => {+ busy.current = false+ props.current.onCommit?.('delete')+ // The row at this index now shows the next set (rows are keyed by index); the closeKey+ // effect puts it back. If nothing changed after all, this does.+ later(() => { if (anim.current) reset() }, 300)+ }+ if (typeof outer.animate === 'function') {+ anim.current = outer.animate([{ height: h + 'px' }, { height: '0px' }], { duration: 180, easing: 'cubic-bezier(.32,.72,0,1)', fill: 'forwards' })+ anim.current.onfinish = done+ } else done()+ }, 170)+ }++ const doCopy = () => {+ if (shared.open?.el === outerRef.current) shared.open = null+ setOpenSide(null)+ vibrate(15)+ if (reducedMotion()) { paint(0); props.current.onCommit?.('copy'); return }+ busy.current = true+ paint(0, 220)+ later(() => { busy.current = false; props.current.onCommit?.('copy') }, 200)+ }++ const release = v => {+ const outer = outerRef.current+ if (!outer) return+ const act = commitFor(o.current, outer.clientWidth || 1, v)+ if (act === 'delete') doDelete()+ else if (act === 'copy') doCopy()+ else if (act === 'open-delete') openTo('delete')+ else if (act === 'open-copy') openTo('copy')+ else close()+ }++ // Any change the row's owner names closes it at once and drops a gesture mid-way.+ useEffect(() => { reset() }, [closeKey, reset])+ useEffect(() => () => { clearTimers(); anim.current?.cancel?.(); if (shared.open?.el === outerRef.current) shared.open = null }, [])++ // touchmove is attached by hand and not passive, so a locked sideways swipe can keep the page+ // from scrolling under it on browsers that do not honour touch-action on the row (see the+ // comment block in SwipeToDelete.jsx). Before the lock it does nothing.+ useEffect(() => {+ install()+ const el = outerRef.current+ if (!el) return+ const onTouchMove = e => { if (g.current?.lock === 'x' && e.cancelable) e.preventDefault() }+ el.addEventListener('touchmove', onTouchMove, { passive: false })+ return () => el.removeEventListener('touchmove', onTouchMove)+ }, [])++ const onPointerDown = e => {+ if (busy.current || g.current) return+ if (e.pointerType !== 'touch' && e.pointerType !== 'pen') return+ if (e.target.closest?.('.subrow,.swpane')) return+ const width = typeof window !== 'undefined' ? window.innerWidth : 0+ if (width && inEdgeZone(e.clientX, width)) return+ g.current = { id: e.pointerId, x0: e.clientX, y0: e.clientY, o0: o.current, lock: null, samples: [] }+ }+ const onPointerMove = e => {+ const d = g.current+ if (!d || d.id !== e.pointerId) return+ const dx = e.clientX - d.x0, dy = e.clientY - d.y0+ if (!d.lock) {+ const axis = axisOf(dx, dy)+ if (!axis) return+ if (axis === 'y') { g.current = null; return }+ d.lock = 'x'+ try { outerRef.current?.setPointerCapture?.(e.pointerId) } catch { /* the gesture still tracks */ }+ swallowNextClick()+ const a = typeof document !== 'undefined' ? document.activeElement : null+ if (a && a !== document.body && outerRef.current?.contains(a)) a.blur?.()+ if (shared.open && shared.open.el !== outerRef.current) closeOpenRow()+ outerRef.current?.classList.add('dragging')+ props.current.onStart?.()+ }+ const logical = dx * lineDir()+ const now = e.timeStamp || Date.now()+ d.samples.push([now, logical])+ while (d.samples.length > 2 && now - d.samples[0][0] > 100) d.samples.shift()+ const w = outerRef.current?.clientWidth || 1+ paint(Math.max(-w, Math.min(w, shapeOffset(d.o0 + logical, w))))+ }+ const end = (e, cancelled) => {+ const d = g.current+ if (!d || d.id !== e.pointerId) return+ g.current = null+ if (!d.lock) return+ outerRef.current?.classList.remove('dragging')+ release(cancelled ? 0 : velocityOf(d.samples))+ }++ // The first-use hint: a nudge toward the start (red shows), back, toward the end, back.+ const peek = useCallback(() => {+ if (reducedMotion() || busy.current || g.current) return+ busy.current = true+ const steps = [[-PEEK, 250], [0, 250], [PEEK, 250], [0, 250]]+ let k = 0+ const step = () => {+ if (k >= steps.length) { busy.current = false; return }+ const [off, ms] = steps[k++]+ paint(off, ms)+ later(step, ms + 50)+ }+ step()+ }, [paint])++ return {+ outerRef, frontRef, delRef, cpRef, openSide, peek,+ pressDelete: doDelete, pressCopy: doCopy,+ handlers: {+ onPointerDown, onPointerMove,+ onPointerUp: e => end(e, false),+ onPointerCancel: e => end(e, true),+ },+ }+}diff --git a/frontend/src/lib/use-swipe-row.test.js b/frontend/src/lib/use-swipe-row.test.jsnew file mode 100644index 000000000..c249c6192--- /dev/null+++ b/frontend/src/lib/use-swipe-row.test.js@@ -0,0 +1,81 @@+import { describe, it, expect } from 'vitest'+import { commitFor, shapeOffset, armedFor, axisOf, inEdgeZone, velocityOf, REVEAL, OPEN_MIN, EDGE } from './use-swipe-row.js'++// Offsets and velocities are logical (negative = toward the start of the line: left in English,+// right in Arabic), so one table covers both directions; the component turns them into pixels.+const W = 360++describe('commitFor', () => {+ it('deletes past 60 % of the row, or a flick toward the start past 40 %', () => {+ expect(commitFor(-0.6 * W, W, 0)).toBe('delete')+ expect(commitFor(-0.59 * W, W, 0)).toBe('open-delete')+ expect(commitFor(-0.4 * W, W, -0.6)).toBe('delete')+ expect(commitFor(-0.39 * W, W, -0.6)).toBe('open-delete')+ expect(commitFor(-0.4 * W, W, -0.5)).toBe('open-delete') // a flick has to beat 0.5 px/ms+ })++ it('copies past 40 % of the row, or a flick toward the end past 25 %', () => {+ expect(commitFor(0.4 * W, W, 0)).toBe('copy')+ expect(commitFor(0.39 * W, W, 0)).toBe('open-copy')+ expect(commitFor(0.25 * W, W, 0.6)).toBe('copy')+ expect(commitFor(0.24 * W, W, 0.6)).toBe('open-copy')+ })++ it('leaves the row open on its button from 40 px, shut below', () => {+ expect(commitFor(-OPEN_MIN, W)).toBe('open-delete')+ expect(commitFor(-(OPEN_MIN - 1), W)).toBe('close')+ expect(commitFor(OPEN_MIN, W)).toBe('open-copy')+ expect(commitFor(OPEN_MIN - 1, W)).toBe('close')+ expect(commitFor(0, W, 3)).toBe('close')+ })++ it('a flick back the other way closes', () => {+ expect(commitFor(-REVEAL, W, 0.8)).toBe('close')+ expect(commitFor(REVEAL, W, -0.8)).toBe('close')+ })+})++describe('shapeOffset', () => {+ it('follows the finger up to the commit point, then at a third of the pace', () => {+ expect(shapeOffset(-100, W)).toBe(-100)+ expect(shapeOffset(-0.6 * W, W)).toBeCloseTo(-0.6 * W)+ expect(shapeOffset(-0.6 * W - 100, W)).toBeCloseTo(-0.6 * W - 35)+ expect(shapeOffset(0.4 * W + 100, W)).toBeCloseTo(0.4 * W + 35)+ expect(shapeOffset(5 * W, W)).toBe(W)+ })+})++describe('armedFor', () => {+ it('names the side that acts if let go now', () => {+ expect(armedFor(-0.6 * W, W)).toBe('delete')+ expect(armedFor(-0.5 * W, W)).toBeNull()+ expect(armedFor(0.4 * W, W)).toBe('copy')+ expect(armedFor(0.3 * W, W)).toBeNull()+ })+})++describe('axisOf', () => {+ it('waits for 8 px, and sideways has to beat vertical 1.5 to 1', () => {+ expect(axisOf(7, 0)).toBeNull()+ expect(axisOf(8, 0)).toBe('x')+ expect(axisOf(15, 10)).toBe('y') // a 34° diagonal is a scroll+ expect(axisOf(16, 10)).toBe('x')+ expect(axisOf(0, 9)).toBe('y')+ })+})++describe('inEdgeZone', () => {+ it('leaves the 24 px at either screen edge to the back gestures', () => {+ expect(inEdgeZone(EDGE - 1, 390)).toBe(true)+ expect(inEdgeZone(EDGE, 390)).toBe(false)+ expect(inEdgeZone(390 - EDGE, 390)).toBe(false)+ expect(inEdgeZone(390 - EDGE + 1, 390)).toBe(true)+ })+})++describe('velocityOf', () => {+ it('reads px/ms between the first and last sample', () => {+ expect(velocityOf([[0, 0]])).toBe(0)+ expect(velocityOf([[0, 0], [50, -40], [100, -80]])).toBeCloseTo(-0.8)+ })+})