Skip to content

fix(toast): cancel exit timer on show change or unmount to prevent stale onClose (closes #23) - #305

Open
Ranjeet2063 wants to merge 1 commit into
Movalabs-crew:mainfrom
Ranjeet2063:fix/toast-exit-timer-cleanup
Open

Ranjeet2063 wants to merge 1 commit into
Movalabs-crew:mainfrom
Ranjeet2063:fix/toast-exit-timer-cleanup

Conversation

@Ranjeet2063

Copy link
Copy Markdown
Contributor

Summary of Changes

Resolves issue #23 where the 300ms exit timer cleanup in components/Toast.jsx and components/Notification.tsx was returned from inside the outer setTimeout callback where it was never executed. When a new toast was shown within that 300ms window or the component was unmounted mid-exit, the stale onClose fired and hid the new toast prematurely.

Key Changes

  1. Restructured Timer Cleanup:
    • In components/Toast.jsx, tracked timer and exitTimer in the useEffect body.
    • Returned a proper cleanup function that clears both timer and exitTimer whenever show, onClose, or time changes or on unmount.
    • Applied the exact same corrected cleanup pattern to components/Notification.tsx (Toast component).
  2. Comprehensive Unit Tests:
    • Created tests/components/Toast.test.tsx using Vitest fake timers verifying for both components:
      1. Showing toast A, then re-showing toast B within 300ms cancels stale onClose for A without prematurely invoking onClose for B.
      2. Unmounting mid-exit transition cancels the exit timer and does not fire onClose.

Verification (2x Verified)

  • npx vitest run tests/components/Toast.test.tsx (4/4 tests pass)
  • npm run type-check (0 errors)
  • npm run lint (0 errors)
  • npx prettier --check (100% formatted)

Closes #23

Copilot AI lite review requested due to automatic review settings September 4, 2026 21:48
@vercel

vercel Bot commented Sep 4, 2026

Copy link
Copy Markdown

@Ranjeet2063 is attempting to deploy a commit to the Oluremi's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
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.

[Bounty: $100] Fix Toast exit-timer cleanup so stale onClose calls cannot hide a fresh toast

2 participants