Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/viewer-reading-controls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@transloadit/viewer': minor
---

Control PDF fit or zoom across readers with `zoom` and `onZoomChange`, and opt into tap-to-expand on compact pages with `expandOnPageTap`. More actions stays open through repeated zoom steps.

Manual zoom now keeps its absolute text size when the reader resizes or enters fullscreen. Fit modes follow the available space. Fullscreen zoom and fit choices last until exit, which restores the saved inline setting. In controlled readers, echo `onZoomChange` into `zoom` for the current view. A different host-supplied `zoom` overrides both views and becomes the inline setting restored on exit. Desktop page clicks request native fullscreen immediately, while touch waits for a single tap.

The PDF toolbar shows the page total as `/ {count}`. Customize the page border with `--tl-viewer-page-edge`. The reader paints its focus ring above the page for keyboard `:focus-visible` and programmatic focus, including `autoFocus` in WebKit.

RTF previews display recognized monospace fonts using `ui-monospace, monospace`.
449 changes: 440 additions & 9 deletions packages/img/browser/documents.spec.ts

Large diffs are not rendered by default.

31 changes: 30 additions & 1 deletion packages/img/browser/fixture.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { PdfZoom } from '@transloadit/viewer/react/viewer'

import { useDocumentPreview, Viewer } from '@transloadit/viewer/react/viewer'
import { useState } from 'react'
import { createRoot } from 'react-dom/client'
Expand All @@ -17,6 +19,8 @@ const params = new URLSearchParams(location.search)
function Fixture() {
const [missingWorker, setMissingWorker] = useState(params.has('missingWorker'))
const [currentPage, setCurrentPage] = useState(2)
const [zoom, setZoom] = useState<PdfZoom>('width')
const [large, setLarge] = useState(false)
const [loaded, setLoaded] = useState(0)
const preview = useDocumentPreview({ source, format: 'markdown' })
const stalled = params.has('stalled')
Expand All @@ -27,7 +31,7 @@ function Fixture() {
const [renewals, setRenewals] = useState(0)
if (params.has('rich')) {
const bytes = new TextEncoder().encode(
String.raw`{\rtf1\ansi{\colortbl;\red128\green0\blue0;\red255\green255\blue0;\red136\green136\blue136;\red137\green137\blue137;}\cf1 Maroon\par\cf2 Yellow\par\cf3 Below threshold\par\cf4 Above threshold}`,
String.raw`{\rtf1\ansi{\fonttbl{\f0 Arial;}{\f1 cOuRiEr NeW;}}{\colortbl;\red128\green0\blue0;\red255\green255\blue0;\red136\green136\blue136;\red137\green137\blue137;}\f1 Fixed width\par\f0\cf1 Maroon\par\cf2 Yellow\par\cf3 Below threshold\par\cf4 Above threshold}`,
).buffer
const columns = Array.from({ length: 12 }, () => 'T')
const markdown = [
Expand Down Expand Up @@ -64,6 +68,30 @@ function Fixture() {
</>
)
}
if (params.has('handbook'))
return (
<div style={{ width: large ? 700 : 322 }}>
<Viewer
key={String(large)}
kind="pdf"
mode="pdfjs"
src={`${fileOrigin}/handbook`}
title={large ? 'Larger handbook' : 'Handbook'}
expandOnPageTap={params.has('tap')}
page={currentPage}
onPageChange={(next) => {
setCurrentPage(next)
if (params.has('pageZoom')) setZoom(2)
}}
zoom={zoom}
onZoomChange={setZoom}
originalLink={<button type="button">Download</button>}
/>
<button type="button" onClick={() => setLarge(!large)}>
Switch reader
</button>
</div>
)
if (params.has('frame'))
return (
<Viewer
Expand Down Expand Up @@ -91,6 +119,7 @@ function Fixture() {
page={params.has('controlled') ? currentPage : undefined}
onPageChange={setCurrentPage}
autoFocus={params.has('focus')}
expandOnPageTap={params.has('tap')}
filename="slides.pdf"
renew={(signal) => {
signal.throwIfAborted()
Expand Down
6 changes: 6 additions & 0 deletions packages/img/docs/react-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ Set `documentKey` to a stable identifier for the immutable asset version. Update

Import the stylesheet for responsive controls, fullscreen layout and the positioned text layer. Each page keeps its old canvas and text until the replacement is ready, with up to four million backing pixels per canvas. Give the reader a height-constrained container to fit the available space below its toolbar and status. In an auto-height container the page area defaults to `70dvh`. Narrow readers follow the page aspect ratio and move zoom, fit and download actions into More actions. The optional `--tl-viewer-pdf-height` adjusts the page area. Readers can select text, type a page number, zoom, fit width, fit the page or expand to fullscreen. Set `autoFocus` to focus the reader on opening. Swipe horizontally to turn pages when the page fits the viewport width. Left/Right and PageUp/PageDown move one page, Home/End jump to the ends. On vertically overflowing pages, start a new wheel gesture at the top or bottom of the page to turn one page. Fitted pages leave wheel scrolling to the host. Escape closes More actions first, then exits fullscreen. Pass an `originalLink` element only when downloads are allowed. For short-lived URLs the app can resolve a fresh grant when the user activates that action.

For an inline reader that opens a separate larger view, keep `page` and `zoom` in the host and pass `onPageChange` and `onZoomChange` to both readers. The exported `PdfZoom` type accepts `'width'` (the default), `'page'`, or a positive absolute scale, where 1 is 100%. Fit modes recalculate for each viewport. Manual zoom keeps the same text size. Zoom buttons step between one quarter and four times the starting fit scale. When a separate reader starts at a manual scale, its zoom steps use that scale as their starting point.

Entering fullscreen saves the inline fit or scale and its zoom-step limits. Zoom and fit choices made in fullscreen apply until exit, when the reader restores the saved inline setting. For controlled readers, echo each `onZoomChange` value into `zoom` to accept the choice for the current view. Supplying a different `zoom` while expanded is an explicit host override for both views and becomes the new inline setting. On exit, `onZoomChange` reports the restored setting if it differs from the displayed zoom.

Set `expandOnPageTap` to open fullscreen when the user taps the page in a reader up to 512 CSS pixels wide. With a mouse, clicking the page background requests native fullscreen immediately and double-clicking text selects a word. On touchscreens, a double tap leaves the reader inline. The option defaults to false.

A strict application CSP can allow `script-src 'self'`, `worker-src 'self'`, `style-src 'self'`, `font-src 'self'` and `connect-src 'self' https://your-file-origin.example`. Serve chunks, fonts and the worker on the app origin. The worker response also needs `connect-src 'self'` and `script-src 'self'` to load matching maps, fonts and scan decoders. This configuration uses PDF.js fallback colors for ICC profiles.

`mode="frame"` is the default. Point it at an endpoint verified to serve `application/pdf` throughout navigation, and allow that host in `frame-src`. Pass its verified `mime` when available. The frame shows `loadingFallback` while opening. A browser error or a load exceeding `timeoutMs` (30 seconds by default) shows Retry. Use `renderError(reason, retry)` to customize this feedback. The browser frame receives an absolute `src` resolved against the document base URI. `onLoad(event)` receives the React iframe load event. `onError(reason)` reports invalid inputs or browser-reported failures. Browsers hide cross-origin HTTP status from iframe callbacks. Set `unsupportedFallback` for browsers that report no inline PDF support. For a GET-only signed URL, resolve the grant from your data layer and pass it directly to PDF.js. Office documents require upstream conversion.
Expand Down
Loading
Loading