feat: better style for notifications

This commit is contained in:
Lucas
2026-11-03 16:00:26 +01:00
parent 8c766c3bc2
commit be713620ef
6 changed files with 101 additions and 38 deletions
@@ -1 +1 @@
C:/Users/lsdg/OneDrive - ETS ROBERT SAS 49/Bureau/Projects/dsp-thomann-interface/src-tauri/target/aarch64-linux-android/debug/libthomann_dsp_app_lib.so C:/Users/lsdg/OneDrive - ETS ROBERT SAS 49/Bureau/Projects/dsp-thomann-interface/src-tauri/target/aarch64-linux-android/release/libthomann_dsp_app_lib.so
File diff suppressed because one or more lines are too long
+50 -19
View File
@@ -1,23 +1,20 @@
.notifications { .notifications {
position: fixed; position: fixed;
right: var(--space-5); right: var(--space-5);
top: var(--space-5); top: calc(var(--topbar-h) + var(--space-3));
z-index: 9999; z-index: 9999;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: var(--space-3); gap: var(--space-3);
width: 320px; width: min(320px, calc(100vw - 2 * var(--space-5)));
pointer-events: none; pointer-events: none;
animation: notification-in var(--dur-slow) var(--ease-out); /* no "forwards" */
touch-action: pan-y;
} }
.notification { .notification {
display: flex; display: flex;
align-items: center; align-items: flex-start;
justify-content: space-between; justify-content: space-between;
gap: var(--space-3); gap: var(--space-3);
@@ -25,14 +22,34 @@
border-radius: var(--radius-md); border-radius: var(--radius-md);
font-size: 14px; font-size: 14px;
line-height: 1.45;
box-shadow: var(--shadow-md); box-shadow: var(--shadow-md);
animation: notification-in var(--dur-slow) var(--ease-out) forwards; /* no "forwards": it would keep overriding the inline drag transform */
animation: notification-in var(--dur-slow) var(--ease-out);
pointer-events: auto; pointer-events: auto;
min-width: 0;
/* swipe behavior */
touch-action: pan-y; /* browser keeps vertical scroll, we handle horizontal */
user-select: none;
-webkit-user-select: none;
cursor: grab;
will-change: transform, opacity;
} }
.notification--dragging {
cursor: grabbing;
}
/* once the user touches it, the inline style fully controls transform/opacity */
.notification--touched:not(.notification-out) {
animation: none;
}
/* must stay "forwards" so it holds its end state until removal */
.notification-out { .notification-out {
animation: notification-out var(--dur-slow) var(--ease-in) forwards; animation: notification-out var(--dur-slow) var(--ease-in) forwards;
} }
@@ -76,9 +93,10 @@
.notification-message { .notification-message {
flex: 1; flex: 1;
overflow: hidden; min-width: 0;
text-overflow: ellipsis; white-space: normal;
white-space: nowrap; overflow-wrap: anywhere;
word-break: normal;
} }
@keyframes notification-in { @keyframes notification-in {
@@ -105,13 +123,7 @@
} }
} }
/* After a swipe-dismiss, the inline transition takes over (no restart from translateX(0)) */
.notification--swiped {
animation: none;
}
@media (pointer: coarse) { @media (pointer: coarse) {
/* Undo the global 44px min-height on the small × button */
.notification-close { .notification-close {
min-height: 0; min-height: 0;
} }
@@ -119,11 +131,30 @@
@media (max-width: 768px) { @media (max-width: 768px) {
.notifications { .notifications {
top: calc(var(--topbar-h, 0px) + var(--space-2)); top: calc(var(--topbar-h) + var(--space-2));
left: max(var(--space-3), var(--safe-left, 0px)); left: max(var(--space-3), var(--safe-left));
right: max(var(--space-3), var(--safe-right, 0px)); right: max(var(--space-3), var(--safe-right));
width: auto; width: auto;
max-width: 420px; max-width: 420px;
margin-inline: auto; margin-inline: auto;
} }
.notification {
padding: var(--space-3);
font-size: 14px;
align-items: flex-start;
}
.notification-message {
white-space: normal;
overflow: visible;
text-overflow: unset;
overflow-wrap: anywhere;
}
}
@media (prefers-reduced-motion: reduce) {
.notification {
animation-duration: 1ms;
}
} }
+50 -18
View File
@@ -8,9 +8,11 @@ type Props = {
onHover: (id: number, hovering: boolean) => void; onHover: (id: number, hovering: boolean) => void;
}; };
const DRAG_START_PX = 8; // movement before it counts as a swipe const DRAG_START_PX = 6; // movement before it counts as a swipe
const DISMISS_RATIO = 0.35; // fraction of the width to drag before it dismisses const DISMISS_RATIO = 0.35; // fraction of width to drag before it dismisses
const DISMISS_MS = 160; const FLICK_VELOCITY = 0.5; // px/ms, a fast flick dismisses even below the threshold
const DISMISS_MS = 180;
const SNAP_MS = 220;
function NotificationItem({ function NotificationItem({
n, n,
@@ -23,25 +25,34 @@ function NotificationItem({
}) { }) {
const ref = useRef<HTMLDivElement>(null); const ref = useRef<HTMLDivElement>(null);
const startX = useRef<number | null>(null); const startX = useRef<number | null>(null);
const dxRef = useRef(0);
const locked = useRef(false); const locked = useRef(false);
const last = useRef({ x: 0, t: 0, v: 0 }); // for velocity
const [dx, setDx] = useState(0); const [dx, setDx] = useState(0);
const [dragging, setDragging] = useState(false); const [dragging, setDragging] = useState(false);
const [swiped, setSwiped] = useState(false); const [swiped, setSwiped] = useState(false);
const [interacted, setInteracted] = useState(false); // disables the entry animation after first touch
function updateDx(v: number) {
dxRef.current = v;
setDx(v);
}
function reset() { function reset() {
const wasLocked = locked.current; const wasLocked = locked.current;
startX.current = null; startX.current = null;
locked.current = false; locked.current = false;
setDragging(false); setDragging(false);
setDx(0); updateDx(0); // transition handles the snap-back
if (wasLocked) onHover(n.id, false); if (wasLocked) onHover(n.id, false);
} }
function onPointerDown(e: React.PointerEvent<HTMLDivElement>) { function onPointerDown(e: React.PointerEvent<HTMLDivElement>) {
if (swiped || n.removing) return; if (swiped || n.removing) return;
if ((e.target as HTMLElement).closest('button')) return; // let the × button work if ((e.target as HTMLElement).closest('button')) return;
startX.current = e.clientX; startX.current = e.clientX;
last.current = { x: e.clientX, t: performance.now(), v: 0 };
locked.current = false; locked.current = false;
} }
@@ -53,13 +64,25 @@ function NotificationItem({
if (Math.abs(delta) < DRAG_START_PX) return; if (Math.abs(delta) < DRAG_START_PX) return;
locked.current = true; locked.current = true;
e.currentTarget.setPointerCapture(e.pointerId); e.currentTarget.setPointerCapture(e.pointerId);
setInteracted(true);
setDragging(true); setDragging(true);
onHover(n.id, true); // pause the auto-dismiss timer onHover(n.id, true); // pause auto-dismiss timer
}
setDx(delta);
} }
function onPointerUp(e: React.PointerEvent<HTMLDivElement>) { // velocity (smoothed)
const now = performance.now();
const dt = now - last.current.t;
if (dt > 0) {
const instant = (e.clientX - last.current.x) / dt;
last.current.v = last.current.v * 0.6 + instant * 0.4;
}
last.current.x = e.clientX;
last.current.t = now;
updateDx(delta);
}
function finish(e: React.PointerEvent<HTMLDivElement>) {
if (startX.current === null) return; if (startX.current === null) return;
if (!locked.current) { if (!locked.current) {
@@ -68,13 +91,18 @@ function NotificationItem({
} }
const width = ref.current?.offsetWidth ?? 300; const width = ref.current?.offsetWidth ?? 300;
if (Math.abs(dx) > width * DISMISS_RATIO) { const dist = dxRef.current;
const dir = dx > 0 ? 1 : -1; const v = last.current.v;
const flicked = Math.abs(v) > FLICK_VELOCITY && Math.sign(v) === Math.sign(dist);
const farEnough = Math.abs(dist) > width * DISMISS_RATIO;
if (flicked || farEnough) {
const dir = dist > 0 ? 1 : -1;
startX.current = null; startX.current = null;
locked.current = false; locked.current = false;
setSwiped(true); setSwiped(true);
setDragging(false); setDragging(false);
setDx(dir * width * 1.2); updateDx(dir * width * 1.1);
window.setTimeout(() => onRemove(n.id), DISMISS_MS); window.setTimeout(() => onRemove(n.id), DISMISS_MS);
} else { } else {
reset(); reset();
@@ -85,14 +113,17 @@ function NotificationItem({
} }
} }
const touched = dx !== 0 || swiped; const width = ref.current?.offsetWidth ?? 300;
const style: React.CSSProperties | undefined = touched const progress = Math.min(1, Math.abs(dx) / (width * 0.8));
const style: React.CSSProperties | undefined = interacted
? { ? {
transform: `translateX(${dx}px)`, transform: `translateX(${dx}px)`,
opacity: swiped ? 0 : Math.max(0.3, 1 - Math.abs(dx) / 300), opacity: swiped ? 0 : 1 - progress * 0.9,
transition: dragging transition: dragging
? 'none' ? 'none'
: `transform ${DISMISS_MS}ms ease-out, opacity ${DISMISS_MS}ms ease-out`, : `transform ${swiped ? DISMISS_MS : SNAP_MS}ms cubic-bezier(0.22, 1, 0.36, 1),
opacity ${swiped ? DISMISS_MS : SNAP_MS}ms ease-out`,
} }
: undefined; : undefined;
@@ -103,7 +134,8 @@ function NotificationItem({
'notification', 'notification',
n.type, n.type,
n.removing && !swiped ? 'notification-out' : '', n.removing && !swiped ? 'notification-out' : '',
swiped ? 'notification--swiped' : '', interacted ? 'notification--touched' : '',
dragging ? 'notification--dragging' : '',
] ]
.filter(Boolean) .filter(Boolean)
.join(' ')} .join(' ')}
@@ -112,7 +144,7 @@ function NotificationItem({
onMouseLeave={() => onHover(n.id, false)} onMouseLeave={() => onHover(n.id, false)}
onPointerDown={onPointerDown} onPointerDown={onPointerDown}
onPointerMove={onPointerMove} onPointerMove={onPointerMove}
onPointerUp={onPointerUp} onPointerUp={finish}
onPointerCancel={reset} onPointerCancel={reset}
> >
<span className="notification-message" title={n.message}> <span className="notification-message" title={n.message}>