import { Index, Show, createEffect, createSignal, onCleanup, onMount, type Component } from "solid-js"; import { IconChevronLeft, IconChevronRight, IconPlus, IconX } from "@/components/ui/icons"; import { cn } from "@/lib/cn"; import { formatUnreadCount } from "@/lib/notifications"; import { clamp01, radialOffsets, readFloatingButton, writeFloatingButton } from "@/lib/floating-button"; import { useT } from "@/stores/preferences-context"; export interface FloatingAction { id: string; label: string; Icon: Component<{ class?: string }>; badge?: number; onSelect: () => void; } /** Button diameter. */ const SIZE = 56; /** Gap the resting button keeps from the layer's edges. */ const MARGIN = 12; /** Radial item diameter. */ const ITEM = 44; /** Distance from the button centre to each radial item centre. */ const RADIUS = 84; /** Movement under this is a tap, not a drag — a thumb is never perfectly still. */ const TAP_SLOP = 8; /** How close the button centre has to come to the hide target to snap into it. */ const HIDE_REACH = 64; const badgeText = formatUnreadCount; const clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value)); /** * The phone shell's floating action button, standing in for the desktop * header. Tapping it fans its actions out around it; dragging moves it * anywhere between the status bar and the tab bar; dropping it on the hide * target that rises while dragging parks it as a small tab on the nearest * side edge, and one tap on that tab brings it back where it was. * * Position and parked state are remembered per device (lib/floating-button). * Drag follows the pointer-event pattern of MobileNavSheet: pointer capture on * the button itself, touch-none so the browser does not claim the gesture for * scrolling. */ export function FloatingActionButton(props: { actions: FloatingAction[]; /** Unread total shown on the button and, as a dot, on the parked tab. */ badge: number; /** An overlay owns the screen; step aside without losing state. */ hidden: boolean; }) { const t = useT(); const saved = readFloatingButton(); const [pos, setPos] = createSignal({ x: saved.x, y: saved.y }); const [parked, setParked] = createSignal(saved.parked); const [open, setOpen] = createSignal(false); const [bounds, setBounds] = createSignal({ w: 0, h: 0 }); // Live top-left while a drag is under way; null at rest. const [drag, setDrag] = createSignal<{ left: number; top: number } | null>(null); const [overHide, setOverHide] = createSignal(false); let layer: HTMLDivElement | undefined; let fab: HTMLButtonElement | undefined; const measure = () => { if (!layer) return; const rect = layer.getBoundingClientRect(); setBounds({ w: rect.width, h: rect.height }); }; onMount(() => { measure(); window.addEventListener("resize", measure); onCleanup(() => window.removeEventListener("resize", measure)); }); const persist = () => writeFloatingButton({ ...pos(), parked: parked() }); const travelX = () => Math.max(0, bounds().w - SIZE - 2 * MARGIN); const travelY = () => Math.max(0, bounds().h - SIZE - 2 * MARGIN); const restLeft = () => MARGIN + pos().x * travelX(); const restTop = () => MARGIN + pos().y * travelY(); const hideLeft = () => bounds().w / 2 - SIZE / 2; const hideTop = () => bounds().h - SIZE - MARGIN; const left = () => (overHide() ? hideLeft() : (drag()?.left ?? restLeft())); const top = () => (overHide() ? hideTop() : (drag()?.top ?? restTop())); createEffect(() => { if (props.hidden) setOpen(false); }); createEffect(() => { if (!open()) return; const onKey = (event: KeyboardEvent) => { if (event.key !== "Escape") return; setOpen(false); fab?.focus(); }; window.addEventListener("keydown", onKey); onCleanup(() => window.removeEventListener("keydown", onKey)); }); let start: { x: number; y: number; left: number; top: number } | null = null; let moved = false; // A drag that ends over the button is followed by a click on mouse (not // always on touch), which must not toggle the menu. Reset on every press so // a swallowed click cannot leave the next real tap ignored. let suppressClick = false; const startDrag = (event: PointerEvent) => { if (event.pointerType === "mouse" && event.button !== 0) return; suppressClick = false; moved = false; start = { x: event.clientX, y: event.clientY, left: left(), top: top() }; (event.currentTarget as HTMLElement).setPointerCapture(event.pointerId); }; const moveDrag = (event: PointerEvent) => { if (!start) return; const dx = event.clientX - start.x; const dy = event.clientY - start.y; if (!moved && Math.hypot(dx, dy) < TAP_SLOP) return; if (!moved) { moved = true; setOpen(false); } const next = { left: clamp(start.left + dx, 0, bounds().w - SIZE), top: clamp(start.top + dy, 0, bounds().h - SIZE), }; setDrag(next); const hit = Math.hypot(next.left - hideLeft(), next.top - hideTop()) < HIDE_REACH; if (hit && !overHide()) navigator.vibrate?.(10); setOverHide(hit); }; const endDrag = (commit: boolean) => { if (!start) return; start = null; if (!moved) return; suppressClick = true; const last = drag(); if (commit && overHide()) { // Parked at the spot it was dragged from, so a tap on the tab puts it // back there rather than on the hide target. setParked(true); persist(); } else if (commit && last) { setPos({ x: travelX() ? clamp01((last.left - MARGIN) / travelX()) : pos().x, y: travelY() ? clamp01((last.top - MARGIN) / travelY()) : pos().y, }); persist(); } setDrag(null); setOverHide(false); }; const toggle = () => { if (suppressClick) { suppressClick = false; return; } setOpen(!open()); }; const restore = () => { setParked(false); persist(); }; const select = (action: FloatingAction) => { setOpen(false); action.onSelect(); }; const centreX = () => left() + SIZE / 2; const centreY = () => top() + SIZE / 2; const offsets = () => radialOffsets( props.actions.length, bounds().w ? centreX() / bounds().w : 1, bounds().h ? centreY() / bounds().h : 1, RADIUS, ); // Top-left of item i, kept inside the layer as a last resort for fans the // heading alone does not keep on screen. const itemPos = (i: number) => { const offset = offsets()[i] ?? { dx: 0, dy: 0 }; return { left: clamp(centreX() + offset.dx - ITEM / 2, 4, bounds().w - ITEM - 4), top: clamp(centreY() + offset.dy - ITEM / 2, 4, bounds().h - ITEM - 4), }; }; const parkedRight = () => pos().x >= 0.5; const dragging = () => drag() !== null; return ( <> ); }} {/* Parked: a slim tab on the side the button rested nearest. The visible pill is narrow; the button around it is a full thumb target. */} ); }