From 38782b89feafb4c0fffc29aeefcb46cf097b80c3 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 26 Oct 2026 15:43:49 +0100 Subject: [PATCH] feat: remove notification with slide --- .../Notifications/Notifications.css | 26 ++++ .../Notifications/Notifications.tsx | 144 +++++++++++++++--- 2 files changed, 151 insertions(+), 19 deletions(-) diff --git a/src/components/Notifications/Notifications.css b/src/components/Notifications/Notifications.css index 085abcd..8d8ebfd 100644 --- a/src/components/Notifications/Notifications.css +++ b/src/components/Notifications/Notifications.css @@ -10,6 +10,9 @@ width: 320px; pointer-events: none; + + animation: notification-in var(--dur-slow) var(--ease-out); /* no "forwards" */ + touch-action: pan-y; } .notification { @@ -101,3 +104,26 @@ opacity: 0; } } + +/* After a swipe-dismiss, the inline transition takes over (no restart from translateX(0)) */ +.notification--swiped { + animation: none; +} + +@media (pointer: coarse) { + /* Undo the global 44px min-height on the small × button */ + .notification-close { + min-height: 0; + } +} + +@media (max-width: 768px) { + .notifications { + top: calc(var(--topbar-h, 0px) + var(--space-2)); + left: max(var(--space-3), var(--safe-left, 0px)); + right: max(var(--space-3), var(--safe-right, 0px)); + width: auto; + max-width: 420px; + margin-inline: auto; + } +} \ No newline at end of file diff --git a/src/components/Notifications/Notifications.tsx b/src/components/Notifications/Notifications.tsx index a1900fc..12355b9 100644 --- a/src/components/Notifications/Notifications.tsx +++ b/src/components/Notifications/Notifications.tsx @@ -1,3 +1,4 @@ +import { useRef, useState } from 'react'; import type { Notification } from '../../types'; import './Notifications.css'; @@ -7,29 +8,134 @@ type Props = { onHover: (id: number, hovering: boolean) => void; }; +const DRAG_START_PX = 8; // movement before it counts as a swipe +const DISMISS_RATIO = 0.35; // fraction of the width to drag before it dismisses +const DISMISS_MS = 160; + +function NotificationItem({ + n, + onRemove, + onHover, +}: { + n: Notification; + onRemove: (id: number) => void; + onHover: (id: number, hovering: boolean) => void; +}) { + const ref = useRef(null); + const startX = useRef(null); + const locked = useRef(false); + + const [dx, setDx] = useState(0); + const [dragging, setDragging] = useState(false); + const [swiped, setSwiped] = useState(false); + + function reset() { + const wasLocked = locked.current; + startX.current = null; + locked.current = false; + setDragging(false); + setDx(0); + if (wasLocked) onHover(n.id, false); + } + + function onPointerDown(e: React.PointerEvent) { + if (swiped || n.removing) return; + if ((e.target as HTMLElement).closest('button')) return; // let the × button work + startX.current = e.clientX; + locked.current = false; + } + + function onPointerMove(e: React.PointerEvent) { + if (startX.current === null) return; + const delta = e.clientX - startX.current; + + if (!locked.current) { + if (Math.abs(delta) < DRAG_START_PX) return; + locked.current = true; + e.currentTarget.setPointerCapture(e.pointerId); + setDragging(true); + onHover(n.id, true); // pause the auto-dismiss timer + } + setDx(delta); + } + + function onPointerUp(e: React.PointerEvent) { + if (startX.current === null) return; + + if (!locked.current) { + startX.current = null; + return; + } + + const width = ref.current?.offsetWidth ?? 300; + if (Math.abs(dx) > width * DISMISS_RATIO) { + const dir = dx > 0 ? 1 : -1; + startX.current = null; + locked.current = false; + setSwiped(true); + setDragging(false); + setDx(dir * width * 1.2); + window.setTimeout(() => onRemove(n.id), DISMISS_MS); + } else { + reset(); + } + + if (e.currentTarget.hasPointerCapture(e.pointerId)) { + e.currentTarget.releasePointerCapture(e.pointerId); + } + } + + const touched = dx !== 0 || swiped; + const style: React.CSSProperties | undefined = touched + ? { + transform: `translateX(${dx}px)`, + opacity: swiped ? 0 : Math.max(0.3, 1 - Math.abs(dx) / 300), + transition: dragging + ? 'none' + : `transform ${DISMISS_MS}ms ease-out, opacity ${DISMISS_MS}ms ease-out`, + } + : undefined; + + return ( +
onHover(n.id, true)} + onMouseLeave={() => onHover(n.id, false)} + onPointerDown={onPointerDown} + onPointerMove={onPointerMove} + onPointerUp={onPointerUp} + onPointerCancel={reset} + > + + {n.message} + + + +
+ ); +} + export default function Notifications({ notifications, onRemove, onHover }: Props) { return (
{notifications.map((n) => ( -
onHover(n.id, true)} - onMouseLeave={() => onHover(n.id, false)} - > - - {n.message} - - - -
+ ))}
); -} +} \ No newline at end of file