Skip to content

[react-dom] ViewTransition permanently leaks inline display: block and negative margins after finishing on inline elements wrapping a block child #38452

Backlog

Description

@Tortleya

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

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

In ReactFiberConfigDOM.js:

  1. Inside applyViewTransitionName, when computedStyle.display === 'inline' and countClientRects(rects) === 1, lines #L1548-L1553 assign either 'inline-block' (when rects.length === 1) or 'block' (when rects.length > 1, such as an inline element wrapping a single block child) and set negative marginTop / marginBottom in both branches:
    style.display = rects.length === 1 ? 'inline-block' : 'block';
    style.marginTop = '-' + computedStyle.paddingTop;
    style.marginBottom = '-' + computedStyle.paddingBottom;
  2. Inside restoreViewTransitionName, the cleanup guard at #L1599 only checks 'inline-block':
    if (style.display === 'inline-block') {
    Because style.display was set to 'block' in the block-child branch, cleanup is skipped entirely, leaving display: block and 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions