Portal/rsconcept/frontend/src/components/View/PDFViewer.tsx

34 lines
916 B
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
import useWindowSize from '@/hooks/useWindowSize';
import { useFitHeight } from '@/stores/appLayout';
2024-06-07 20:17:03 +03:00
/** Maximum width of the viewer. */
const MAXIMUM_WIDTH = 1600;
/** Minimum width of the viewer. */
2024-06-07 20:17:03 +03:00
const MINIMUM_WIDTH = 300;
interface PDFViewerProps {
/** PDF file to display. */
file?: string;
/** Offset from the left side of the window. */
2024-06-07 20:17:03 +03:00
offsetXpx?: number;
/** Minimum width of the viewer. */
2024-06-07 20:17:03 +03:00
minWidth?: number;
}
/**
* Displays a PDF file using an embedded viewer.
*/
2025-02-07 10:53:49 +03:00
export function PDFViewer({ file, offsetXpx, minWidth = MINIMUM_WIDTH }: PDFViewerProps) {
2024-06-07 20:17:03 +03:00
const windowSize = useWindowSize();
const pageWidth = Math.max(minWidth, Math.min((windowSize?.width ?? 0) - (offsetXpx ?? 0) - 10, MAXIMUM_WIDTH));
const pageHeight = useFitHeight('1rem');
2024-06-07 20:17:03 +03:00
return <embed src={`${file}#toolbar=0`} className='p-3' style={{ width: pageWidth, height: pageHeight }} />;
2024-06-07 20:17:03 +03:00
}