Skip to content

Commit

Permalink
Fix batch updates (#28)
Browse files Browse the repository at this point in the history
* Fix batch updates

* cleanup

* Readme update

* Fix rich colors and icon alignment

* Change timeout
  • Loading branch information
emilkowalski authored Mar 9, 2023
1 parent ca91200 commit 082bca6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Most basic toast. You can customize it (and any other type) by passing an option
toast('Event has been created');
```

With icon and description:
With custom icon and description:

```jsx
toast('Event has been created', {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
"@types/react": "^18.0.26",
"prettier": "^2.8.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tsup": "^6.4.0",
"turbo": "1.6",
"typescript": "^4.8.4"
},
"peerDependencies": {
"react": "^18.0.0"
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"packageManager": "[email protected]"
}
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';

import React from 'react';
import ReactDOM from 'react-dom';

import './styles.css';
import { getAsset, Loader } from './assets';
Expand Down Expand Up @@ -135,7 +136,7 @@ const Toast = (props: ToastProps) => {
}
});
};

if (toast.promise instanceof Promise) {
promiseHandler(toast.promise);
} else if (typeof toast.promise === 'function') {
Expand Down Expand Up @@ -241,7 +242,7 @@ const Toast = (props: ToastProps) => {
data-index={index}
data-front={isFront}
data-swiping={swiping}
data-type={promiseStatus !== 'loading' ? promiseStatus : toastType}
data-type={promiseStatus !== 'loading' && promiseStatus ? promiseStatus : toastType}
data-invert={invert}
data-swipe-out={swipeOut}
data-expanded={Boolean(expanded || (expandByDefault && mounted))}
Expand Down Expand Up @@ -429,7 +430,10 @@ const Toaster = (props: ToasterProps) => {
return;
}

setToasts((toasts) => [toast, ...toasts]);
// Don't batch update toasts to prevent wrong calculations
ReactDOM.flushSync(() => {
setToasts((toasts) => [toast, ...toasts]);
});
});
}, []);

Expand Down
6 changes: 6 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,18 @@
[data-sonner-toast][data-promise='true'] [data-icon] > svg {
opacity: 0;
transform: scale(0.8);
transform-origin: center;
animation: sonner-fade-in 300ms ease forwards;
}

[data-sonner-toast] [data-icon] > * {
flex-shrink: 0;
}

[data-sonner-toast] [data-icon] svg {
margin-left: -1px;
}

[data-sonner-toast] [data-content] {
display: flex;
flex-direction: column;
Expand Down Expand Up @@ -410,6 +415,7 @@
}

.sonner-loading-wrapper[data-visible='false'] {
transform-origin: center;
animation: sonner-fade-out 0.2s ease forwards;
}

Expand Down

1 comment on commit 082bca6

@vercel
Copy link

@vercel vercel bot commented on 082bca6 Mar 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.