feat: remove notification with slide
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<HTMLDivElement>(null);
|
||||
const startX = useRef<number | null>(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<HTMLDivElement>) {
|
||||
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<HTMLDivElement>) {
|
||||
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<HTMLDivElement>) {
|
||||
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 (
|
||||
<div
|
||||
ref={ref}
|
||||
className={[
|
||||
'notification',
|
||||
n.type,
|
||||
n.removing && !swiped ? 'notification-out' : '',
|
||||
swiped ? 'notification--swiped' : '',
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' ')}
|
||||
style={style}
|
||||
onMouseEnter={() => onHover(n.id, true)}
|
||||
onMouseLeave={() => onHover(n.id, false)}
|
||||
onPointerDown={onPointerDown}
|
||||
onPointerMove={onPointerMove}
|
||||
onPointerUp={onPointerUp}
|
||||
onPointerCancel={reset}
|
||||
>
|
||||
<span className="notification-message" title={n.message}>
|
||||
{n.message}
|
||||
</span>
|
||||
|
||||
<button
|
||||
className="tab-close notification-close"
|
||||
onClick={() => onRemove(n.id)}
|
||||
aria-label="Dismiss notification"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Notifications({ notifications, onRemove, onHover }: Props) {
|
||||
return (
|
||||
<div className="notifications">
|
||||
{notifications.map((n) => (
|
||||
<div
|
||||
key={n.id}
|
||||
className={`notification ${n.type} ${n.removing ? 'notification-out' : ''}`}
|
||||
onMouseEnter={() => onHover(n.id, true)}
|
||||
onMouseLeave={() => onHover(n.id, false)}
|
||||
>
|
||||
<span className="notification-message" title={n.message}>
|
||||
{n.message}
|
||||
</span>
|
||||
|
||||
<button
|
||||
className="tab-close notification-close"
|
||||
onClick={() => onRemove(n.id)}
|
||||
aria-label="Dismiss notification"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
<NotificationItem key={n.id} n={n} onRemove={onRemove} onHover={onHover} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user