@pdfslick/react 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/README.md ADDED
@@ -0,0 +1,119 @@
1
+ <br><br>
2
+
3
+ ![readme-header](https://pdfslick.dev/pdfslick_logo.svg)
4
+
5
+ <br><br>
6
+ <div align="center">
7
+ View and Interact with PDFs in React and SolidJS apps
8
+ <br><br>
9
+
10
+ [Getting Started](https://pdfslick.dev/docs) | [Examples](https://pdfslick.dev/examples)
11
+
12
+ <br><br>
13
+ </div>
14
+
15
+ ---
16
+ <br>
17
+
18
+ PDFSlick is a library that enables viewing of and interaction with PDF documents in React and SolidJS apps.
19
+ It's build on top of Mozilla's [PDF.js](https://github.com/mozilla/pdf.js), and utilises [Zustand](https://github.com/pmndrs/zustand) to provide a reactive store for the loaded documents.
20
+
21
+ ## Motivation
22
+
23
+ [PDF.js](https://github.com/mozilla/pdf.js) is an amazing piece of software. It is also a very stable and mature one — it powers the PDF viewer in Mozilla Firefox and it's been around since 2011. However, it's all Vanilla JavaScript, and when it comes to using it with libraries like React and SolidJS (although possible) it's a litte bit hard in terms of integrating it in these Component- and reactive-like environments. PDFSlick attempts to wrap all of that fascinating functionality into one that is easier to fit in React and SolidJS worlds — as components and a reactive store.
24
+
25
+ ## Core Concepts
26
+
27
+ The core of PDFSlick is within the `@pdfslick/core` package. It wraps `PDF.js`'s functionality and links it to the store. This `@pdfslick/core` package is the basis for the React and SolidJS packages, which additionally transform the store and make it suitable for the adequate library, as well as providing components for the PDF viewer and thumbnails.
28
+
29
+ Depending on your needs, at this you might find it useful to continue with learning more about using PDFSlick with React and SolidJS respsectivelly. However, if really interested you can learn more about using PDFSlick's `@pdfslick/core` package with Vanilla JS apps and with libraries other than React and SolidJS in the sections below.
30
+
31
+ ## Getting Started with React
32
+
33
+ To get started with React run:
34
+ ```shell
35
+ npm install @pdfslick/react
36
+ # yarn add @pdfslick/react
37
+ # pnpm add @pdfslick/react
38
+ ```
39
+
40
+ You can start using PDFSlick with the `usePDFSlick()` hook, like with the following basic example:
41
+ ```jsx
42
+ import { usePDFSlick } from "@pdfslick/react";
43
+ import PDFNavigation from "./PDFNavigation";
44
+
45
+ import "@pdfslick/react/dist/pdf_viewer.css";
46
+
47
+ type PDFViewerAppProps = {
48
+ pdfFilePath: string;
49
+ };
50
+
51
+ const SimplePDFViewer = ({ pdfFilePath }: PDFViewerAppProps) => {
52
+ const { viewerRef, usePDFSlickStore, PDFSlickViewer } = usePDFSlick(pdfFilePath, {
53
+ singlePageViewer: true,
54
+ scaleValue: "page-fit"
55
+ });
56
+
57
+ return (
58
+ <div className="absolute inset-0 bg-slate-200/70 pdfSlick">
59
+ <div className="flex-1 relative h-full">
60
+ <PDFSlickViewer {...{ viewerRef, usePDFSlickStore }} />
61
+ <PDFNavigation {...{ usePDFSlickStore }} />
62
+ </div>
63
+ </div>
64
+ );
65
+ };
66
+
67
+ export default SimplePDFViewer;
68
+ ```
69
+
70
+ Provided with the PDF Document path and options object, the `usePDFSlick()` hook returns an object consisting (among the other things) of:
71
+ - `PDFSlickViewer` — the PDF Viewer component used for viewing the PDF document
72
+ - `viewerRef` — a `RefCallback` that is provided as a prop to the `<PDFSlickViewer />` component
73
+ - `usePDFSlickStore` — a hook to the PDFSlick store
74
+
75
+ <br><br>
76
+ [Read more about using PDFSlick with React](https://pdfslick.dev/docs/react) | [Checkout the React Examples](./apps/web/examples)
77
+
78
+ ## SolidJS
79
+
80
+ To get started with PDFSlick for SolidJS React run:
81
+ ```shell
82
+ npm install @pdfslick/solid
83
+ # yarn add @pdfslick/solid
84
+ # pnpm add @pdfslick/solid
85
+ ```
86
+
87
+ You can start using PDFSlick with the `usePDFSlick()` hook, like with the following basic example:
88
+ ```jsx
89
+ import { Component, createSignal } from "solid-js";
90
+ import { usePDFSlick } from "@pdfslick/solid";
91
+
92
+ type PDFViewerAppProps = {
93
+ pdfFilePath: string;
94
+ };
95
+
96
+ const PDFViewerApp: Component<PDFViewerAppProps> = ({ pdfFilePath }) => {
97
+ const { viewerRef, pdfSlickStore: store, PDFSlickViewer } =
98
+ usePDFSlick(pdfFilePath);
99
+
100
+ return (
101
+ <div class="absolute inset-0 bg-slate-200/70 flex flex-col pdfSlick">
102
+ <div class="flex-1 relative h-full">
103
+ <PDFSlickViewer {...{ store, viewerRef }} />
104
+ </div>
105
+ </div>
106
+ );
107
+ };
108
+
109
+ export default PDFViewerApp;
110
+ ```
111
+
112
+ Provided with the PDF Document path and options object, the `usePDFSlick()` hook returns an object consisting (among the other things) of:
113
+ - `PDFSlickViewer` — the PDF Viewer component used for viewing the PDF document
114
+ - `viewerRef` — a `RefCallback` that is provided as a prop to the `<PDFSlickViewer />` component
115
+ - `pdfSlickStore` — the PDFSlick store
116
+
117
+ <br><br>
118
+ [Read more about using PDFSlick with SolidJS](https://pdfslick.dev/docs/solid) | [Checkout the SolidJS Examples](./apps/solidweb/src/examples)
119
+
@@ -0,0 +1,20 @@
1
+ import { ReactNode } from "react";
2
+ import type { TUsePDFSlickStore } from "./";
3
+ export type PDFSlickThumbProps = {
4
+ pageNumber: number;
5
+ width: number;
6
+ height: number;
7
+ scale: number;
8
+ rotation: number;
9
+ loaded: boolean;
10
+ pageLabel: string | null;
11
+ src: string | null;
12
+ };
13
+ export type PDFSlickThumbnailsContainerProps = {
14
+ children: ({ pageNumber, src, width, height, scale, rotation, pageLabel, loaded, }: PDFSlickThumbProps) => ReactNode;
15
+ thumbsRef: (instance: HTMLElement | null) => void;
16
+ usePDFSlickStore: TUsePDFSlickStore;
17
+ className?: string;
18
+ };
19
+ export declare function PDFSlickThumbnails({ children: renderChild, thumbsRef, usePDFSlickStore, className, }: PDFSlickThumbnailsContainerProps): JSX.Element;
20
+ //# sourceMappingURL=PDFSlickThumbnails.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PDFSlickThumbnails.d.ts","sourceRoot":"","sources":["../PDFSlickThumbnails.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGlC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,IAAI,CAAC;AAE5C,MAAM,MAAM,kBAAkB,GAAG;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,gCAAgC,GAAG;IAC7C,QAAQ,EAAE,CAAC,EACT,UAAU,EACV,GAAG,EACH,KAAK,EACL,MAAM,EACN,KAAK,EACL,QAAQ,EACR,SAAS,EACT,MAAM,GACP,EAAE,kBAAkB,KAAK,SAAS,CAAC;IACpC,SAAS,EAAE,CAAC,QAAQ,EAAE,WAAW,GAAG,IAAI,KAAK,IAAI,CAAC;IAClD,gBAAgB,EAAE,iBAAiB,CAAC;IACpC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,wBAAgB,kBAAkB,CAAC,EACjC,QAAQ,EAAE,WAAW,EACrB,SAAS,EACT,gBAAgB,EAChB,SAAS,GACV,EAAE,gCAAgC,eA8ClC"}
@@ -0,0 +1,10 @@
1
+ /// <reference types="react" />
2
+ /// <reference types="react" />
3
+ import type { TUsePDFSlickStore } from "@pdfslick/react";
4
+ export type PDFSlickViewerProps = {
5
+ viewerRef: (instance: HTMLElement) => void;
6
+ usePDFSlickStore: TUsePDFSlickStore;
7
+ className?: string;
8
+ };
9
+ export default function PDFSlickViewer({ usePDFSlickStore, viewerRef, className, }: PDFSlickViewerProps): JSX.Element;
10
+ //# sourceMappingURL=PDFSlickViewer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PDFSlickViewer.d.ts","sourceRoot":"","sources":["../PDFSlickViewer.tsx"],"names":[],"mappings":";;AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEzD,MAAM,MAAM,mBAAmB,GAAG;IAChC,SAAS,EAAE,CAAC,QAAQ,EAAE,WAAW,KAAK,IAAI,CAAC;IAC3C,gBAAgB,EAAE,iBAAiB,CAAC;IACpC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,EACrC,gBAAgB,EAChB,SAAS,EACT,SAAS,GACV,EAAE,mBAAmB,eAwCrB"}
@@ -0,0 +1,4 @@
1
+ export * from "./usePDFSlick";
2
+ export * from "./PDFSlickThumbnails";
3
+ export * from "@pdfslick/core";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.tsx"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAA;AACpC,cAAc,gBAAgB,CAAA"}
@@ -0,0 +1,116 @@
1
+ import { useState, useMemo, useCallback, useEffect } from 'react';
2
+ import { useStore } from 'zustand';
3
+ import { create, PDFSlick } from '@pdfslick/core';
4
+ export * from '@pdfslick/core';
5
+ import { jsx } from 'react/jsx-runtime';
6
+ import { useMeasure, useDebounce } from 'react-use';
7
+ import { createPortal } from 'react-dom';
8
+
9
+ function PDFSlickViewer({ usePDFSlickStore, viewerRef, className, }) {
10
+ const pdfSlick = usePDFSlickStore((s) => s.pdfSlick);
11
+ const scaleValue = usePDFSlickStore((s) => s.scaleValue);
12
+ const [resizeRef, { width }] = useMeasure();
13
+ useDebounce(() => {
14
+ if (width) {
15
+ if (pdfSlick &&
16
+ (scaleValue === "auto" ||
17
+ scaleValue === "page-fit" ||
18
+ scaleValue === "page-width")) {
19
+ pdfSlick.viewer.currentScaleValue = scaleValue;
20
+ }
21
+ pdfSlick === null || pdfSlick === void 0 ? void 0 : pdfSlick.viewer.update();
22
+ }
23
+ }, 0, [width]);
24
+ return (jsx("div", Object.assign({ ref: (el) => {
25
+ viewerRef(el);
26
+ resizeRef(el);
27
+ }, id: "viewerContainer", className: `pdfSlickContainer ${className !== null && className !== void 0 ? className : ""}`, style: {
28
+ position: "absolute",
29
+ inset: 0,
30
+ } }, { children: jsx("div", { id: "viewer", className: "pdfSlickViewer pdfViewer" }) })));
31
+ }
32
+
33
+ function PDFSlickThumbnails({ children: renderChild, thumbsRef, usePDFSlickStore, className, }) {
34
+ const thumbnailViews = usePDFSlickStore((s) => s.thumbnailViews);
35
+ const pdfSlick = usePDFSlickStore((s) => s.pdfSlick);
36
+ const [resizeRef, { width }] = useMeasure();
37
+ useDebounce(() => {
38
+ if (width) {
39
+ pdfSlick === null || pdfSlick === void 0 ? void 0 : pdfSlick.forceRendering();
40
+ }
41
+ }, 0, [width]);
42
+ return (jsx("div", Object.assign({ ref: (el) => {
43
+ thumbsRef(el);
44
+ resizeRef(el);
45
+ } }, { className }, { style: {
46
+ position: "absolute",
47
+ overflow: "auto",
48
+ inset: 0
49
+ } }, { children: Array.from(thumbnailViews).map(([pageNumber, view]) => {
50
+ var _a;
51
+ return createPortal(renderChild({
52
+ pageNumber,
53
+ width: view.canvasWidth,
54
+ height: view.canvasHeight,
55
+ scale: view.scale,
56
+ src: (_a = view === null || view === void 0 ? void 0 : view.src) !== null && _a !== void 0 ? _a : null,
57
+ rotation: view.rotation,
58
+ pageLabel: view.pageLabel,
59
+ loaded: view.loaded,
60
+ }), view.div);
61
+ }) })));
62
+ }
63
+
64
+ function createStore(store) {
65
+ function usePDFSlickStore(selector, equals) {
66
+ return useStore(store, selector, equals);
67
+ }
68
+ return usePDFSlickStore;
69
+ }
70
+ /**
71
+ *
72
+ * @param url PDF Document path
73
+ * @param options PDFSlick Options
74
+ * @returns
75
+ */
76
+ const usePDFSlick = (url, options) => {
77
+ const [isDocumentLoaded, setIsDocumentLoaded] = useState(false);
78
+ const [container, setContainer] = useState(null);
79
+ const [thumbs, setThumbs] = useState(null);
80
+ const store = useMemo(() => create(), []);
81
+ const usePDFSlickStore = useMemo(() => createStore(store), []);
82
+ const viewerRef = useCallback((node) => {
83
+ if (node !== null) {
84
+ setContainer(node);
85
+ }
86
+ }, []);
87
+ const thumbsRef = useCallback((node) => {
88
+ if (node !== null) {
89
+ setThumbs(node);
90
+ }
91
+ }, []);
92
+ useEffect(() => {
93
+ if (url && container) {
94
+ const pdfSlick = new PDFSlick({
95
+ container,
96
+ thumbs: thumbs,
97
+ store,
98
+ options,
99
+ });
100
+ pdfSlick.loadDocument(url, options).then(() => setIsDocumentLoaded(true));
101
+ store.setState({ pdfSlick });
102
+ }
103
+ return () => { };
104
+ }, [url, container]);
105
+ return {
106
+ isDocumentLoaded,
107
+ viewerRef,
108
+ thumbsRef,
109
+ usePDFSlickStore,
110
+ store,
111
+ PDFSlickViewer,
112
+ PDFSlickThumbnails,
113
+ };
114
+ };
115
+
116
+ export { PDFSlickThumbnails, createStore, usePDFSlick };
@@ -0,0 +1,32 @@
1
+ /// <reference types="node" />
2
+ import { RefCallback } from "react";
3
+ import { StoreApi } from "zustand";
4
+ import type { PDFSlickState, PDFSlickOptions } from "@pdfslick/core";
5
+ import PDFSlickViewer from "./PDFSlickViewer";
6
+ import { PDFSlickThumbnails } from "./PDFSlickThumbnails";
7
+ export type TUsePDFSlickStore = {
8
+ (): PDFSlickState;
9
+ <T>(selector: (state: PDFSlickState) => T, equals?: ((a: T, b: T) => boolean) | undefined): T;
10
+ };
11
+ type TUsePDFSlick = (url: string | URL | undefined, options?: PDFSlickOptions) => {
12
+ isDocumentLoaded: boolean;
13
+ viewerRef: RefCallback<HTMLElement>;
14
+ thumbsRef: RefCallback<HTMLElement>;
15
+ store: StoreApi<PDFSlickState>;
16
+ usePDFSlickStore: TUsePDFSlickStore;
17
+ PDFSlickViewer: typeof PDFSlickViewer;
18
+ PDFSlickThumbnails: typeof PDFSlickThumbnails;
19
+ };
20
+ export declare function createStore(store: StoreApi<PDFSlickState>): {
21
+ (): PDFSlickState;
22
+ <T>(selector: (state: PDFSlickState) => T, equals?: ((a: T, b: T) => boolean) | undefined): T;
23
+ };
24
+ /**
25
+ *
26
+ * @param url PDF Document path
27
+ * @param options PDFSlick Options
28
+ * @returns
29
+ */
30
+ export declare const usePDFSlick: TUsePDFSlick;
31
+ export {};
32
+ //# sourceMappingURL=usePDFSlick.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePDFSlick.d.ts","sourceRoot":"","sources":["../usePDFSlick.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAoC,WAAW,EAAW,MAAM,OAAO,CAAC;AAC/E,OAAO,EAAE,QAAQ,EAAY,MAAM,SAAS,CAAC;AAE7C,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACrE,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,aAAa,CAAC;IAClB,CAAC,CAAC,EACA,QAAQ,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,CAAC,EACrC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC,GAAG,SAAS,GAC7C,CAAC,CAAC;CACN,CAAC;AAEF,KAAK,YAAY,GAAG,CAClB,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,EAE7B,OAAO,CAAC,EAAE,eAAe,KACtB;IACH,gBAAgB,EAAE,OAAO,CAAC;IAC1B,SAAS,EAAE,WAAW,CAAC,WAAW,CAAC,CAAC;IACpC,SAAS,EAAE,WAAW,CAAC,WAAW,CAAC,CAAC;IACpC,KAAK,EAAE,QAAQ,CAAC,aAAa,CAAC,CAAC;IAC/B,gBAAgB,EAAE,iBAAiB,CAAC;IACpC,cAAc,EAAE,OAAO,cAAc,CAAC;IACtC,kBAAkB,EAAE,OAAO,kBAAkB,CAAC;CAC/C,CAAC;AAEF,wBAAgB,WAAW,CAAC,KAAK,EAAE,QAAQ,CAAC,aAAa,CAAC;QAC3B,aAAa;0BAEtB,aAAa,kCACN,OAAO;EAUnC;AAED;;;;;GAKG;AACH,eAAO,MAAM,WAAW,EAAE,YA+CzB,CAAC"}