Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

BrowserDocumentFrame.tsx

40 lines1.3 KB

1/**
2 * Chromium's viewer opens with its own toolbar, a thumbnail rail and a small
3 * zoom. The panel header is the only chrome we want, so ask for the page
4 * alone, fitted to the panel width. Pinch and keyboard zoom, scrolling, text
5 * selection and find still work inside the frame.
6 */
7const PDF_VIEWER_FRAGMENT = "#toolbar=0&view=FitH";
8​
9export const isPdfPreviewFile = (path: string): boolean =>
10 /\.pdf$/i.test(path.split(/[?#]/, 1)[0] ?? "");
11​
12/**
13 * Renders an HTML or PDF document from its URL. HTML runs in a sandboxed frame
14 * with an opaque origin, so a page cannot reach the app's session or storage.
15 * The built-in PDF viewer needs an unsandboxed frame; a PDF runs no scripts.
16 */
17export function BrowserDocumentFrame(props: {
18 readonly src: string;
19 readonly title: string;
20 readonly pdf: boolean;
21}) {
22 const className = "min-h-0 flex-1 border-0 bg-white";
23 return props.pdf ? (
24 // oxlint-disable-next-line react/iframe-missing-sandbox
25 <iframe
26 key={props.src}
27 src={`${props.src}${PDF_VIEWER_FRAGMENT}`}
28 title={props.title}
29 className={className}
30 />
31 ) : (
32 <iframe
33 key={props.src}
34 src={props.src}
35 title={props.title}
36 className={className}
37 sandbox="allow-scripts allow-forms allow-popups allow-modals"
38 />
39 );