English | 简体中文
Say goodbye to software downloads! Our powerful online viewer gives you instant access to all your files. Effortlessly open and preview DOCX, XLSX, PPTX, TXT, CODE and many other formats with just one click.
From complex Excel spreadsheets to important Word documents and critical PowerPoint presentations, we've got you covered. Experience the freedom of an all-in-one platform designed for ultimate convenience.
Live Demo: https://viewer.sweetui.com
- Multi-format support — DOCX, PPTX, XLSX, TXT / code files
- Framework-agnostic — Vanilla JS core with React / Vue adapters
- Pure front-end parsing — Files never leave the browser, privacy-safe
- Smart encoding detection — TXT auto-detects UTF-8 / UTF-16 / GBK / GB18030
- Line-number sync — TXT preview with line numbers, highlight, scroll sync
- TXT dark mode — Built-in dark theme for TXT / code preview
- Modal / inline — Full-screen overlay or embedded container modes
- Event system — loadstart / loaded / error / close
npm install sweet-viewer
# or
pnpm add sweet-viewer
# or
yarn add sweet-viewerimport { createViewer } from "sweet-viewer";
import "sweet-viewer/style.css"; // optional, if the build does not inject styles
const viewer = createViewer("#container", { overlay: true });
viewer.loadFile(file);
viewer.on("loaded", () => console.log("rendered"));
viewer.on("error", (e) => console.error(e));import { SweetViewerReact } from "sweet-viewer/react";
import "sweet-viewer/style.css";
function App() {
const [file, setFile] = useState<File | null>(null);
return (
<>
<input type="file" onChange={(e) => setFile(e.target.files?.[0] || null)} />
<SweetViewerReact source={file} overlay onLoaded={() => console.log("done")} />
</>
);
}The component renders a wrapper <div class="sv-container"> (plus your custom class). You can customize it via optional className and style props:
<SweetViewerReact
source={file}
className="my-viewer"
style={{ height: "80vh", borderRadius: 12 }}
/><script setup lang="ts">
import { SweetViewerVue } from "sweet-viewer/vue";
import "sweet-viewer/style.css";
import { ref } from "vue";
const file = ref<File | null>(null);
</script>
<template>
<input type="file" @change="(e) => file = (e.target as HTMLInputElement).files?.[0] || null" />
<SweetViewerVue :source="file" overlay @loaded="() => console.log('done')" />
</template>The Vue component also supports optional class and style props:
<SweetViewerVue
:source="file"
class="my-viewer"
:style="{ height: '80vh', borderRadius: '12px' }"
/>| Prop | Type | Default | Description |
|---|---|---|---|
| source | File | Blob | ArrayBuffer | string | null |
null |
Document source to preview (auto-reloads on change) |
| fileName | string |
— | File name for type inference (optional, auto-detected from File) |
| className (React) / class (Vue) | string |
— | Custom class appended to the wrapper div (after sv-container) |
| style | React.CSSProperties / Record<string, any> |
— | Custom inline styles for the wrapper div |
| theme | "light" | "dark" |
"light" |
Theme |
| overlay | boolean |
false |
Full-screen overlay mode |
| showHeader | boolean |
true |
Show file name header bar |
| showClose | boolean |
true |
Show close button |
| txtMaxSize | number |
5242880 |
Max bytes for TXT preview. Set to 0 or Infinity to disable |
| txtMaxLines | number |
20000 |
Max lines rendered for TXT (truncated beyond) |
| maxSize | number |
52428800 |
Max bytes for DOCX/PPTX/XLSX (50MB). Set to 0 or Infinity to disable |
React events:
onLoadStart/onLoaded/onError/onClose
Vue events:@loadstart/@loaded/@error/@close
| Option | Type | Default | Description |
|---|---|---|---|
| theme | "light" | "dark" |
"light" |
Theme (dark mode currently only affects TXT / code preview) |
| overlay | boolean |
false |
Full-screen overlay mode |
| showHeader | boolean |
true |
Show file name header bar |
| showClose | boolean |
true |
Show close button |
| width | string | number |
"100%" |
Container width |
| height | string | number |
"100%" |
Container height |
| txtMaxSize | number |
5242880 |
Max bytes for TXT preview. Set to 0 or Infinity to disable |
| txtMaxLines | number |
20000 |
Max lines rendered for TXT (truncated beyond) |
| maxSize | number |
52428800 |
Max bytes for DOCX/PPTX/XLSX (50MB). Set to 0 or Infinity to disable |
load(source, name?)— Load and render a documentloadFile(file)— Shortcut, pass a File directlyclear()— Clear current contentsetTheme(theme)— Switch themesetWidth(width)/setHeight(height)— Update container size at runtimesetShowClose(bool)/setShowHeader(bool)— Toggle UI elements at runtimeshow()— Re-display after overlay closeon(event, cb)/off(event, cb)— Event listenersdestroy()— Destroy the instance
loadstart— Loading startedloaded— Loading completederror— An error occurredclose— Viewer closed
| Type | Extensions | Underlying Library |
|---|---|---|
| DOCX | .docx | docx-preview |
| PPTX | .pptx | pptx-preview |
| XLSX | .xlsx | xlsx-preview |
| TXT / Code | .txt .md .json .js .ts .csv .log .xml .yml ... | Built-in |
Try it live: https://viewer.sweetui.com
git clone https://github.com/leuvi/sweet-viewer.git
cd sweet-viewer
npm install
npm run devOpen http://localhost:5173 and drop a file to preview.
MIT © @苹果熊