Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
emilkowalski committed Jan 15, 2025
1 parent 0434498 commit 37dff4c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
26 changes: 21 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { toast, ToastState } from './state';
import './styles.css';
import {
isAction,
SwipeDirection,
type ExternalToast,
type HeightT,
type ToasterProps,
Expand Down Expand Up @@ -45,6 +46,25 @@ function cn(...classes: (string | undefined)[]) {
return classes.filter(Boolean).join(' ');
}

function getDefaultSwipeDirections(position: string): Array<SwipeDirection> {
const [y, x] = position.split('-');
const directions: Array<SwipeDirection> = [];

if (y === 'top') {
directions.push('top');
} else if (y === 'bottom') {
directions.push('bottom');
}

if (x === 'left') {
directions.push('left');
} else if (x === 'right') {
directions.push('right');
}

return directions;
}

const Toast = (props: ToastProps) => {
const {
invert: ToasterInvert,
Expand Down Expand Up @@ -315,16 +335,13 @@ const Toast = (props: ToastProps) => {
);
const timeTaken = new Date().getTime() - dragStartTime.current?.getTime();

// Calculate total swipe amount based on the locked direction
const swipeAmount = swipeDirection === 'x' ? swipeAmountX : swipeAmountY;
const velocity = Math.abs(swipeAmount) / timeTaken;

// Remove only if threshold is met
if (Math.abs(swipeAmount) >= SWIPE_THRESHOLD || velocity > 0.11) {
setOffsetBeforeRemove(offset.current);
toast.onDismiss?.(toast);

// Set the swipe out direction based on the movement
if (swipeDirection === 'x') {
setSwipeOutDirection(swipeAmountX > 0 ? 'right' : 'left');
} else {
Expand All @@ -349,7 +366,7 @@ const Toast = (props: ToastProps) => {
const yDelta = event.clientY - pointerStartRef.current.y;
const xDelta = event.clientX - pointerStartRef.current.x;

const swipeDirections = props.swipeDirections ?? ['bottom', 'right'];
const swipeDirections = props.swipeDirections ?? getDefaultSwipeDirections(position);

// Determine swipe direction if not already locked
if (!swipeDirection && (Math.abs(xDelta) > 5 || Math.abs(yDelta) > 5)) {
Expand Down Expand Up @@ -835,7 +852,6 @@ const Toaster = forwardRef<HTMLElement, ToasterProps>(function Toaster(props, re
expanded={expanded}
pauseWhenPageIsHidden={pauseWhenPageIsHidden}
swipeDirections={props.swipeDirections}
y={props.y}
/>
))}
</ol>
Expand Down
4 changes: 2 additions & 2 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@
:where([data-sonner-toast][data-swiping='true'])::before {
content: '';
position: absolute;
left: 0;
right: 0;
left: -50%;
right: -50%;
height: 100%;
z-index: -1;
}
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export interface ToasterProps {
offset?: Offset;
mobileOffset?: Offset;
dir?: 'rtl' | 'ltr' | 'auto';
swipeDirections?: SwipeDirection[];
/**
* @deprecated Please use the `icons` prop instead:
* ```jsx
Expand All @@ -144,7 +145,7 @@ export interface ToasterProps {
pauseWhenPageIsHidden?: boolean;
}

type SwipeDirection = 'top' | 'right' | 'bottom' | 'left';
export type SwipeDirection = 'top' | 'right' | 'bottom' | 'left';

export interface ToastProps {
toast: ToastT;
Expand Down
9 changes: 8 additions & 1 deletion website/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ export default function Home() {
return (
<div className="wrapper light">
<Head />
<Toaster theme="light" richColors={richColors} closeButton={closeButton} expand={expand} position={position} />
<Toaster
theme="light"
richColors={richColors}
closeButton={closeButton}
expand={expand}
position={position}
swipeDirections={['bottom', 'left', 'right']}
/>
<main className="container">
<Hero />
<div className="content">
Expand Down

0 comments on commit 37dff4c

Please sign in to comment.