Skip to content

Commit

Permalink
Add infinite toast (#46)
Browse files Browse the repository at this point in the history
* Add infinite toast

* Update readme
  • Loading branch information
emilkowalski authored Mar 20, 2023
1 parent 7844652 commit 4694553
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ You can also use the dismiss method without the id to dismiss all toasts.
toast.dismiss();
```

### Programmatically remove toast

You can change the duration of each toast by using the `duration` property, or change the duration of all toasts like this:

```jsx
<Toaster duration={10000} />
```

```jsx
toast('Event has been created', {
duration: 10000,
});

// Persisent toast
toast('Event has been created', {
duration: Infinity,
});
```

## Keyboard focus

You can focus on the toast area by pressing ⌥/alt + T. You can override it by providing an array of event.code values for each key.
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const Toast = (props: ToastProps) => {
}, [toast, removeToast, setHeights, offset]);

React.useEffect(() => {
if (toast.promise && promiseStatus === 'loading') return;
if ((toast.promise && promiseStatus === 'loading') || toast.duration === Infinity) return;
let timeoutId: NodeJS.Timeout;

// Pause the timer on each hover
Expand Down
3 changes: 3 additions & 0 deletions test/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export default function Home() {
>
Render Custom Toast
</button>
<button data-testid="infinity-toast" className="button" onClick={() => toast('My Toast', { duration: Infinity })}>
Render Infinity Toast
</button>
<Toaster />
</>
);
Expand Down
8 changes: 8 additions & 0 deletions test/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ test.describe('Basic functionality', () => {
await timeout;
await expect(page.locator('[data-sonner-toast]')).toHaveCount(1);
});

test('toast is not removed if duration is set to infinity', async ({ page }) => {
await page.getByTestId('infinity-toast').click();
await page.hover('[data-sonner-toast]');
const timeout = new Promise((resolve) => setTimeout(resolve, 5000));
await timeout;
await expect(page.locator('[data-sonner-toast]')).toHaveCount(1);
});
});

0 comments on commit 4694553

Please sign in to comment.