From f99700c696b03ede5128dec52035b66d613529ac Mon Sep 17 00:00:00 2001 From: s8613 Date: Thu, 29 May 2025 09:19:02 +0200 Subject: [PATCH] Made PDF bit more responsive. --- project/frontend/src/components/pdfViewer.tsx | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/project/frontend/src/components/pdfViewer.tsx b/project/frontend/src/components/pdfViewer.tsx index f90af0b..5e0ad8d 100644 --- a/project/frontend/src/components/pdfViewer.tsx +++ b/project/frontend/src/components/pdfViewer.tsx @@ -1,5 +1,5 @@ import { Document, Page, pdfjs } from "react-pdf"; -import { useState } from 'react'; +import { useState, useRef, useEffect } from 'react'; import 'react-pdf/dist/esm/Page/AnnotationLayer.css'; import 'react-pdf/dist/esm/Page/TextLayer.css'; import { Box, IconButton } from '@mui/material'; @@ -15,11 +15,26 @@ pdfjs.GlobalWorkerOptions.workerSrc = new URL( export default function PDFViewer() { const [numPages, setNumPages] = useState(null); const [pageNumber, setPageNumber] = useState(1); + const [containerWidth, setContainerWidth] = useState(null); + + const containerRef = useRef(null); const onDocumentLoadSuccess = ({ numPages }: { numPages: number }) => { setNumPages(numPages); }; + useEffect(() => { + const updateWidth = () => { + if (containerRef.current) { + setContainerWidth(containerRef.current.offsetWidth); + } + }; + + updateWidth(); + window.addEventListener('resize', updateWidth); + return () => window.removeEventListener('resize', updateWidth); + }, []); + return ( - + {containerWidth && ( + + )}