viewdoc 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +151 -0
- package/dist/index.cjs +994 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +316 -0
- package/dist/index.d.ts +316 -0
- package/dist/index.js +946 -0
- package/dist/index.js.map +1 -0
- package/dist/styles.css +198 -0
- package/dist/styles.css.map +1 -0
- package/dist/styles.d.cts +2 -0
- package/dist/styles.d.ts +2 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# viewdoc
|
|
2
|
+
|
|
3
|
+
Universal React document viewer — images, PDFs, Word documents and Excel spreadsheets, all with zoom, pan, fullscreen and a consistent toolbar.
|
|
4
|
+
|
|
5
|
+
```tsx
|
|
6
|
+
import { DocViewer } from 'viewdoc'
|
|
7
|
+
import 'viewdoc/styles.css'
|
|
8
|
+
|
|
9
|
+
<DocViewer uri="https://example.com/report.pdf" />
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
That's it — `DocViewer` detects the file type from the URL and renders the right viewer.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install viewdoc
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`react` and `react-dom` (17+) are peer dependencies — install them if your project doesn't already have them.
|
|
21
|
+
|
|
22
|
+
Don't forget the stylesheet:
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import 'viewdoc/styles.css'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Supported file types
|
|
29
|
+
|
|
30
|
+
| Type | Extensions | Powered by |
|
|
31
|
+
|---|---|---|
|
|
32
|
+
| Images | `.png` `.jpg/.jpeg` `.gif` `.webp` `.svg` `.bmp` `.ico` `.avif` | native `<img>` |
|
|
33
|
+
| PDF | `.pdf` | `pdfjs-dist` |
|
|
34
|
+
| Word | `.docx` | `mammoth` |
|
|
35
|
+
| Excel | `.xlsx` `.xls` (legacy binary) `.xlsm` `.csv` | `xlsx` (SheetJS) |
|
|
36
|
+
|
|
37
|
+
**Not supported:** old binary `.doc` (pre-2007 Word) — there's no reliable way to parse it in the browser. TIFF/HEIC/RAW images also aren't supported since browsers can't decode them natively.
|
|
38
|
+
|
|
39
|
+
## Components
|
|
40
|
+
|
|
41
|
+
### `DocViewer` (recommended)
|
|
42
|
+
|
|
43
|
+
Auto-detects the file type and renders the matching viewer.
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
<DocViewer uri="https://example.com/invoice.xlsx" fileName="invoice.xlsx" />
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Detection reads the URL's path (ignoring query strings, so presigned S3/CloudFront URLs work correctly). If your URL has no reliable extension (e.g. an opaque storage key), force it:
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
<DocViewer uri="https://example.com/blob/9f8a2b" type="pdf" />
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`type` also accepts a MIME type (`"application/pdf"`, `"image/png"`, etc.) if that's what you have on hand.
|
|
56
|
+
|
|
57
|
+
If detection fails and no `type` is given, `DocViewer` renders a `fallback` node (or a small default message):
|
|
58
|
+
|
|
59
|
+
```tsx
|
|
60
|
+
<DocViewer uri={file.url} fallback={<p>Can't preview this file type.</p>} />
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Individual viewers
|
|
64
|
+
|
|
65
|
+
Use these directly if you already know the file type, or want a smaller bundle (each one is independently tree-shakeable):
|
|
66
|
+
|
|
67
|
+
```tsx
|
|
68
|
+
import { ImageViewer, PdfViewer, DocxViewer, XlsxViewer } from 'viewdoc'
|
|
69
|
+
|
|
70
|
+
<ImageViewer uri="https://example.com/photo.jpg" />
|
|
71
|
+
<PdfViewer uri="https://example.com/report.pdf" />
|
|
72
|
+
<DocxViewer uri="https://example.com/letter.docx" />
|
|
73
|
+
<XlsxViewer uri="https://example.com/budget.xlsx" />
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Props
|
|
77
|
+
|
|
78
|
+
`DocViewer` and all four viewers share the same prop shape (`DocViewer` just adds `type` and `fallback` on top):
|
|
79
|
+
|
|
80
|
+
| Prop | Type | Default | Description |
|
|
81
|
+
|---|---|---|---|
|
|
82
|
+
| `uri` | `string` | *required* | URL or data URI of the file |
|
|
83
|
+
| `fileName` | `string` | — | Alt text / suggested download filename |
|
|
84
|
+
| `className` / `style` | — | — | Applied to the outer wrapper |
|
|
85
|
+
| `width` / `height` | `number \| string` | `1200` / `700` | Viewer size (px number or any CSS size) |
|
|
86
|
+
| `minScale` / `maxScale` / `zoomStep` | `number` | varies by viewer | Zoom range and increment |
|
|
87
|
+
| `enableZoomControls` | `boolean` | `true` | Show zoom out/in/reset buttons |
|
|
88
|
+
| `enableWheelZoom` | `boolean` | `true` | Ctrl/Cmd + scroll wheel to zoom |
|
|
89
|
+
| `enablePan` *(ImageViewer only)* | `boolean` | `true` | Click-drag-release panning of the image |
|
|
90
|
+
| `enableFullscreen` | `boolean` | `true` | Show fullscreen toggle |
|
|
91
|
+
| `enableDownload` | `boolean` | `true` | Show download button |
|
|
92
|
+
| `onDownload` | `() => void` | — | Custom download handler; defaults to downloading `uri` |
|
|
93
|
+
| `theme` | `ViewerTheme` | — | Override colors/radii (see below) |
|
|
94
|
+
| `floating` | `boolean` | `true` | Render as a centered, draggable floating window vs. an embedded block that fills the parent |
|
|
95
|
+
| `windowDraggable` | `boolean` | `true` | When floating, allow dragging via the toolbar's grip icon |
|
|
96
|
+
| `defaultPosition` | `{ x: number; y: number }` | centered in viewport | Initial position when floating |
|
|
97
|
+
|
|
98
|
+
Every boolean defaults to `true` — pass `false` to turn a feature off. Nothing needs to be configured to get a fully working viewer.
|
|
99
|
+
|
|
100
|
+
## Theming
|
|
101
|
+
|
|
102
|
+
Pass a `theme` object to override the default dark look. Any field you omit keeps its default:
|
|
103
|
+
|
|
104
|
+
```tsx
|
|
105
|
+
<DocViewer
|
|
106
|
+
uri={file.url}
|
|
107
|
+
theme={{
|
|
108
|
+
background: '#0b1220',
|
|
109
|
+
toolbarBackground: '#111827',
|
|
110
|
+
toolbarBorderColor: '#1f2937',
|
|
111
|
+
toolbarButtonHoverBackground: '#1f2937',
|
|
112
|
+
textColor: '#93c5fd',
|
|
113
|
+
borderRadius: '16px',
|
|
114
|
+
}}
|
|
115
|
+
/>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Available fields: `background`, `toolbarBackground`, `toolbarBorderColor`, `toolbarButtonHoverBackground`, `textColor`, `borderRadius`, `toolbarButtonRadius`.
|
|
119
|
+
|
|
120
|
+
## Embedded vs. floating mode
|
|
121
|
+
|
|
122
|
+
By default every viewer opens as a **floating window**: centered on screen, draggable by its toolbar grip icon, sized 1200×700. This is meant for "preview this file" use cases (e.g. clicking a file in a list).
|
|
123
|
+
|
|
124
|
+
If you want the viewer to sit inline in your layout instead — filling whatever container you put it in — set `floating={false}`:
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
<div style={{ width: 600, height: 400 }}>
|
|
128
|
+
<DocViewer uri={file.url} floating={false} />
|
|
129
|
+
</div>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
In non-floating mode, the viewer still renders at its `width`/`height` (default 1200×700) and centers itself inside the parent container.
|
|
133
|
+
|
|
134
|
+
## Interaction reference
|
|
135
|
+
|
|
136
|
+
- **Zoom**: `+`/`−` buttons, click the `%` label or the reset icon to return to 100%, or hold Ctrl/Cmd and scroll
|
|
137
|
+
- **Pan**: click-drag the content directly (images), or click-drag/scroll anywhere on the canvas (PDF, DOCX, XLSX — these use native scrolling under the hood, so the mouse wheel always works too)
|
|
138
|
+
- **Move the window**: click-drag the small grip icon (⊹) at the left of the toolbar (only shown when `floating` is `true`)
|
|
139
|
+
- **Fullscreen**: toggle button on the right of the toolbar
|
|
140
|
+
- **Download**: button next to fullscreen; downloads `uri` unless you pass `onDownload`
|
|
141
|
+
|
|
142
|
+
## Known limitations
|
|
143
|
+
|
|
144
|
+
- Old binary `.doc`/legacy Word formats before 2007 aren't supported (only `.docx`)
|
|
145
|
+
- The Excel table view is read-only — no cell editing or selection
|
|
146
|
+
- `DocxViewer` and `XlsxViewer` render content via `dangerouslySetInnerHTML` using HTML generated from the file itself (by `mammoth`/`xlsx`) — treat untrusted files with the same caution you'd apply to any HTML you didn't author
|
|
147
|
+
- PDF rendering resolves its worker script via `import.meta.url`, which requires an ESM-aware bundler (Vite, Webpack 5+, esbuild). CommonJS (`require`) consumers may need extra worker configuration
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
MIT
|