@@ -41,6 +41,8 @@ import type {
4141 OnRenderTextLayerError ,
4242 OnRenderTextLayerSuccess ,
4343 PageCallback ,
44+ PageContextType ,
45+ PageRenderProps ,
4446 RenderMode ,
4547} from './shared/types.js' ;
4648
@@ -63,7 +65,7 @@ export type PageProps = {
6365 * @example ref
6466 */
6567 canvasRef ?: React . Ref < HTMLCanvasElement > ;
66- children ?: React . ReactNode ;
68+ children ?: React . ReactNode | ( ( props : PageRenderProps ) => React . ReactNode ) ;
6769 /**
6870 * Class name(s) that will be added to rendered element along with the default `react-pdf__Page`.
6971 *
@@ -489,7 +491,7 @@ export default function Page(props: PageProps): React.ReactElement {
489491 const childContext = useMemo (
490492 ( ) =>
491493 // Technically there cannot be page without pageIndex, pageNumber, rotate and scale, but TypeScript doesn't know that
492- page && isProvided ( pageIndex ) && pageNumber && isProvided ( rotate ) && isProvided ( scale )
494+ isProvided ( pageIndex ) && pageNumber && isProvided ( rotate ) && isProvided ( scale )
493495 ? {
494496 _className,
495497 canvasBackground,
@@ -589,12 +591,23 @@ export default function Page(props: PageProps): React.ReactElement {
589591 }
590592
591593 function renderChildren ( ) {
594+ function isFulfilledContext ( context : PageContextType ) : context is PageRenderProps {
595+ return Boolean ( context ?. page ) ;
596+ }
597+
598+ if ( ! isFulfilledContext ( childContext ) ) {
599+ // Impossible, but TypeScript doesn't know that
600+ throw new Error ( 'page is undefined' ) ;
601+ }
602+
603+ const resolvedChildren = typeof children === 'function' ? children ( childContext ) : children ;
604+
592605 return (
593606 < PageContext . Provider value = { childContext } >
594607 { renderMainLayer ( ) }
595608 { renderTextLayer ( ) }
596609 { renderAnnotationLayer ( ) }
597- { children }
610+ { resolvedChildren }
598611 </ PageContext . Provider >
599612 ) ;
600613 }
0 commit comments