Skip to content

Commit

Permalink
feat: improve lift animation, better swiping UX (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilkowalski authored Nov 3, 2024
1 parent baa9bf7 commit 0c5fab4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const Toast = (props: ToastProps) => {
const [removed, setRemoved] = React.useState(false);
const [swiping, setSwiping] = React.useState(false);
const [swipeOut, setSwipeOut] = React.useState(false);
const [isSwiped, setIsSwiped] = React.useState(false);
const [offsetBeforeRemove, setOffsetBeforeRemove] = React.useState(0);
const [initialHeight, setInitialHeight] = React.useState(0);
const remainingTime = React.useRef(toast.duration || durationFromToaster || TOAST_LIFETIME);
Expand Down Expand Up @@ -132,7 +133,7 @@ const Toast = (props: ToastProps) => {
const toastNode = toastRef.current;
if (toastNode) {
const height = toastNode.getBoundingClientRect().height;
// Add toast height tot heights array after the toast is mounted
// Add toast height to heights array after the toast is mounted
setInitialHeight(height);
setHeights((h) => [{ toastId: toast.id, height, position: toast.position }, ...h]);
return () => setHeights((h) => h.filter((height) => height.toastId !== toast.id));
Expand Down Expand Up @@ -259,6 +260,7 @@ const Toast = (props: ToastProps) => {
data-styled={!Boolean(toast.jsx || toast.unstyled || unstyled)}
data-mounted={mounted}
data-promise={Boolean(toast.promise)}
data-swiped={isSwiped}
data-removed={removed}
data-visible={isVisible}
data-y-position={y}
Expand Down Expand Up @@ -306,6 +308,7 @@ const Toast = (props: ToastProps) => {
toast.onDismiss?.(toast);
deleteToast();
setSwipeOut(true);
setIsSwiped(false);
return;
}

Expand All @@ -317,6 +320,11 @@ const Toast = (props: ToastProps) => {

const yPosition = event.clientY - pointerStartRef.current.y;
const isHighlighted = window.getSelection()?.toString().length > 0;
const swipeAmount = Number(toastRef.current?.style.getPropertyValue('--swipe-amount').replace('px', '') || 0);

if (swipeAmount > 0) {
setIsSwiped(true);
}

if (isHighlighted) return;

Expand Down Expand Up @@ -656,6 +664,7 @@ const Toaster = forwardRef<HTMLElement, ToasterProps>(function Toaster(props, re
data-sonner-toaster
data-theme={actualTheme}
data-y-position={y}
data-expanded={expanded}
data-x-position={x}
style={
{
Expand Down
5 changes: 4 additions & 1 deletion src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
transition: transform 400ms ease;
}

:where([data-sonner-toaster]:hover) {
:where([data-sonner-toaster][data-expanded='true']) {
transform: translateY(-10px);
}

Expand Down Expand Up @@ -337,6 +337,9 @@
[data-sonner-toast][data-swiping='true'] {
transform: var(--y) translateY(var(--swipe-amount, 0px));
transition: none;
}

[data-sonner-toast][data-swiped='true'] {
user-select: none;
}

Expand Down

0 comments on commit 0c5fab4

Please sign in to comment.