Let me look at this more carefully. The key issue is about what happens when a child component suspends inside a Tabs.Panel and the Suspense boundary is outside Tabs.Root.
Let me trace through the code flow:
- User clicks Tab B
setValue("b") is called (uncontrolled)
- React re-renders, Tab B's panel now has
open = true
useTransitionStatus(true) → mounted transitions from false to true
- Panel renders children →
SuspendingChild calls use(dataPromise) → suspends
- React catches the promise at the
<Suspense> boundary OUTSIDE Tabs.Root
- React shows the fallback, effectively unmounting the
Tabs.Root treeWhen the promise resolves:
- React re-renders the
<Suspense> boundary's children
Tabs.Root re-renders from scratch (since it was unmounted by the Suspense boundary)
But wait - when React re-renders after the Suspense resolves, does it re-mount the component from scratch?
Actually, in React 19, when a Suspense boundary shows its fallback, it unmounts the content tree. When the promise resolves, it re-mounts the content tree. So the Tabs.Root state is lost - defaultValue="a" takes effect again, and Tab A is selected.
But the issue says this is a regression from v1.4.1 to v1.5.0, so something changed.
Wait, actually I think the issue might be different. Let me re-read the issue more carefully:
"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.Root component), 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 problem is:
- Click Tab B
- Panel B content does NOT appear (but the tab appears selected)
- Click Tab B again → Panel B content appears
This means the value changes to "b" (Tab B appears selected), but the panel doesn't render its content. The second click somehow makes it work.
This is very different from what I was thinking. The Suspense doesn't necessarily unmount the whole tree. Let me think again...
When SuspendingChild suspends inside Tabs.Panel value="b":
- React looks for the nearest Suspense boundary
- The nearest boundary is outside
Tabs.Root
- React catches the promise at that boundary
Now, what happens? In React 19, when a component suspends during a re-render (not the initial render):
- If the Suspense boundary has already rendered content (which it has - the initial Tabs tree rendered fine), React will NOT show the fallback. Instead, it keeps the last successfully rendered content visible.
- The component that triggered the re-render (
setValue("b")) already committed its state change. So value is "b".
- But the render of the children (Panel B content) is abandoned because of the suspension.
So after clicking Tab B:
value = "b" (committed)
- Tab B appears selected
- Panel B has
open = true
- Panel B's
useTransitionStatus started: mounted was set to true
- But then the child suspended, and the render was abandoned
When the promise resolves:
- React retries the render
- Panel B:
open = true
- What is
mounted?
This is the key question. In useTransitionStatus:
const [mounted, setMounted] = React.useState(open);
if (open && !mounted) {
setMounted(true);
setTransitionStatus('starting');
}
On the first render when open changes to true, mounted is false, so setMounted(true) is called. This is a state update during render (the "bailout" pattern). React processes this synchronously and re-renders the component.
But then the child suspends. The question is: does React commit the mounted = true state?
In React, when a component suspends during render, the in-progress render is abandoned. The committed state remains what it was before the render started. So mounted would still be false.
When the promise resolves and React retries:
- Panel B renders with
open = true, mounted = false
setMounted(true) is called again
- React re-renders Panel B with
mounted = true
- Panel renders children
SuspendingChild calls use(dataPromise) → promise is resolved → returns value
- Panel B content renders
This should work! So why doesn't it?
Hmm, let me think about this more carefully. Maybe the issue is that setMounted(true) causes React to re-render, but the re-render happens in the same microtask, and the child suspends again...
No wait, the promise is already resolved at this point. So use(dataPromise) should return the resolved value.
Let me re-read the PR #4704 changes more carefully to find what changed.
Actually, wait. Let me re-read the issue description 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."
So clicking Tab B the first time → Tab B is selected but panel content is empty/missing.
Clicking Tab B the second time → Panel B content appears.
But if Tab B is already selected (value = "b"), clicking it again shouldn't trigger onValueChange because in TabsTab.tsx:
function onClick(event) {
if (active || disabled) {
return;
}
// ...
}
So the second click would be a no-op! Unless the value was reset to something else in between.
Hmm, so maybe the Suspense DOES cause the Tabs tree to unmount and remount. Let me reconsider.
Actually, I think the behavior in React 19 might be:
- Click Tab B →
setValue("b") → re-render
- Panel B starts rendering, child suspends
- React catches at Suspense boundary
- Since the boundary has existing committed content (the initial render), React keeps showing the old content
- BUT the state update (
setValue("b")) is committedWait, but then Tab B would be selected, and when the promise resolves, Panel B should render.
Unless there's something about useTransitionStatus that prevents the panel from rendering...
Actually, let me reconsider. Maybe the issue is NOT about React Suspense unmounting the tree, but about the useTransitionStatus hook behavior.
When Panel B first renders with open = true:
mounted = false (initial state from useState(open) where open was false on first render)
setMounted(true) → state update during render
- React re-renders Panel B with
mounted = true
shouldRender = keepMounted || mounted = true
- Panel renders children
- Child suspends
When React abandons the render:
mounted state... hmm, this is the question.
In React, setState during render (the "bailout" pattern) is processed synchronously. React applies the state change immediately within the current render pass. If the render pass completes, the state is committed. If the render pass is abandoned (due to Suspense), the state might be rolled back.
Actually, I think in React 19, when you call setState during render, it's like getDerivedStateFromProps. The state change is applied to the work-in-progress fiber. If the render completes, it's committed. If the render is abandoned, the work-in-progress is discarded, and the committed state remains unchanged.
So after the Suspense catches:
mounted in committed state = false
value in committed state = "b" (this was committed by the event handler, not during render)
When the promise resolves:
- React re-renders
- Panel B:
open = true, mounted = false
setMounted(true) → mounted = true
- Panel renders children
- Child resolves
- Content shows
This should work!
Wait, but what if the issue is more subtle? What if the Suspense boundary's behavior in React 19 is different from what I'm assuming?
Let me look at this from a completely different angle. Let me check if there's something in the useIsoLayoutEffect in TabsRoot that could cause issues.
Actually, I wonder if the problem is related to the commitAutomaticValueChange(null, REASONS.missing) call. Let me think about when tabMap.size === 0 could happen in the context of a Suspense cycle.
When the Suspense catches and the promise resolves, React re-renders the tree. But before the layout effects run, the tabMap might be empty because the CompositeList children haven't registered yet.
Wait, tabMap is React state in TabsRoot, not a ref. It persists across re-renders. But the CompositeList populates it via setTabMap in a layout effect. So after the initial render, tabMap has entries.
But if the Suspense boundary causes the Tabs tree to unmount and remount... hmm.
Actually, let me check: does React unmount the content of a Suspense boundary when the fallback is shown?
In React 19:
- If the Suspense boundary has no existing content (first render), it shows the fallback
- If the Suspense boundary has existing content, it keeps the existing content visible (doesn't show fallback)
So in our case:
- Initial render succeeds → Suspense boundary has content (Tabs tree)
- Click Tab B → re-render → child suspends
- Suspense boundary has existing content → keeps showing it
- State
value = "b" is committed
- When promise resolves → re-renders
So the Tabs tree is NOT unmounted. The state is preserved. value = "b".
But then Panel B should render when the promise resolves! Unless something prevents it.
Let me look at the useOpenChangeComplete hook more carefully:
useOpenChangeComplete({
open,
ref: panelRef,
onComplete() {
if (!open) {
setMounted(false);
}
},
});
When open changes from false to true, the effect fires. It waits for animations to finish, then calls onComplete. Since open is true, onComplete does nothing.
But what if the effect from the old render (where open was false) fires its cleanup and the onComplete has a stale closure?
Actually, onComplete is wrapped in useStableCallback, so it always reads the latest open value. So this shouldn't be an issue.
Hmm, let me look at useTransitionStatus one more time:
if (open && !mounted) {
setMounted(true);
setTransitionStatus('starting');
}
This runs during render. If the child suspends after this point, the setMounted(true) might or might not be committed.
Actually wait, I just realized: setMounted(true) is called during render. In React, calling setState during render is handled by React as a "state bailout" - React immediately re-renders the component with the new state. This is different from setState in an event handler.
So the sequence is:
- Panel B renders with
open = true, mounted = false
setMounted(true) → React immediately re-renders Panel B with mounted = true
- Panel B renders with
mounted = true → shouldRender = true
- Panel B renders children → child suspends
Now, the question: when the child suspends and React catches it at the Suspense boundary, does the mounted = true state persist?
I think the answer is YES, because setMounted(true) was processed as a render-phase state update (like getDerivedStateFromProps), which is applied to the work-in-progress fiber. When the Suspense catches, React doesn't roll back the work-in-progress state; it just doesn't commit the DOM changes.
Wait, actually, I think in React 19, when a component suspends during render:
- The render is abandoned
- State updates from event handlers are committed
- State updates from render-phase (like
setMounted(true) during render) are NOT committed - they're part of the abandoned work-in-progress
Hmm, I'm not sure about this. Let me think differently.
Actually, in React, calling setState during render is treated like getDerivedStateFromProps. The state change is applied to the work-in-progress fiber immediately. If the render completes, it's committed. If the render is abandoned, the work-in-progress is discarded.
So after the Suspense catches:
mounted = false (last committed state)
value = "b" (committed from event handler)
When the promise resolves:
- React re-renders
- Panel B:
open = true, mounted = false
setMounted(true) → React re-renders Panel B with mounted = true
- Panel renders children
- Child resolves (promise already resolved)
- Content shows
This should work! So what's wrong?
Let me look at the TabsRoot code more carefully. Maybe there's something in the layout effect that interferes.
Wait, I just had a thought. What if the issue is about useOpenChangeComplete and the animation flow?
When Panel B opens (open changes to true):
useTransitionStatus sets mounted = true, transitionStatus = 'starting'
useOpenChangeComplete effect fires
- It calls
runOnceAnimationsFinish(onComplete, ...)
- The
useAnimationsFinished hook:
const element = resolveRef(elementOrRef);
if (element == null) {
return;
}
If the panel element doesn't exist yet (because the Suspense interrupted the render before the DOM was committed), then `element` is `null` and the function returns immediately.
But wait, if the render was abandoned, the effect wouldn't have been committed either. Effects only run after a successful commit.
Hmm, let me reconsider. When does the render complete vs get abandoned?
Sequence:
1. User clicks Tab B → `setValue("b")` (event handler, state committed)
2. React starts re-rendering TabsRoot with `value = "b"`
3. Panel B renders, `setMounted(true)` is called during render
4. Panel B renders children, child suspends
5. React finds the Suspense boundaryNow, does React commit the render up to this point, or abandon it entirely?
In React 19, when a component suspends during render:
- If the Suspense boundary has existing content (our case), React keeps the existing content
- The new render is NOT committed - it's abandoned
- When the promise resolves, React retries the render from where it left off
So the state from step 3 (`mounted = true`) is NOT committed. `mounted` remains `false`.
When the promise resolves, React retries:
1. Panel B: `open = true`, `mounted = false`
2. `setMounted(true)` → `mounted = true`
3. Panel renders children
4. Child resolves (promise already resolved)
5. Content shows
This should work! Unless there's something else going on.
Wait, let me look at the actual React behavior for `use()` more carefully. When `use()` suspends:
1. React throws the promise
2. React walks up the tree to find the nearest Suspense boundary
3. If the boundary has no fallback (or has already committed content), the render is abandoned
4. The promise is tracked
5. When the promise resolves, React re-renders from the Suspense boundary
But step 3 - what exactly happens? Does React keep the committed DOM? Yes. Does it commit any new state? No, the render was abandoned.
OK so after the first click:
- `value` = "b" (from event handler)
- `mounted` = `false` (render was abandoned)
- DOM shows old content (Panel A)
When the promise resolves:
- React re-renders from Suspense boundary
- TabsRoot renders with `value = "b"`
- Panel B: `open = true`, `mounted = false`
- `setMounted(true)` → `mounted = true`
- Panel renders children
- Child resolves
- Content shows
This should work!
Unless... the Suspense boundary behavior is different. Let me re-read the React 19 docs on Suspense.
Actually, I think I may have been wrong. In React 19, when a component suspends and the Suspense boundary already has committed content:
- The committed content stays visible (this is correct)
- But React might NOT abandon the render entirely - it might keep the in-progress state
Hmm, I'm going in circles. Let me try to actually reproduce the issue by running the test.
But wait, the test might not work because JSDOM doesn't support `use()` properly. Let me check.
Actually, let me think about this from a completely different angle. Maybe the issue is not about Suspense at all, but about the `useTransitionStatus` hook's behavior with the new `useIsoLayoutEffect` logic.
Let me look at the sequence of events more carefully:
1. Initial render: `value = "a"`, Panel A renders2. Click Tab B → `setValue("b")`
3. React re-renders TabsRoot
4. In the layout effect:
- `tabMap.size > 0`
- `selectedTabMetadata` for "b" exists
- `selectionIsDisabled = false`
- `selectionIsMissing = false`
- `shouldNotifyInitialValueChange = false` (hasExplicitDefaultValueProp is true)
- Nothing happens, value stays "b"
5. Panel B: `open = true`, `setMounted(true)`, renders children
6. Child suspends
7. Suspense catches
But wait, step 4 happens in a layout effect, which runs AFTER the render. And step 5 happens during the render. So the order is:
1. React re-renders TabsRoot with `value = "b"`
2. Panel B renders: `open = true`, `setMounted(true)`, renders children
3. Child suspends
4. Layout effects don't run (render was abandoned)
When the promise resolves:
5. React re-renders from Suspense boundary
6. TabsRoot renders with `value = "b"`
7. Panel B renders: `open = true`, `mounted = false` (from abandoned render), `setMounted(true)`
8. Panel renders children
9. Child resolves
10. Layout effects run:
- CompositeList registers children
- setTabMap is called
- TabsRoot layout effect runsIn the layout effect (step 10):
- `tabMap.size` - what is it? It's the state from the LAST COMMITTED render. The last committed render had `tabMap` with entries (from the initial mount). So `tabMap.size > 0`.
- `selectedTabMetadata` for "b" exists
- Everything is fine
So this should work!
OK, I think I need to actually try something. Let me look at the code path one more time and see if there's something I'm missing.
Actually, wait. Let me re-read the issue title: "Tabs.Panel content not rendered if a child component suspends without boundary within Tabs.Root"
"without boundary within Tabs.Root" - meaning there's no Suspense boundary inside Tabs.Root. The nearest boundary is outside.
And "the suspense unmounts the tab root" - the author says the Suspense unmounts the tab root. But in React 19, when the Suspense boundary has existing content, it shouldn't unmount it.
Unless... the behavior is different for the initial render vs re-renders. Let me think...
Actually, wait. The reproduction has:
```jsx
<Suspense fallback={<div>Loading…</div>}>
<Tabs.Root defaultValue="a">
On initial render, Tabs.Root renders successfully with Tab A selected. The Suspense boundary has committed content.
When Tab B is clicked and the child suspends:
- The Suspense boundary already has committed content
- React should keep the committed content visibleBut the author says "the suspense unmounts the tab root". Maybe the author is wrong about the mechanism, and the actual issue is different.
Let me look at the reproduction more carefully. The dataPromise is created once at module level:
const dataPromise = new Promise<string>((resolve) =>
setTimeout(() => resolve('Panel B is rendered'), 500)
);
This means the promise resolves after 500ms. If the user clicks Tab B within 500ms, the child suspends. After 500ms, the promise resolves and React retries.
But in the test environment, everything is synchronous. The setTimeout won't actually wait 500ms.
Hmm, let me look at this from the perspective of the useTransitionStatus hook one more time.Actually, I just realized something. When Panel B renders with open = true and calls setMounted(true), this is a synchronous state update during render. React processes it immediately and re-renders Panel B with mounted = true. Then Panel B renders its children.
But setTransitionStatus('starting') is also called. And there's this code in useTransitionStatus:
useIsoLayoutEffect(() => {
if (!open || enableIdleState) {
return undefined;
}
const frame = AnimationFrame.request(() => {
setTransitionStatus(undefined);
});
return () => {
AnimationFrame.cancel(frame);
};
}, [enableIdleState, open]);
This runs when open changes to true. It schedules a setTransitionStatus(undefined) on the next animation frame. But this is a layout effect, which only runs after the render is committed. If the render is abandoned (due to Suspense), this effect doesn't run.
When the promise resolves and React retries:
open = true, mounted = false (from abandoned render)
setMounted(true) → mounted = true
setTransitionStatus('starting') → transitionStatus = 'starting'
- Panel renders children
- Child resolves
- Content shows
But wait, transitionStatus = 'starting' might cause the data-starting-style attribute to be applied, which could hide the content via CSS!
Looking at the state attributes mapping:
const stateAttributesMapping: StateAttributesMapping<TabsPanelState> = {
...tabsStateAttributesMapping,
...transitionStatusMapping,
};
And transitionStatusMapping:
export const transitionStatusMapping = {
transitionStatus(value: TransitionStatus) {
return {
'data-ending-style': value === 'ending' ? '' : undefined,
'data-starting-style': value === 'starting' ? '' : undefined,
};
},
};
So when transitionStatus = 'starting', the element gets data-starting-style attribute. This is used for CSS transitions. If the user has CSS that hides elements with data-starting-style, the content would be hidden.
But the issue says the content is NOT rendered at all, not that it's hidden by CSS. And the reproduction code doesn't have any CSS.
Hmm, but there's this in useTransitionStatus:
useIsoLayoutEffect(() => {
if (!open || enableIdleState) {
return undefined;
}
const frame = AnimationFrame.request(() => {
// Avoid `flushSync` here due to Firefox.
setTransitionStatus(undefined);
});
return () => {
AnimationFrame.cancel(frame);
};
}, [enableIdleState, open]);
After the render commits (when the promise resolves), this layout effect runs. It schedules setTransitionStatus(undefined) on the next animation frame. This would remove the data-starting-style attribute.
But the useOpenChangeComplete hook:
useOpenChangeComplete({
open,
ref: panelRef,
onComplete() {
if (!open) {
setMounted(false);
}
},
});
This waits for animations to finish. Since there are no CSS animations in the reproduction, it should resolve immediately.
Wait, but useAnimationsFinished has:
if (typeof resolvedElement.getAnimations !== 'function' || globalThis.BASE_UI_ANIMATIONS_DISABLED) {
fnToExecute();
return;
}
In JSDOM, getAnimations might not be a function, so fnToExecute() is called immediately. And fnToExecute is:
const done = () => {
ReactDOM.flushSync(fnToExecute);
};
Wait, that's not right. fnToExecute is the onComplete callback. And done calls fnToExecute via flushSync.
So onComplete is called via flushSync. Since open is true, onComplete does nothing.
OK so this shouldn't cause issues.
Let me try yet another angle. What if the issue is that when the Suspense boundary re-renders after the promise resolves, the TabsRoot re-renders, and the useIsoLayoutEffect fires with a stale tabMap?
Actually, I wonder if there's a subtle issue with how CompositeList populates tabMap during a Suspense re-render. Let me trace through:
After the promise resolves:
- React re-renders from Suspense boundary
- TabsRoot renders with
value = "b"
- CompositeList renders
- Tab components render and register via
useCompositeListItem
- Panel B renders with
open = true, mounted = false
setMounted(true) → mounted = true
- Panel renders children → child resolves → content shows
Then layout effects fire:
8. Tab components' layout effects: register with CompositeList
9. CompositeList's layout effect: onMapChange(sortedMap) → setTabMap(sortedMap)
10. TabsRoot's layout effect: ...
In step 10, tabMap is the state from the PREVIOUS render (before this one). It should have entries from the initial mount.
But wait, setTabMap was called in step 9 with the NEW sorted map. This triggers a state update, which means another re-render. But the layout effect in step 10 runs with the OLD tabMap.
Hmm, but the OLD tabMap should still have entries. So tabMap.size > 0 and everything is fine.
OK I'm going to try a completely different approach. Let me look at what the commitAutomaticValueChange(null, REASONS.missing) call does and when it could be triggered.
if (tabMap.size === 0) {
if (!didRegisterTabsRef.current || value === null) {
return;
}
commitAutomaticValueChange(null, REASONS.missing);
return;
}
For this to fire:
tabMap.size === 0 - the tab map is empty
didRegisterTabsRef.current === true - tabs were previously registered
value !== null - there's a selected value
When could tabMap be empty after being populated? This could happen if:
- The
CompositeList was unmounted and remounted
- The
setTabMap(new Map()) was called
But tabMap is React state in TabsRoot. It's only modified by setTabMap. And setTabMap is called from CompositeList's onMapChange.
Wait, but what if during a Suspense cycle, the CompositeList re-renders and calls onMapChange with an empty map? That could happen if the tab elements are not yet registered.
Actually, looking at CompositeList:
const sortedMap = React.useMemo(() => {
disableEslintWarning(mapTick);
const newMap = new Map<Element, CompositeMetadata<Metadata>>();
const sortedNodes = Array.from(map.keys())
.filter((node) => node.isConnected)
.sort(sortByDocumentPosition);
sortedNodes.forEach((node, index) => {
const metadata = map.get(node) ?? ({} as CompositeMetadata<Metadata>);
newMap.set(node, { ...metadata, index });
});
return newMap;
}, [map, mapTick]);
The map is a ref that persists across re-renders. The mapTick is a state variable that triggers re-computation. The sorted map is computed from the ref, so it should have entries as long as the elements are connected.
But wait, what if the elements are NOT connected? If the Suspense boundary replaced the content with the fallback, the elements would be disconnected, and sortedNodes would be empty. But I said the Suspense boundary keeps existing content...
Hmm, let me reconsider. Actually, in React 19, when a component suspends during a re-render and the Suspense boundary has existing content:
- The existing committed content stays in the DOM
- The new render is abandoned
- When the promise resolves, React re-renders from the Suspense boundary
The existing content includes the tab elements, which are connected. So sortedNodes should have entries.
But when React re-renders from the Suspense boundary (after the promise resolves), it creates a NEW tree. The old tree is replaced. During this process, the elements might briefly be disconnected.
Actually, I don't think that's how it works. React re-renders the existing tree, it doesn't create a new one.
OK, I'm going to take a step back and think about what the actual fix should be.
The issue says PR #4704 is the cause. The key change in PR #4704 is the addition of commitAutomaticValueChange(null, REASONS.missing) when tabMap.size === 0 and didRegisterTabsRef.current === true and value !== null.
The fix would be to NOT call commitAutomaticValueChange(null, REASONS.missing) in this case, or to handle it differently.
But wait, the commitAutomaticValueChange(null, ...) is supposed to handle the case where the selected tab is missing (removed from the DOM). In that case, the value should be reset to null.
But during a Suspense cycle, the tabs might temporarily be "missing" (not registered yet) even though they exist. This could trigger the false positive.
Let me think about the exact scenario:
- Initial render: tabs register,
tabMap has entries, didRegisterTabsRef = true
- Click Tab B →
setValue("b")
- Panel B renders, child suspends
- Suspense catches → render abandonedWhen the promise resolves:
- React re-renders from Suspense boundary
- TabsRoot renders
- The
useIsoLayoutEffect runs
- What is
tabMap at this point?
tabMap is React state. In step 5, when React re-renders, it uses the committed state. The committed tabMap has entries from the initial render. So tabMap.size > 0.
But wait, does the CompositeList's onMapChange get called during the re-render? No, it's called in a layout effect, which runs after the render. So during the render, tabMap has the old entries.
And the layout effect checks tabMap.size. Since tabMap has entries, tabMap.size > 0, and the tabMap.size === 0 branch is NOT taken.
So commitAutomaticValueChange(null, ...) is NOT called. Everything should be fine.
I'm really confused about what the actual issue is. Let me try running the test to see if I can reproduce it.
Actually, wait. 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"
What if the issue is not about the value being reset, but about the panel rendering? What if Panel B's shouldRender is false even though open is true?
shouldRender = keepMounted || mounted
If mounted is false, shouldRender is false. But setMounted(true) should be called when open changes to true.
Unless... mounted was already true from a previous render, and then something set it back to false.
Wait, let me look at the useOpenChangeComplete hook again:
onComplete() {
if (!open) {
setMounted(false);
}
},
This only sets mounted = false when !open. So if open is true, this shouldn't fire.
But what about the animation flow? Let me trace through what happens when the Suspense catches:
- Panel B renders with
open = true
setMounted(true) → mounted = true
setTransitionStatus('starting') → transitionStatus = 'starting'
- Panel renders children → child suspends
- Render abandoned
When the promise resolves:
6. React re-renders
7. Panel B: open = true, mounted = false (from abandoned render)
8. setMounted(true) → mounted = true
9. setTransitionStatus('starting') → transitionStatus = 'starting'
10. Panel renders children → child resolves → content shows
Then the layout effects run:
11. useIsoLayoutEffect for transition status:
js const frame = AnimationFrame.request(() => { setTransitionStatus(undefined); });
This schedules setTransitionStatus(undefined) on the next animation frame.
useOpenChangeComplete effect:
runOnceAnimationsFinish(onComplete, abortController.signal);
This waits for animations. Since there are no CSS animations, onComplete is called immediately via flushSync.
onComplete: if (!open) → open is true → does nothing.
So after the layout effects, mounted = true, transitionStatus = 'starting'. The content should be visible.
Then on the next animation frame:
13. setTransitionStatus(undefined) → transitionStatus = undefined
Everything seems fine!
Unless... the useOpenChangeComplete effect from the PREVIOUS render (before the Suspense) fires its cleanup and does something unexpected.
When the Suspense catches (step 5):
- The render is abandoned
- Effects from the abandoned render are NOT committed
- The effects from the PREVIOUS committed render are still active
The previous committed render had open = false (Panel B was not open). The useOpenChangeComplete effect with open = false is still active. Its onComplete checks if (!open) → true → setMounted(false).
But onComplete is only called when animations finish. And the effect was waiting for the "close" animation to finish. If Panel B was never open before, there are no close animations, so onComplete might be called immediately.
Wait, but Panel B was never rendered with open = true before (it was hidden). So the useOpenChangeComplete effect with open = false would have run on the initial render, and since there are no animations, onComplete would have been called immediately. But open is false, so setMounted(false) would be called. But mounted is already false, so this is a no-op.
Hmm, but what about the useStableCallback wrapping? onComplete always reads the latest open value. So if open changes to true after the effect ran, the callback would still see open = false at the time it was called.
Wait, no. useStableCallback creates a stable reference that always calls the latest version of the callback. So if the callback is called after open changes, it would see open = true.
But the effect's cleanup would abort the callback. And the new effect (with open = true) would set up a new callback.
OK, I think the issue might be more subtle. Let me look at what happens when:
- Initial render: Panel B is not open,
mounted = false
useOpenChangeComplete effect runs with open = false
runOnceAnimationsFinish(onComplete, ...) is called
- No animations →
onComplete is called immediately
onComplete: if (!open) → open is false → setMounted(false) → no-op (already false)
When Tab B is clicked:
6. setValue("b") → re-render
7. Panel B: open = true, mounted = false
8. setMounted(true) → mounted = true
9. Panel renders children → child suspends
10. Render abandoned
At this point, the useOpenChangeComplete effect from step 2 is still active (since the render in step 6-9 was abandoned, the effect cleanup didn't run).
When the promise resolves:
11. React re-renders
12. Panel B: open = true, mounted = false
13. setMounted(true) → mounted = true
14. Panel renders children → child resolves → content shows
Then effects run:
15. Old effect (from step 2) cleanup: abortController.abort()
16. New effect setup: runOnceAnimationsFinish(onComplete, ...)
17. No animations → onComplete called → if (!open) → open is true → does nothing
This should be fine!
But wait, what if the old effect (step 2) fires its callback AFTER the new render (step 11) but BEFORE the new effect (step 16) runs? The old effect's onComplete might read the NEW open = true value (because of useStableCallback) and... not do anything. That's fine.
OK, I really can't figure out the issue from pure code analysis. Let me try running the test.
Actually, hold on. Let me think about this one more time. Maybe the issue is NOT about useTransitionStatus or useOpenChangeComplete, but about the useIsoLayoutEffect in TabsRoot.
When the promise resolves and React re-renders:
- TabsRoot renders with
value = "b"
- Children render
- Layout effects fire
In the layout effect:
if (tabMap.size === 0) {
if (!didRegisterTabsRef.current || value === null) {
return;
}
commitAutomaticValueChange(null, REASONS.missing);
return;
}
What if tabMap is empty at this point? This could happen if the CompositeList hasn't had a chance to populate tabMap yet.
But tabMap is React state. It was set during the initial render. Unless the Suspense cycle caused it to be reset...
Actually, wait. Let me look at how setTabMap is called:
In TabsRoot:
const [tabMap, setTabMap] = React.useState(
() => new Map<Node, CompositeMetadata<TabsTab.Metadata> | null>(),
);
And setTabMap is passed to CompositeList via context. The CompositeList calls setTabMap in its useIsoLayoutEffect:
useIsoLayoutEffect(() => {
// ...
onMapChange(sortedMap);
}, [onMapChange, sortedMap, elementsRef, labelsRef, mapTick]);
This runs AFTER the render is committed. So during the render, tabMap has the value from the last committed state.
Now, when the Suspense catches (step 10 above), the render is abandoned. The last committed state has tabMap with entries from the initial render.
When the promise resolves and React re-renders:
tabMap still has entries from the initial render
tabMap.size > 0
- The
tabMap.size === 0 branch is NOT taken
So commitAutomaticValueChange(null, ...) is NOT called.
Hmm, but what if the CompositeList was unmounted and remounted during the Suspense cycle? In that case, the map ref would be reset, and the sortedMap would be empty. But setTabMap wouldn't be called until the layout effect runs. So tabMap would still have the old entries.
Wait, but if the CompositeList is inside TabsRoot, and TabsRoot is inside the Suspense boundary, then when the Suspense catches, the CompositeList is also suspended. When the promise resolves, the CompositeList re-renders. But the map ref in CompositeList is created with useRefWithInit, so it persists across re-renders.
Unless the Suspense boundary causes the entire tree to unmount and remount. In that case, the CompositeList would be a new instance, and its map ref would be empty.
But I said earlier that in React 19, when the Suspense boundary has existing content, it doesn't unmount it. So the CompositeList instance persists, and its map ref still has entries.
OK, I think the issue might be something I'm not seeing. Let me try to reproduce it.
Actually, wait. Let me re-read the issue one more time:
"Potentially a side effect of https://github.com/mui/base-ui/pull/4704"
PR #4704 title: "[tabs] Fire onValueChange for automatic tab selection (#4704)"
The PR added the commitAutomaticValueChange and notifyAutomaticValueChange logic. The key change is in the useIsoLayoutEffect.
Let me look at the diff one more time, focusing on the specific code that could cause issues:
In the old code (v1.4.1):
useIsoLayoutEffect(() => {
if (isControlled || tabMap.size === 0) {
return;
}
// ... handle disabled/missing tabs
});
In the new code (v1.5.0):
useIsoLayoutEffect(() => {
if (isControlled) {
return;
}
// ...
if (tabMap.size === 0) {
if (!didRegisterTabsRef.current || value === null) {
return;
}
commitAutomaticValueChange(null, REASONS.missing);
return;
}
didRegisterTabsRef.current = true;
// ... handle disabled/missing tabs
});
The new code has a new behavior: when tabMap.size === 0 and didRegisterTabsRef.current === true and value !== null, it calls commitAutomaticValueChange(null, REASONS.missing), which sets value = null.
Now, when could tabMap.size === 0 AND didRegisterTabsRef.current === true?
didRegisterTabsRef.current is set to true in the layout effect when tabMap.size > 0. So once tabs are registered, it stays true.
But tabMap is React state. If the component unmounts and remounts, tabMap would be reset to empty, and didRegisterTabsRef would be reset to false.
So the only way tabMap.size === 0 AND didRegisterTabsRef.current === true is if tabMap was cleared but the component wasn't unmounted.
When could tabMap be cleared? Only if setTabMap(new Map()) is called. And setTabMap is called from CompositeList's onMapChange. If CompositeList calls onMapChange with an empty map, tabMap would be empty.
When would CompositeList have an empty map? If no children are registered. But the tab elements should be registered...
Wait, I just realized something. The CompositeList uses map which is a ref. When children mount, they call register which adds to map. When children unmount, they call unregister which removes from map. The sortedMap is computed from map in a useMemo that depends on mapTick.
If all children unregister (e.g., because they unmount), map would be empty, and sortedMap would be empty. Then onMapChange(sortedMap) would be called with an empty map, and setTabMap(new Map()) would set tabMap to empty.
But the children shouldn't all unregister unless they all unmount. And they shouldn't unmount unless the Suspense boundary causes them to.
Hmm, but if the Suspense boundary DOES cause the tree to unmount (which I've been debating), then the children would unregister, and tabMap would become empty.
Let me reconsider: does React 19 unmount the content of a Suspense boundary when showing the fallback?
Actually, I think I've been wrong about React 19 behavior. Let me check:
In React 19:
- When a component suspends, the nearest Suspense boundary shows its fallback
- The content of the Suspense boundary IS unmounted (effects cleanup runs)
- When the promise resolves, the content is re-mounted (effects setup runs)
But wait, there's a subtlety. In React 18+, when a Suspense boundary already has committed content and a child suspends during a re-render:
- If the Suspense boundary has a
fallback prop, it shows the fallback (unmounts content)
- The committed content is replaced by the fallback
Wait, actually I think the behavior is:
- React 18+: When a component suspends during render, React shows the nearest Suspense boundary's fallback. This replaces any existing content.
- React 19: Same behavior.
So the Tabs tree IS unmounted when the child suspends! And when the promise resolves, the tree is re-mounted from scratch.
If the tree is re-mounted:
defaultValue = "a" → value = "a"
- Tab A is selected
- Panel A renders
The user would need to click Tab B again. And since the promise is already resolved, it works on the second click.
But this behavior should be the same in v1.4.1 and v1.5.0! The Suspense behavior is a React thing, not a Base UI thing.
Unless... in v1.5.0, something in the TabsRoot layout effect causes the value to be set to something unexpected during the re-mount.
Let me trace through the re-mount in v1.5.0:
- TabsRoot mounts with
defaultValue = "a", value = "a"
tabMap is empty (initial state)
didRegisterTabsRef.current = false (initial ref)
- Children render
- Layout effects run:
a. Tab components register with CompositeList
b. CompositeList:
onMapChange(sortedMap) → setTabMap(sortedMap)
c. TabsRoot layout effect: tabMap.size === 0 (old state), didRegisterTabsRef.current = false → returns early
- Re-render triggered by
setTabMap
tabMap now has entries
- Layout effects run:
a. TabsRoot layout effect:
tabMap.size > 0 → didRegisterTabsRef.current = true
b. selectedTabMetadata for "a" exists
c. Everything is fine, value stays "a"
Then the user clicks Tab B:
9. setValue("b") → re-render
10. Panel B renders, child resolves (promise already resolved)
11. Panel B content shows
This should work!
But wait, what if during the re-mount (step 1-8), the layout effect in step 5c doesn't return early because didRegisterTabsRef.current is true?
No, didRegisterTabsRef.current is false on a fresh mount. It's initialized with React.useRef(false).
Unless... the ref persists across Suspense boundary unmount/remount cycles? No, refs are component instance-specific. When the component unmounts, the ref is garbage collected. When it re-mounts, a new ref is created with false.
So didRegisterTabsRef.current is false on re-mount. The tabMap.size === 0 branch returns early.
Everything seems fine!
OK, I think the issue might be about the timing of when setTabMap is called and when the layout effect runs. Let me think about this more carefully.
Actually, wait. I just realized something. setTabMap is called from CompositeList's layout effect, which runs BEFORE TabsRoot's layout effect (because child effects run before parent effects). And setTabMap is a state update, which means it triggers a re-render. But the re-render doesn't happen until after all layout effects in the current commit have finished.
So the sequence is:
- Render: TabsRoot renders with
value = "a", tabMap = {} (empty)
- Commit: DOM is updated
- Layout effects (children first):
a. Tab components register with CompositeList
b. CompositeList layout effect:
setTabMap(sortedMap) → state update queued
c. TabsRoot layout effect: tabMap.size === 0 (old state), didRegisterTabsRef = false → returns early
- React processes queued state updates → re-render
- Render: TabsRoot renders with
value = "a", tabMap = {entries}
- Commit: DOM is updated
- Layout effects:
a. TabsRoot layout effect:
tabMap.size > 0, didRegisterTabsRef = true
b. Everything fine
This is the normal flow. The issue must be about a different timing.
Actually, I just had another thought. What if the issue is not about the tabMap at all, but about the selectedTabMetadata?
When the promise resolves and React re-renders:
- TabsRoot renders with
value = "b"
selectedTabMetadata is computed from tabMap and value
- If
tabMap has entries, selectedTabMetadata for "b" exists4. But what if tabMap is stale?
Hmm, tabMap is React state. When React re-renders after the Suspense resolves, it uses the committed state. The committed tabMap has entries from the initial render. So selectedTabMetadata should be found.
But wait, what if the Suspense boundary causes the entire tree to unmount, and on re-mount, tabMap is empty? Then selectedTabMetadata would be undefined, and selectionIsMissing would be true (since value = "a" is not null).
Wait, no, on re-mount, value is "a" (from defaultValue). And selectedTabMetadata for "a" would be... well, if tabMap is empty, it would be undefined. So selectionIsMissing = undefined == null && "a" !== null = true && true = true.
Then the layout effect would:
if (selectionIsDisabled || selectionIsMissing) {
const fallbackValue = firstEnabledTabValue ?? null;
if (value === fallbackValue) {
shouldNotifyInitialValueChangeRef.current = false;
return;
}
// ...
commitAutomaticValueChange(fallbackValue, fallbackReason);
return;
}
firstEnabledTabValue would be undefined (no tabs registered yet). So fallbackValue = null.
value = "a", fallbackValue = null. value !== fallbackValue. So commitAutomaticValueChange(null, ...) is called, setting value = null!
But wait, this only happens if tabMap.size > 0 AND selectionIsMissing. If tabMap.size === 0, the earlier check would return early (since didRegisterTabsRef = false).
And selectionIsMissing depends on selectedTabMetadata, which depends on tabMap. If tabMap is empty, selectedTabMetadata is undefined. But we already handled tabMap.size === 0 above, so we wouldn't reach this code.
OK, I think the flow is correct. Let me try yet another approach.
Actually, let me re-read the issue description one more time. It says:
"Potentially a side effect of https://github.com/mui/base-ui/pull/4704"
And:
"The only affected version is v1.5.0"
"v1.4.1 is not affected"
So the issue is specifically about v1.5.0. And PR #4704 is the suspected cause.
Let me look at the PR #4704 diff one more time and focus on what could cause the panel to not render:
The key change is:
if (tabMap.size === 0) {
if (!didRegisterTabsRef.current || value === null) {
return;
}
commitAutomaticValueChange(null, REASONS.missing);
return;
}
If this code runs with tabMap.size === 0, didRegisterTabsRef.current === true, and value !== null, it sets value to null. This would cause no panel to be shown.
For this to happen in the Suspense scenario:
tabMap must be empty
didRegisterTabsRef.current must be true
value must not be null
Could this happen when the Suspense resolves and React re-renders?
After the Suspense resolves:
tabMap has entries from the committed state (not empty)
didRegisterTabsRef.current is true
value is "b"
So tabMap.size > 0, and the tabMap.size === 0 branch is NOT taken.
But what if the Suspense boundary causes the tree to unmount, and on re-mount:
tabMap is empty (new state)
didRegisterTabsRef.current is false (new ref)
value is "a" (default)
So tabMap.size === 0 AND didRegisterTabsRef.current === false → returns early. No issue.
Hmm, I'm really stuck. Let me just try to run the test and see what happens.
Actually, wait. I just thought of something. What if the issue is that when the Suspense catches, React unmounts the Tabs tree, and the unmounting triggers the layout effect cleanup? And the cleanup does something that causes issues?
Let me look at the layout effect cleanup:
useIsoLayoutEffect(() => {
// ... the main logic
}, [...]);
There's no explicit cleanup in this layout effect. But the dependencies include setValue, notifyAutomaticValueChange, etc. These are stable callbacks, so they don't change.
Hmm, but what about the useIsoLayoutEffect in useTransitionStatus?
useIsoLayoutEffect(() => {
if (!open || enableIdleState) {
return undefined;
}
const frame = AnimationFrame.request(() => {
setTransitionStatus(undefined);
});
return () => {
AnimationFrame.cancel(frame);
};
}, [enableIdleState, open]);
When the component unmounts (due to Suspense), this cleanup runs: AnimationFrame.cancel(frame). This is fine.
And:
useIsoLayoutEffect(() => {
if (!open || !enableIdleState) {
return undefined;
}
// ...
}, [enableIdleState, open, mounted, transitionStatus]);
This one has mounted and transitionStatus in the dependencies. When the component unmounts, the cleanup runs. Fine.
OK, I really can't figure out the issue from pure analysis. Let me try to run the test.
But wait, I need to think about whether the test environment supports use(). The JSDOM environment might not support it properly.
Actually, React.use() is a React API, not a DOM API. It should work in JSDOM as long as React 19 is being used.
Let me try running the test.