Close a phone panel with the swipe back off its edge

An open panel now answers the swipe that undoes the one which opened it: a left drag closes the rail's drawer, a right drag the bot panel, through a panel-local listener sharing swipe-gesture.ts with the page swipes. SidePanel carries the bot panel's phone width (420px, at most 92vw) for both, and field detection moves to touch-in-field.ts so both hooks ignore a touch in a field.
This commit is contained in:
2026-11-22 15:36:07 +01:00
parent 7460163287
commit 35b9808d97
9 changed files with 206 additions and 9 deletions
+1 -1
View File
@@ -192,7 +192,7 @@ Set in Settings > Environment (super admin), not `.env`. `envs` (private; encryp
- Notification `badge` is `icons/badge-96.png` (white silhouette; Android masks by alpha, so never the opaque app icon). Brave on Android has no WebAPK, so its notifications always carry Brave's name and icon.
- `public/sw.js` is served `no-store`; navigations network-only (authenticated HTML never cached); `/api/`, `/ws`, `/media/`, `/computers/`, `/build-info.json` never intercepted. A new worker takes over by itself (`skipWaiting` on install; nothing in it is per-version) and never reloads a page; there is no update prompt. In development the worker and caches are actively removed (`clear-development-service-worker.ts`, stub `/sw.js`).
- **Precache (production):** a navigation without the current build cookie gets `loading.html`, which downloads all of `.bunext/public` into `cobble-assets-<build_id>`, verifies, sets the cookie and reloads (falls back to network on stall). React knows nothing of this.
- Phone shell: rail is a slide-over below `md` (the shared `src/components/general/side-panel.tsx`, always mounted so it slides both ways; the rail passes `inertWhenClosed={false}` since it is the page's own rail from `md` up); rail internals use the `@rail:` container variant, not viewport `lg:`. Two swipes anywhere on a phone page move between panes: to the right opens the rail's drawer, to the left the conversation's bot panel (`cobble:open-bot-panel`, `src/utils/bot-panel-open-event.ts`, answered only by a conversation's header, so a page without one slides nothing). They are flicks, read by `src/hooks/use-horizontal-swipe.ts` (a field, a `data-no-swipe` panel, a box that scrolls sideways, a text selection, a finger down longer than `AppData.MobileSwipeMaxMs`, an open twui modal, a screen from `md` up and the open drawer all own the touch), with the thresholds and the one-decision state machine in `src/utils/swipe-gesture.ts` (`AppData.MobileSwipeMinPx`, `MobileSwipeAxisRatio`), tested without a browser. `LiveSocket` is remounted on online/pageshow/visibility since twui stops retrying after three attempts. Nothing is queued offline.
- Phone shell: rail is a slide-over below `md` (the shared `src/components/general/side-panel.tsx`, always mounted so it slides both ways; the rail passes `inertWhenClosed={false}` since it is the page's own rail from `md` up); rail internals use the `@rail:` container variant, not viewport `lg:`. Two swipes anywhere on a phone page move between panes: to the right opens the rail's drawer, to the left the conversation's bot panel (`cobble:open-bot-panel`, `src/utils/bot-panel-open-event.ts`, answered only by a conversation's header, so a page without one slides nothing). They are flicks, read by `src/hooks/use-horizontal-swipe.ts` (a field, a `data-no-swipe` panel, a box that scrolls sideways, a text selection, a finger down longer than `AppData.MobileSwipeMaxMs`, an open twui modal, a screen from `md` up and the open drawer all own the touch), with the thresholds and the one-decision state machine in `src/utils/swipe-gesture.ts` (`AppData.MobileSwipeMinPx`, `MobileSwipeAxisRatio`), tested without a browser. An open panel answers the one swipe back off its edge (`src/hooks/use-swipe-to-close.ts`, `sidePanelCloseSwipe`): a left drag closes the rail's drawer, a right drag the bot panel; `SidePanel` gives both the same phone width (`w-[420px] max-w-[92vw]`). `LiveSocket` is remounted on online/pageshow/visibility since twui stops retrying after three attempts. Nothing is queued offline.
- **Web Push:** `push_subscriptions`, `user_presence`; VAPID keys generated into `envs` by `ensureVapidKeys`. `pushBotNotice` is the one trigger, skipped while the user is present (`AppData.PushPresenceWindowMs`, per user not per device). Best effort, no retry. Needs HTTPS.
### Bot files and folders
@@ -54,6 +54,17 @@ describe("SidePanel", () => {
expect(html.match(/data-no-swipe/g)?.length).toBe(2);
});
test("the rail's drawer and the bot panel open to the same phone width", () => {
const html = renderToString(
<SidePanel side="left" open onClose={onClose} label="Bots">
<p>crew</p>
</SidePanel>,
);
expect(html).toContain("w-[420px]");
expect(html).toContain("max-w-[92vw]");
});
test("a panel that is also part of the page is never inert", () => {
const html = renderToString(
<SidePanel
+17 -3
View File
@@ -1,8 +1,10 @@
"use client";
import type { ReactNode } from "react";
import { useRef, type ReactNode, type RefObject } from "react";
import Stack from "@moduletrace/twui/layout/Stack";
import { twMerge } from "tailwind-merge";
import useSwipeToClose from "@/src/hooks/use-swipe-to-close";
import sidePanelCloseSwipe from "@/src/utils/side-panel-close-swipe";
type Props = {
/** Which edge it slides in from. */
@@ -34,7 +36,10 @@ const Edges = {
* to wait on a timer for an exit to play.
*
* The panel carries `data-no-swipe`, so a phone's sideways swipes never
* start on it or on its backdrop: what is open stays the one on top.
* start on it or on its backdrop: what is open stays the one on top. An
* open panel closes on the one swipe it does answer, a drag back towards the
* edge it slid in from (`useSwipeToClose`), which is the way back out of the
* swipe that opened it.
*/
export default function SidePanel({
side,
@@ -48,6 +53,14 @@ export default function SidePanel({
}: Props) {
const edge = Edges[side];
const hidden = !open && inertWhenClosed;
const panel_ref = useRef<HTMLDivElement>(null);
useSwipeToClose({
panel_ref,
enabled: open,
direction: sidePanelCloseSwipe({ side }),
on_close: onClose,
});
return (
<>
@@ -64,8 +77,9 @@ export default function SidePanel({
aria-hidden={hidden || undefined}
inert={hidden}
data-no-swipe
componentRef={panel_ref as RefObject<HTMLDivElement>}
className={twMerge(
"fixed inset-y-0 z-40 w-[280px] max-w-[85vw] shrink-0 h-full gap-0! items-stretch bg-paper border-0 border-solid border-hairline shadow-popover transition-transform duration-[var(--m-base)] pt-[env(safe-area-inset-top)] pb-[env(safe-area-inset-bottom)]",
"fixed inset-y-0 z-40 w-[420px] max-w-[92vw] shrink-0 h-full gap-0! items-stretch bg-paper border-0 border-solid border-hairline shadow-popover transition-transform duration-[var(--m-base)] pt-[env(safe-area-inset-top)] pb-[env(safe-area-inset-bottom)]",
edge.place,
open ? "translate-x-0" : edge.closed,
className,
+5 -4
View File
@@ -6,6 +6,7 @@ import swipeGesture, {
IdleSwipeGesture,
type SwipeGesture,
} from "@/src/utils/swipe-gesture";
import touchInField from "@/src/utils/touch-in-field";
/**
* Below this width the rail is a slide-over (Tailwind's `md`), so the
@@ -15,11 +16,10 @@ import swipeGesture, {
const PhoneMaxWidth = 768;
/**
* What a touch on belongs to instead of the page: a field being typed in,
* a code editor, or a panel the app slid in itself.
* What a touch on belongs to instead of the page: a panel the app slid in
* itself. A field is `touchInField`.
*/
const OwnedTargets =
"input, textarea, select, [contenteditable='true'], .cm-editor, [data-no-swipe]";
const OwnedTargets = "[data-no-swipe]";
type Params = {
/**
@@ -38,6 +38,7 @@ type Params = {
*/
function ownsTouch({ target }: { target: EventTarget | null }): boolean {
if (!(target instanceof Element)) return true;
if (touchInField({ target })) return true;
if (target.closest(OwnedTargets)) return true;
for (let node: Element | null = target; node; node = node.parentElement) {
+120
View File
@@ -0,0 +1,120 @@
"use client";
import { useEffect, useRef, type RefObject } from "react";
import { AppData } from "@/src/data/app-data";
import swipeGesture, {
IdleSwipeGesture,
type SwipeGesture,
} from "@/src/utils/swipe-gesture";
import touchInField from "@/src/utils/touch-in-field";
type Params = {
/** The panel itself: only a finger that lands on it counts. */
panel_ref: RefObject<HTMLElement | null>;
/** Only while it is open; a panel that is closed keeps no listener. */
enabled: boolean;
/** The way back off the edge the panel slid in from. */
direction: "left" | "right";
on_close: () => void;
};
/**
* Closes an open side panel with a phone swipe back towards the edge it slid
* in from, where the page's own swipes are blocked on it (`data-no-swipe`).
* It reads the finger on the panel alone, so the panel that is open is the
* one that answers, and leaves the page's swipe hook to the page.
*
* The thresholds and the state machine are `swipe-gesture.ts`, the same ones
* the page's swipes use: a flick, not a long drag, and one that is clearly
* more sideways than up or down.
*/
export default function useSwipeToClose({
panel_ref,
enabled,
direction,
on_close,
}: Params) {
const close = useRef(on_close);
close.current = on_close;
useEffect(() => {
const panel = panel_ref.current;
if (!panel || !enabled) return;
let gesture: SwipeGesture = IdleSwipeGesture;
let touched_at = 0;
const restart = () => {
gesture = IdleSwipeGesture;
};
const onStart = (event: TouchEvent) => {
restart();
if (event.touches.length != 1) return;
if (document.getElementById("twui-modal-root")?.childElementCount) return;
if (touchInField({ target: event.target })) return;
const touch = event.touches[0];
if (!touch) return;
touched_at = Date.now();
gesture = swipeGesture({
gesture: IdleSwipeGesture,
input: { type: "start", x: touch.clientX, y: touch.clientY },
}).gesture;
};
const onMove = (event: TouchEvent) => {
if (!gesture.touching) return;
const touch = event.touches[0];
if (!touch || event.touches.length != 1) {
restart();
return;
}
/**
* A finger that has rested on the panel, or that is selecting
* text, is reading it, not pushing it away.
*/
if (Date.now() - touched_at > AppData.MobileSwipeMaxMs) {
restart();
return;
}
if (window.getSelection()?.toString()) {
restart();
return;
}
const step = swipeGesture({
gesture,
input: { type: "move", x: touch.clientX, y: touch.clientY },
});
gesture = step.gesture;
if (step.swipe != direction) return;
restart();
close.current();
};
panel.addEventListener("touchstart", onStart, { passive: true });
panel.addEventListener("touchmove", onMove, { passive: true });
panel.addEventListener("touchend", restart);
panel.addEventListener("touchcancel", restart);
return () => {
panel.removeEventListener("touchstart", onStart);
panel.removeEventListener("touchmove", onMove);
panel.removeEventListener("touchend", restart);
panel.removeEventListener("touchcancel", restart);
};
}, [panel_ref, enabled, direction]);
}
@@ -76,7 +76,6 @@ export default function BotPanel({ bot, open, on_open, on_close }: Props) {
open={open}
onClose={on_close}
label={`Edit ${name}`}
className="w-[420px] max-w-[92vw]"
>
<Row className="shrink-0 gap-3! flex-nowrap items-center px-4 py-3 border-0 border-b border-solid border-hairline">
<Stack className="shrink-0">
+17
View File
@@ -0,0 +1,17 @@
import { describe, expect, test } from "bun:test";
import sidePanelCloseSwipe from "./side-panel-close-swipe";
describe("sidePanelCloseSwipe", () => {
test("a panel from the left closes on a swipe back to the left", () => {
expect(sidePanelCloseSwipe({ side: "left" })).toBe("left");
});
test("a panel from the right closes on a swipe back to the right", () => {
expect(sidePanelCloseSwipe({ side: "right" })).toBe("right");
});
test("closing is always the opposite of the page swipe that opened it", () => {
expect(sidePanelCloseSwipe({ side: "left" })).not.toBe("right");
expect(sidePanelCloseSwipe({ side: "right" })).not.toBe("left");
});
});
+15
View File
@@ -0,0 +1,15 @@
export type SidePanelSide = "left" | "right";
type Params = {
side: SidePanelSide;
};
/**
* The swipe that closes a panel: back towards the edge it slid in from, so
* the way it came. The rail slides in from the left and closes on a drag to
* the left; the bot panel slides in from the right and closes on a drag to
* the right. That is the opposite of the page swipe that opens each one.
*/
export default function sidePanelCloseSwipe({ side }: Params): "left" | "right" {
return side == "left" ? "left" : "right";
}
+20
View File
@@ -0,0 +1,20 @@
/**
* Where a sideways drag belongs to the control instead of the page: typing,
* selecting a field's text or moving its caret all live here.
*/
export const FieldTouchTargets =
"input, textarea, select, [contenteditable='true'], .cm-editor";
type Params = {
target: EventTarget | null;
};
/**
* Whether a touch landed on a field, which owns it: a swipe read there would
* take a caret or a selection, not a pane.
*/
export default function touchInField({ target }: Params): boolean {
if (!(target instanceof Element)) return false;
return Boolean(target.closest(FieldTouchTargets));
}