Skip to content

Commit 70695f6

Browse files
authored
Merge branch 'main' into main
2 parents 5555a85 + d454258 commit 70695f6

File tree

23 files changed

+1243
-4105
lines changed

23 files changed

+1243
-4105
lines changed

packages/react-pdf/README.md

Lines changed: 8 additions & 42 deletions
Large diffs are not rendered by default.

packages/react-pdf/package.json

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,30 @@
11
{
22
"name": "react-pdf",
3-
"version": "9.2.1",
3+
"version": "10.0.1",
44
"description": "Display PDFs in your React app as easily as if they were images.",
55
"type": "module",
66
"sideEffects": [
77
"*.css"
88
],
9-
"main": "./dist/cjs/index.js",
10-
"module": "./dist/esm/index.js",
9+
"main": "./dist/index.js",
1110
"source": "./src/index.ts",
12-
"types": "./dist/cjs/index.d.ts",
11+
"types": "./dist/index.d.ts",
1312
"exports": {
14-
".": {
15-
"import": "./dist/esm/index.js",
16-
"require": "./dist/cjs/index.js"
17-
},
18-
"./dist/Page/AnnotationLayer.css": {
19-
"import": "./dist/esm/Page/AnnotationLayer.css",
20-
"require": "./dist/cjs/Page/AnnotationLayer.css"
21-
},
22-
"./dist/Page/TextLayer.css": {
23-
"import": "./dist/esm/Page/TextLayer.css",
24-
"require": "./dist/cjs/Page/TextLayer.css"
25-
},
13+
".": "./dist/index.js",
2614
"./*": "./*"
2715
},
2816
"scripts": {
2917
"build": "yarn build-js && yarn copy-styles",
30-
"build-js": "yarn build-js-esm && yarn build-js-cjs && yarn build-js-cjs-package",
31-
"build-js-esm": "tsc --project tsconfig.build.json --outDir dist/esm",
32-
"build-js-cjs": "tsc --project tsconfig.build.json --outDir dist/cjs --module commonjs --moduleResolution node --verbatimModuleSyntax false",
33-
"build-js-cjs-package": "echo '{\n \"type\": \"commonjs\"\n}' > dist/cjs/package.json",
18+
"build-js": "tsc --project tsconfig.build.json",
3419
"clean": "node -e \"fs.rmSync('./dist', { recursive: true, force: true })\"",
35-
"copy-styles": "cpy 'src/**/*.css' dist/esm && cpy 'src/**/*.css' dist/cjs",
20+
"copy-styles": "cpy 'src/**/*.css' dist",
3621
"format": "biome format",
3722
"lint": "biome lint",
3823
"prepack": "yarn clean && yarn build",
3924
"test": "yarn lint && yarn tsc && yarn format && yarn unit",
4025
"tsc": "tsc",
4126
"unit": "vitest",
42-
"watch": "yarn build-js-esm --watch & yarn build-js-cjs --watch & node --eval \"fs.watch('src', () => child_process.exec('yarn copy-styles'))\""
27+
"watch": "yarn build-js --watch & node --eval \"fs.watch('src', () => child_process.exec('yarn copy-styles'))\""
4328
},
4429
"keywords": [
4530
"pdf",
@@ -55,9 +40,9 @@
5540
"clsx": "^2.0.0",
5641
"dequal": "^2.0.3",
5742
"make-cancellable-promise": "^2.0.0",
58-
"make-event-props": "^1.6.0",
59-
"merge-refs": "^1.3.0",
60-
"pdfjs-dist": "4.8.69",
43+
"make-event-props": "^2.0.0",
44+
"merge-refs": "^2.0.0",
45+
"pdfjs-dist": "5.3.31",
6146
"tiny-invariant": "^1.0.0",
6247
"warning": "^4.0.0"
6348
},

packages/react-pdf/src/Document.spec.tsx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ const OK = Symbol('OK');
2424
function ChildInternal({
2525
renderMode,
2626
rotate,
27+
scale,
2728
}: {
2829
renderMode?: string | null;
2930
rotate?: number | null;
31+
scale?: number | null;
3032
}) {
31-
return <div data-testid="child" data-rendermode={renderMode} data-rotate={rotate} />;
33+
return (
34+
<div data-testid="child" data-rendermode={renderMode} data-rotate={rotate} data-scale={scale} />
35+
);
3236
}
3337

3438
function Child(props: React.ComponentProps<typeof ChildInternal>) {
@@ -392,6 +396,24 @@ describe('Document', () => {
392396
expect(child.dataset.rotate).toBe('90');
393397
});
394398

399+
it('passes scale prop to its children', async () => {
400+
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
401+
402+
const { container } = render(
403+
<Document file={pdfFile.file} loading="Loading" onLoadSuccess={onLoadSuccess} scale={1.5}>
404+
<Child />
405+
</Document>,
406+
);
407+
408+
expect.assertions(1);
409+
410+
await onLoadSuccessPromise;
411+
412+
const child = getByTestId(container, 'child');
413+
414+
expect(child.dataset.scale).toBe('1.5');
415+
});
416+
395417
it('does not overwrite renderMode prop in its children when given renderMode prop to both Document and its children', async () => {
396418
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
397419

@@ -433,6 +455,24 @@ describe('Document', () => {
433455
expect(child.dataset.rotate).toBe('180');
434456
});
435457

458+
it('does not overwrite scale prop in its children when given scale prop to both Document and its children', async () => {
459+
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
460+
461+
const { container } = render(
462+
<Document file={pdfFile.file} loading="Loading" onLoadSuccess={onLoadSuccess} scale={1.5}>
463+
<Child scale={2} />
464+
</Document>,
465+
);
466+
467+
expect.assertions(1);
468+
469+
await onLoadSuccessPromise;
470+
471+
const child = getByTestId(container, 'child');
472+
473+
expect(child.dataset.scale).toBe('2');
474+
});
475+
436476
it('passes optionalContentConfig prop to its children', async () => {
437477
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
438478
const optionalContentConfig: OptionalContentConfig = await pdf5.getOptionalContentConfig();

packages/react-pdf/src/Document.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ export type DocumentProps = {
205205
* @example 90
206206
*/
207207
rotate?: number | null;
208+
* Document scale.
209+
*
210+
* @default 1
211+
* @example 0.5
212+
*/
213+
scale?: number;
208214
/**
209215
* An {@link OptionalContentConfig} created from {@link PDFDocumentProxy.getOptionalContentConfig}.
210216
* If `null`, the configuration will be fetched automatically with the default visibility states set.
@@ -268,6 +274,7 @@ const Document: React.ForwardRefExoticComponent<
268274
options,
269275
renderMode,
270276
rotate,
277+
scale,
271278
optionalContentConfig,
272279
...otherProps
273280
},
@@ -587,6 +594,7 @@ const Document: React.ForwardRefExoticComponent<
587594
registerPage,
588595
renderMode,
589596
rotate,
597+
scale,
590598
unregisterPage,
591599
optionalContentConfig,
592600
}),
@@ -597,6 +605,7 @@ const Document: React.ForwardRefExoticComponent<
597605
registerPage,
598606
renderMode,
599607
rotate,
608+
scale,
600609
unregisterPage,
601610
optionalContentConfig,
602611
],
@@ -635,9 +644,6 @@ const Document: React.ForwardRefExoticComponent<
635644
className={clsx('react-pdf__Document', className)}
636645
// Assertion is needed for React 18 compatibility
637646
ref={inputRef as React.Ref<HTMLDivElement>}
638-
style={{
639-
['--scale-factor' as string]: '1',
640-
}}
641647
{...eventProps}
642648
>
643649
{renderContent()}

packages/react-pdf/src/Page.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -627,13 +627,19 @@ export default function Page(props: PageProps): React.ReactElement {
627627
data-page-number={pageNumber}
628628
// Assertion is needed for React 18 compatibility
629629
ref={mergeRefs(inputRef as React.Ref<HTMLDivElement>, pageElement)}
630-
style={{
631-
['--scale-factor' as string]: `${scale}`,
632-
backgroundColor: canvasBackground || 'white',
633-
position: 'relative',
634-
minWidth: 'min-content',
635-
minHeight: 'min-content',
636-
}}
630+
style={
631+
{
632+
'--scale-round-x': '1px',
633+
'--scale-round-y': '1px',
634+
'--scale-factor': '1',
635+
'--user-unit': `${scale}`,
636+
'--total-scale-factor': 'calc(var(--scale-factor) * var(--user-unit))',
637+
backgroundColor: canvasBackground || 'white',
638+
position: 'relative',
639+
minWidth: 'min-content',
640+
minHeight: 'min-content',
641+
} as React.CSSProperties
642+
}
637643
{...eventProps}
638644
>
639645
{renderContent()}

packages/react-pdf/src/Page/AnnotationLayer.css

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
background-image: var(--annotation-unfocused-field-background);
116116
border: 2px solid var(--input-unfocused-border-color);
117117
box-sizing: border-box;
118-
font: calc(9px * var(--scale-factor)) sans-serif;
118+
font: calc(9px * var(--total-scale-factor)) sans-serif;
119119
height: 100%;
120120
margin: 0;
121121
vertical-align: top;
@@ -246,21 +246,21 @@
246246

247247
.annotationLayer .popupWrapper {
248248
position: absolute;
249-
font-size: calc(9px * var(--scale-factor));
249+
font-size: calc(9px * var(--total-scale-factor));
250250
width: 100%;
251-
min-width: calc(180px * var(--scale-factor));
251+
min-width: calc(180px * var(--total-scale-factor));
252252
pointer-events: none;
253253
}
254254

255255
.annotationLayer .popup {
256256
position: absolute;
257-
max-width: calc(180px * var(--scale-factor));
257+
max-width: calc(180px * var(--total-scale-factor));
258258
background-color: rgba(255, 255, 153, 1);
259-
box-shadow: 0 calc(2px * var(--scale-factor)) calc(5px * var(--scale-factor))
259+
box-shadow: 0 calc(2px * var(--total-scale-factor)) calc(5px * var(--total-scale-factor))
260260
rgba(136, 136, 136, 1);
261-
border-radius: calc(2px * var(--scale-factor));
262-
padding: calc(6px * var(--scale-factor));
263-
margin-left: calc(5px * var(--scale-factor));
261+
border-radius: calc(2px * var(--total-scale-factor));
262+
padding: calc(6px * var(--total-scale-factor));
263+
margin-left: calc(5px * var(--total-scale-factor));
264264
cursor: pointer;
265265
font: message-box;
266266
white-space: normal;
@@ -269,7 +269,7 @@
269269
}
270270

271271
.annotationLayer .popup > * {
272-
font-size: calc(9px * var(--scale-factor));
272+
font-size: calc(9px * var(--total-scale-factor));
273273
}
274274

275275
.annotationLayer .popup h1 {
@@ -278,18 +278,18 @@
278278

279279
.annotationLayer .popupDate {
280280
display: inline-block;
281-
margin-left: calc(5px * var(--scale-factor));
281+
margin-left: calc(5px * var(--total-scale-factor));
282282
}
283283

284284
.annotationLayer .popupContent {
285285
border-top: 1px solid rgba(51, 51, 51, 1);
286-
margin-top: calc(2px * var(--scale-factor));
287-
padding-top: calc(2px * var(--scale-factor));
286+
margin-top: calc(2px * var(--total-scale-factor));
287+
padding-top: calc(2px * var(--total-scale-factor));
288288
}
289289

290290
.annotationLayer .richText > * {
291291
white-space: pre-wrap;
292-
font-size: calc(9px * var(--scale-factor));
292+
font-size: calc(9px * var(--total-scale-factor));
293293
}
294294

295295
.annotationLayer .highlightAnnotation,

packages/react-pdf/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ export type { DocumentProps } from './Document.js';
1515
export type { OutlineProps } from './Outline.js';
1616
export type { PageProps } from './Page.js';
1717
export type { ThumbnailProps } from './Thumbnail.js';
18+
export type {
19+
PasswordResponses as PasswordResponsesType,
20+
StructTreeNode,
21+
TextContent,
22+
TextItem,
23+
TextMarkedContent,
24+
} from './shared/types.js';
1825

1926
import { displayWorkerWarning } from './shared/utils.js';
2027

sample/next-app/app/Sample.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import { useCallback, useId, useState } from 'react';
44
import { useResizeObserver } from '@wojtekmaj/react-hooks';
55
import { pdfjs, Document, Page } from 'react-pdf';
6-
import 'react-pdf/dist/esm/Page/AnnotationLayer.css';
7-
import 'react-pdf/dist/esm/Page/TextLayer.css';
6+
import 'react-pdf/dist/Page/AnnotationLayer.css';
7+
import 'react-pdf/dist/Page/TextLayer.css';
88

99
import './Sample.css';
1010

sample/next-app/app/page.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import Sample from './Sample.js';
1+
'use client';
2+
3+
import dynamic from 'next/dynamic';
4+
5+
const Sample = dynamic(() => import('./Sample.js'), {
6+
ssr: false,
7+
});
28

39
export default function Page() {
410
return <Sample />;

0 commit comments

Comments
 (0)