import { useRef } from 'react'; import { SNACKBAR_SWIPE_DISMISS_THRESHOLD } from './constants'; const DISMISS_CLASS = 'wds-Snackbar--swipe-dismiss'; const SETTLE_CLASS = 'wds-Snackbar--swipe-settle'; type SwipePointer = { id: number; target: Element; startX: number; startY: number; threshold: number; }; /** * Follows one primary pointer's downward, mostly vertical movement without re-rendering. * Release dismisses at half the surface height; shorter or cancelled gestures settle back. * A drag consumes its follow-up click, so dragging from the action cannot activate it. */ const useSnackbarSwipeDismiss = ({ onDismissRequest, onSwipeChange, }: { onDismissRequest: () => void; /** Reports when a gesture starts and ends so auto-dismiss can pause beneath the pointer. */ onSwipeChange: (swiping: boolean) => void; }) => { const containerRef = useRef(null); const pointerRef = useRef(null); const suppressClick = useRef(false); const endGesture = () => { pointerRef.current = null; onSwipeChange(false); }; const settle = () => { containerRef.current?.classList.add(SETTLE_CLASS); containerRef.current?.style.removeProperty('transform'); }; const handlePointerDown = (event: React.PointerEvent) => { if (event.button !== 0 || !event.isPrimary || pointerRef.current) { return; } const target = event.target as Element; // Keeps move/up events coming when a mouse leaves the Snackbar mid-drag. target.setPointerCapture?.(event.pointerId); containerRef.current?.classList.remove(DISMISS_CLASS, SETTLE_CLASS); suppressClick.current = false; pointerRef.current = { id: event.pointerId, target, startX: event.clientX, startY: event.clientY, threshold: event.currentTarget.offsetHeight * SNACKBAR_SWIPE_DISMISS_THRESHOLD, }; onSwipeChange(true); }; const handlePointerMove = (event: React.PointerEvent) => { const pointer = pointerRef.current; if (pointer?.id !== event.pointerId) { return; } const offsetY = event.clientY - pointer.startY; const offsetX = event.clientX - pointer.startX; if (offsetY > 0 && offsetY > Math.abs(offsetX)) { containerRef.current?.style.setProperty('transform', `translateY(${offsetY}px)`); } else { containerRef.current?.style.removeProperty('transform'); } }; const handlePointerUp = (event: React.PointerEvent) => { const pointer = pointerRef.current; if (pointer?.id !== event.pointerId) { return; } endGesture(); const offsetY = event.clientY - pointer.startY; const offsetX = event.clientX - pointer.startX; const container = containerRef.current; if (offsetY >= pointer.threshold && offsetY > Math.abs(offsetX)) { suppressClick.current = true; if (container) { // Include wrapper padding and the safe area, not only the surface, to clear the viewport. container.classList.add(DISMISS_CLASS); container.style.setProperty( 'transform', `translateY(${Math.max(offsetY, container.offsetHeight)}px)`, ); } onDismissRequest(); } else { // Capture retargets the click to the pressed control even when released outside it, // unlike a native button; cancel it in that case. const bounds = pointer.target.closest('a, button')?.getBoundingClientRect(); suppressClick.current = !!bounds && (offsetX !== 0 || offsetY !== 0) && (event.clientX < bounds.left || event.clientX > bounds.right || event.clientY < bounds.top || event.clientY > bounds.bottom); settle(); } }; const handlePointerCancel = (event: React.PointerEvent) => { if (pointerRef.current?.id !== event.pointerId) { return; } endGesture(); settle(); }; /** Ends a gesture still in progress when the Snackbar starts exiting for another reason. */ const cancelSwipe = () => { const pointer = pointerRef.current; if (!pointer) { return; } pointer.target.releasePointerCapture?.(pointer.id); endGesture(); settle(); }; const handleClickCapture = (event: React.MouseEvent) => { if (suppressClick.current && event.detail > 0) { suppressClick.current = false; event.preventDefault(); event.stopPropagation(); } }; return { cancelSwipe, containerRef, handleClickCapture, handlePointerCancel, handlePointerDown, handlePointerMove, handlePointerUp, }; }; export default useSnackbarSwipeDismiss;