diff --git a/components/previews/PDFPreview.tsx b/components/previews/PDFPreview.tsx index acd5ce1..2d119c8 100644 --- a/components/previews/PDFPreview.tsx +++ b/components/previews/PDFPreview.tsx @@ -1,4 +1,4 @@ -import { FunctionComponent, useState } from 'react' +import { createRef, FunctionComponent, useEffect, useRef, useState } from 'react' import { Document, Page, pdfjs } from 'react-pdf' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' @@ -12,60 +12,82 @@ const PDFPreview: FunctionComponent<{ file: any }> = ({ file }) => { const [totalPages, setTotalPages] = useState(0) const [loadingText, setLoadingText] = useState('Loading PDF ...') + const [pdfContainerWidth, setPdfContainerWidth] = useState(400) + const pdfContainter = useRef(null) + const onDocumentLoadSuccess = (pdf: any) => { setTotalPages(pdf.numPages) } + useEffect(() => { + setPdfContainerWidth(pdfContainter.current ? pdfContainter.current.offsetWidth : 400) + }, []) + return ( -
-
- } - onLoadProgress={({ loaded, total }) => { - setLoadingText(`Loading PDF ${Math.round((loaded / total) * 100)}%`) - }} - > - - -
-
- -
- Page{' '} - { - const v = parseInt(e.target.value) - if (v <= totalPages && v >= 0) { - setPageNumber(v) - } + <> +
+
+ } + onLoadProgress={({ loaded, total }) => { + setLoadingText(`Loading PDF ${Math.round((loaded / total) * 100)}%`) }} - > - /{totalPages} + > + 400 ? pdfContainerWidth * 0.8 : pdfContainerWidth} + /> +
- + +
+ +
+ Page{' '} + { + const v = parseInt(e.target.value) + if (v <= totalPages && v >= 0) { + setPageNumber(v) + } + }} + > + /{totalPages} +
+ +
+
+
-
+ ) } diff --git a/styles/globals.css b/styles/globals.css index a701530..d10112d 100644 --- a/styles/globals.css +++ b/styles/globals.css @@ -3,3 +3,7 @@ @tailwind base; @tailwind components; @tailwind utilities; + +.react-pdf__Page__canvas { + @apply mx-auto; +}