Skip to content

fix(toast): reset remaining time ref when duration changes after pause / resume#3926

Open
LongTangGithub wants to merge 2 commits into
radix-ui:mainfrom
LongTangGithub:fix/toast-timer-stale-remaining-time
Open

fix(toast): reset remaining time ref when duration changes after pause / resume#3926
LongTangGithub wants to merge 2 commits into
radix-ui:mainfrom
LongTangGithub:fix/toast-timer-stale-remaining-time

Conversation

@LongTangGithub

Copy link
Copy Markdown

Closes #2233

Description

closeTimerRemainingTimeRef is initialized once at mount with the duration value and never updated when the prop changes on re-renders.

This means a toast that was paused while duration=Infinity will hold Infinity in the ref permanently. So when duration later changes to a finite value, any subsequent pause/resume cycle calls startTimer(Infinity) and the toast never auto-closes.

Root cause:

// Initialized once at mount, never synced to prop changes
const closeTimerRemainingTimeRef = React.useRef(duration);

On resume, the timer is restarted using this ref:

startTimer(closeTimerRemainingTimeRef.current); // → startTimer(Infinity)

Fix:

Added a useEffect that syncs closeTimerRemainingTimeRef whenever duration changes:

  • Not paused: reset to the new duration (the open/duration effect handles restarting the actual timer)
  • Paused: clamp to the new duration if it's smaller, so resume uses the correct remaining time

Manual Testing

A new Storybook story DurationChangeAfterPause has been added to reproduce this scenario.

  1. Run pnpm dev and open Components/Toast → DurationChangeAfterPause
  2. Click Start — toast opens with duration=Infinity (loading state)
  3. Hover over the toast to pause it, then move away to resume
  4. Wait 3 seconds — toast transitions to "Done! Will close in 2s"
  5. Hover and un-hover again

Before fix:

  • Toast never closes — timer is stuck at Infinity

After fix:

  • Toast closes ~2 seconds after un-hovering, with a progress bar reflecting the remaining time correctly. The ⏸ PAUSED indicator also confirms pause/resume is working as expected.

Video Demo

2233_fix.mp4

@changeset-bot

changeset-bot Bot commented Jun 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8e2ce0a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@radix-ui/react-toast Patch
radix-ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Toast doesn't dismiss because of pause/resume issue

1 participant