Skip to content

Commit 33dbb83

Browse files
committed
feat(Toast): the swipe follows iOS notification banners
Toast's touch swipe now reads the notification banner's measured values (iOS 26.2 simulator, 60 fps recordings of HID-driven drags) from a new Toast domain of the UIKit table, motion/uikit/toast.ts: - the pan begins at UIKit's hysteresis, but the card stays still for the first 14 px of finger travel and then follows at 0.55 of the finger, so it never jumps; - a release dismisses when the card is 15.7 px out or the finger was moving toward the edge at 105 px/s (UIKit's estimator keeps a stopped finger's last speed); anything else springs back on the smooth system spring (0.5 s, critically damped) on the compositor, handed the card's release velocity, kept under Reduce Motion as the banner keeps it; - a dismissed card plays the ordinary exit from where the finger left it (its row stops clipping it), never moving back toward rest; - no fade or scale while dragging, and touches pass through while the card springs back, as the banner never catches a settling card. The four private --_toast-swipe-* variables are gone. toastSwipe.ts holds the math, unit-tested against every recorded banner release; a real-touch interaction spec covers the follow, the settle curve, the dismiss paths, the pass-through, the settle through a blocked main thread and no Layout or Paint while dragging or settling.
1 parent d1b2046 commit 33dbb83

15 files changed

Lines changed: 1326 additions & 818 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[fix] A Toast's swipe now follows iOS notification banners. The card stays still for the first 14 px of finger travel and then follows at 0.55 of the finger, with no jump. On release, the finger's travel projected with its velocity decides: a quick flick dismisses even when it barely moved the card, and a release short of dismissing springs the card back. A dismissed card leaves from where the finger left it and never moves back toward rest. The swipe no longer fades or shrinks the card. While the card springs back, touches pass through to the page.
6+
7+
@vjeux

‎apps/storybook/stories/Toast.stories.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ export const MobileSwipeToDismiss: StoryObj = {
498498
render: () => (
499499
<MobileCanvas
500500
title="Swipe dismissal"
501-
description="Swipe is an enhancement only; the surface fades as it approaches the edge, and the visible close button remains the simple alternative.">
501+
description="Swipe is an enhancement only: as with iOS notification banners, the card follows the finger after a short distance at about half its speed, a quick flick dismisses it, and the visible close button remains the simple alternative.">
502502
<ToastViewport position="topEnd" isTopLayer={false} maxVisible={2}>
503503
<MockCard>
504504
<Text type="supporting" color="secondary">

‎packages/core/src/Toast/Toast.doc.mjs‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,36 +102,12 @@ export const docs = {
102102
default: 'var(--spacing-2)',
103103
private: true,
104104
},
105-
{
106-
name: '--_toast-swipe-y',
107-
description: 'Private active swipe offset along the block axis',
108-
default: '0px',
109-
private: true,
110-
},
111-
{
112-
name: '--_toast-swipe-exit-y',
113-
description: 'Private completed-swipe exit offset',
114-
default: 'var(--_toast-swipe-y)',
115-
private: true,
116-
},
117-
{
118-
name: '--_toast-swipe-opacity',
119-
description: 'Private opacity feedback during an accepted swipe',
120-
default: '1',
121-
private: true,
122-
},
123-
{
124-
name: '--_toast-swipe-scale',
125-
description: 'Private scale feedback during an accepted swipe',
126-
default: '1',
127-
private: true,
128-
},
129105
],
130106
},
131107

132108
usage: {
133109
description:
134-
'Toast shows a brief, non-blocking notification to confirm an action or present temporary information. Use it for scenarios where the user needs feedback but not a decision, such as saving, deleting, or changing a status.\n\nFor production use, prefer the `useToast()` hook; it handles positioning, stacking, auto-dismiss, and deduplication via `ToastViewport`. Toasts stay within viewport and safe-area gutters, wrap long message content, and enter, exit, or swipe-dismiss toward their configured top or bottom edge. The vertical swipe uses the same spatial model as placement motion: top Toasts leave upward and bottom Toasts leave downward. Swipe waits for dominant edge-directed intent before cancelling native touch movement and reports the existing manual dismissal reason. Pen is supported as direct-contact input; mouse drag is excluded to avoid conflicting with desktop text selection, where the visible close control remains available. Set `isAutoHide: false` explicitly when an action or message must remain available. The `Toast` component renders the visual toast element inline and is useful for previews, documentation, and static showcases where the viewport lifecycle is not needed.',
110+
'Toast shows a brief, non-blocking notification to confirm an action or present temporary information. Use it for scenarios where the user needs feedback but not a decision, such as saving, deleting, or changing a status.\n\nFor production use, prefer the `useToast()` hook; it handles positioning, stacking, auto-dismiss, and deduplication via `ToastViewport`. Toasts stay within viewport and safe-area gutters, wrap long message content, and enter, exit, or swipe-dismiss toward their configured top or bottom edge. The vertical swipe uses the same spatial model as placement motion: top Toasts leave upward and bottom Toasts leave downward. As with iOS notification banners, the card starts to follow the finger after a short distance and at about half its speed; a quick flick dismisses it, and a release short of dismissing springs it back. Swipe waits for dominant edge-directed intent before cancelling native touch movement and reports the existing manual dismissal reason. Pen is supported as direct-contact input; mouse drag is excluded to avoid conflicting with desktop text selection, where the visible close control remains available. Set `isAutoHide: false` explicitly when an action or message must remain available. The `Toast` component renders the visual toast element inline and is useful for previews, documentation, and static showcases where the viewport lifecycle is not needed.',
135111
bestPractices: [
136112
{
137113
guidance: true,

‎packages/core/src/Toast/Toast.tsx‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
/**
66
* @file Toast.tsx
7-
* @input Uses React timers, touch/pen gesture events, Toast options, Button/Icon,
8-
* MediaTheme, tokens, and placement motion inherited from ToastViewport
9-
* @output Exports the rendered Toast surface and its pause/swipe/dismiss behavior
7+
* @input Uses React timers, the swipe gesture (useToastGesture), Toast options,
8+
* Button/Icon, MediaTheme, tokens, and placement motion inherited from ToastViewport
9+
* @output Exports the rendered Toast surface and its pause/swipe/dismiss behavior;
10+
* a swipe dismissal plays the ordinary exit from where the finger left the card
1011
* @position Core implementation; rendered by ToastViewport and documented by Toast.doc.mjs
1112
*
1213
* SYNC: When Toast layout, timer pause, media theme, or dismissal behavior changes,
@@ -74,12 +75,13 @@ const styles = stylex.create({
7475
width: 400,
7576
maxWidth: '100%',
7677
boxShadow: shadowVars['--shadow-med'],
77-
opacity: 'var(--_toast-swipe-opacity, 1)',
7878
fontFamily: typographyVars['--font-family-body'],
7979
fontSize: typeScaleDefaults['--text-body-size'],
8080
lineHeight: typeScaleDefaults['--text-body-leading'],
81-
transform:
82-
'translateY(var(--_toast-swipe-y, 0px)) scale(var(--_toast-swipe-scale, 1))',
81+
transform: 'translateY(0px)',
82+
// The swipe moves the card with `translate` every frame: its own layer
83+
// keeps that off the main thread's paint.
84+
willChange: 'translate',
8385
transitionProperty: 'opacity, transform',
8486
transitionDuration: {
8587
default: durationVars['--duration-fast'],
@@ -112,7 +114,7 @@ const styles = stylex.create({
112114
},
113115
exiting: {
114116
opacity: 0,
115-
transform: `translateY(var(--_toast-swipe-exit-y, var(--_toast-swipe-y, var(--_toast-slide-y, ${TOAST_EDGE_DRIFT})))) scale(var(--_toast-swipe-scale, 1))`,
117+
transform: `translateY(var(--_toast-slide-y, ${TOAST_EDGE_DRIFT}))`,
116118
},
117119
endContent: {
118120
flexShrink: 0,
@@ -146,6 +148,11 @@ export interface ToastProps {
146148

147149
interface ToastSurfaceProps extends ToastProps {
148150
gestureDirection: ToastGestureDirection;
151+
/**
152+
* Called when a swipe dismisses the toast, just before `onDismiss`: the
153+
* card leaves from where the finger left it, so its row must not clip it.
154+
*/
155+
onSwipeExit?: () => void;
149156
}
150157

151158
/**
@@ -182,6 +189,7 @@ export function ToastSurface({
182189
onDismiss,
183190
renderContent,
184191
gestureDirection,
192+
onSwipeExit,
185193
}: ToastSurfaceProps) {
186194
const t = useTranslator();
187195
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
@@ -262,14 +270,20 @@ export function ToastSurface({
262270
const dismissFromGesture = useCallback(() => {
263271
onDismissRef.current('manual');
264272
}, []);
265-
const {rootRef, bindings: gestureBindings} = useToastGesture({
273+
const onSwipeExitRef = useRef(onSwipeExit);
274+
onSwipeExitRef.current = onSwipeExit;
275+
const handleSwipeExit = useCallback(() => {
276+
onSwipeExitRef.current?.();
277+
}, []);
278+
const {rootRef} = useToastGesture({
266279
direction: gestureDirection,
267280
enabled: !isExiting,
268281
canPauseTimer: isAutoHide,
269282
isTimerPaused,
270283
pauseTimer,
271284
resumeTimer,
272285
dismiss: dismissFromGesture,
286+
onSwipeExit: handleSwipeExit,
273287
shouldIgnoreTarget: isInteractiveTarget,
274288
});
275289

@@ -294,7 +308,6 @@ export function ToastSurface({
294308
onMouseLeave={resumeTimer}
295309
onFocusCapture={pauseTimer}
296310
onBlurCapture={resumeTimer}
297-
{...gestureBindings}
298311
{...mergeProps(
299312
themeProps('toast', {type}),
300313
stylex.props(

0 commit comments

Comments
 (0)