Skip to content

Commit

Permalink
feat: improve swiping (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilkowalski authored Nov 2, 2024
1 parent 0f320bf commit 1849ab4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,20 +316,11 @@ const Toast = (props: ToastProps) => {
if (!pointerStartRef.current || !dismissible) return;

const yPosition = event.clientY - pointerStartRef.current.y;
const xPosition = event.clientX - pointerStartRef.current.x;

const clamp = y === 'top' ? Math.min : Math.max;
const clampedY = clamp(0, yPosition);
const swipeStartThreshold = event.pointerType === 'touch' ? 10 : 2;
const isAllowedToSwipe = Math.abs(clampedY) > swipeStartThreshold;

if (isAllowedToSwipe) {
toastRef.current?.style.setProperty('--swipe-amount', `${yPosition}px`);
} else if (Math.abs(xPosition) > swipeStartThreshold) {
// User is swiping in wrong direction so we disable swipe gesture
// for the current pointer down interaction
pointerStartRef.current = null;
}
const isHighlighted = window.getSelection()?.toString().length > 0;

if (isHighlighted) return;

toastRef.current?.style.setProperty('--swipe-amount', `${Math.max(0, yPosition)}px`);
}}
>
{closeButton && !toast.jsx ? (
Expand Down Expand Up @@ -643,7 +634,13 @@ const Toaster = forwardRef<HTMLElement, ToasterProps>(function Toaster(props, re

return (
// Remove item from normal navigation flow, only available via hotkey
<section aria-label={`${containerAriaLabel} ${hotkeyLabel}`} tabIndex={-1} aria-live="polite" aria-relevant="additions text" aria-atomic="false">
<section
aria-label={`${containerAriaLabel} ${hotkeyLabel}`}
tabIndex={-1}
aria-live="polite"
aria-relevant="additions text"
aria-atomic="false"
>
{possiblePositions.map((position, index) => {
const [y, x] = position.split('-');

Expand Down
1 change: 1 addition & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@
[data-sonner-toast][data-swiping='true'] {
transform: var(--y) translateY(var(--swipe-amount, 0px));
transition: none;
user-select: none;
}

[data-sonner-toast][data-swipe-out='true'][data-y-position='bottom'],
Expand Down

0 comments on commit 1849ab4

Please sign in to comment.