On touch screens, dragging the Expenses or Balances tab down from the top slides a "Pull to add expense" pill out from under the header; releasing once it reads "Release to add" opens the add sheet. Sideways swipes, upward scrolls and touches on form controls are left alone, and the browser's pull-to-refresh is suppressed only while a pull is in progress. The visible Add buttons stay; the pill is aria-hidden. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HA82gVKYyo9BHLHPW4qr4x
123 lines
4.2 KiB
Svelte
123 lines
4.2 KiB
Svelte
<script lang="ts">
|
|
// Pull-down-to-add: on a touch screen, dragging the page down from the very
|
|
// top slides a pill out from under the header; letting go once it reads
|
|
// "Release to add" opens the add-expense sheet. The visible Add buttons stay,
|
|
// so this is a shortcut, not the only way in (the pill is aria-hidden).
|
|
import { t } from '../lib/settings.svelte.ts';
|
|
import Icon from './Icon.svelte';
|
|
|
|
const { enabled, onadd }: { enabled: boolean; onadd: () => void } = $props();
|
|
|
|
/** How far the pill must travel (after resistance) before a release adds. */
|
|
const THRESHOLD = 72;
|
|
const MAX = 110;
|
|
/** Finger movement before we decide whether this is a vertical pull. */
|
|
const SLOP = 8;
|
|
|
|
let pull = $state(0);
|
|
let top = $state(0);
|
|
let releasing = $state(false);
|
|
const armed = $derived(pull >= THRESHOLD);
|
|
|
|
$effect(() => {
|
|
if (!enabled) return;
|
|
// Tracking state for one touch; null = not a candidate for a pull.
|
|
let start: { x: number; y: number } | null = null;
|
|
let pulling = false;
|
|
let wasArmed = false;
|
|
|
|
const reset = () => {
|
|
start = null;
|
|
pulling = false;
|
|
wasArmed = false;
|
|
releasing = true;
|
|
pull = 0;
|
|
};
|
|
|
|
const onStart = (e: TouchEvent) => {
|
|
start = null;
|
|
if (e.touches.length !== 1 || window.scrollY > 0) return;
|
|
// Leave form controls (search box, selects) to do their own thing.
|
|
if ((e.target as Element | null)?.closest('input, textarea, select, dialog')) return;
|
|
const tch = e.touches[0];
|
|
start = { x: tch.clientX, y: tch.clientY };
|
|
top = document.querySelector('.topbar')?.getBoundingClientRect().bottom ?? 0;
|
|
};
|
|
|
|
const onMove = (e: TouchEvent) => {
|
|
if (!start) return;
|
|
const tch = e.touches[0];
|
|
const dx = tch.clientX - start.x;
|
|
const dy = tch.clientY - start.y;
|
|
if (!pulling) {
|
|
if (Math.abs(dx) < SLOP && Math.abs(dy) < SLOP) return;
|
|
// Sideways swipes and upward scrolls aren't ours.
|
|
if (dy <= 0 || Math.abs(dx) > Math.abs(dy) || window.scrollY > 0) {
|
|
start = null;
|
|
return;
|
|
}
|
|
pulling = true;
|
|
releasing = false;
|
|
}
|
|
// Stop the browser's own pull-to-refresh / rubber band while we pull.
|
|
if (e.cancelable) e.preventDefault();
|
|
pull = Math.min(MAX, Math.max(0, dy - SLOP) * 0.55);
|
|
const nowArmed = pull >= THRESHOLD;
|
|
if (nowArmed && !wasArmed) navigator.vibrate?.(10);
|
|
wasArmed = nowArmed;
|
|
};
|
|
|
|
const onEnd = () => {
|
|
if (!start) return;
|
|
const add = pulling && pull >= THRESHOLD;
|
|
reset();
|
|
if (add) onadd();
|
|
};
|
|
|
|
window.addEventListener('touchstart', onStart, { passive: true });
|
|
window.addEventListener('touchmove', onMove, { passive: false });
|
|
window.addEventListener('touchend', onEnd);
|
|
window.addEventListener('touchcancel', reset);
|
|
return () => {
|
|
window.removeEventListener('touchstart', onStart);
|
|
window.removeEventListener('touchmove', onMove);
|
|
window.removeEventListener('touchend', onEnd);
|
|
window.removeEventListener('touchcancel', reset);
|
|
pull = 0;
|
|
};
|
|
});
|
|
</script>
|
|
|
|
<div
|
|
class="pull"
|
|
class:armed
|
|
class:releasing
|
|
aria-hidden="true"
|
|
style:top="{top}px"
|
|
style:transform="translate(-50%, {pull - 48}px)"
|
|
style:opacity={Math.min(1, pull / 32)}
|
|
>
|
|
<span class="c"><Icon name={armed ? 'plus' : 'down'} 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>
|