@@ -11,8 +11,6 @@ import 'react-pdf/dist/Page/TextLayer.css';
1111import {
1212 DEFAULT_HEIGHT ,
1313 DEFAULT_SHOULD_GROW_WHEN_SCROLLING ,
14- FOOTER_HEIGHT ,
15- HEADER_HEIGHT ,
1614 MAIN_CONTENT_ID ,
1715} from '../constants' ;
1816import LoadingSkeleton from '../ui/LoadingSkeleton' ;
@@ -69,6 +67,7 @@ export default function usePdfReader(args: PdfReaderArguments): ReaderReturn {
6967 const isFetching = ! state . resource ;
7068 const isParsed = typeof state . numPages === 'number' ;
7169 const [ containerRef , containerSize ] = useMeasure < HTMLDivElement > ( ) ;
70+ const [ pageHeight , setPageHeight ] = React . useState < number > ( 0 ) ;
7271
7372 // dispatch action when arguments change
7473 React . useEffect ( ( ) => {
@@ -154,6 +153,24 @@ export default function usePdfReader(args: PdfReaderArguments): ReaderReturn {
154153 resizePage ( containerSize , state . fitMode ) ;
155154 } , [ containerSize , resizePage , state . fitMode ] ) ;
156155
156+ const setInitialPageHeight = React . useCallback ( ( ) => {
157+ if (
158+ pageHeight === 0 &&
159+ state . pdfWidth &&
160+ state . pdfHeight &&
161+ containerSize . width
162+ ) {
163+ const margin = 16 ;
164+ const aspectRatio = state . pdfHeight / state . pdfWidth ;
165+ const initialPageHeight = ( containerSize . width - margin ) * aspectRatio ;
166+ setPageHeight ( initialPageHeight ) ;
167+ }
168+ } , [ pageHeight , state . pdfWidth , state . pdfHeight , containerSize . width ] ) ;
169+
170+ React . useEffect ( ( ) => {
171+ setInitialPageHeight ( ) ;
172+ } , [ setInitialPageHeight ] ) ;
173+
157174 /**
158175 * Update the atStart/atEnd state to tell the UI whether to show the prev/next buttons
159176 * Whether to have the next/prev buttons enabled. We disable them:
@@ -252,7 +269,7 @@ export default function usePdfReader(args: PdfReaderArguments): ReaderReturn {
252269 const intersectionRatios = React . useRef < { [ page : number ] : number } > ( { } ) ;
253270 const lastMostVisiblePage = React . useRef < number > ( state . pageNumber ) ;
254271
255- const handlePageInView = React . useCallback (
272+ const onInView = React . useCallback (
256273 ( pageNum : number , ratio : number ) => {
257274 if ( ! state . settings ?. isScrolling ) return ;
258275 intersectionRatios . current [ pageNum ] = ratio ;
@@ -363,9 +380,6 @@ export default function usePdfReader(args: PdfReaderArguments): ReaderReturn {
363380 }
364381 }
365382
366- const shouldGrow = state . settings ?. isScrolling && growWhenScrolling ;
367- const finalHeight = shouldGrow ? 'initial' : height ;
368-
369383 // the reader is active but loading a page
370384 return {
371385 type : 'PDF' ,
@@ -380,18 +394,15 @@ export default function usePdfReader(args: PdfReaderArguments): ReaderReturn {
380394 tabIndex = { - 1 }
381395 id = { MAIN_CONTENT_ID }
382396 ref = { containerRef }
383- height = { finalHeight }
397+ height = { pageHeight }
398+ sx = { {
399+ '.react-pdf__Document' : {
400+ height : `${ pageHeight } px` ,
401+ overflowX : 'hidden' ,
402+ overflowY : 'auto' ,
403+ } ,
404+ } }
384405 >
385- { /* FIXME: POC, update this with more a react proach. chakra.factory throws memory leak error.*/ }
386- < style >
387- { `
388- .react-pdf__Document {
389- height: calc(100vh - ${ HEADER_HEIGHT + FOOTER_HEIGHT } px);
390- overflow-x: hidden;
391- overflow-y: auto;
392- }
393- ` }
394- </ style >
395406 < Document
396407 file = { state . resource }
397408 onLoadSuccess = { onDocumentLoadSuccess }
@@ -403,14 +414,15 @@ export default function usePdfReader(args: PdfReaderArguments): ReaderReturn {
403414 Array . from ( new Array ( state . numPages ) , ( _ , index ) => (
404415 < ScrollPage
405416 key = { `page_${ index + 1 } ` }
406- width = { containerSize . width }
417+ width = { containerSize . width - 16 }
407418 height = { state . pageHeight }
408419 placeholderHeight = { state . pdfHeight }
409420 placeholderWidth = { state . pdfWidth }
410421 scale = { state . scale }
411422 pageNumber = { index + 1 }
412423 onLoadSuccess = { onRenderSuccess }
413- onInView = { handlePageInView }
424+ allowInView = { ! isFetching }
425+ onInView = { onInView }
414426 fitMode = { state . fitMode }
415427 />
416428 ) ) }
0 commit comments