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) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /base-ui, current file: none) bash-$
Assistant
Let me analyze this issue. The bug is about Tabs.Panel content not being rendered when a child component suspends without a boundary within Tabs.Root. The issue mentions this is a regression in v1.5.0, potentially caused by PR #4704.
Let me start by exploring the repository structure to understand the codebase and find the relevant files.
Assistant
Let me look at the TabsRoot.tsx file first to understand how the tabs state is managed.
Assistant
The open command seems to have failed. Let me try a different approach.
Assistant
Now let me look at the TabsPanel.tsx file to understand how the panel content is rendered:
Assistant
Let me look at the
useTransitionStatushook to understand how the mounted state is tracked:Assistant
Now let me look at the PR #4704 that's mentioned as the potential cause of the issue. Let me check the git log:
Assistant
Let me check the specific PR #4704 that's mentioned as potentially causing the issue:
Assistant
Let me see the actual diff for the TabsRoot.tsx in that PR:
Assistant
Now let me understand the issue better. The issue is about Suspense - when a child of a Tab panel suspends and the Suspense boundary is outside the Tabs.Root, the panel content doesn't render on the first tab activation. Let me think about what's happening:
- User clicks on tab B
- Tab B's panel tries to render
- A child component suspends
- React unwinds the render and the Tabs.Root gets unmounted temporarily
- When React retries, the panel content doesn't render
The key part is in
TabsPanel.tsx. The issue is likely related to theuseTransitionStatushook wheremountedstate is managed. When the component suspends and the whole Tabs.Root unmounts, themountedstate is lost.Let me also look at how the
mountedstate interacts with the panel registration:Assistant
Let me look more carefully at the TabsPanel code. The issue is that when a Suspense boundary causes a re-render, the
mountedstate inuseTransitionStatusmight not be set properly. Let me check the exact flow:Assistant
Let me see the rest of the file, specifically the registration effect:
Assistant
Now I understand the issue better. The problem is in the
useIsoLayoutEffectthat registers/unregisters the tab panel:useIsoLayoutEffect(() => { if (hidden && !keepMounted) { return undefined; } // ... registerMountedTabPanel(value, id); return () => { unregisterMountedTabPanel(value, id); }; }, [hidden, keepMounted, value, id, registerMountedTabPanel, unregisterMountedTabPanel]);When a panel suspends:
- User clicks on tab B
openbecomes true for panel BuseTransitionStatussetsmountedto true (in render phase)- The panel renders, but its child suspends
- React catches the suspense and re-renders from a boundary outside Tabs.Root
- During the re-render, the
selectedValueis still "b", but since the component tree is being thrown away,mountedstate is lost - On the retry render,
mountedstarts asfalseagain (initial state is based onopen) - But wait -
useTransitionStatussetsmountedduring render whenopen && !mounted
Let me look at
useTransitionStatusmore closely:Assistant
Now let me look at the
useIsoLayoutEffectthat's used - is it a client-only effect that doesn't run during server rendering?Assistant
command
Assistant
Now let me trace through what happens when a child suspends:
- Tab B is clicked,
selectedValuechanges to "b" - Panel B's
openbecomestrue - In
useTransitionStatus, sinceopenis true andmountedis initially false, it callssetMounted(true)synchronously during render - Panel renders, but
SuspendingChildthrows a promise - React suspends and the nearest boundary (outside Tabs.Root) catches it
- The entire Tabs.Root tree is thrown away (unmounted) during the fallback render
- When the promise resolves, React retries rendering
- But now Tabs.Root remounts fresh -
selectedValueis still "b" but all state is fresh - Panel B re-renders with
open = true, anduseTransitionStatusshould setmounted = true
Wait, actually the issue might be more subtle. Let me re-read the
useTransitionStatushook:const [mounted, setMounted] = React.useState(open);If
openistrueinitially,mountedstarts astrue. But when the component suspends and the tree is thrown away, the state should be reset when it remounts.Actually, I think I need to look at how
openis computed. When the component first mounts after a suspense recovery,selectedValueshould still be "b" if the user had selected tab B. But the issue says the panel content isn't rendered.Let me look at the registration logic more carefully:
useIsoLayoutEffect(() => { if (hidden && !keepMounted) { return undefined; // <-- early return when hidden } // ... registerMountedTabPanel(value, id); // ... }, [hidden, keepMounted, value, id, registerMountedTabPanel, unregisterMountedTabPanel]);The
hiddenvariable is!mounted. So ifmountedisfalse,hiddenistrue, and the registration effect returns early without registering the panel.But wait, let me re-check. The
shouldRendercheck is:const shouldRender = keepMounted || mounted; if (!shouldRender) { return null; }If
keepMountedisfalse(default) andmountedisfalse, the panel returnsnullearly and doesn't render at all!The problem might be that when a suspense happens and then recovers:
- After suspense recovery, the panel re-renders
useTransitionStatusstarts freshuseState(open)- ifopenis still true, mounted should be true
Hmm, but if the value "b" was set by clicking, it should persist. Let me think about this differently.
Let me check what happens when
selectedValuechanges. The issue says clicking tab B for the first time doesn't show the panel content, but clicking a second time does. This suggests that on the first click:selectedValuechanges to "b"- Panel B re-renders with
open = true useTransitionStatusseesopen = true, setsmounted = true- Panel renders but suspends
- Suspense boundary catches and shows fallback
- The whole Tabs.Root is unmounted during fallback
When the promise resolves and React retries: 7. Tabs.Root remounts 8. Panel B re-renders
But wait - since Tabs.Root is uncontrolled, its state is lost! When it remounts,
valuegoes back todefaultValue="a". SoselectedValueis now "a" again!That's the bug! When the Suspense boundary unmounts the Tabs.Root, the selected value reverts to the default value, and the panel that was being viewed is no longer selected.
Let me verify this theory by checking how the TabsRoot manages its value:
- Tab B is clicked,
Assistant
Yes, I can confirm my theory. The TabsRoot uses
useControlledfor its value state. When the Suspense boundary causes the Tabs.Root to unmount and then remount, the state is lost, andvaluereverts todefaultValue.Now let me look at the test file to see if there are existing tests for this behavior:
Assistant
No existing tests for Suspense. Let me now think about the actual fix. The issue is that when a panel's child suspends and the suspense boundary is outside the Tabs.Root, the Tabs.Root is unmounted and its state (the selected value) is lost.
Looking back at the issue description:
The only affected version is v1.5.0 v1.4.1 is not affected
This suggests the issue was introduced in a recent change. Let me check what changed between v1.4.1 and v1.5.0 related to tabs:
Assistant
The issue mentions PR #4704 as the potential cause. But looking at the git history, there's also a more recent commit
[tabs] Fix state edge cases (#4935)that might be relevant. Let me check what that fix does:Assistant
This commit doesn't seem to be directly related to the suspense issue. Let me now look at what changed in PR #4704 that could have introduced this regression. Looking at the diff more carefully, I think the key change is in the automatic value change logic. But let me first create a test script to reproduce the issue:
Assistant
Let me look at the test setup to understand how to run tests:
Assistant
Let me understand the current behavior more deeply. The issue says the panel isn't rendered when first opening a tab after a suspense. Let me trace through the code flow again.
Looking at the issue more carefully - when a child suspends and the suspense boundary unmounts the tab root, the problem is that the panel's
mountedstate gets lost. Let me check if there's something specific about how theuseTransitionStatusinitial state works:Assistant
I think I now understand the issue better. Let me re-trace what happens:
Tabs.RootwithdefaultValue="a"renders initially- User clicks tab B,
valuechanges to "b" - Panel B's
openbecomestrue,useTransitionStatussetsmounted = trueduring render - Panel B renders, but child suspends
- Suspense boundary catches and shows fallback - Tabs.Root is completely unmounted
- Promise resolves, React retries
- Tabs.Root remounts with
defaultValue="a", sovalueis "a" again - Panel B has
open = false(since value is "a"), somountedstarts asfalse - Panel B returns
nullsince!keepMounted && !mounted
Wait, that would mean after suspense recovery, tab A would be selected again, not tab B. But the issue says "Only when clicking the trigger a second time, the panel is rendered." This suggests that the tab B trigger still shows as selected, but the panel content is not shown.
Let me re-read the issue more carefully. The issue says:
the next boundary is outside the
Tabs.RootcomponentSo the suspense boundary unmounts the Tabs.Root. After recovery:
- If uncontrolled: value goes back to default
- But the user sees they clicked tab B and it should still be selected
Actually, wait. Let me re-read the Stackblitz example. The
defaultValueis "a". When clicking tab B:- Value changes to "b"
- Panel B suspends
- Entire tree is thrown away (including the value state)
- Fallback shows
- After recovery, Tabs.Root remounts with value = "a" (default)
But wait, that would mean tab A is selected again after recovery. The issue says clicking a second time works, which means after recovery, the user would need to click tab B again to see its content.
Let me check if there's something else going on. Maybe the issue is about a different scenario - where the suspense boundary catches the error but the Tabs.Root state is somehow preserved, but the panel's
mountedstate is reset.Let me look at the pnpm installed version to understand what version we're working with:
Assistant
Now let me think about this more carefully. Let me re-read the bug report:
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.So the flow is:
- Initial render: Tab A is selected, Panel A shows (no suspense)
- User clicks Tab B
- Panel B's child suspends
- Suspense boundary outside Tabs.Root catches it
- After recovery: Tab B should still be selected (value is "b"), but Panel B's content doesn't render
- User clicks Tab B again
- Now Panel B renders
This suggests that after recovery, the
valuestate in TabsRoot is preserved (value = "b"), but something about the panel'smountedstate is wrong.But if the Tabs.Root was completely unmounted, how could the value be preserved? Unless... the suspense doesn't actually unmount the Tabs.Root, but rather React throws the render and retries without fully unmounting?
Let me think about this differently. When React encounters a suspended component:
- React renders Tabs.Root with value "b"
- Panel B tries to render but suspends
- React catches the suspense at the boundary outside Tabs.Root
- React shows the fallback
- During this time, the entire tree including Tabs.Root is discarded
- When the promise resolves, React starts fresh from the Suspense boundary
- Tabs.Root remounts
But the key insight is: when Tabs.Root remounts, what is its value?
If
defaultValue="a"is used (uncontrolled), thenvaluewill be "a" on remount. But the issue says clicking a second time works, which implies the tab trigger for B is still visually selected.Actually, I think I might be overcomplicating this. Let me create a test script to reproduce the issue first:
Assistant
The test is taking too long. Let me try running it with a timeout and focus on a specific test:
Assistant
`
Assistant
I need to run a command to check the pnpm version and proceed with investigating the issue.
Assistant
Let me look at the version history to find what changed between v1.4.1 and v1.5.0:
Assistant
No tags found. Let me investigate the code more directly to understand the suspense issue.
Assistant
Let me explore the
useBaseUiIdhook anduseOpenChangeCompleteto understand the full picture:Assistant
Let me now think more deeply about the issue. The problem is:
- When a tab panel's child suspends, and the Suspense boundary is outside Tabs.Root
- 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
The key insight is that when React suspends and the boundary is outside Tabs.Root, the entire Tabs.Root tree is thrown away. When React retries, Tabs.Root remounts with fresh state.
But wait - the issue says "when opening a tab for the first time" which suggests the user hasn't yet selected that tab. So the scenario is:
- Tabs.Root mounts with defaultValue="a" - Panel A is shown
- User clicks tab B
- Panel B's
openbecomes true,useTransitionStatussetsmounted = trueduring render - Panel B renders its children, but SuspendingChild suspends
- React catches at Suspense boundary (outside Tabs.Root)
- The entire Tabs.Root tree is thrown away and fallback is shown
- Promise resolves, React retries
- Tabs.Root remounts with defaultValue="a" (since it's uncontrolled)
- Now Panel B has
open = false(since value is "a") - Panel B's
useTransitionStatusinitializes withmounted = false - Panel B returns
nullbecause!keepMounted && !mounted
But the issue says clicking a second time works. After step 8-10, if the user clicks tab B again: 12. value changes to "b" 13. Panel B's
openbecomes true 14. Panel B renders... but wait, should it suspend again?Actually, no! The promise has already resolved, so the SuspendingChild won't suspend this time. Panel B renders successfully.
So the real issue is: after the first suspense recovery, Tabs.Root's value reverts to the default value "a", so tab B is no longer selected. The user has to click tab B again.
But this seems like expected behavior - if the component unmounts, its state is lost. The issue says this worked in v1.4.1 but not in v1.5.0.
Hmm, let me re-read the issue more carefully:
The panel content is not rendered also when first opening a tab like it did in v1.4.1.
This suggests that in v1.4.1, after the suspense recovered, the panel content WAS rendered. So there must be something different about how the panel rendering works.
Wait, I think I need to look at this more carefully. The key line in TabsPanel.tsx is:
const shouldRender = keepMounted || mounted; if (!shouldRender) { return null; }And
mountedcomes fromuseTransitionStatus(open). InuseTransitionStatus:const [mounted, setMounted] = React.useState(open); if (open && !mounted) { setMounted(true); setTransitionStatus('starting'); }So when
openis true,mountedshould be set to true synchronously during render. This means if Panel B hasopen = true, it should render.But after the suspense recovery, if Tabs.Root reverts to value "a", then Panel B has
open = falseandmounted = false, so it returnsnull.The issue description says the user clicks tab B and the panel doesn't render. But if after recovery, value is "a", then tab B wouldn't be selected, so the panel wouldn't be expected to render.
Unless... the issue is that the value DOES persist somehow, but the panel's
mountedstate doesn't.Let me think about this differently. What if the Suspense boundary doesn't completely unmount Tabs.Root but only throws the render?
Actually, React's suspense model works like this:
- During render, if a component suspends (throws a promise), React catches it
- React "rolls back" the render for the subtree under the suspended component
- The Suspense boundary shows its fallback
- The promise resolves
- React retries the render
The key question is: does React unmount the entire subtree from the Suspense boundary down, or does it just rollback the render?
For a Suspense boundary that's outside Tabs.Root, when Panel B suspends:
- React tries to render the entire tree
- Panel B suspends
- React catches at the Suspense boundary
- React discards the work for the entire subtree (including Tabs.Root)
- Fallback is shown
- Promise resolves
- React retries from the Suspense boundary
When React retries, it starts fresh. The Tabs.Root remounts, so all its state is reset.
But wait, what if the Tabs.Root is actually kept mounted and only the Panel B is the one that gets retried? That depends on the structure...
Looking at the example code:
<Suspense fallback={<div>Loading…</div>}> <Tabs.Root defaultValue="a"> ... <Tabs.Panel value="b"> <SuspendingChild /> </Tabs.Panel> </Tabs.Root> </Suspense>When Panel B suspends, React will:
- Show the fallback
- Discard the entire Tabs.Root tree
- When promise resolves, retry from the Suspense boundary
So Tabs.Root will remount with fresh state, including
value = "a"(the defaultValue).But the issue says the problem is that "panel content is not rendered when opening a tab for the first time" and "only when clicking the trigger a second time, the panel is rendered." This suggests that after recovery, tab B IS selected (value is "b") but the panel doesn't render.
Hmm, let me reconsider. Maybe the issue is about the
useIdhook. Let me check ifuseIduses state:<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/utils/src/useId.ts 2>/dev/null </tool_call>
Assistant
Let me use the correct tool call syntax.
Assistant
The tool call succeeded. Let me continue investigating the issue.
Assistant
Let me check the git history for TabsPanel.tsx to see what changes might have affected this behavior:
Assistant
Let me look at the PR #4704 that's mentioned in the issue as potentially causing the regression. Let me also check the TabsPanel changes that might have introduced this bug. Let me look at the commit [tabs] Fix Panel
keepMountedbehavior (#3372) which might be relevant.Assistant
This commit looks relevant. Let me see the TabsPanel changes in detail:
Assistant
This is very interesting! Let me look at the diff more carefully:
Before (v1.4.1 or earlier):
const hidden = tabPanelValue !== selectedValue; // ... { children: hidden && !keepMounted ? undefined : children },After (the commit that changed behavior):
const hidden = value !== selectedValue; // ... useIsoLayoutEffect(() => { if (hidden && !keepMounted) { return undefined; } // register the panel }); const shouldRender = !hidden || keepMounted; if (!shouldRender) { return null; }So in the old version, the panel was always rendered but with
childrenset toundefinedwhen hidden. This means the panel element itself was always in the DOM.In the new version, the panel returns
nullwhen hidden, so it's not rendered at all. This is the key difference!But wait, this doesn't directly explain the suspense issue. Let me think about this more...
Actually, I think the issue is that in the new version:
- When Panel B opens and suspends, React catches at the Suspense boundary
- The fallback is shown and the entire Tabs.Root tree is discarded
- When React retries, Tabs.Root remounts with fresh state
- Panel B's
openis still true (because the value is still "b" in the parent's click handler)... wait, no. If Tabs.Root is uncontrolled, its state is lost.
Hmm, but the issue says the user needs to click tab B again to see the content. This implies that after recovery, tab B is NOT selected. So clicking tab B again selects it and the content renders (because the promise has resolved).
So this might actually be expected behavior - when a component unmounts and remounts, its state is lost.
But the issue says "like it did in v1.4.1". Let me think about why it worked in v1.4.1...
Wait! I see it now! In the old version:
{ children: hidden && !keepMounted ? undefined : children }The panel element was always rendered (even when hidden), just with
undefinedchildren. This means the panel was always registered in the TabsRoot context!In the new version:
const shouldRender = !hidden || keepMounted; if (!shouldRender) { return null; }When the panel is hidden, it returns
nulland doesn't register itself with the TabsRoot.But this still doesn't fully explain the suspense issue...
Actually, let me look at the current code again. The current code uses
useTransitionStatuswhich hasmountedstate:const { mounted, transitionStatus, setMounted } = useTransitionStatus(open); const hidden = !mounted;So
hiddenis based onmounted, not onopen. The panel uses a transition animation.And:
const shouldRender = keepMounted || mounted; if (!shouldRender) { return null; }So the panel renders when
keepMountedis true ORmountedis true.In
useTransitionStatus:const [mounted, setMounted] = React.useState(open); if (open && !mounted) { setMounted(true); setTransitionStatus('starting'); }So when
openis true,mountedis set to true synchronously during render.Now, when Panel B suspends:
- User clicks tab B
- Panel B's
openbecomes true useTransitionStatussetsmounted = trueduring rendershouldRenderis true- Panel renders but suspends in its child
- React catches at Suspense boundary
- Fallback is shown
- After promise resolves, React retries
When React retries, Tabs.Root has been completely discarded and remounts. The key question is: what is the value of Tabs.Root after remount?
If it's uncontrolled with
defaultValue="a", thenvalueis "a" again. So Panel B'sopenis false,mountedis false, and it returns null.But the issue says the problem is that the panel "is not rendered when opening a tab for the first time" and works "when clicking the trigger a second time". This sounds like after recovery, tab B IS selected (otherwise clicking a second time wouldn't be needed - the user would need to click tab B from tab A).
So maybe the value IS preserved? Let me think about this differently...
Actually, wait. I think I've been misunderstanding the scenario. Let me re-read the issue:
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."when opening a tab for the first time" - this is the initial render, not after clicking!
So the scenario might be:
- Tabs.Root mounts with defaultValue="a"
- Panel A renders normally
- But what if Panel A also suspends? Or what if the user navigates to a different page and then comes back?
Actually, I think the scenario is:
- Tabs.Root mounts with defaultValue="a"
- User clicks tab B
- Panel B's open becomes true, mounted becomes true
- Panel B renders but suspends
- Fallback is shown
- After promise resolves, React retries
- Tabs.Root remounts with defaultValue="a"
- Tab B is no longer selected
- User clicks tab B again
- Now Panel B renders (promise already resolved, no suspense)
But the issue says the expected behavior is that the panel should render "like it did in v1.4.1". So in v1.4.1, after step 6, the panel content WAS rendered.
The difference between v1.4.1 and the current version might be in how the panel registration works. In the old version, the panel was always rendered (just with undefined children). In the new version, it returns null when hidden.
But that still doesn't explain why v1.4.1 would work... unless the registration of panels affects whether the value is preserved.
Actually, I think I need to look at this from a different angle. The PR #4704 mentions "Fire onValueChange for automatic tab selection". Let me see if there's something in that PR that affects the behavior when tabs register/unregister.
Let me look at the current code flow:
- Panel registers itself with TabsRoot via
registerMountedTabPanel - Panel unregisters via cleanup function in the effect
If a panel suspends before the effect runs, it never registers. Then when it retries, it might try to register again...
Wait! I think I found the issue!
Look at the
useIsoLayoutEffectin TabsPanel.tsx:useIsoLayoutEffect(() => { if (hidden && !keepMounted) { return undefined; } // ... registerMountedTabPanel(value, id); return () => { unregisterMountedTabPanel(value, id); }; }, [hidden, keepMounted, value, id, registerMountedTabPanel, unregisterMountedTabPanel]);When a panel suspends, the render is interrupted and effects never run. So the panel never registers itself with TabsRoot!
Now, in TabsRoot, there's logic that checks if the current selection is valid:
const selectionIsMissing = selectedTabMetadata == null && value !== null;If the selected panel hasn't registered (because it suspended),
selectedTabMetadatamight be null... but wait,selectedTabMetadatais about tabs, not panels.Let me re-read TabsRoot.tsx:
const selectedTabMetadata = React.useMemo(() => { for (const tabMetadata of tabMap.values()) { if (tabMetadata != null && tabMetadata.value === value) { return tabMetadata; } } return undefined; }, [tabMap, value]);This is about tabs (the triggers), not panels. The tabs register themselves separately.
OK, let me look at the
mountedTabPanelsstate:const [mountedTabPanels, setMountedTabPanels] = React.useState( () => new Map<TabsTab.Value | number, string>(), );This maps panel values to their IDs. It's used for
aria-controlsandaria-labelledby.I think the key issue might be in the automatic value change logic in TabsRoot. Let me look at it:
useIsoLayoutEffect(() => { if (isControlled) { return; } // ... if (tabMap.size === 0) { if (!didRegisterTabsRef.current || value === null) { return; } commitAutomaticValueChange(null, REASONS.missing); return; } didRegisterTabsRef.current = true; const selectionIsDisabled = selectedTabMetadata?.disabled; const selectionIsMissing = selectedTabMetadata == null && value !== null; // ... if (selectionIsDisabled || selectionIsMissing) { const fallbackValue = firstEnabledTabValue ?? null; // ... commitAutomaticValueChange(fallbackValue, fallbackReason); return; } // ... }, [...]);This effect runs when tabs register. If the selected tab is disabled or missing, it automatically falls back to the first enabled tab.
But wait, this is about tabs, not panels. The tabs (triggers) register independently of the panels.
Let me think about the actual flow again:
- User clicks tab B
onValueChangeis called,setValue("b")- TabsRoot re-renders with
value = "b" - Panel B's
openbecomes true - Panel B's
useTransitionStatussetsmounted = trueduring render shouldRenderis true (becausemountedis true)- Panel B renders its element
- Panel B renders its children, which includes
<SuspendingChild /> <SuspendingChild />suspends (throws a promise)- React catches at the Suspense boundary outside TabsRoot
- The entire TabsRoot tree is discarded and fallback is shown
- Promise resolves
- React retries rendering from Suspense boundary
- TabsRoot remounts with
value = defaultValue = "a"(state is lost) - Now tab A is selected, not tab B
So after recovery, the value reverts to the default. This is expected for an uncontrolled component.
But the issue says the expected behavior is that the panel should render. This implies that in v1.4.1, after the suspense recovery, tab B was still selected.
How could that happen? Unless v1.4.1 didn't use the same state management...
Actually, let me re-read the issue one more time. The key sentence is:
Only when clicking the trigger a second time, the panel is rendered.
This suggests that after recovery, clicking tab B again shows the panel. If the value reverted to "a", clicking tab B would change it to "b" again, and the panel would render.
So the issue might be that after the first click:
- Value changes to "b"
- Panel B suspends
- After recovery, value is back to "a"
- User clicks tab B again
- Value changes to "b"
- Panel B renders (no suspense because promise resolved)
But wait, this seems like expected behavior! Why would it be different in v1.4.1?
Unless... the issue is that after the first click, even though the value reverts to "a", the user expects tab B to still be selected. This is a UX issue, not a code issue.
Actually, I think I've been overthinking this. Let me re-read the Stackblitz example:
export default function App() { return ( <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> ); }When the user clicks tab B:
- Value changes to "b"
- Panel B tries to render
- SuspendingChild suspends
- Suspense catches and shows fallback
- TabsRoot is unmounted
- Promise resolves
- TabsRoot remounts with value = "a" (default)
Now, after step 7, the user sees tab A selected (because value is "a"). The user then clicks tab B again, and the panel renders.
The issue says this worked in v1.4.1. So in v1.4.1, after step 6, the panel content WAS rendered. This means the value was still "b" after recovery.
How could the value be preserved? The only way is if the TabsRoot didn't completely unmount.
Actually, wait. Let me reconsider React's Suspense behavior. When a component suspends:
- React catches the thrown promise
- React rolls back the render of the suspended subtree
- The Suspense boundary shows its fallback
- BUT - the state of components above the suspended component is preserved!
In this case, the suspended component is
<SuspendingChild />which is a child of<Tabs.Panel>. The Suspense boundary is outside<Tabs.Root>. So when<SuspendingChild>suspends:- React rolls back from
<SuspendingChild>up to the Suspense boundary - This includes
<Tabs.Panel>and<Tabs.Root> - The fallback is rendered
But here's the key: when React rolls back, does it call cleanup effects? Does it "unmount" the components?
According to React's documentation, when Suspense catches a promise, it "rolls back" the render phase. This means:
- Render phase state updates are discarded
- Effects don't run for the rolled-back subtree
- Components are NOT unmounted in the traditional sense - they just haven't committed yet
So when the promise resolves and React retries:
- TabsRoot is NOT remounted - it's the same instance
- Its state (value = "b") is preserved
- Panel B tries to render again
But wait, if that's the case, then
valueshould still be "b" after recovery. Let me re-read my understanding...Actually, I think there are two different Suspense behaviors:
-
During initial render (mount): If a component suspends during mount, React rolls back and shows fallback. When the promise resolves, React retries the mount. State IS preserved because the component instance is the same.
-
After commit: If a component suspends after it has already been committed, React unmounts it, shows fallback, and remounts when the promise resolves. State is NOT preserved because the component was unmounted.
In our case, the user clicks tab B (this is an event, so TabsRoot is already committed). Then Panel B suspends. This is case 2: React needs to unmount the subtree and show fallback.
Hmm, but actually, when the user clicks tab B,
setValue("b")triggers a re-render. This re-render commits the change. But if Panel B suspends during this render, the render is interrupted before commit.So the flow is:
- User clicks tab B
setValue("b")is called- React starts rendering with
value = "b" - Panel B renders but suspends
- React catches at Suspense boundary
- React rolls back the render (the
value = "b"update is discarded) - Fallback is shown with
value = "a"(the previous committed state) - Promise resolves
- React retries the render with
value = "b"(the update is still pending!)
Wait, is that right? Let me think about this more carefully...
When
setValue("b")is called, React schedules a state update. Then it starts rendering. During render, Panel B suspends. React catches and rolls back. The rollback discards the render phase, but what about the state update?In React 18+, when a render is interrupted by a Suspense boundary, the state update that triggered the render is NOT discarded. It's retried when the promise resolves.
So the flow should be:
- User clicks tab B
setValue("b")is called, scheduling a state update- React starts rendering with
value = "b" - Panel B suspends
- React rolls back the render
- Fallback is shown (with the previous committed state, value = "a")
- Promise resolves
- React retries the render with
value = "b"(the pending update is still there!) - Panel B renders successfully
If this is correct, then after step 8, value should be "b" and Panel B should render!
But the issue says the panel doesn't render. So something else must be going on...
Let me look at the
useTransitionStatushook again:const [mounted, setMounted] = React.useState(open); if (open && !mounted) { setMounted(true); setTransitionStatus('starting'); }When React retries with
value = "b":- Panel B's
openis true useState(open)- this initializesmountedbased on the currentopenvalue- Since
openis true,mountedstarts as true
Wait, but React state initialization only happens once per component instance. If the component didn't fully mount (because it suspended), and then React retries, the state initialization should still be based on the initial value passed to
useState.But here's the thing: if React rolls back the render, the component instance is discarded and a new one is created on retry. So
useState(open)is called again, andmountedis initialized toopen(which is true).So after retry,
mountedshould be true and the panel should render!Unless... there's something about how the state update is batched or how React handles the suspended render.
OK let me think about this differently. What if the issue is not about the state being lost, but about the
idbeing different?Looking at
useBaseUiId:export function useBaseUiId(idOverride?: string): string | undefined { return useId(idOverride, 'base-ui'); }And
useId:export function useId(idOverride?: string, prefix?: string): string | undefined { if (maybeReactUseId !== undefined) { const reactId = maybeReactUseId(); return idOverride ?? (prefix ? `${prefix}-${reactId}` : reactId); } return useGlobalId(idOverride, prefix); }React's
useIdis stable for a component instance. But if the component is rolled back and remounted, the ID might change.But actually, looking at the registration effect:
useIsoLayoutEffect(() => { if (hidden && !keepMounted) { return undefined; } // ... registerMountedTabPanel(value, id); return () => { unregisterMountedTabPanel(value, id); }; }, [hidden, keepMounted, value, id, registerMountedTabPanel, unregisterMountedTabPanel]);The
idis used to register the panel. If theidchanges between renders (because the component was remounted), the old registration is still there, and a new one is added.But
mountedTabPanelsis a Map keyed by panel value. So if the same panel value registers with a different ID, the Map entry is updated:const registerMountedTabPanel = useStableCallback( (panelValue: TabsTab.Value | number, panelId: string) => { setMountedTabPanels((prev) => { if (prev.get(panelValue) === panelId) { return prev; } const next = new Map(prev); next.set(panelValue, panelId); return next; }); }, );This should work correctly even with different IDs.
OK, I'm going to take a different approach. Let me look at what exactly changed between v1.4.1 and v1.5.0 in the tabs implementation. The issue says the problem started in v1.5.0.
Actually wait, let me re-read the issue title more carefully:
[tabs] Tabs.Panel content not rendered if a child component suspends without boundary within Tabs.Root
This says "without boundary within Tabs.Root", meaning there's no Suspense boundary inside Tabs.Root, only outside.
And the issue says "Only when clicking the trigger a second time, the panel is rendered." This suggests:
- First click on tab B: panel doesn't render (suspends, fallback shows)
- After promise resolves, user clicks tab B again: panel renders
If the value was preserved (value = "b" after recovery), the user wouldn't need to click again. So the value must NOT be preserved.
But how? If React retries the state update, value should be "b"...
OH WAIT. I think I finally understand. The issue might be about the
mountedTabPanelsMap.Let me trace through:
-
Initial render: TabsRoot with value="a"
- Panel A:
open=true,mounted=true, effect runs, registers with id="base-ui-a" - Panel B:
open=false,mounted=false, doesn't render (returns null)
- Panel A:
-
User clicks tab B, value changes to "b"
- Panel A:
open=false,mounted=true(was true), effect cleanup unregisters - Panel B:
open=true,mounted=true(set during render) - Panel B renders but suspends
- React rolls back
- Panel A:
-
After rollback:
- TabsRoot value is still "a" (the committed state)
- Panel A is rendered
- Panel B is not rendered (or its render is rolled back)
- Panel B's effect never ran, so it never registered
-
Promise resolves, React retries with value="b"
- TabsRoot value becomes "b"
- Panel B tries to render
- Panel B's
mountedstarts as true (useState(open) with open=true) - Panel B renders... but wait, should it work?
Hmm, this should work. Unless there's a race condition or something about how the effects run.
Actually, let me look at the issue from a different angle. The issue mentions PR #4704 as potentially causing the bug. Let me see what changed:
PR #4704 added:
notifyAutomaticValueChangefunctionshouldNotifyInitialValueChangeRefshouldHonorDisabledDefaultValueRefdidRegisterTabsRef- Complex automatic value change logic
The key change might be in the automatic value change logic. When tabs register and the selected tab is disabled or missing, the code automatically falls back to the first enabled tab.
But in our scenario, the tab B trigger IS registered (it doesn't suspend). Only the panel suspends. So the automatic value change logic shouldn't affect this.
Unless... the
mountedTabPanelsMap is used to determine if a tab is valid?Let me check the
selectedTabMetadatacalculation:const selectedTabMetadata = React.useMemo(() => { for (const tabMetadata of tabMap.values()) { if (tabMetadata != null && tabMetadata.value === value) { return tabMetadata; } } return undefined; }, [tabMap, value]);This uses
tabMap, notmountedTabPanels. So the panel registration shouldn't affect the selected tab metadata.Wait, I just realized something. The
tabMapis for tabs (triggers), andmountedTabPanelsis for panels. They're separate!So the automatic value change logic only cares about tabs, not panels. And tabs don't suspend in this scenario (only the panel suspends).
Let me look at this from yet another angle. What if the issue is in the
useTransitionStatushook specifically?When Panel B suspends:
- Panel B renders with
open=true useTransitionStatussetsmounted=trueduring render- The panel returns an element
- The child suspends
- React rolls back
Now, when React retries:
- Panel B renders again with
open=true useState(open)is called again...
But wait, does React call
useStateagain if the component was rolled back? Or does it reuse the same state?In React's Suspense model, when a render is rolled back:
- The component instance is discarded
- On retry, a new render is started
useStateis called with the same initial value- The state update that was in progress (setMounted(true)) is NOT applied because the render was rolled back
So on retry:
useState(open)withopen=truereturns[true, setMounted]- But the
if (open && !mounted)check:open=true,mounted=true, so the condition is false mountedstays true
Hmm, so
mountedshould be true on retry. The panel should render.Unless... there's something about how React handles state updates that happen during a suspended render.
Actually, I think the issue might be more subtle. Let me think about this:
When React rolls back a suspended render:
- The component tree is rolled back
- Any state updates that were made during the render are discarded
- But the component instances are also discarded
On retry, React starts fresh:
- New component instances are created
useState(open)initializesmountedtoopen- Since
openis still true (the state update that triggered this render is still pending),mountedis initialized to true
So the panel should render on retry!
Unless... the issue is that the
valuestate update is also rolled back?Wait, let me reconsider. When
setValue("b")is called and React starts rendering:- React updates TabsRoot's state to
value="b" - React renders TabsRoot
- Panel B suspends
- React catches at Suspense boundary
- React rolls back...
What does "rolls back" mean exactly?
In React 18, when a render is interrupted by Suspense:
- The work in progress is discarded
- The pending state update is NOT discarded - it's retried
- But the render that was in progress is discarded
So when React retries:
- TabsRoot still has the pending update
value="b" - The render continues from the Suspense boundary
- TabsRoot is rendered again with
value="b" - Panel B renders again
- This time, the child doesn't suspend (promise resolved)
- The render completes
So the panel should render! Unless there's something wrong with the current implementation.
Let me look at the current code more carefully. Maybe there's a specific issue with how
useTransitionStatushandles this case.Actually, let me look at the code change mentioned in the issue - PR #4704. The issue says it was introduced by this PR. Let me see what it changed in the TabsRoot:
The main change in PR #4704 was the automatic value change logic:
useIsoLayoutEffect(() => { if (isControlled) { return; } if (tabMap.size === 0) { if (!didRegisterTabsRef.current || value === null) { return; } commitAutomaticValueChange(null, REASONS.missing); return; } didRegisterTabsRef.current = true; const selectionIsDisabled = selectedTabMetadata?.disabled; const selectionIsMissing = selectedTabMetadata == null && value !== null; // ... if (selectionIsDisabled || selectionIsMissing) { const fallbackValue = firstEnabledTabValue ?? null; // ... commitAutomaticValueChange(fallbackValue, fallbackReason); return; } if (shouldNotifyInitialValueChange && selectedTabMetadata != null) { notifyAutomaticValueChange(value, REASONS.initial); shouldNotifyInitialValueChangeRef.current = false; } }, [...]);This effect runs when tabs register. When
value !== nullandselectedTabMetadata == null(selection is missing), it falls back to the first enabled tab.But in our case, tab B IS in the tabMap (it's a trigger, not a panel). So
selectedTabMetadatashould not be null.Unless... wait. The
tabMapis populated when tabs register themselves. What if a tab's registration depends on something that also suspends?Let me look at how tabs register:
<tool_call>
I found the key insight! Let me look at the TabsRoot automatic value change logic more carefully:
<tool_call> <function=bash_terminal> <parameter=command> cd /base-ui && git show 2540428f0 -- packages/react/src/tabs/root/TabsRoot.tsx | head -200 </tool_call>
Assistant
Let me run the git show command again properly.
Assistant
Let me look at the test file for TabsPanel to understand how tests are structured and then look more carefully at the issue. I need to understand the exact flow of what happens when a tab panel suspends.
Assistant
Let me look at the
useOpenChangeCompletehook which is used in TabsPanel:Assistant
Let me look at the
useControlledhook to understand how it handles state initialization:Assistant
Let me look at the
useRenderElementhook to understand how children are handled:Assistant
Let me look at the useRenderElement hook to understand how the element is rendered.
Assistant
Let me create a test script to reproduce this issue and understand the problem better.
Assistant
Let me re-analyze the issue more carefully. The key issue is that when a panel suspends and the suspense boundary is outside Tabs.Root, the panel content is not rendered on the first tab click.
Looking at the TabsPanel code:
const open = value === selectedValue; const { mounted, transitionStatus, setMounted } = useTransitionStatus(open); const hidden = !mounted; // ... const shouldRender = keepMounted || mounted; if (!shouldRender) { return null; }The issue is in
useTransitionStatus. Whenopenistrue, it callssetMounted(true)during render:if (open && !mounted) { setMounted(true); setTransitionStatus('starting'); }But here's the problem: when a child component suspends, React interrupts the render and rolls back. The
setMounted(true)call during render is part of the interrupted render, so it gets rolled back too!When React retries after the promise resolves:
useState(open)initializesmountedtoopen(which istruesincevalueshould still be "b")- The
if (open && !mounted)check:openis true,mountedis true (from initialization), so the condition is false mountedstaystrue
Wait, that should work... Let me think again.
Actually, the problem might be that after the suspense recovery, the Tabs.Root value has changed back to the default value "a". But that doesn't match the issue description which says clicking a second time works.
Let me re-read the issue:
Only when clicking the trigger a second time, the panel is rendered.
So after recovery:
- Tab B trigger appears selected (visually)
- But Panel B content is NOT rendered
- Clicking tab B again shows the content
This means after recovery,
valueis "b" (tab B is selected), but Panel B'smountedis somehow false.Wait! I think I found it! The issue is:
-
User clicks tab B, value changes to "b"
-
Panel B renders,
open = true -
useState(open)-openis true, somountedis initialized totrue -
if (open && !mounted)- open=true, mounted=true, condition is FALSE, so setMounted is NOT called -
Panel renders its children
-
SuspendingChild suspends!
-
React rolls back the render
-
Promise resolves, React retries
-
Panel B renders again
-
useState(open)- BUT HERE'S THE KEY:useStatepreserves state between renders of the same component instance. But when React rolls back due to suspense, does it reset state?
Actually, when React suspends during render and then retries, it creates a NEW fiber for the component. The
useStateinitialization happens again. Somountedis initialized toopenagain.But wait -
openshould still betruebecausevalueis still "b"...OH WAIT! I think I finally see it!
The problem might be about WHEN the value changes. Let me trace through more carefully:
- Initial render: value = "a"
- User clicks tab B
onValueChangeis called, which callssetValue("b")- React re-renders TabsRoot with value = "b"
- Panel B's
openbecomestrue - Panel B's
useState(open)initializesmounted = true - The
if (open && !mounted)check is skipped (mounted is already true from init) shouldRender = keepMounted || mounted=false || true=true- Panel B renders its element
- Panel B renders its children (including SuspendingChild)
- SuspendingChild suspends!
- React rolls back
After rollback, when React retries: 13. TabsRoot re-renders with value = "b" (the state update was scheduled) 14. Panel B's
openistrue15. Panel B'suseState(open)- but wait, is this a new component instance?Here's where it gets tricky. When React suspends:
- The suspended fiber is marked as "suspended"
- React shows the fallback
- When the promise resolves, React retries the suspended fiber
The key question: does
useStatere-initialize on retry?According to React's behavior,
useStateinitialization only happens on the FIRST render. On subsequent renders (including retries after suspense),useStatereturns the current state value.But when React rolls back a suspended render, the render was never completed. So the state update
setMounted(true)from step 6 was never actually applied because... wait, no.useState(open)returns[true, setMounted]becauseopenis true. The initial value ofmountedistrue.So on retry:
useState(open)returns[true, setMounted]because:- If this is considered a "first render" after rollback, initialization gives
mounted = open = true - If this is considered a "subsequent render", state was never updated (setMounted was never called), so
mountedis still the initial valuetrue
- If this is considered a "first render" after rollback, initialization gives
In both cases,
mountedshould betrue!Hmm, let me reconsider. Maybe the issue is different. Let me look at the actual flow more carefully.
Actually, wait. I just realized something. The
useState(open)initialization uses the value ofopenat initialization time. But on a re-render,useStatedoesn't re-read the initial value - it returns the stored state.So:
- Initial mount:
open = false(value is "a") useState(false)→mounted = false- User clicks tab B, value becomes "b"
- Re-render:
open = true useStatereturns stored state:mounted = false(not re-initialized!)if (open && !mounted): open=true, mounted=false → TRUE!setMounted(true)is called during rendersetTransitionStatus('starting')is called during render- Panel renders
- Child suspends
- React rolls back!
After rollback:
- The render-phase state updates (
setMounted(true),setTransitionStatus('starting')) are discarded - The component's state is still
mounted = false(from before the render started) - The component instance is preserved (it wasn't unmounted)
When React retries: 12.
open = true(value is still "b") 13.useStatereturns stored state:mounted = false(the update was rolled back!) 14.if (open && !mounted): open=true, mounted=false → TRUE! 15.setMounted(true)is called again 16.shouldRender = keepMounted || mounted=false || false=false!Wait, that's the bug! The
mountedvariable in the render is stillfalseeven thoughsetMounted(true)was called!The issue is that
setMounted(true)schedules a state update, but the current render's value ofmountedis stillfalse. React batches state updates and applies them after the render completes.So when we check
shouldRender = keepMounted || mounted,mountedis stillfalseeven though we've calledsetMounted(true)!Actually wait, let me re-read the code:
const [mounted, setMounted] = React.useState(open); if (open && !mounted) { setMounted(true); setTransitionStatus('starting'); } // ... const shouldRender = keepMounted || mounted; if (!shouldRender) { return null; }Yes! The
mountedvariable in this render isfalse(the value before the state update).setMounted(true)schedules an update, butmounteddoesn't change until the NEXT render.So when a panel first opens (when
openchanges fromfalsetotrue):useStatereturns the oldmountedvalue (false)if (open && !mounted)triggerssetMounted(true)- But
mountedis stillfalsein this render! shouldRender = keepMounted || mounted = false || false = false- Panel returns
null!
But wait, this would mean the panel NEVER renders on first open, which is clearly not the intended behavior. So there must be something I'm missing...
OH! I see it now. Look at the
useStateinitialization:const [mounted, setMounted] = React.useState(open);This uses
openas the initial value. On the FIRST render of the component (mount),mountedis initialized to the current value ofopen.But on SUBSEQUENT renders (re-renders),
useStateignores the initial value and returns the stored state.So the flow is:
- Component mounts with
open = false useState(false)→mounted = false(initialization)- User clicks, value changes,
open = true - Re-render
useState(false)→ returns storedmounted = false(initial value is ignored!)if (open && !mounted)→setMounted(true)is called- But
mountedis stillfalsein this render! shouldRender = false- Panel returns
null! - NEXT render:
mounted = true(the update was applied) shouldRender = true- Panel renders!
So the panel has a one-render delay! When you first click a tab, the panel doesn't render until the NEXT render (after
setMounted(true)is applied).This is the bug! The
setMounted(true)is called during render, but its effect isn't visible until the next render. In normal operation, the next render happens quickly (synchronously or on next frame), so the delay is not noticeable.But when the panel suspends:
- Panel starts rendering with
open = true useStatereturnsmounted = falseif (open && !mounted)→setMounted(true)is calledshouldRender = false- Panel returns
null - But wait, if panel returns
null, the child never renders, so there's no suspense!
Hmm, that doesn't match the issue description either. Let me re-think...
Actually, let me re-read the useTransitionStatus code:
const [mounted, setMounted] = React.useState(open);This line is INSIDE the
useTransitionStatushook. Theopenparameter is passed from the caller.On mount:
open = false(Panel B is not selected)useState(false)→mounted = falseif (open && !mounted)→open = false, condition is FALSE- Returns
{ mounted: false, ... }
After clicking tab B:
open = true- Re-render
useState(false)→useStateignores initial value, returns storedmounted = false- Wait, that's wrong!
useState(open)- the initial valueopenistruenow, butuseStateignores it on re-renders
Actually wait, I need to think about this more carefully.
useState(initialValue)- theinitialValueargument is only used on the FIRST render. On subsequent renders, React returns the current state value.So:
- First render (mount):
useState(false)returns[false, setMounted]because that's the initial value - Second render (after click):
useState(true)is called, but React ignores thetrueand returns[false, setMounted]because that's the stored state
So after clicking tab B:
useStatereturnsmounted = falseif (open && !mounted):true && true→ TRUEsetMounted(true)is called- But
mountedis stillfalsein this render shouldRender = false- Panel returns
null - No suspense because panel never rendered!
Then in the NEXT render:
useStatereturnsmounted = true(the update was applied)shouldRender = true- Panel renders
- Child suspends
So the flow is actually:
- Panel B starts rendering, returns null (because mounted is false)
- NEXT render: mounted is true, panel renders
- Child suspends
- Suspense shows fallback
- Promise resolves
- React retries
- mounted is still true (it was committed)
- Panel renders
- Child renders (no more suspense)
Hmm, but this means the panel SHOULD render after the first click (in the second render). The issue says it doesn't render on the first click...
Let me re-read the issue description again:
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."the suspense unmounts the tab root" - this is a key detail! The suspense boundary is OUTSIDE Tabs.Root. When the panel suspends, the ENTIRE Tabs.Root is unmounted.
So the flow is:
- Initial render: value = "a", Panel A shows, Panel B has mounted = false
- Click tab B: value = "b"
- Panel B re-render: mounted = false (stored state), setMounted(true) called
- NEXT render: Panel B: mounted = true, shouldRender = true
- Panel B renders
- Child suspends!
- Suspense catches, unmounts entire Tabs.Root (including all panels)
- Fallback shows
- Promise resolves
- React retries from Suspense boundary
- Tabs.Root REMOUNTS with fresh state
- value = defaultValue = "a" (fresh state)
- Panel B: open = false, mounted = false (fresh state, initialized to open which is false)
- Panel B returns null
Wait, but then the user would see tab A selected, not tab B. The issue says clicking a second time works, which would make sense if value reverted to "a".
But the issue says "the panel content is not rendered when opening a tab for the first time". This implies the tab IS selected but the panel content doesn't show.
Hmm, I'm confused. Let me look at this from a different angle.
Maybe the issue is that after step 10-11, the value is still "b" because the Suspense retry happens in the same batch as the click? But Tabs.Root was unmounted, so its state should be lost...
Unless the Suspense doesn't actually unmount Tabs.Root? Let me think about when React actually unmounts components during Suspense...
Actually, I think I've been confusing two scenarios:
Scenario A: Suspense during initial render (mount phase)
- Component starts rendering
- Suspends
- React shows fallback
- Component never commits
- When promise resolves, React retries the render
- State IS preserved because the component was never committed/unmounted
Scenario B: Suspense after commit
- Component is already rendered (committed)
- Something triggers a re-render (e.g., user clicks)
- During re-render, component suspends
- React shows fallback
- The component subtree is unmounted
- When promise resolves, React remounts
- State is NOT preserved
In our case, the user clicks tab B after the initial render (commit). So this is Scenario B:
- Tabs.Root is already committed with value = "a"
- User clicks, value changes to "b"
- Panel B renders but suspends
- React catches at Suspense boundary
- React unmounts the entire Tabs.Root subtree
- Fallback is shown
- When promise resolves, React remounts Tabs.Root
- Tabs.Root starts fresh with value = "a" (defaultValue)
So after recovery, value is "a", not "b". The user sees tab A selected. They need to click tab B again to see its content.
But the issue says the panel doesn't render "when opening a tab for the first time" and works "when clicking the trigger a second time". This matches!
So the issue is: after the suspense recovery, the value reverts to the default. The user has to click the tab again.
But the issue says this worked in v1.4.1. How could it have worked?
Maybe in v1.4.1, the panel was always rendered (even when hidden), so the Suspense would catch before the value reverted? Or maybe the panel registration logic was different?
Let me look at what changed between v1.4.1 and v1.5.0 that could affect this. The issue mentions PR #4704.
Actually, I think I need to step back and understand what the expected behavior should be. If a tab panel suspends and the suspense boundary is outside Tabs.Root, should the selected value be preserved?
In React's Suspense model, when a component suspends after commit, the component subtree is unmounted and its state is lost. So the value reverting to the default is expected behavior.
But the issue says it worked in v1.4.1. So either:
- v1.4.1 had different behavior that preserved the value
- The issue description is wrong about v1.4.1 working
- There's something else going on
Let me check the actual v1.4.1 code to see what was different. But I don't have access to that specific version. Let me check the git history instead.
Actually, I think the real issue might be different. Let me re-read the issue one more time:
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
Maybe the issue is NOT about the value reverting. Maybe after recovery, value IS still "b", but the panel's
mountedstate is somehow reset tofalse.How could that happen?
- User clicks tab B
- value = "b", Panel B renders
- Panel B's
mounted= true - Child suspends
- Suspense unmounts Tabs.Root
- Tabs.Root state is lost (value reverts to "a")
No wait, if Tabs.Root is unmounted, value reverts to "a". So Panel B would have open = false.
Unless... the Tabs.Root is NOT actually unmounted? What if the Suspense boundary only catches the suspended component without unmounting the parent?
Hmm, I think I need to actually run the code and see what happens. Let me try to create a minimal test case.
Actually, let me look at the issue from yet another angle. The PR #4704 mentions "Fire onValueChange for automatic tab selection". What if the issue is related to the automatic value change logic?
Looking at the TabsRoot code, after PR #4704:
useIsoLayoutEffect(() => { if (isControlled) { return; } if (tabMap.size === 0) { if (!didRegisterTabsRef.current || value === null) { return; } commitAutomaticValueChange(null, REASONS.missing); return; } didRegisterTabsRef.current = true; const selectionIsDisabled = selectedTabMetadata?.disabled; const selectionIsMissing = selectedTabMetadata == null && value !== null; if (!selectionIsDisabled && value === initialDefaultValueRef.current) { shouldHonorDisabledDefaultValueRef.current = false; } if ( shouldHonorDisabledDefaultValueRef.current && selectionIsDisabled && value === initialDefaultValueRef.current ) { return; } const shouldNotifyInitialValueChange = shouldNotifyInitialValueChangeRef.current; if (selectionIsDisabled || selectionIsMissing) { const fallbackValue = firstEnabledTabValue ?? null; if (value === fallbackValue) { shouldNotifyInitialValueChangeRef.current = false; return; } let fallbackReason: TabsRoot.ChangeEventReason = REASONS.missing; if (shouldNotifyInitialValueChange) { fallbackReason = REASONS.initial; } else if (selectionIsDisabled) { fallbackReason = REASONS.disabled; } commitAutomaticValueChange(fallbackValue, fallbackReason); return; } if (shouldNotifyInitialValueChange && selectedTabMetadata != null) { notifyAutomaticValueChange(value, REASONS.initial); shouldNotifyInitialValueChangeRef.current = false; } }, [...]);This effect checks if the selected tab is missing or disabled, and if so, falls back to the first enabled tab. The key is
selectionIsMissing = selectedTabMetadata == null && value !== null.But
selectedTabMetadatais about the TAB (trigger), not the PANEL. So this shouldn't be affected by panel suspension.Hmm, but wait. What if the issue is about
mountedTabPanels? Let me look at how it's used.Actually, I think I need to approach this differently. Let me look at the specific change that might have caused this issue. The issue mentions that v1.4.1 works but v1.5.0 doesn't. The key change in v1.5.0 is the PR #4704.
But wait, the issue also mentions:
Potentially a side effect of https://github.com/mui/base-ui/pull/4704
So the author of the issue isn't sure. Let me look at what changed in the TabsPanel between v1.4.1 and v1.5.0.
Actually, I realize I should look at the actual test case to understand the expected behavior. Let me search for any existing tests related to Suspense.
Let me also look at the
useTransitionStatushook more carefully. The issue might be in how it handles themountedstate during suspense.Actually, I think I finally understand the issue!
The problem is in the
useTransitionStatushook:const [mounted, setMounted] = React.useState(open); if (open && !mounted) { setMounted(true); setTransitionStatus('starting'); }When a component suspends:
- The render is interrupted
- State updates made during the render (like
setMounted(true)) are NOT applied - When React retries, the component re-renders with the OLD state
But here's the key:
useState(open)usesopenas the initial value. On retry, if the component instance is preserved (not unmounted),useStatereturns the stored state. But if the component WAS unmounted (as in our scenario where Tabs.Root is unmounted), then on remount,useState(open)re-initializes with the current value ofopen.So:
- Tabs.Root unmounts (state lost)
- Tabs.Root remounts
- value = defaultValue = "a" (default state)
- Panel B: open = false
- useState(false) → mounted = false
- shouldRender = false
- Panel B returns null
This is what I described before. The panel doesn't render because value reverted to "a".
But the issue says clicking a second time works. If value is "a", clicking tab B would change it to "b", and Panel B would render.
So the expected behavior in v1.4.1 must have been different. Perhaps in v1.4.1, the panel was rendered even when hidden (with children set to undefined), so the Suspense would catch during render and the value would be preserved somehow.
OR, perhaps the issue is different: after the Suspense recovery, value is still "b" (the click update is preserved), but the panel's
mountedstate is false.Let me think about this. When the Suspense boundary is outside Tabs.Root and the panel suspends:
- Tabs.Root is unmounted
- All of its state is lost, including
value - Tabs.Root remounts
valueis re-initialized todefaultValue
But wait! The
valuestate update was made by the click handler. When Tabs.Root unmounts, does the pending state update get lost?In React, when a component unmounts:
- All state is cleared
- Pending state updates are discarded
So yes, the
value = "b"update would be lost when Tabs.Root unmounts.Hmm, but what if the Suspense doesn't actually unmount Tabs.Root? What if it just "rolls back" the render?
Let me think about this more carefully. In React's Suspense model:
Suspense during commit (after component was already rendered):
- The component subtree is rolled back
- The component is NOT technically "unmounted" - it's rolled back to its previous committed state
- Effects are NOT cleaned up
- When the promise resolves, React retries the render
- The component instance is preserved
- State is preserved (it was committed before the suspended render)
Wait, is that right? Let me reconsider...
When Panel B suspends after the user clicks tab B:
- TabsRoot's previous committed state: value = "a"
- User clicks tab B, triggering setValue("b")
- React starts rendering with value = "b"
- Panel B renders but suspends
- React catches at Suspense boundary
- React rolls back the render to the previous committed state (value = "a")
- Fallback is shown
- Promise resolves
- React retries the render with value = "b" (the pending state update)
If this is correct, then after step 9, value should be "b" and Panel B should render!
But the issue says it doesn't render. So either:
- React actually unmounts the component (clearing state)
- There's something else going on
Let me look at React's actual behavior. When a component suspends during an update:
Actually, I think the behavior depends on whether the component was already committed before the update:
Case 1: Component suspends during initial mount
- Component was never committed
- React rolls back and shows fallback
- When promise resolves, React retries the mount
- State IS preserved (useState is called again with initial values)
Case 2: Component suspends during an update (after commit)
- Component was committed
- React rolls back the update
- If the Suspense boundary is inside the component tree, React shows fallback and keeps the component
- If the Suspense boundary is OUTSIDE the component tree, React unmounts the entire subtree
In our case, the Suspense boundary is OUTSIDE Tabs.Root. So when Panel B (inside Tabs.Root) suspends, React unmounts the entire Tabs.Root subtree!
So:
- Tabs.Root unmounts
- All state is lost (value = "a" is gone, the pending "b" update is discarded)
- Fallback is shown
- Promise resolves
- React remounts from the Suspense boundary
- Tabs.Root mounts with fresh state (value = defaultValue = "a")
OK so now I'm pretty sure the value reverts to "a" after recovery. The user needs to click tab B again.
But the issue says this worked in v1.4.1. So in v1.4.1, somehow the value was preserved. How?
One possibility: In v1.4.1, the panel was always rendered (even when hidden), which means the Suspense would happen during initial mount, not during an update. And during initial mount, React preserves the state (because the component was never committed).
Let me check the old TabsPanel code again:
In the old version:
{ children: hidden && !keepMounted ? undefined : children }The panel always rendered its element, but with
undefinedchildren when hidden. This means:- Panel B always rendered its element (even when hidden)
- Panel B's children (including SuspendingChild) were only rendered when the panel was visible
- But if the panel was hidden, SuspendingChild was never rendered, so no suspense
In the new version:
const shouldRender = keepMounted || mounted; if (!shouldRender) { return null; }The panel returns
nullwhen hidden, so it's not rendered at all. This means:- Panel B is only rendered when mounted
- When mounted, Panel B renders and SuspendingChild suspends
- React catches and shows fallback
Hmm, but the behavior should be the same in both cases when the panel IS visible. The difference is only when the panel is hidden.
Wait, but there's another change: the
mountedstate!In the old version,
hiddenwas computed as:const hidden = tabPanelValue !== selectedValue;In the new version,
hiddenis computed as:const hidden = !mounted;Where
mountedcomes fromuseTransitionStatus(open). Themountedstate is initialized toopenbut has a delay becausesetMounted(true)is called during render.So in the old version:
- Panel B:
hidden = ("b" !== "a") = true - Panel renders element with
children = undefined(because hidden) - SuspendingChild is never rendered
- No suspense
After clicking tab B: 5.
hidden = ("b" !== "b") = false6. Panel renders element with children 7. SuspendingChild renders and suspends!In the new version:
- Panel B:
mounted = false(initialized toopen = false) hidden = !false = trueshouldRender = keepMounted || mounted = false || false = false- Panel returns null!
After clicking tab B: 5.
mountedis still false (setMounted(true) was called in previous render but hasn't taken effect)Wait, that's wrong. After clicking tab B: 6. Panel B re-renders with
open = true7.useStatereturnsmounted = false(stored state, not re-initialized) 8.if (open && !mounted)→setMounted(true)is called 9. Butmountedis stillfalsein this render 10.shouldRender = keepMounted || mounted = false || false = false11. Panel returns null! 12. SuspendingChild is never rendered! 13. No suspense!NEXT render: 14.
useStatereturnsmounted = true(the update was applied) 15.shouldRender = keepMounted || mounted = false || true = true16. Panel renders! 17. SuspendingChild renders and suspends!So in the new version, the panel has a one-render delay before it starts rendering! This means:
- Click tab B
- Panel B doesn't render (mounted = false)
- Next render: Panel B renders, SuspendingChild suspends
- Suspense catches, unmounts Tabs.Root
- Fallback shows
- Promise resolves
- Tabs.Root remounts with value = "a"
- Panel B: open = false, mounted = false
- Panel B returns null
In the old version:
- Click tab B
- Panel B renders (hidden = false, children are rendered)
- SuspendingChild suspends
- Suspense catches, unmounts Tabs.Root
- Fallback shows
- Promise resolves
- Tabs.Root remounts with value = "a"
- Panel B: hidden = true (value is "a")
- Panel B renders element with children = undefined
Wait, in the old version, Panel B still rendered (with undefined children). But that doesn't explain why the issue says v1.4.1 worked.
Actually, let me re-read the issue more carefully:
The panel content is not rendered also when first opening a tab like it did in v1.4.1.
Hmm, "also when first opening a tab" - this suggests that the panel content was rendered in v1.4.1 even after the first click. But if the value reverts to "a", the panel shouldn't be selected...
Unless the issue is about a different scenario. What if the initial defaultValue is "b" (the tab that suspends)?
Let me check the Stackblitz example:
<Tabs.Root defaultValue="a">No, defaultValue is "a". So tab A is selected initially, and tab B is the one that suspends.
OK, I think I need to just test this. Let me create a test file and run it to see the actual behavior.
Actually wait, I just realized something. The issue says:
If a tab panel has a child that suspends and the suspense unmounts the tab root
What if the issue is NOT about the value reverting, but about the panel's internal state? What if after recovery, value IS "b" (for some reason), but the panel's
mountedstate is false?How could value be "b" after recovery? If the Suspense doesn't unmount Tabs.Root but instead just rolls back...
Actually, I think this depends on how React handles the Suspense. Let me look at this from a different perspective.
When a child component suspends during an UPDATE (not initial mount), and the Suspense boundary is outside the parent component:
- React starts rendering the update
- Child suspends
- React catches at Suspense boundary
- React shows fallback
- React DOES NOT unmount the parent - instead, it rolls back the update to the parent's last committed state
- The parent component remains mounted
- When promise resolves, React retries the update
If this is correct, then Tabs.Root would remain mounted with its previous state (value = "a"), and the pending update (value = "b") would be retried.
So the flow would be:
- Tabs.Root committed with value = "a"
- User clicks tab B, setValue("b")
- React renders TabsRoot with value = "b"
- Panel B suspends
- React rolls back to value = "a"
- Fallback shows
- Promise resolves
- React retries with value = "b"
- Tabs.Root renders with value = "b"
- Panel B renders... but does it?
In step 10, Panel B's
open = true. But what about itsmountedstate?If Panel B was previously rendered (committed) with
mounted = false, and the update was rolled back, then Panel B is still committed withmounted = false. When React retries withvalue = "b":- Panel B re-renders
useStatereturnsmounted = false(still committed with this value)if (open && !mounted)→setMounted(true)is called- But
mountedis stillfalsein this render! shouldRender = false- Panel returns
null!
NEXT render:
mounted = trueshouldRender = true- Panel renders!
So the bug is: when the panel first opens (open changes from false to true), the
mountedstate is still false in the current render becausesetMounted(true)is called during render. This means the panel has a one-render delay before it starts rendering.In normal operation, this delay is fine because React quickly re-renders. But when the panel suspends during this delayed render, the suspense causes the update to be rolled back, and on retry, the panel STILL has
mounted = false!Wait, but if the update was rolled back, the next render would be a retry with the same value. Let me trace through more carefully:
- Tabs.Root committed with value = "a"
- Panel B committed with mounted = false, open = false
- User clicks tab B, setValue("b")
- React renders with value = "b"
- Panel B renders:
- open = true
- useState returns mounted = false (committed value)
- if (open && !mounted) → setMounted(true)
- shouldRender = false
- Returns null!
- Since Panel B returned null, its child (SuspendingChild) is not rendered
- No suspense!
- Render completes
- Tabs.Root is committed with value = "b"
- Panel B is committed with mounted = true (the update from step 5 was applied)
Wait, but in step 5, setMounted(true) was called. Even though the render returned null, the state update is still applied after the render completes. So in step 10, mounted = true.
NEXT render (some event triggers it, or React auto-schedules it): 11. Panel B renders: - open = true - useState returns mounted = true - shouldRender = true - Renders element - SuspendingChild renders and suspends! 12. React catches at Suspense boundary 13. React rolls back
Now, what happens in step 13? React rolls back the render. Panel B was committed with mounted = true. After rollback:
- Panel B still has mounted = true (it was committed)
- The pending render (which would have suspended) is discarded
After promise resolves: 14. React retries with value = "b" 15. Panel B renders: - open = true - useState returns mounted = true (still committed) - shouldRender = true - Renders element - SuspendingChild renders... no more suspense (promise resolved) 16. Panel B content is shown!
So in this flow, the panel SHOULD render after the promise resolves. This doesn't match the issue.
Unless... the rollback in step 13 actually changes the state? Let me think about this more carefully.
When React rolls back a suspended render:
- The render that was in progress is discarded
- Any state updates made during that render are NOT applied
- The component keeps its last committed state
So in step 12-13:
- React was rendering with value = "b"
- Panel B was rendering with mounted = true
- SuspendingChild suspended
- React rolled back
- Panel B's state is still mounted = true (committed value)
After promise resolves and React retries:
- value is still "b" (pending update)
- Panel B's mounted is still true (committed value)
- shouldRender = true
- Panel renders
So the panel should render! This contradicts the issue.
Hmm, let me reconsider. Maybe the issue is about a specific edge case that I'm not considering.
Actually, I just realized something. In my analysis, I assumed that the panel's
mountedstate was committed before the suspense. But what if the panel was never committed withmounted = true?Let me trace through a different scenario:
- Tabs.Root committed with value = "a"
- Panel B was never rendered (it returned null because open = false, mounted = false, shouldRender = false)
Wait, if shouldRender was false, the panel returned null. Was it committed with null, or was it never rendered at all?
In React, if a component returns null, it IS still committed - it just renders nothing. So Panel B IS committed with mounted = false.
OK so:
- Panel B committed with mounted = false
- User clicks tab B, setValue("b")
- React renders with value = "b"
- Panel B renders:
- open = true
- useState returns mounted = false (committed value)
- if (open && !mounted) → setMounted(true)
- shouldRender = false
- Returns null!
- Render completes
- Tabs.Root committed with value = "b"
- Panel B committed with mounted = true (the update was applied)
But wait, in step 4, Panel B returned null. This means the component finished rendering. So the state update (setMounted(true)) IS applied.
NEXT render: 8. Panel B renders:
- open = true
- useState returns mounted = true
- shouldRender = true
- Renders element
- SuspendingChild suspends!
- React catches, rolls back
- Panel B is committed with mounted = true (the update from step 7)
- After rollback, Panel B still has mounted = true
- Promise resolves
- React retries
- Panel B renders with mounted = true, shouldRender = true
- SuspendingChild renders (no more suspense)
- Content is shown!
So again, the panel should render! This is still not matching the issue.
Wait, let me reconsider the "rollback" behavior. When React rolls back a suspended render:
- The component subtree that was being rendered is discarded
- If the Suspense boundary is outside the component tree, the ENTIRE component tree is discarded
- This includes any uncommitted state updates
So in step 9:
- React rolls back the render
- Tabs.Root's pending state update (value = "b" from step 2) was already committed in step 6
- But Panel B's pending state update (mounted = true from step 7) was already committed
- After rollback, Tabs.Root has value = "b" (committed) and Panel B has mounted = true (committed)
Hmm, but the issue says the panel doesn't render. So there must be something else going on.
Let me re-read the issue one more time:
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."the suspense unmounts the tab root" - this explicitly says the tab root is UNMOUNTED. Not rolled back, but UNMOUNTED.
So when the panel suspends and the Suspense boundary is outside Tabs.Root:
- React unmounts Tabs.Root
- All state is lost
- Tabs.Root is removed from the tree
- Fallback is shown
- Promise resolves
- React remounts Tabs.Root from scratch
- Tabs.Root's state is re-initialized (value = defaultValue = "a")
- Panel B: open = false, mounted = false
- shouldRender = false
- Panel B returns null
After step 10, tab B is NOT selected (value is "a"). The user needs to click tab B again.
But the issue says "Only when clicking the trigger a second time, the panel is rendered." This matches!
And the issue says "The panel content is not rendered also when first opening a tab like it did in v1.4.1." This suggests that in v1.4.1, after the first click (and suspense recovery), the panel content WAS rendered.
So in v1.4.1, somehow the value was preserved (or the panel was rendered even with value = "a").
Let me look at what changed. The key change in v1.5.0 is the transition status logic. In v1.4.1, the panel was rendered differently.
Wait, but in my analysis, even in v1.4.1, after Tabs.Root unmounts and remounts, value would revert to "a". So how could the panel have been rendered?
Unless the Suspense boundary behavior was different in v1.4.1. But that doesn't make sense - Suspense is a React feature, not a Base UI feature.
Let me think about this differently. Maybe in v1.4.1, the panel was always rendered (with or without children), so the SuspendingChild was part of the render tree even when the panel was hidden. This would mean:
- Initial render: Panel B is rendered with SuspendingChild (but with
hiddenstyle) - SuspendingChild suspends during initial render
- Suspense catches and shows fallback
- Promise resolves
- React retries
- Panel B renders with SuspendingChild
- SuspendingChild renders (no more suspense)
- Panel B content is shown
But this doesn't match the issue description which says "when opening a tab for the first time". The initial defaultValue is "a", not "b".
Actually wait, let me re-read the Stackblitz example:
function SuspendingChild() { return <div>{use(dataPromise)}</div>; } export default function App() { return ( <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> ); }defaultValue is "a", so tab A is selected initially. Panel A has no suspense. Tab B has the suspending child.
In v1.4.1, if the panel was always rendered (with hidden style), then:
- Initial render: Tab A selected, Panel A shown
- Panel B is rendered but hidden (hidden style, or children = undefined)
- SuspendingChild is NOT rendered because Panel B's children are undefined (or the panel is hidden)
- No suspense on initial render
Then when user clicks tab B: 5. Tab B selected, Panel B's children are rendered 6. SuspendingChild suspends 7. Suspense catches, unmounts Tabs.Root 8. After recovery, value = "a" (reverted) 9. Tab A is selected again
This is the same as v1.5.0 behavior. So how did v1.4.1 work?
Unless... the panel's children were ALWAYS rendered, even when hidden. In that case:
- Initial render: Tab A selected
- Panel B is rendered with SuspendingChild
- SuspendingChild suspends during initial render
- Suspense catches and shows fallback
- Tabs.Root is rolled back (not unmounted, because it was never committed)
- Promise resolves
- React retries the render
- Tab A is still selected (default)
- Panel A is shown
- Panel B is rendered with SuspendingChild
- SuspendingChild renders (no more suspense)
- Panel B is hidden (but in the DOM)
So in v1.4.1, the SuspendingChild was rendered during initial mount, which caused suspense. After recovery, the children were already in the tree (but hidden).
In v1.5.0, the panel returns null when hidden, so SuspendingChild is never rendered until the panel is shown. This causes the issue:
- Initial render: Tab A selected
- Panel B returns null (hidden)
- No suspense
When user clicks tab B: 4. Panel B should render, but
mounted = false(one-render delay) 5. Panel B returns null 6. Next render: mounted = true 7. Panel B renders, SuspendingChild suspends 8. Suspense catches, unmounts Tabs.Root (because it was committed) 9. After recovery, value = "a" 10. Panel B returns nullSo the key difference is: in v1.4.1, the suspense happened during initial mount (state preserved), while in v1.5.0, the suspense happens after an update (state lost).
Now, the fix should ensure that the panel renders immediately when
openchanges, without the one-render delay. Or it should preserve the state across the Suspense boundary.The simplest fix is to change the logic so that when
openis true, the panel renders immediately (without waiting formountedto become true).Looking at the code:
const { mounted, transitionStatus, setMounted } = useTransitionStatus(open); const hidden = !mounted; // ... const shouldRender = keepMounted || mounted; if (!shouldRender) { return null; }The issue is that
mountedstarts asopen(from useState initialization), but on a re-render, it uses the stored state (which might befalse). ThensetMounted(true)is called, but it takes effect in the next render.The fix should be: when
openis true, always render the panel (ignoremounted). Or use a different approach to trackmounted.Actually, looking at
useTransitionStatusmore carefully:const [mounted, setMounted] = React.useState(open); if (open && !mounted) { setMounted(true); setTransitionStatus('starting'); }The
ifblock setsmounted = truewhenopenis true andmountedis false. Butmountedin this render is still the old value. The update takes effect in the next render.The fix could be to compute
mountedinline instead of using state:// Instead of: // const hidden = !mounted; // Use: const hidden = !open;But this would break the transition animation logic. The
mountedstate is used to track when the animation starts and ends.Actually, the fix should be to ensure that the panel renders when
openis true, regardless ofmounted. Themountedstate should only affect the animation, not whether the panel renders.Looking at the code:
const shouldRender = keepMounted || mounted; if (!shouldRender) { return null; }The issue is that
shouldRenderdepends onmounted, which has a one-render delay. The fix should be:const shouldRender = keepMounted || mounted || open; if (!shouldRender) { return null; }Wait, but that would render the panel even when
openis true but the animation hasn't started. That might be the intended behavior!Actually, looking at this more carefully, I think the issue is that when
openchanges from false to true, the panel should render immediately. Themountedstate is supposed to track whether the panel has been "shown" (for animation purposes), but it shouldn't prevent rendering.Let me look at the useTransitionStatus hook again:
export function useTransitionStatus( open: boolean, enableIdleState: boolean = false, deferEndingState: boolean = false, ) { const [transitionStatus, setTransitionStatus] = React.useState<TransitionStatus>( open && enableIdleState ? 'idle' : undefined, ); const [mounted, setMounted] = React.useState(open); if (open && !mounted) { setMounted(true); setTransitionStatus('starting'); } // ... return { mounted, setMounted, transitionStatus, }; }The
mountedstate is initialized toopen. Whenopenchanges from false to true:useStatereturns the oldmountedvalue (false)- The
ifblock callssetMounted(true) - But
mountedis still false in this render transitionStatusis set to 'starting'
So the intent is that on the first render when
openbecomes true:mountedis false (will be true in next render)transitionStatusis 'starting'
But the problem is that
mountedis used to determine whether to render the panel. Ifmountedis false, the panel returns null, even thoughopenis true andtransitionStatusis 'starting'.The fix should be: the panel should render when
openis true ORmountedis true. Themountedstate should only be used for the ending transition (when closing the panel).Looking at the TabsPanel code:
const shouldRender = keepMounted || mounted;This should be:
const shouldRender = keepMounted || mounted || open;Or even simpler, we should change the logic to:
const shouldRender = keepMounted || open || (transitionStatus === 'ending');This way, the panel renders when it's open, when it's kept mounted, or when it's in the ending transition (closing animation).
Actually wait, let me think about this more carefully. The
mountedstate is used for two purposes:- Track whether the panel should be in the DOM
- Determine the
hiddenattribute for accessibility
For the opening case:
- When
openbecomes true, the panel should be in the DOM mountedshould become true
For the closing case:
- When
openbecomes false, the panel should stay in the DOM during the closing animation mountedshould become true (so the panel is in the DOM)- After the animation completes,
setMounted(false)is called, removing the panel from the DOM
The issue is in the opening case. When
openbecomes true,setMounted(true)is called, but the state update takes effect in the next render. During this render,mountedis still false, soshouldRenderis false, and the panel returns null.The fix is simple: when
openis true,shouldRendershould be true, regardless ofmounted. Themountedstate should only be used to track the closing case.Let me also think about whether there's a fix in the
useTransitionStatushook instead. The issue is thatsetMounted(true)doesn't take effect until the next render. What if we change the hook to return the correctmountedvalue immediately?if (open && !mounted) { setMounted(true); setTransitionStatus('starting'); // Immediately return the updated value mounted = true; // But this won't work because mounted is const }Actually, we can't change the state value during the same render. The only way is to use a different variable.
const isEffectivelyMounted = open || mounted;But this changes the semantics of
mounted. It's no longer just about whether the panel has been shown - it's also about whether the panel is open.I think the cleanest fix is in TabsPanel.tsx:
const shouldRender = keepMounted || open || mounted;Or we can change
hidden:const hidden = !mounted && !open;But wait,
hiddenis also used for thearia-hiddenattribute and accessibility. Let me check whathiddenis used for.Looking at the code:
const hidden = !mounted; // ... hidden, // passed to state // ... hidden, // passed as attribute to the element // ... useIsoLayoutEffect(() => { if (hidden && !keepMounted) { return undefined; } // register the panel }, [hidden, ...]);So
hiddenis:- Part of the component state (for data-* attributes)
- The
hiddenHTML attribute (which hides the element) - Used to determine whether to register the panel
If we change
hiddento!mounted && !open, the panel would be registered even whenopenis true butmountedis false. This seems correct.But there's a subtlety: the
hiddenHTML attribute would be false whenopenis true, which means the element would be visible (not hidden). But ifmountedis false, we might not want to show it yet (for animation purposes).Hmm, this is getting complicated. Let me think about what the correct behavior should be.
When
openchanges from false to true:- The panel should be in the DOM (shouldRender = true)
- The panel should be visible (hidden = false) - or should it be hidden for the animation?
- The panel should be registered with TabsRoot
For the animation to work, the panel should be in the DOM but possibly with
data-starting-styleattribute. The panel should not be hidden via thehiddenattribute - instead, it should use CSS transitions.So the fix should be:
shouldRender = keepMounted || open || mounted- panel renders when open or mountedhidden = !mounted- this is fine for the animation (the panel is in the DOM buthiddenattribute might be used for initial state)
Wait, but the
hiddenattribute on the element would hide it even if it's in the DOM. Let me re-read the code:props: [ { 'aria-labelledby': correspondingTabId, hidden, // This sets the hidden attribute on the element id, role: 'tabpanel', tabIndex: open ? 0 : -1, inert: inertValue(!open), [TabsPanelDataAttributes.index as string]: index, }, elementProps, ],If
hiddenis true, the element has thehiddenattribute, which makes it not visible and not focusable. This is different fromdisplay: nonebecausehiddenalso removes it from the accessibility tree.So for the opening animation, we might want
hidden = trueinitially (to hide the panel), thenhidden = falseafter the animation completes.Actually, looking at the
transitionStatusMapping, the panel usesdata-starting-styleanddata-ending-stylefor animations, not thehiddenattribute. Thehiddenattribute is used to completely hide the panel when it's not mounted.OK, I think I understand now. The
hiddenattribute is used for:- When the panel is not mounted:
hidden = true - When the panel is mounted:
hidden = false
And the animation uses
data-starting-styleanddata-ending-stylewith CSS transitions.So the flow should be:
open = false: panel is not in the DOM (shouldRender = false)open = true: panel is added to the DOM (shouldRender = true),transitionStatus = 'starting',hidden = false(becausemountedis set to true)- User clicks to close:
open = false,transitionStatus = 'ending', panel stays in DOM - Animation completes:
setMounted(false), panel is removed from DOM
But the current code has a bug: in step 2,
mountedis still false (becausesetMounted(true)hasn't taken effect), soshouldRender = falseand the panel is not in the DOM.The fix is to ensure that when
openis true, the panel is in the DOM. The simplest fix is:const shouldRender = keepMounted || open || mounted;But wait, this would also make
hidden = !mountedshowhidden = trueeven whenopen = true. That's wrong - when the panel is opening, it should be visible (hidden = false).Actually, let me re-read the useTransitionStatus hook:
if (open && !mounted) { setMounted(true); setTransitionStatus('starting'); }When
openis true andmountedis false,setMounted(true)is called. This schedules an update that will take effect in the next render. But in the current render,mountedis still false.The hook is designed so that
mountedbecomes true synchronously (in the next render), andtransitionStatusbecomes 'starting'. The 'starting' status is used for CSS transitions.But the issue is that
mountedbeing false in the current render causesshouldRenderto be false, so the panel is not in the DOM.I think the fix should be in the
useTransitionStatushook to return the correctmountedvalue. We can use a local variable:const [internalMounted, setInternalMounted] = React.useState(open); const mounted = open || internalMounted;This way,
mountedis always true whenopenis true, regardless of the internal state.Or we can fix it in TabsPanel.tsx:
const shouldRender = keepMounted || open || mounted;I think the second fix is cleaner because it doesn't change the semantics of the
useTransitionStatushook.But wait, there's another issue. The
hiddenvariable:const hidden = !mounted;If
mountedis false (butopenis true), thenhiddenis true. The panel would be hidden via thehiddenattribute. But we want the panel to be visible when it's opening.So we also need to fix
hidden:const hidden = !mounted && !open;Hmm, but this might break the animation. The
hiddenattribute is used to determine whether the panel should be visible.Actually, looking at the code again, I think the
hiddenattribute on the element is set based onhiddenfrom the state, which is!mounted. But the animation usesdata-starting-styleanddata-ending-style. So the flow is:open = true,mountedis still falsehidden = !mounted = true- Panel is rendered with
hiddenattribute - But wait, if
shouldRender = false, the panel is not rendered at all!
So the issue is primarily with
shouldRender. Let me focus on that.The fix should be:
const shouldRender = keepMounted || mounted || open;And for
hidden, we should use:const hidden = !open && !mounted;Or actually, let me think about this more carefully. The
hiddenattribute on the element is set byuseRenderElement. Let me look at how it's used:props: [ { hidden, // ... }, elementProps, ],So
hiddenis passed as an intrinsic prop. Ifhiddenis true, the element has thehiddenHTML attribute.For the animation to work:
- When the panel opens, it should be in the DOM
- The
hiddenattribute should be false (panel is visible) - The
data-starting-styleattribute should be set for the CSS transition
But currently:
mounted = false(hasn't been set yet)hidden = true- Panel is hidden via the
hiddenattribute - Even if
shouldRender = true, the panel is hidden
So we need both fixes:
shouldRender = keepMounted || mounted || open- panel is in the DOMhidden = !open && !mounted- panel is not hidden when open
Wait, actually, let me re-read the code. The
hiddenis also used in the state object:const state: TabsPanelState = { hidden, orientation, tabActivationDirection, transitionStatus, };And it's used for the
data-hiddenattribute viastateAttributesMapping. Let me check what the mapping does:<tool_call> <function=bash_terminal> <parameter=command> cat /base-ui/packages/react/src/internals/stateAttributesMapping.ts </tool_call>
Assistant
I need to read the stateAttributesMapping file to understand how the
hiddenstate is mapped to attributes.Assistant
Now I understand the bug! The issue is in
TabsPanel.tsx. When a panel first opens (openchanges fromfalsetotrue), thesetMounted(true)call inuseTransitionStatusdoesn't take effect until the next render. During this render,mountedis stillfalse, soshouldRender = falseand the panel returnsnull.On the next render,
mountedbecomestrueand the panel renders. But if the panel's child suspends during this render, the Suspense boundary (outside Tabs.Root) catches it and unmounts the entire Tabs.Root. When React retries, Tabs.Root has fresh state with the default value, so the panel is no longer selected.The fix is to ensure the panel renders when
openis true, not just whenmountedis true:Assistant
I now understand the bug. The issue is in
TabsPanel.tsx. When a tab panel opens (openbecomestrue), theuseTransitionStatushook callssetMounted(true)during render, but the state update doesn't take effect until the next render. During the current render,mountedis stillfalse, soshouldRender = keepMounted || mountedevaluates tofalse, and the panel returnsnull.On the next render,
mountedbecomestrue, the panel renders, and if a child suspends, the Suspense boundary outside Tabs.Root catches it and unmounts the entire tree. After recovery, Tabs.Root remounts with fresh state.The fix is to ensure the panel renders when
openis true, not just whenmountedis true:Assistant
Now let me also fix the
hiddenvariable. Whenopenis true butmountedis still false (during the first render when opening), thehiddenattribute should befalseso the panel is visible and the opening animation can work correctly:Assistant
Let me also update the
hiddenvariable to ensure the panel is visible whenopenis true:Assistant
Now let me also fix the
hiddenvariable. Whenopenis true, the panel should not be hidden, even ifmountedis still false in the first render after opening:Assistant
Let me verify my changes are correct by viewing the file.
Assistant
Looking at this more carefully, my fix ensures that when
openbecomes true, the panel renders immediately in the same render cycle as the state update. This is critical because:Without the fix: The panel doesn't render until the NEXT render cycle (after
mountedbecomes true). By then, the state update has already been committed. When the child suspends, React unmounts the entire tree (since the Suspense boundary is outside Tabs.Root), and the state is lost.With the fix: The panel renders in the SAME render cycle as the state update. When the child suspends, React rolls back (not unmounts) because the state update hasn't been committed yet. After the promise resolves, React retries with the same pending state update, and the panel content is shown.
Let me verify the changes look correct:
Assistant
Let me verify the changes are correct and check if there are existing tests to run:
Assistant
Let me verify the full context of the changes:
Assistant
The changes look correct. Let me verify there are no issues by checking if any tests exist and can be run:
Assistant
The changes look correct. Let me verify the logic by analyzing the edge cases:
Before the fix:
- When
openbecomestrue,shouldRenderwaskeepMounted || mountedwhich evaluates tofalsebecausemountedis stillfalsein the same render cycle (state updates take effect in the next render) - This caused a one-render delay before the panel rendered
- When a child suspends during that delayed render, React unmounts the entire Tabs.Root (since the update was already committed), and state is lost
After the fix:
- When
openbecomestrue,shouldRender = keepMounted || open || mountedevaluates totrueimmediately - The panel renders in the same render cycle as the state update
- If a child suspends, React rolls back (doesn't unmount) because the state update hasn't been committed yet
- After the promise resolves, React retries with the pending state update, and the panel content is shown
Let me also verify the
hiddenchange handles all cases correctly:open=true, mounted=false:hidden=false✓ (panel visible when opening)open=true, mounted=true:hidden=false✓ (panel visible when open)open=false, mounted=true:hidden=false✓ (panel visible during closing animation)open=false, mounted=false:hidden=true✓ (panel hidden when closed)
- When
Assistant
Let me verify my fix is correct by checking if there are any edge cases I might have missed. Let me also run the existing tests to make sure I haven't broken anything.
Assistant
The TabsPanel tests pass. Let me also run the TabsRoot tests to make sure the changes don't break anything else.
Assistant
Excellent! All tests pass. Let me run all tabs-related tests to make sure everything is working:
Assistant
All 171 tests pass (38 skipped are likely browser-specific or animation tests). Let me also run a quick sanity check on other components that might use
useTransitionStatus:Assistant
The
useTransitionStatushook is used in several other components. My fix is specific to the TabsPanel and shouldn't affect other components. Let me also run some related tests to make sure the hook tests pass:Assistant
No specific tests for
useTransitionStatus. Let me check if there are any other tests that might be affected by my changes:Assistant
All tests pass! Let me now clean up the test file I created earlier and submit my changes.
Assistant
Let me verify the final state of my changes before submitting.
Assistant
My fix is complete. Let me summarize the changes:
Summary of Changes
File modified:
packages/react/src/tabs/panel/TabsPanel.tsxTwo changes were made:
-
Line 61: Changed
hidden = !mountedtohidden = !open && !mounted- This ensures the
hiddenattribute isfalsewhen the panel is opening, so the panel is visible during the opening animation.
- This ensures the
-
Line 117: Changed
shouldRender = keepMounted || mountedtoshouldRender = keepMounted || open || mounted- This ensures the panel renders immediately when
openbecomestrue, instead of waiting formountedto becometruein the next render cycle.
- This ensures the panel renders immediately when
Root cause: The
useTransitionStatushook callssetMounted(true)during render whenopenchanges fromfalsetotrue. However, state updates in React take effect in the next render, so during the current rendermountedis stillfalse. This caused a one-render delay before the panel rendered.When a child component suspends during that delayed render, the Suspense boundary (outside Tabs.Root) catches the suspension. Since the state update had already been committed (after the delayed render), React unmounts the entire Tabs.Root tree and loses all state. After recovery, the user had to click the tab again.
With this fix, the panel renders immediately when
openistrue. If a child suspends, React rolls back (not unmounts) because the state update hasn't been committed yet. After the promise resolves, React retries with the pending state update, and the panel content is shown correctly.All 171 tabs-related tests pass.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →
deepseek/deepseek-v4-proCompare side by side →
deepseek/deepseek-v4-proCompare side by side →
deepseek/deepseek-v4-proCompare side by side →