I'm trying to show a PDF in my page by using "react-pdf". I am now struggling with making it responsive. I am now using this custom hook to get the size of the contentRect:
import { useEffect, useState, useCallback } from "react"; interface Size { width: number; height: number; } const useResizeObserver = (targetRef: HTMLElement | null): Size => { const [size, setSize] = useState<Size>({ width: 0, height: 0 }); const handleResize = useCallback((entries: ResizeObserverEntry[]) => { const [entry] = entries; if (entry) { setSize({ width: entry.contentRect.width, height: entry.contentRect.height, }); } }, []); useEffect(() => { if (!targetRef) return; const resizeObserver = new ResizeObserver(handleResize); resizeObserver.observe(targetRef); return () => { resizeObserver.disconnect(); }; }, [targetRef, handleResize]); return size; }; export default useResizeObserver; This is my PDFViewer.tsx that is used in the page.tsx:
"use client"; import React, { useRef, useState } from "react"; import { Document, Page } from "react-pdf"; import "react-pdf/dist/esm/Page/AnnotationLayer.css"; import { pdfjs } from "react-pdf"; import "react-pdf/dist/Page/TextLayer.css"; import type { PDFDocumentProxy } from 'pdfjs-dist'; import useResizeObserver from "@/hooks/useResizeObserver"; pdfjs.GlobalWorkerOptions.workerSrc = new URL( "pdfjs-dist/build/pdf.worker.min.mjs", import.meta.url ).toString(); const PDFViewer: React.FC = () => { const [numPages, setNumPages] = useState<number>(0); const [pageNumber, setPageNumber] = useState<number>(1); const containerRef = useRef<HTMLDivElement | null>(null); const { width: pdfWidth, height: pdfHeight } = useResizeObserver(containerRef.current); const pdfUrl = "/docs/menu.pdf"; function onDocumentLoadSuccess({ numPages: nextNumPages }: PDFDocumentProxy): void { setNumPages(nextNumPages); } return ( <div className="flex flex-row left-2 mt-4 justify-center items-center w-full h-auto px-4"> <div className="flex flex-1 font-generalRegular justify-right text-right text-primary text-6xl px-4"> {" < "} </div> <div id="containerRef" ref={containerRef} className="flex flex-8 flex-col font-generalRegular justify-center text-center text-primary text-sm px-4"> <p>Menu</p> <p>Page {pageNumber} of {numPages}</p> <p>Width: {pdfWidth}</p> <Document file={pdfUrl} onLoadSuccess={onDocumentLoadSuccess}> <Page pageNumber={pageNumber} width={pdfWidth}/> </Document> </div> <div className="flex flex-1 font-generalRegular justify-left text-left text-primary text-6xl px-4"> {" > "} </div> </div> ); }; export default PDFViewer; As you might notice, I've added a text element to print the width. Yet, it never changes. Even when I constantly resize the browser window. Can anybody tell me what went wrong here?
Thank you.