diff --git a/.changeset/toast-swipe-follows-banner.md b/.changeset/toast-swipe-follows-banner.md new file mode 100644 index 0000000000000..ce91826e1552f --- /dev/null +++ b/.changeset/toast-swipe-follows-banner.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/core': patch +--- + +[feat] Under a finger (a coarse primary pointer), a Toast 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. A swipe that turns back away from the toast's edge stretches the card that way and springs it back. On release, a quick flick toward the edge dismisses even when it barely moved the card, as does a drag that leaves the card far enough out; anything else springs the card back. However a toast is dismissed (a swipe, the close button, the timeout, the app's dismiss), the card leaves as the banner does: it slides past its edge from where it is, without fading, and its row closes after it. A finger that lands on the card while it springs back or leaves catches it, and the card follows the finger. Before, a toast under a finger moved with the finger as it faded and shrank, and faded out when dismissed. With a fine pointer nothing changes. Under reduced motion nothing slides, springs or drifts: under a finger (a coarse primary pointer) the toast fades in place, and with a mouse or keyboard it changes at once. + +@vjeux diff --git a/apps/sandbox/scripts/next-export-routes.json b/apps/sandbox/scripts/next-export-routes.json index 67a3b0ccb2503..bbcd8673a73e4 100644 --- a/apps/sandbox/scripts/next-export-routes.json +++ b/apps/sandbox/scripts/next-export-routes.json @@ -67,6 +67,7 @@ "/pages/text-types/", "/pages/textarea-counter/", "/pages/theme-family/", + "/pages/toast-swipe-cases/", "/pages/topnav-menu/", "/pages/y2k-palette/", "/templates/", diff --git a/apps/sandbox/src/app/(raw)/pages/toast-swipe-cases/caseIds.ts b/apps/sandbox/src/app/(raw)/pages/toast-swipe-cases/caseIds.ts new file mode 100644 index 0000000000000..75a594a5e832f --- /dev/null +++ b/apps/sandbox/src/app/(raw)/pages/toast-swipe-cases/caseIds.ts @@ -0,0 +1,35 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +/** + * @file caseIds.ts + * @input None + * @output The Toast swipe case ids, their labels, and the parser for `?case=` + * @position Shared by the Toast swipe cases page and its cases. + * + * SYNC: A new case needs a toast body in cases.tsx. + */ + +export const TOAST_SWIPE_CASES = [ + 'swipe-away', + 'turn-back', + 'pull-away', +] as const; + +export type ToastSwipeCaseId = (typeof TOAST_SWIPE_CASES)[number]; + +export const TOAST_SWIPE_CASE_LABELS: Readonly< + Record +> = { + 'swipe-away': + 'Swipe a top toast up and away. A short drag springs it back; a quick flick or a longer drag sends it off the top', + 'turn-back': + 'Drag the toast up, then move back down before you lift. A release that moves back springs the card back', + 'pull-away': + 'Drag the toast up a little, then pull it down past where it started. The card stretches down and springs back', +}; + +export function parseToastSwipeCase(value: string | null): ToastSwipeCaseId { + return (TOAST_SWIPE_CASES as readonly string[]).includes(value ?? '') + ? (value as ToastSwipeCaseId) + : TOAST_SWIPE_CASES[0]; +} diff --git a/apps/sandbox/src/app/(raw)/pages/toast-swipe-cases/cases.tsx b/apps/sandbox/src/app/(raw)/pages/toast-swipe-cases/cases.tsx new file mode 100644 index 0000000000000..e3bdb4d819a96 --- /dev/null +++ b/apps/sandbox/src/app/(raw)/pages/toast-swipe-cases/cases.tsx @@ -0,0 +1,53 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +'use client'; + +/** + * @file cases.tsx + * @input A case id from caseIds.ts + * @output The case: a top toast to swipe up and away, to turn back, or to + * pull past where it started + * @position The Toast swipe cases page's content. + */ + +import {useEffect, useRef} from 'react'; +import {Button} from '@astryxdesign/core/Button'; +import {ToastViewport, useToast} from '@astryxdesign/core/Toast'; +import type {ToastDismissFn} from '@astryxdesign/core/Toast'; +import type {ToastSwipeCaseId} from './caseIds'; + +const BODIES: Readonly> = { + 'swipe-away': 'Swipe this toast up and away.', + 'turn-back': 'Drag this toast up, then back down before you lift.', + 'pull-away': + 'Drag this toast up a little, then pull it down past where it started.', +}; + +function ShowToast({id}: {id: ToastSwipeCaseId}) { + const toast = useToast(); + // The case's one toast. Showing it again replaces it. + const dismissRef = useRef(null); + // Synchronizes the toast system with the case: leaving the case dismisses + // its toast. + useEffect(() => () => dismissRef.current?.(), []); + return ( +