Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inability to update toast duration dynamically #529 #533

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ const Toast = (props: ToastProps) => {

offset.current = React.useMemo(() => heightIndex * gap + toastsHeightBefore, [heightIndex, toastsHeightBefore]);

React.useEffect(() => {
remainingTime.current = duration;
}, [duration]);

React.useEffect(() => {
// Trigger enter animation without using CSS animation
setMounted(true);
Expand Down
17 changes: 17 additions & 0 deletions test/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ export default function Home({ searchParams }: any) {
>
Updated Toast
</button>
<button
data-testid="update-toast-duration"
className="button"
onClick={() => {
const toastId = toast('My Unupdated Toast, Updated After 3 Seconds', {
duration: 10000,
});
setTimeout(() => {
toast('My Updated Toast, Close After 1 Second', {
id: toastId,
duration: 1000,
});
}, 3000);
}}
>
Updated Toast Duration
</button>
<button
data-testid="string-description"
className="button"
Expand Down
16 changes: 16 additions & 0 deletions test/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ test.describe('Basic functionality', () => {
await expect(page.getByText('My Updated Toast')).toHaveCount(1);
});

test('should update toast content and duration after 3 seconds', async ({ page }) => {
await page.getByTestId('update-toast-duration').click();

const initialToast = page.getByText('My Unupdated Toast, Updated After 3 Seconds');
await expect(initialToast).toBeVisible();

await page.waitForTimeout(3000);
const updatedToast = page.getByText('My Updated Toast, Close After 1 Second');
await expect(updatedToast).toBeVisible();

await expect(initialToast).not.toBeVisible();

await page.waitForTimeout(1200);
await expect(updatedToast).not.toBeVisible();
});

test('cancel button is rendered with custom styles', async ({ page }) => {
await page.getByTestId('custom-cancel-button-toast').click();
const button = await page.locator('[data-cancel]');
Expand Down