Skip to content

Commit 03d749d

Browse files
committed
fix rotation issues
1 parent 4381220 commit 03d749d

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/PDFPreviewer.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ type Props = {
2727
onLoadError?: () => void;
2828
containerStyle?: CSSProperties;
2929
contentContainerStyle?: CSSProperties;
30-
/** Rotation angle for all pages (0, 90, 180, 270 degrees) */
3130
rotation?: RotationDegrees;
3231
};
3332

@@ -216,6 +215,14 @@ function PDFPreviewer({
216215
}
217216
}, [containerWidth, containerHeight, rotation]);
218217

218+
/**
219+
* Scroll back to the top whenever rotation changes so the list offset
220+
* is consistent regardless of how page dimensions change.
221+
*/
222+
useLayoutEffect(() => {
223+
listRef.current?.scrollTo(0);
224+
}, [rotation]);
225+
219226
useLayoutEffect(() => {
220227
if (!containerRef.current) {
221228
return undefined;
@@ -260,7 +267,7 @@ function PDFPreviewer({
260267
style={{...styles.list, ...contentContainerStyle}}
261268
outerRef={setListAttributes}
262269
width={isSmallScreen ? pageWidth : containerWidth}
263-
height={containerHeight}
270+
height={numPages === 1 && estimatedPageHeight < containerHeight ? estimatedPageHeight : containerHeight}
264271
itemCount={numPages}
265272
itemSize={calculatePageHeight}
266273
estimatedItemSize={calculatePageHeight(0)}
@@ -269,7 +276,6 @@ function PDFPreviewer({
269276
estimatedPageHeight,
270277
calculatePageHeight,
271278
getDevicePixelRatio,
272-
containerHeight,
273279
numPages,
274280
}}
275281
>

src/PageRenderer.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@ type Props = {
1212
calculatePageHeight: (pageIndex: number) => number;
1313
getDevicePixelRatio: (width: number, height: number) => number | undefined;
1414
numPages: number;
15-
containerHeight: number;
1615
};
1716
};
1817

1918
function PageRenderer({index, style, data}: Props) {
20-
const {pageWidth, estimatedPageHeight, calculatePageHeight, getDevicePixelRatio, numPages, containerHeight} = data;
19+
const {pageWidth, estimatedPageHeight, calculatePageHeight, getDevicePixelRatio, numPages} = data;
2120
/**
2221
* Render a specific page based on its index.
2322
* The method includes a wrapper to apply virtualized styles.
2423
*/
2524
const pageHeight = calculatePageHeight(index);
2625
const devicePixelRatio = getDevicePixelRatio(pageWidth, pageHeight);
27-
const parsedHeight = parseFloat(style.height as unknown as string);
2826
const parsedTop = parseFloat(style.top as unknown as string);
29-
const topPadding = numPages > 1 || parsedHeight > containerHeight ? parsedTop + PAGE_BORDER : (containerHeight - parsedHeight) / 2;
27+
const topPadding = numPages === 1 ? parsedTop : parsedTop + PAGE_BORDER;
3028
return (
3129
<div style={{...styles.pageWrapper, ...style, top: `${topPadding}px`}}>
3230
<Page

0 commit comments

Comments
 (0)