Skip to content

An opinionated toast component for React.

Fix updating duration, update docs (#206)

Emil Kowalski31c276f5a784
diff --git a/src/index.tsx b/src/index.tsx
index 0723998..0c2af4a 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -154,14 +154,14 @@ const Toast = (props: ToastProps) => {
React.useEffect(() => {
if ((toast.promise && toastType === 'loading') || toast.duration === Infinity) return;
let timeoutId: NodeJS.Timeout;
-
+ let remainingTime = duration;
// Pause the timer on each hover
const pauseTimer = () => {
if (lastCloseTimerStartTimeRef.current < closeTimerStartTimeRef.current) {
// Get the elapsed time since the timer started
const elapsedTime = new Date().getTime() - closeTimerStartTimeRef.current;
- closeTimerRemainingTimeRef.current = closeTimerRemainingTimeRef.current - elapsedTime;
+ remainingTime = remainingTime - elapsedTime;
}
lastCloseTimerStartTimeRef.current = new Date().getTime();
@@ -169,11 +169,12 @@ const Toast = (props: ToastProps) => {
const startTimer = () => {
closeTimerStartTimeRef.current = new Date().getTime();
+
// Let the toast know it has started
timeoutId = setTimeout(() => {
toast.onAutoClose?.(toast);
deleteToast();
- }, closeTimerRemainingTimeRef.current);
+ }, remainingTime);
};
if (expanded || interacting) {
diff --git a/website/src/components/CodeBlock/index.tsx b/website/src/components/CodeBlock/index.tsx
index c55b580..05ea4c8 100644
--- a/website/src/components/CodeBlock/index.tsx
+++ b/website/src/components/CodeBlock/index.tsx
@@ -25,9 +25,9 @@ const theme = {
},
},
{
- types: ['atrule', 'keyword', 'attr-name', 'selector'],
+ types: ['atrule', 'keyword', 'attr-name', 'selector', 'string'],
style: {
- color: 'var(--gray10)',
+ color: 'var(--gray11)',
},
},
{
diff --git a/website/src/pages/toast.mdx b/website/src/pages/toast.mdx
index 843115a..1cdfe7e 100644
--- a/website/src/pages/toast.mdx
+++ b/website/src/pages/toast.mdx
@@ -129,6 +129,18 @@ toast('Hello World', {
## Other
+### Updating toasts
+
+You can update a toast by using the `toast` function and passing it the id of the toast you want to update, the rest stays the same.
+
+```jsx
+const toastId = toast('Sonner');
+
+toast.success('Toast has been updated', {
+ id: toastId,
+});
+```
+
### On Close Callback
You can pass `onDismiss` and `onAutoClose` callbacks to each toast. `onDismiss` gets fired when either the close button gets clicked or the toast is swiped. `onAutoClose` fires when the toast disappears automatically after it's timeout (`duration` prop).