Skip to content

Fix/toast timeout cleanup#3794

Open
j2h30728 wants to merge 3 commits intoradix-ui:mainfrom
j2h30728:fix/toast-timeout-cleanup
Open

Fix/toast timeout cleanup#3794
j2h30728 wants to merge 3 commits intoradix-ui:mainfrom
j2h30728:fix/toast-timeout-cleanup

Conversation

@j2h30728
Copy link

Description

Fixes #3703

Issue Summary

The Toast component's internal close timer (closeTimerRef) is not cleared when the component unmounts. This causes ReferenceError: document is not defined errors in test environments.

Reproduction scenario:

  1. Toast opens → Timer starts (e.g., auto-close after 5 seconds)
  2. Test ends / Component unmounts after 3 seconds
  3. Timer callback executes after remaining 2 seconds
  4. handleClose tries to access document.activeElement
  5. Error occurs because document doesn't exist in test environment

Solution

Added a cleanup effect in ToastImpl to clear closeTimerRef on unmount:

React.useEffect(() => {
  return () => {
    window.clearTimeout(closeTimerRef.current);
  };
}, []);

This follows the same pattern used in other Radix components:

Changes

File Change
packages/react/toast/src/toast.tsx Added cleanup effect to clear timer on unmount (+7 lines)
packages/react/toast/src/toast.test.tsx Added tests for timer cleanup behavior (new file)

@changeset-bot
Copy link

changeset-bot bot commented Jan 18, 2026

🦋 Changeset detected

Latest commit: 4ca1268

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

@j2h30728 j2h30728 force-pushed the fix/toast-timeout-cleanup branch from 52180cb to e051b29 Compare January 18, 2026 09:38
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 handleClose timeout not cleared on unmount

1 participant