Gestures (GroupGestures.svelte replaces PullToAdd.svelte): - Pull-to-add moves to the bottom: pulling up past the end of the Expenses or Balances tab raises a pill from behind the tab bar. The top is left to the browser's pull-to-refresh. - Swiping left/right switches between Expenses, Balances, Stats and Activity; the list follows the finger and the new tab slides in. Touches within 24px of the screen edge (OS back gesture), in form controls or in sideways-scrolling elements are ignored. Group settings, same features in less space: - Share: one row (Copy link, Share icon, QR); shorter intro. - Group: currency and default category side by side; Save appears only when something changed; hints trimmed. - Members: "You are" picker merged in; rename/archive as icon buttons; add member is the last row. - Notifications: a single switch. Repeating expenses: hidden when none. - Danger zone: two rows; the delete confirmation opens on tap. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HA82gVKYyo9BHLHPW4qr4x
215 lines
7.4 KiB
Svelte
215 lines
7.4 KiB
Svelte
<script lang="ts">
|
|
// Touch gestures on the group page (shortcuts; every action also has a button):
|
|
// - Pull up past the end of the page: a pill rises from behind the tab bar;
|
|
// letting go once it reads "Release to add" opens the add-expense sheet.
|
|
// Bottom rather than top, so the browser's pull-to-refresh keeps working.
|
|
// - Swipe sideways: previous / next tab. Touches near the screen edges are
|
|
// left to the OS back/forward gestures.
|
|
import { t } from '../lib/settings.svelte.ts';
|
|
import Icon from './Icon.svelte';
|
|
|
|
const {
|
|
enabled,
|
|
addEnabled,
|
|
tabs,
|
|
current,
|
|
onadd,
|
|
ontab,
|
|
}: {
|
|
/** Gestures at all (off while a sheet or the identity picker is open). */
|
|
enabled: boolean;
|
|
/** Pull-up-to-add on this tab. */
|
|
addEnabled: boolean;
|
|
tabs: readonly string[];
|
|
current: string;
|
|
onadd: () => void;
|
|
ontab: (key: string, dir: 1 | -1) => void;
|
|
} = $props();
|
|
|
|
/** How far the pill must rise (after resistance) before a release adds. */
|
|
const THRESHOLD = 72;
|
|
const MAX = 110;
|
|
/** Finger movement before we decide which gesture this is. */
|
|
const SLOP = 10;
|
|
/** Touches starting this close to the left/right edge belong to the OS. */
|
|
const EDGE = 24;
|
|
|
|
let pull = $state(0);
|
|
let bottom = $state(0);
|
|
let releasing = $state(false);
|
|
const armed = $derived(pull >= THRESHOLD);
|
|
|
|
const atBottom = () => window.scrollY + window.innerHeight >= document.documentElement.scrollHeight - 2;
|
|
|
|
/** True if the touch starts inside something that scrolls sideways itself. */
|
|
function inHorizontalScroller(el: Element | null): boolean {
|
|
for (; el && el !== document.body; el = el.parentElement) {
|
|
const ox = getComputedStyle(el).overflowX;
|
|
if ((ox === 'auto' || ox === 'scroll') && el.scrollWidth > el.clientWidth) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
$effect(() => {
|
|
if (!enabled) return;
|
|
// Read props through closures so the listeners always see current values
|
|
// without being re-attached on every tab change.
|
|
const add = () => addEnabled;
|
|
const tabList = () => tabs;
|
|
const cur = () => current;
|
|
|
|
type Track = { x: number; y: number; t: number; mode: 'undecided' | 'add' | 'swipe'; canAdd: boolean; canSwipe: boolean };
|
|
let tr: Track | null = null;
|
|
let wasArmed = false;
|
|
const page = () => document.querySelector<HTMLElement>('.layout');
|
|
|
|
function neighbour(dir: 1 | -1): string | undefined {
|
|
const list = tabList();
|
|
return list[list.indexOf(cur()) + dir];
|
|
}
|
|
|
|
function resetPull() {
|
|
releasing = true;
|
|
pull = 0;
|
|
wasArmed = false;
|
|
}
|
|
|
|
function resetSwipe(el: HTMLElement | null) {
|
|
if (!el) return;
|
|
el.style.transition = 'transform 0.2s ease';
|
|
el.style.transform = '';
|
|
el.addEventListener('transitionend', () => (el.style.transition = ''), { once: true });
|
|
}
|
|
|
|
const onStart = (e: TouchEvent) => {
|
|
tr = null;
|
|
if (e.touches.length !== 1) return;
|
|
const target = e.target as Element | null;
|
|
if (target?.closest('input, textarea, select, dialog')) return;
|
|
const tch = e.touches[0];
|
|
const nearEdge = tch.clientX < EDGE || tch.clientX > window.innerWidth - EDGE;
|
|
tr = {
|
|
x: tch.clientX, y: tch.clientY, t: e.timeStamp, mode: 'undecided',
|
|
canAdd: add() && atBottom(),
|
|
canSwipe: !nearEdge && !inHorizontalScroller(target),
|
|
};
|
|
if (tr.canAdd) {
|
|
const bar = document.querySelector('.tabbar')?.getBoundingClientRect();
|
|
// The tab bar floats at the bottom on phones; the pill hides behind it.
|
|
bottom = bar && getComputedStyle(document.querySelector('.tabbar')!).position === 'fixed'
|
|
? window.innerHeight - bar.top + 8 : 16;
|
|
}
|
|
};
|
|
|
|
const onMove = (e: TouchEvent) => {
|
|
if (!tr) return;
|
|
const tch = e.touches[0];
|
|
const dx = tch.clientX - tr.x;
|
|
const dy = tch.clientY - tr.y;
|
|
if (tr.mode === 'undecided') {
|
|
if (Math.abs(dx) < SLOP && Math.abs(dy) < SLOP) return;
|
|
if (tr.canSwipe && Math.abs(dx) > Math.abs(dy) * 1.2) tr.mode = 'swipe';
|
|
else if (tr.canAdd && dy < 0 && Math.abs(dy) > Math.abs(dx) && atBottom()) {
|
|
tr.mode = 'add';
|
|
releasing = false;
|
|
} else {
|
|
tr = null; // ordinary scrolling
|
|
return;
|
|
}
|
|
}
|
|
if (e.cancelable) e.preventDefault();
|
|
if (tr.mode === 'add') {
|
|
pull = Math.min(MAX, Math.max(0, -dy - SLOP) * 0.55);
|
|
const nowArmed = pull >= THRESHOLD;
|
|
if (nowArmed && !wasArmed) navigator.vibrate?.(10);
|
|
wasArmed = nowArmed;
|
|
} else {
|
|
const el = page();
|
|
if (!el) return;
|
|
// Rubber-band at the first and last tab.
|
|
const free = neighbour(dx < 0 ? 1 : -1) !== undefined;
|
|
const shift = free ? dx * 0.6 : dx * 0.15;
|
|
el.style.transition = '';
|
|
el.style.transform = `translateX(${shift}px)`;
|
|
}
|
|
};
|
|
|
|
const onEnd = (e: TouchEvent) => {
|
|
if (!tr) return;
|
|
const t0 = tr;
|
|
tr = null;
|
|
if (t0.mode === 'add') {
|
|
const go = pull >= THRESHOLD;
|
|
resetPull();
|
|
if (go) onadd();
|
|
} else if (t0.mode === 'swipe') {
|
|
const tch = e.changedTouches[0];
|
|
const dx = tch.clientX - t0.x;
|
|
const speed = Math.abs(dx) / Math.max(1, e.timeStamp - t0.t);
|
|
const dir: 1 | -1 = dx < 0 ? 1 : -1;
|
|
const next = neighbour(dir);
|
|
const el = page();
|
|
if (next && (Math.abs(dx) > window.innerWidth * 0.25 || (Math.abs(dx) > 40 && speed > 0.4))) {
|
|
if (el) { el.style.transition = ''; el.style.transform = ''; }
|
|
ontab(next, dir);
|
|
} else {
|
|
resetSwipe(el);
|
|
}
|
|
}
|
|
};
|
|
|
|
const onCancel = () => {
|
|
if (tr?.mode === 'add') resetPull();
|
|
if (tr?.mode === 'swipe') resetSwipe(page());
|
|
tr = null;
|
|
};
|
|
|
|
window.addEventListener('touchstart', onStart, { passive: true });
|
|
window.addEventListener('touchmove', onMove, { passive: false });
|
|
window.addEventListener('touchend', onEnd);
|
|
window.addEventListener('touchcancel', onCancel);
|
|
return () => {
|
|
window.removeEventListener('touchstart', onStart);
|
|
window.removeEventListener('touchmove', onMove);
|
|
window.removeEventListener('touchend', onEnd);
|
|
window.removeEventListener('touchcancel', onCancel);
|
|
const el = page();
|
|
if (el) el.style.transform = '';
|
|
pull = 0;
|
|
};
|
|
});
|
|
</script>
|
|
|
|
<div
|
|
class="pull"
|
|
class:armed
|
|
class:releasing
|
|
aria-hidden="true"
|
|
style:bottom="{bottom}px"
|
|
style:transform="translate(-50%, {48 - pull}px)"
|
|
style:opacity={Math.min(1, pull / 32)}
|
|
>
|
|
<span class="c"><Icon name={armed ? 'plus' : 'up'} size={18} /></span>
|
|
{armed ? t('release_to_add') : t('pull_to_add')}
|
|
</div>
|
|
|
|
<style>
|
|
.pull {
|
|
position: fixed; left: 50%; z-index: 19; pointer-events: none;
|
|
display: flex; align-items: center; gap: 8px;
|
|
padding: 6px 14px 6px 6px; border-radius: 999px;
|
|
background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow);
|
|
color: var(--muted); font-size: 0.88rem; font-weight: 600; white-space: nowrap;
|
|
opacity: 0;
|
|
}
|
|
.c {
|
|
display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%;
|
|
background: var(--surface-2); color: var(--text);
|
|
}
|
|
.armed { color: var(--text); }
|
|
.armed .c { background: var(--accent); color: var(--accent-ink); }
|
|
.releasing { transition: transform 0.2s ease, opacity 0.2s ease; }
|
|
@media (prefers-reduced-motion: reduce) {
|
|
.releasing { transition: none; }
|
|
}
|
|
</style>
|