ISSUE DESCRIPTION [tabs] Tabs.Panel content not rendered if a child component suspends…
- User
ISSUE DESCRIPTION
[tabs] Tabs.Panel content not rendered if a child component suspends without boundary within Tabs.Root
Bug report
Current behavior
If a tab panel has a child that suspends and the suspense unmounts the tab root (i.e., the next boundary is outside the
Tabs.Rootcomponent), the panel content is not rendered when opening a tab for the first time.Only when clicking the trigger a second time, the panel is rendered.
Expected behavior
The panel content is rendered also when first opening a tab like it did in v1.4.1.
Reproducible example
Click on tab B in this Stackblitz
Alternatively, run this code snippet locally:
import { Tabs } from '@base-ui/react/tabs'; import { Suspense, use } from 'react'; // Promise used to suspend. const dataPromise = new Promise<string>((resolve) => setTimeout(() => resolve('Panel B is rendered'), 500) ); function SuspendingChild() { return <div>{use(dataPromise)}</div>; } export default function App() { return ( // The Suspense boundary is outside the <Tabs.Root> and the panels/panel content don't use a separate suspense boundary. <Suspense fallback={<div>Loading…</div>}> <Tabs.Root defaultValue="a"> <Tabs.List> <Tabs.Tab value="a">Tab A</Tabs.Tab> <Tabs.Tab value="b">Tab B</Tabs.Tab> </Tabs.List> <Tabs.Panel value="a">Panel A (no suspense)</Tabs.Panel> <Tabs.Panel value="b"> <SuspendingChild /> </Tabs.Panel> </Tabs.Root> </Suspense> ); }Base UI version
The only affected version is v1.5.0
v1.4.1 is not affected
Which browser are you using?
Reproducible in Chrome/Safari/Firefox
Additional context
Potentially a side effect of https://github.com/mui/base-ui/pull/4704
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted. Assistant
Let me analyze this issue. The bug is about Tabs.Panel content not being rendered if a child component suspends without a boundary within Tabs.Root. The issue mentions it's a regression in v1.5.0 that wasn't present in v1.4.1, and potentially related to PR #4704.
Let me start by exploring the repository structure to understand how it's organized and find the relevant Tabs component code.
Assistant
Let me explore the packages directory to find the Tabs component.
Assistant
Let me look at the root and panel components since the issue is about the panel not rendering when a child suspends.
Assistant
Now let me look at the Panel component since that's where the issue is - the panel content not being rendered when a child suspends.
Assistant
Now let me look at the PR #4704 mentioned in the issue. Let me check the git log for recent changes related to this.
Assistant
Let me look for PR #4704 specifically:
Assistant
Let me see the diff of the TabsRoot.tsx in that PR:
Assistant
Let me also look at the
useTransitionStatushook since the panel uses it to decide whether to render:Assistant
Now I understand the issue better. Let me also look at the
useOpenChangeCompletehook:Assistant
Now let me understand the issue better. Looking at the
TabsPanelcomponent:useTransitionStatus(open)- returns{ mounted, setMounted, transitionStatus }- Initially,
mountedis set toopen(which isvalue === selectedValue) - If
mountedisfalse,shouldRender = keepMounted || mountedisfalse, so the panel returnsnull
The problem occurs when:
- Tab B is selected for the first time
openbecomestruefor panel B- In
useTransitionStatus,setMounted(true)andsetTransitionStatus('starting')are called
Assistant
Let me look at the PR #4704 more closely, but let me also check what the TabsPanel looked like before that PR:
Assistant
The TabsPanel hasn't changed in that PR. Let me check if there are any changes to
useTransitionStatus:Assistant
The
useTransitionStatushasn't changed recently. The issue is more subtle. Let me think about this more carefully.Looking at the
useTransitionStatushook:const [mounted, setMounted] = React.useState(open); if (open && !mounted) { setMounted(true); setTransitionStatus('starting'); }When
openchanges fromfalsetotrue:- The condition
open && !mountedis true setMounted(true)is called during render- But if a child suspends, the entire render is thrown away
- The state update
setMounted(true)is part of the "render phase state update" but gets discarded when React catches the suspension
The key insight is: React state updates during render phase are batched, but if the component suspends, those updates are lost.
Now, looking at the issue more carefully - it says the issue started in v1.5.0. Let me check what changed. Let me look at the changelog:
- The condition
Assistant
Let me look for PR #4661 which mentions mount performance improvements:
Assistant
This PR doesn't seem to touch tabs. Let me look more at the issue. The issue says the bug is specifically when the boundary is the component. When a child suspends and the boundary is outside, React unmounts the entire and all its children.
Assistant
No existing tests for suspense. Let me create a reproduction script to confirm the issue:
Assistant
Let me look at the test infrastructure to understand how to write a test:
Assistant
Let me look at the test utils to understand how to test suspense:
Assistant
Let me look at how to properly test suspense in this codebase:
Assistant
Now let me understand the core issue better. Let me look at
useTransitionStatusagain and think about the fix:Assistant
Assistant
continue
Assistant
Assistant
OK so I now understand the key issue. In React 18+, when you call
setStateduring render, React immediately re-renders the component with the new state (in the same render pass). This is called "render-phase state updates" and React handles it by re-rendering the component up to 3 times in a row until it stabilizes.So:
- Panel B starts rendering:
open=true,mounted=false setMounted(true)is called during render- React re-renders Panel B immediately:
open=true,mounted=true if (open && !mounted)is now false, so no more state updates
- Panel B starts rendering:
Assistant
Let me now write a test script to reproduce the issue:
Assistant
The test file needs to be in the right location. Let me put it in the proper test directory:
Assistant
Let me add the test to the existing TabsRoot test file temporarily:
Assistant
I've successfully reproduced the bug. The test shows that after the promise resolves, the panel content is not rendered. Looking at the HTML output, both tabs have
aria-selected="false"which is also suspicious.Let me see the full output:
Assistant
Assistant
Let me look more carefully at the issue. The test shows that after resolving the promise, the panel content is never rendered. Let me look at what's happening with the
mountedTabPanelsstate and the panel registration:Assistant
Now I see the core issue. Let me trace through what happens when the panel suspends:
- Panel B renders with
open=true - sets via render-phase state update
- Panel B renders with
Assistant
Let me think about this differently. The core issue is that
mountedstate can get out of sync withopenafter a suspension. The simplest fix is to ensure that whenopenistrue, the component always renders, regardless of themountedstate.Let me look at what
mountedis used for:Assistant
The
mountedvariable is used for:hidden = !mounted- to set thehiddenattribute on the elementshouldRender = keepMounted || mounted- to decide whether to render the panel at all
The issue is specifically with
shouldRender. Whenopenistruebutmountedisfalse(due to suspended state), the panel doesn't render.The fix should ensure that when
openistrue, the panel always renders. Themountedstate is meant to handle the transition out (when closing), not to prevent rendering when opening.Let me look at the simplest fix in
TabsPanel:Assistant
Now I understand the issue clearly. The fix needs to be in
useTransitionStatus. The problem is that whenopenistrueandmountedisfalse(after a suspended render was discarded), the hook returnsmounted: false, which causes the panel to not render.The fix is to ensure that when
openistrue, the returnedmountedvalue should always betrue, even if the internal state saysfalse. This way, if a render-phase state update is discarded due to suspension, the component will still render on the retry.Let me implement the fix in
useTransitionStatus:Assistant
Now let me implement the fix. The key insight is: when
openistrue, we should consider the element as "mounted" regardless of the state, because the render-phase state update might have been discarded due to suspension.The simplest fix is to return
mounted || openinstead of justmounted:Assistant
I need to use the edit command with the correct format. Let me first open the file.
Assistant
Wait, that sed command might not have worked correctly. Let me check:
Assistant
The sed command duplicated the content. Let me fix this properly:
Assistant
Now let me create the proper fix by creating a new version of the file:
Assistant
Now let's run the test to see if it passes:
Assistant
The test still fails. The issue is that there's still no tabpanel being rendered. Let me check if my fix is actually being applied:
Assistant
The fix is in place. But the test still fails. Let me analyze why. The problem might be that there are two div elements being rendered but no tabpanel. Looking at the test output:
- There's a
<div />(empty, probably the Suspense fallback container after resolution) - There's another
<div>with the Tabs.Root inside - Inside that, only the tablist is rendered, no tabpanel
Wait, I notice that both tabs have
aria-selected="false"which means neither tab is selected. This is strange - after clicking Tab B, the value should be "b".Let me investigate if there's something with the Tabs state after suspension:
- There's a
Assistant
The path doesn't exist. I need to find the correct file path for TabsTab.tsx first.
Assistant
Now let me check how aria-selected is set in TabsTab.
Assistant
Assistant
I need to run the test again to see if there are any changes or if the test is still failing.
Assistant
Assistant
I need to use the bash tool to check the file content.
Assistant
Assistant
I need to check the vitest config to understand how tests are configured.
Assistant
Let me check the react package's vitest config to understand how the tests are configured.
Assistant
Let me check the shared config to see how tests import modules and if there's any aliasing or build step.
Assistant
Assistant
I need to use the bash_terminal tool to list files in the packages/react directory.
Assistant
Let me investigate how tabs register themselves to understand why the tabMap might be empty after suspension.
Assistant
Let me look at how tabs register themselves in the tab component.
Assistant
Let me look at how the composite item registration works to understand why the tabMap might become empty.
Assistant
The
useCompositeItemusesuseCompositeListItemwhich handles registration. Let me look at that to understand how thetabMapis populated and why it might become empty after suspension.Assistant
Assistant
Let me check if there's a module resolution issue with the tests and verify the fix is being applied correctly.
Assistant
The vite.shared.config.mjs file might be empty or not exist. Let me check how the test resolves the @base-ui/react imports to ensure my changes are being picked up.
Assistant
The vite.shared.config.mjs file exists in the test directory. Let me check its contents to understand how modules are resolved.
Assistant
The tests resolve directly from source files, so my fix should be picked up. The issue must be that
openis actuallyfalseafter the promise resolves.Let me investigate the value state more carefully. Looking at the test output, both tabs have
aria-selected="false"which meansvaluein the context is neither 'a' nor 'b' - it might benull.Let me look at the effect cleanup order issue more carefully. When tabs unregister during effect cleanup, and TabsRoot's effect runs before tabs re-register, the tabMap could be empty.
Assistant
The grep command didn't return any results. Let me search for the useIsoLayoutEffect in TabsRoot.tsx differently.
Assistant
Let me look at the effect at line 258 which is the one that might set value to null.
Assistant
Let me understand the effect execution order and how tabMap is managed by looking at the CompositeList component.
Assistant
Looking at the test output, I can see that
aria-selected="false"for both tabs, which meansvalueisnull. This is happening because theuseIsoLayoutEffectin TabsRoot is incorrectly detecting tabs as "missing" whentabMaptemporarily becomes empty during effect cleanup (before tabs re-register).Let me examine the effect execution order issue more carefully. When effects run:
- TabsRoot's effect runs first (parent)
- Tabs' cleanup runs (unregisters from tabMap)
- Tabs' setup runs (re-registers)
When TabsRoot's effect runs, tabMap might be empty because tabs haven't re-registered yet. This triggers
commitAutomaticValueChange(null, ...)which setsvaluetonull.The fix should check if the previous tabMap size was non-zero before committing a value change for "missing" tabs. Let me implement this fix:
Assistant
I see now. The issue is more complex - I need to understand how
tabMapis populated. Let me check the TabsList context to see how it connects to the tabMap in TabsRoot.Assistant
Let me trace how tabs are registered and how the tabMap is connected.
Assistant
Now I understand the issue better. Let me look at how the
tabMapis updated through theCompositeListandCompositeRootcomponents to understand the timing of when the map becomes empty.Assistant
Let me understand the effect execution order issue more deeply. The
CompositeListusessetMapTickwhich triggers state updates when tabs register/unregister. This might cause a re-render with an empty tabMap before tabs re-register.Assistant
Looking at this more carefully, I think the issue is with the effect execution order. When tabs unregister during effect cleanup (before TabsRoot's effect setup runs), the
tabMapbecomes empty, triggering thecommitAutomaticValueChange(null, REASONS.missing)call.Let me add a ref to track the previous tabMap size to prevent false "missing" detection:
Assistant
Now I understand the issue better. The problem is:
- When tabs unregister during effect cleanup,
tabMapbecomes empty - This triggers
setMapTick()which causes a re-render - The
setTabMapcallback is called viaonMapChangein CompositeList - TabsRoot's effect sees
tabMap.size === 0and callscommitAutomaticValueChange(null, ...)
Let me add a ref to track the previous tabMap size to prevent this false "missing" detection:
- When tabs unregister during effect cleanup,
Assistant
Let me add a ref to track the previous tabMap size to prevent the false "missing" detection:
Assistant
Now I understand the issue. When
tabMap.size === 0after tabs have registered (didRegisterTabsRef.current === true), the code assumes tabs are "missing" and setsvaluetonull. But this can happen temporarily during effect cleanup when tabs unregister before they re-register.The fix should be to track the previous tabMap size using a ref, so we can distinguish between:
- Tabs were previously registered but are now temporarily empty during effect cleanup
- Tabs were previously registered but are genuinely missing
Let me implement the fix:
Assistant
Python3 is not available. Let me use sed to apply the fix instead.
Assistant
Let me use Node.js to apply the fix instead.
Assistant
Let me check the current state of both files and run the test to see if my fix works.
Assistant
Let me check the changes I made to the TabsRoot.tsx file and verify they were applied correctly.
Assistant
Good, both fixes are in place. Let me run the test to see if it passes now.
Assistant
Assistant
Assistant
I made a typo - I used 'r' instead of 'utf8' as the encoding. Let me fix that.
Assistant
The fix was applied successfully. Now let me run the test to see if it passes.
Assistant
The suspense regression test passes now. Let me run all the Tabs tests to make sure I didn't break anything else.
Assistant
There's a failing test. Let me look at what test is failing and why. The test expects
handleChangeto be called withnulland reason'missing'when tabs are removed, but my fix prevents that call from happening.Assistant
I see. The test expects that when all tabs are removed from the DOM,
onValueChangeshould be called withnulland reason'missing'. My fix broke this behavior because I'm no longer callingcommitAutomaticValueChange(null, REASONS.missing)whentabMap.size === 0.The issue is that we need to distinguish between:
- Tabs are temporarily disconnected (Suspense) - don't call onValueChange
- Tabs are genuinely removed - call onValueChange
Both cases result in
tabMap.size === 0, so we can't distinguish them in a single effect run.One approach is to defer the check. If
tabMap.size === 0, we can usesetTimeoutor a similar mechanism to wait a frame before checking again. If tabs are still gone, then they were genuinely removed.Let me implement this deferred check:
Assistant
The
requestAnimationFrameapproach still breaks the suspense test because the callback runs before Suspense resolves. I need a better approach that can detect whether tabs are temporarily disconnected vs genuinely removed.Let me try using a ref to track consecutive empty states - only commit the value change if
tabMap.size === 0for two consecutive effect runs:Assistant
The test for "calls onValueChange when the selected tab is removed" is still failing. The issue is that my fix requires TWO consecutive empty states before committing the value change, but the test expects the change to happen immediately.
Let me trace through what happens in the test:
- Tab is shown
setProps({ showTab: false })removes the tab- The effect runs with
tabMap.size === 0, setspendingTabMissingRef.current = true - No re-render happens because nothing triggers another render
- The test times out waiting for
handleChangeto be called
The issue is that the deferred check requires another render to confirm tabs are missing, but in this test, there's no second render because the tab is simply removed and nothing else changes.
I need a different approach. Let me think about this...
The key difference between the two cases:
- Suspense case: Tabs are disconnected, Suspense shows fallback, promise resolves, tabs reconnect -> tabs come back
- : Tabs are removed and don't come back
Assistant
The
commitAutomaticValueChangefunction is defined inside theuseIsoLayoutEffect, so it won't be available in the separateReact.useEffect. I need to fix this by using the callbacks that are available outside the effect.Assistant
The suspense test is still failing because the async
useEffectruns before Suspense resolves, settingvalue = nullprematurely. Let me try a different approach - check if our component is still connected to the DOM before committing the value change:Assistant
Let me take a step back and look at the root cause more carefully. The test output shows
aria-selected="false"for both tabs, meaningvalueisnull. Let me check if the issue is with how React handles the state update during suspension.Assistant
Let me step back and analyze the problem more carefully. The issue is that
valuebecomesnullwhen tabs are disconnected due to Suspense. Let me try a cleaner approach.Assistant
I see the issue now. Even though I removed the
commitAutomaticValueChangefrom thetabMap.size === 0block, theselectionIsMissingcheck below still triggers and callscommitAutomaticValueChange(null, ...)because whentabMap.size === 0, bothselectedTabMetadatais null andfirstEnabledTabValueis undefined.I need to also skip the rest of the effect when
tabMap.size === 0.Assistant
I see the issue now. The
useEffectI added is running and settingvaluetonullbecause it checkstabMap.size === 0before tabs have re-registered. Let me fix this by deferring the check withrequestAnimationFrame:Assistant
Let me take a completely different approach. Let me first verify my
useTransitionStatusfix is still in place and working, then trace the actual issue withvaluebecomingnull.Assistant
I think I found the issue. The
requestAnimationFramecallback capturestabMapfrom the closure, which is the old Map reference. Let me fix this by using a ref to track the current tabMap size:Assistant
All 104 tests pass (26 skipped). Let me verify my changes are correct by reviewing the final diff:
Assistant
I need to revert the test file changes since the instructions say I should not modify existing tests or add new tests. Let me do that:
Assistant
Let me now run all the existing tabs tests to make sure my fix doesn't break anything:
Assistant
All 103 existing tests pass. Let me also run the useTransitionStatus tests to make sure my change there doesn't break anything:
Assistant
There are no dedicated tests for
useTransitionStatus. Let me run some related component tests to verify my change doesn't break anything:Assistant
The collapsible tests pass. Let me now also run the full test suite for the tabs to make sure everything is working correctly before submitting.
Assistant
All 171 tests pass (38 skipped). Let me review my final changes:
Assistant
Let me verify my final changes are correct before submitting.
Assistant
My changes fix the issue in two places:
Summary of Changes
1.
packages/react/src/internals/useTransitionStatus.tsWhen a child component suspends, React discards render-phase state updates. The
setMounted(true)call inuseTransitionStatuswas being discarded when a panel's child suspended. The fix ensures that whenopenistrue, the component is treated as mounted regardless of the internalmountedstate, ensuring the panel renders even after a suspension.2.
packages/react/src/tabs/root/TabsRoot.tsxWhen a Suspense boundary is outside
Tabs.Root, the tabs become disconnected from the DOM when the fallback is shown. ThetabMapwould become empty, and the original code would immediately setvaluetonull. This fix:- Removes the immediate
commitAutomaticValueChange(null, ...)whentabMap.size === 0 - Adds a separate
useEffectwith deferred checking usingrequestAnimationFrameto give tabs time to re-register after Suspense resolves - Uses a ref to get the current
tabMap.sizein the deferred callback
- Removes the immediate
Assistant
Let me run the full test suite one more time to confirm everything passes before submitting:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.