Skip to content

An opinionated toast component for React.

Fix batch updates (#28)

Emil Kowalski082bca612fef
diff --git a/README.md b/README.md
index 023b8f2..9a0f3c6 100644
--- a/README.md
+++ b/README.md
@@ -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', {
diff --git a/package.json b/package.json
index 600a5c5..158eb27 100644
--- a/package.json
+++ b/package.json
@@ -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]"
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 69d37c7..375730a 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -9,6 +9,7 @@ importers:
'@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
@@ -18,6 +19,7 @@ importers:
'@types/react': 18.0.26
prettier: 2.8.4
react: 18.2.0
+ react-dom: [email protected]
turbo: 1.6.3
typescript: 4.9.4
@@ -2642,7 +2644,6 @@ packages:
loose-envify: 1.4.0
react: 18.2.0
scheduler: 0.23.0
- dev: false
/react-is/16.13.1:
resolution: {integrity: sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==}
@@ -2772,7 +2773,6 @@ packages:
resolution: {integrity: sha512-CtuThmgHNg7zIZWAXi3AsyIzA3n4xx7aNyjwC2VJldO2LMVDhFK+63xGqq6CsJH4rTAt6/M+N4GhZiDYPx9eUw==}
dependencies:
loose-envify: 1.4.0
- dev: false
/semver/6.3.0:
resolution: {integrity: sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==}
diff --git a/src/index.tsx b/src/index.tsx
index de8fa97..0e06973 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -1,6 +1,7 @@
'use client';
import React from 'react';
+import ReactDOM from 'react-dom';
import './styles.css';
import { getAsset, Loader } from './assets';
@@ -135,7 +136,7 @@ const Toast = (props: ToastProps) => {
}
});
};
-
+
if (toast.promise instanceof Promise) {
promiseHandler(toast.promise);
} else if (typeof toast.promise === 'function') {
@@ -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))}
@@ -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]);
+ });
});
}, []);
diff --git a/src/styles.css b/src/styles.css
index 2c2909c..2d84af9 100644
--- a/src/styles.css
+++ b/src/styles.css
@@ -118,6 +118,7 @@
[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;
}
@@ -125,6 +126,10 @@
flex-shrink: 0;
}
+[data-sonner-toast] [data-icon] svg {
+ margin-left: -1px;
+}
+
[data-sonner-toast] [data-content] {
display: flex;
flex-direction: column;
@@ -410,6 +415,7 @@
}
.sonner-loading-wrapper[data-visible='false'] {
+ transform-origin: center;
animation: sonner-fade-out 0.2s ease forwards;
}