Skip to content

fix(layout): paginate nested first children that do not fit instead of clipping them - #3613

Open
JoelBrenstrum wants to merge 2 commits into
diegomura:masterfrom
JoelBrenstrum:fix/pagination-nested-page-start
Open

JoelBrenstrum wants to merge 2 commits into
diegomura:masterfrom
JoelBrenstrum:fix/pagination-nested-page-start

Conversation

@JoelBrenstrum

@JoelBrenstrum JoelBrenstrum commented Oct 27, 2026 •

Copy link
Copy Markdown

Problem

splitNodes has a special case for a node whose children all end up on the next page: if currentChildren.length === 0 it keeps the whole node on the current page, on the assumption that the page is empty and moving the node would only produce an empty page. That check only looks at the node's own siblings, so it also fires for a node that is merely the first child of its parent, however deep it sits on a page that already has content.

When that happens the node's box is clamped to the page edge by splitNode, but its children keep their full layout and are drawn past the bottom margin, over the footer. No warning is logged.

A minimal document that shows it:

Page (height 100)
├── View (height 30)
└── View
    └── View
        └── View wrap={false} (height 80)

The unwrappable view fits on a page but not in the 70pt left under the first view. Its parent has no earlier siblings, so the parent is kept on page 1 with the 80pt view clipped at the page edge, and the document stays one page long.

We hit this with form documents that render option groups as wrap={false} rows inside a chain of single-child containers: whenever the group started near the bottom of a page its last rows were cut off.

Related issues

I ran the repros from #3449, #3586 and #3588 against this branch. In each one the block starts the next page and nothing is drawn past the bottom margin.

Only the legacy engine is affected. The new engine behind experimentalPagination (#3502) already paginates these documents correctly, but the legacy engine is still the default in @react-pdf/layout 5.2.0.

Reproduction

Open in the REPL (runs against the published packages, @react-pdf/layout 5.2.0 with the default legacy engine at the time of writing).

Page 1 ends with the pink block drawn through the bottom margin and over the footer, and page 2 contains nothing but the footer. With this change the block starts page 2.

REPL source
import React from 'react';
import ReactPDF, { Document, Page, View, Text } from '@react-pdf/renderer';

// A wrap={false} block that fits on a page but not in the space left on the
// current one. Because it is the first child of its parent, react-pdf keeps the
// parent on page 1: the block is drawn past the bottom margin, over the footer,
// and page 2 only receives an empty leftover view.
const lines = Array.from({ length: 20 }, (_, i) => (
  <Text key={i}>{`line ${i + 1} of the block that should start on page 2`}</Text>
));

const Repro = () => (
  <Document>
    <Page size="A4" style={{ padding: 30, fontSize: 12 }}>
      <View style={{ height: 600, backgroundColor: '#e8e8e8', padding: 10 }}>
        <Text>Content above (600pt tall)</Text>
      </View>

      <View>
        <View>
          <View wrap={false} style={{ minHeight: 300, backgroundColor: '#f7b2a6', padding: 10 }}>
            {lines}
          </View>
        </View>
      </View>

      <Text
        fixed
        style={{ position: 'absolute', bottom: 12, left: 30 }}
        render={({ pageNumber, totalPages }) => `page ${pageNumber} / ${totalPages}`}
      />
    </Page>
  </Document>
);

ReactPDF.render(<Repro />);

Fix

  • Thread a pageStart flag from splitPage through splitNodes → split → splitView → splitChildren, so the keep-on-page behaviour only applies when every ancestor is also at the top of the page. Fixed and zero-height siblings are ignored when deciding that, so a page footer or an empty element does not count as content.
  • shouldBreak receives the same flag and honours minPresenceAhead on a first child that is not at the page start. Previously a first child could never break for presence, which stranded headings at the foot of a page. Direct callers that omit the argument keep the previous behaviour.

Nothing changes for nodes that genuinely are at the start of a page, so the existing infinite-loop guards still hold. The new should keep a nested first child on the page when it is already at the page start test covers that.

Tests

  • resolvePagination.test.ts: nested first child moves to the next page; nested first child at the page start stays; minPresenceAhead on a first child that is not at the page start.
  • shouldBreak.test.ts: the non-page-start minPresenceAhead case.

The two behavioural pagination tests fail on master and pass with this change. The full @react-pdf/layout suite passes (365 tests).

Includes a changeset for @react-pdf/layout.

🤖 Generated with Claude Code

…f clipping them

When every child of a node moves to the next page, splitNodes kept the
node on the current page if it had no earlier siblings, on the assumption
that the page was empty. The check only looked at the node's own siblings,
so it also fired for a node that was merely the first child of its parent
further down a page. The node was then clamped to the page edge and its
children drawn past the bottom margin, over the footer, with no warning.

Thread a pageStart flag from splitPage through splitNodes, split,
splitView and splitChildren so the keep-on-page behaviour only applies
when every ancestor is at the top of the page too. Fixed and zero-height
siblings are ignored when deciding that, so a page footer or an empty
element does not count as content.

The same flag lets shouldBreak honour minPresenceAhead on a first child
that is not at the page start. Direct callers that omit the flag keep the
previous behaviour.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@vercel

vercel Bot commented Oct 27, 2026

Copy link
Copy Markdown

@JoelBrenstrum is attempting to deploy a commit to the diegomura Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 43a1103

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@react-pdf/layout Patch
@react-pdf/renderer Patch
@react-pdf/examples Patch
@react-pdf/math Patch
@react-pdf/mermaid Patch
@react-pdf/ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@JoelBrenstrum
JoelBrenstrum marked this pull request as ready for review October 28, 2026 02:37

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

minPresenceAhead not working Content taller than one page is squashed/overlapped at the bottom instead of flowing onto the next page

1 participant