@@ -166,6 +166,25 @@ const splitNodes = (height: number, contentArea: number, nodes: SafeNode[]) => {
166166 break ;
167167 }
168168
169+ // Element fits on a full page but not in remaining space - move to next page to keep together
170+ // This handles the case where wrap is true (default) but we still want to avoid splitting
171+ // if the element would fit entirely on the next page
172+ const remainingSpace = height - nodeTop ;
173+ const wouldBarelyFitOnCurrentPage = remainingSpace < nodeHeight * 0.3 ; // Less than 30% fits
174+ if (
175+ fitsInsidePage &&
176+ shouldSplit &&
177+ wouldBarelyFitOnCurrentPage &&
178+ currentChildren . length > 0
179+ ) {
180+ const box = Object . assign ( { } , child . box , { top : child . box . top - height } ) ;
181+ const next = Object . assign ( { } , child , { box } ) ;
182+
183+ currentChildren . push ( ...futureFixedNodes ) ;
184+ nextChildren . push ( next , ...futureNodes ) ;
185+ break ;
186+ }
187+
169188 if ( shouldBreak ) {
170189 const box = Object . assign ( { } , child . box , { top : child . box . top - height } ) ;
171190 const props = Object . assign ( { } , child . props , {
@@ -182,21 +201,23 @@ const splitNodes = (height: number, contentArea: number, nodes: SafeNode[]) => {
182201 if ( shouldSplit || firstBreakableViewChild ) {
183202 const [ currentChild , nextChild ] = split ( child , height , contentArea ) ;
184203
185- // All children are moved to the next page, it doesn't make sense to show the parent on the current page
186- if ( child . children . length > 0 && currentChild . children . length === 0 ) {
187- // But if the current page is empty then we can just include the parent on the current page
188- // if (currentChildren.length === 0) {
189- // currentChildren.push(child, ...futureFixedNodes);
190- // nextChildren.push(...futureNodes);
191- // } else {
192- const box = Object . assign ( { } , nextChild . box , {
193- top : nextChild . box . top - height ,
204+ // Check if all non-fixed children are moved to the next page
205+ const currentNonFixedChildren = currentChild . children
206+ ? currentChild . children . filter ( ( c ) => ! isFixed ( c ) )
207+ : [ ] ;
208+ const hasNonFixedChildren = child . children
209+ ? child . children . filter ( ( c ) => ! isFixed ( c ) ) . length > 0
210+ : false ;
211+
212+ // All meaningful children are moved to the next page, move the parent too
213+ if ( hasNonFixedChildren && currentNonFixedChildren . length === 0 ) {
214+ const box = Object . assign ( { } , child . box , {
215+ top : child . box . top - height ,
194216 } ) ;
195- const next = Object . assign ( { } , nextChild , { box } ) ;
217+ const next = Object . assign ( { } , child , { box } ) ;
196218
197219 currentChildren . push ( ...futureFixedNodes ) ;
198220 nextChildren . push ( next , ...futureNodes ) ;
199- // }
200221 break ;
201222 }
202223
0 commit comments