Repository navigation
Description
Summary
When a <ViewTransition> animates an inline element whose getClientRects() returns multiple rects with exactly one non-zero rect (for example, an inline <span> wrapping a single block child), applyViewTransitionName temporarily sets style.display = 'block' and negative marginTop/marginBottom to work around WebKit bug 290923, but restoreViewTransitionName only cleans up when style.display === 'inline-block'.
As a result, display: block and the negative vertical margins remain permanently written onto the element's inline style after the transition finishes. A plain single-line <span> (which takes the inline-block branch) cleans up properly.
Affected version
[email protected],[email protected](production build in Chrome 158 and headless Chromium)- Verified unchanged on
mainatedae846a9a17442c00429f2b15375afbfa0376c9
Reproduction
Bundle and open this component with [email protected] and [email protected] in Chrome/Chromium (the code path is not gated on __DEV__; verified in NODE_ENV=production). Click Show, wait for the view transition to finish, then click Inspect:
import React, {startTransition, useState, ViewTransition} from 'react';
import {createRoot} from 'react-dom/client';
function App() {
const [show, setShow] = useState(false);
function inspect() {
for (const id of ['block-child', 'plain-inline']) {
const s = document.getElementById(id).style;
console.log(id, [s.display, s.marginTop, s.marginBottom]);
}
}
return (
<>
<button onClick={() => startTransition(() => setShow(true))}>Show</button>
<button onClick={inspect} disabled={!show}>Inspect</button>
{show && (
<ViewTransition name="vt-block-child">
<span id="block-child" style={{paddingTop: 10, paddingBottom: 12}}>
<span style={{display: 'block'}}>Details</span>
</span>
</ViewTransition>
)}
{show && (
<ViewTransition name="vt-plain-inline">
<span id="plain-inline" style={{paddingTop: 10, paddingBottom: 12}}>
Details
</span>
</ViewTransition>
)}
</>
);
}
createRoot(document.body.appendChild(document.createElement('div'))).render(<App />);Actual output
block-child ["block", "-10px", "-12px"]
plain-inline ["", "", ""]
Expected behavior
Both elements only supplied paddingTop / paddingBottom, so after the view transition completes both should restore to ["", "", ""].
Root cause
- Inside
applyViewTransitionName, whencomputedStyle.display === 'inline'andcountClientRects(rects) === 1, lines#L1548-L1553assign either'inline-block'(whenrects.length === 1) or'block'(whenrects.length > 1, such as an inline element wrapping a single block child) and set negativemarginTop/marginBottomin both branches:style.display = rects.length === 1 ? 'inline-block' : 'block'; style.marginTop = '-' + computedStyle.paddingTop; style.marginBottom = '-' + computedStyle.paddingBottom;
- Inside
restoreViewTransitionName, the cleanup guard at#L1599only checks'inline-block':Becauseif (style.display === 'inline-block') {
style.displaywas set to'block'in the block-child branch, cleanup is skipped entirely, leavingdisplay: blockand the negative margins permanently on the DOM node.
(Origin: Safari workaround introduced in #32822.)
Suggested fix
restoreViewTransitionName already reconstructs display, margin, marginTop, and marginBottom from props[STYLE] inside the guard (#L1601-L1624), so either widen #L1599 to if (style.display === 'inline-block' || style.display === 'block') or track an explicit flag/set when applyViewTransitionName overrode style.display so imperative inline display: block styles are never touched unintentionally. Add a regression test exercising the rects.length > 1 (block) branch alongside the rects.length === 1 (inline-block) branch.
Activity