- Developer home
- Embed editors
- Framework components
Framework components
Framework components
The package ships a component for each framework, as subpath exports of mroffice-embed: one install, and each framework is an optional peer dependency, so your bundle takes only the entry you import. Every component opens the same editor as MROffice.open(), takes the same options as props, and reports the same events.
npm install mroffice-embed| Framework | Import | Component, handle, events |
|---|---|---|
React 17+ | mroffice-embed/react | <MROfficeEditor ref={editorRef} … />; ref is the editor's handle; onReady, onSave, … props |
Next.js App Router | mroffice-embed/react | A client component ("use client" is in the entry): use it from a server component; the server renders an empty container |
Vue 3.3+ / Nuxt | mroffice-embed/vue | <MROfficeEditor ref="editor" … />, @ready, @save, …; useMROfficeEditor(container, options) composable; SSR-safe |
Angular 17+ | mroffice-embed/angular | Standalone MROfficeEditorComponent, <mroffice-editor>, [inputs], (ready), (save), …; runs outside the zone, emits inside it; SSR-safe |
Svelte 4/5 / SvelteKit | mroffice-embed/svelte | <MROfficeEditor bind:editor … />, onSave={…} props (or on:save); the use:mroffice action |
Any (Web Component) | mroffice-embed/element | <mroffice-editor> with attributes and properties; mroffice-ready, mroffice-save, … events; also https://mroffice.ai/embed/v1/element.js |
- tokenUrl is the shortest way in: a route of your server that answers { token, appId?, origin? } as JSON (or the token as text). The component calls it with ?reason=start|refresh|reload|mode&mode=… whenever it needs a fresh token. token (a string, or a function as in open()) works as well.
- Props change the editor the cheapest way it allows: callbacks and the token function are read when called; theme and ui.theme are set in place, locale reloads the frame in that language (same document), mode calls setMode with a token signed for it; another file, app, origin, ui (toolbar, features, brand) or downloads opens a new editor. A new token string for the same file is kept for the next fresh token.
- The handle (React ref, Vue template ref, Svelte bind:editor, the Angular component, the element itself) has requestSave(), setMode(), setTheme(), setLocale(), focus(), openFile(), destroy(), getCapabilities(), word / excel / powerpoint / pdf / document (Automation API), ready and current; it stays the same across a reopen.
- Server rendering: every component renders only its container on the server and opens the editor in the browser. React's StrictMode and a moved element keep the session (a token starts one session, once).
- Give the component a height (style, a class, or a flex parent): it fills its container, at least 320 px.
- The new SDK events are props there too: onRequestUsers (your directory for @-mentions), onUsersChange and onMention (Vue @users-change / @mention, Angular (usersChange) / (mention), the element's mroffice-users-change / mroffice-mention).
"use client"; // only for a page that is itself a client component
import { useRef } from "react";
import { MROfficeEditor, type MROfficeEditorHandle } from "mroffice-embed/react";
export function ContractEditor({ contractId }: { contractId: string }) {
const editor = useRef<MROfficeEditorHandle>(null);
return (
<>
<button onClick={() => editor.current?.requestSave()}>Save</button>
<MROfficeEditor
ref={editor}
appId={process.env.NEXT_PUBLIC_MROFFICE_APP_ID}
// asked again for a fresh token whenever one is needed
token={({ mode }) => fetch(`/api/contracts/${contractId}/editor-token?mode=${mode ?? "edit"}`).then((r) => r.text())}
locale="en"
ui={{ toolbar: "full", features: { print: false } }}
onReady={(event) => console.log(event.document.name)}
onSave={(saved) => console.log(saved.version, saved.downloadUrl)}
onError={(error) => console.error(error.code, error.hint)}
style={{ height: "80vh" }}
/>
</>
);
}// app/contracts/[id]/page.tsx — a server component: the editor is a client
// component, rendered as an empty <div> on the server and opened in the browser.
import { MROfficeEditor } from "mroffice-embed/react";
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
return <MROfficeEditor tokenUrl={`/api/mroffice/session?contract=${id}`} style={{ height: "100vh" }} />;
}// GET /api/mroffice/session?reason=start|refresh|reload|mode&mode=edit
// (what tokenUrl calls; npx mroffice-embed init writes it)
import { createSessionToken } from "mroffice-embed/server";
export async function GET(request: Request) {
const user = await requireUser(request); // your sign-in
const mode = new URL(request.url).searchParams.get("mode") === "view" ? "view" : "edit";
const token = await createSessionToken({
appId: process.env.MROFFICE_APP_ID!, secretKey: process.env.MROFFICE_SECRET_KEY!,
file: { key: "contract-42", name: "contract.docx", url: await signedUrlFor("contract-42") },
user: { id: user.id, name: user.name }, mode,
});
return Response.json({ token, appId: process.env.MROFFICE_APP_ID }, { headers: { "cache-control": "no-store" } });
}<script setup lang="ts">
import { ref } from "vue";
import { MROfficeEditor, useMROfficeEditor, type MROfficeEditorHandle } from "mroffice-embed/vue";
const editor = ref<MROfficeEditorHandle | null>(null);
const locale = ref("en"); // changing it reloads the editor in that language, same document
</script>
<template>
<button @click="editor?.requestSave()">Save</button>
<MROfficeEditor ref="editor" token-url="/api/mroffice/session" :locale="locale"
:ui="{ toolbar: 'full' }" @ready="(e) => console.log(e.document.name)" @save="onSaved" @error="onError"
style="height: 80vh" />
</template>
<!-- Or the composable, in a container of your own:
const container = ref<HTMLElement | null>(null);
const { editor, ready, dirty, error } = useMROfficeEditor(container, () => ({ tokenUrl: "/api/mroffice/session" }));
-->import { Component, ViewChild } from "@angular/core";
import { MROfficeEditorComponent } from "mroffice-embed/angular";
import type { ReadyEvent, SavedEvent } from "mroffice-embed";
@Component({
selector: "app-contract",
standalone: true,
imports: [MROfficeEditorComponent],
template: `
<button (click)="editor.requestSave()">Save</button>
<mroffice-editor #editor tokenUrl="/api/mroffice/session" locale="en"
[ui]="{ toolbar: 'full' }" (ready)="onReady($event)" (save)="onSave($event)"
style="height: 80vh" />`,
})
export class ContractComponent {
@ViewChild("editor") editor!: MROfficeEditorComponent;
onReady(event: ReadyEvent) { console.log(event.document.name); }
onSave(saved: SavedEvent) { console.log(saved.version); }
}<script lang="ts">
import { MROfficeEditor, type MROfficeEditorHandle } from "mroffice-embed/svelte";
let editor: MROfficeEditorHandle | undefined = $state(); // Svelte 4: let editor;
</script>
<button onclick={() => editor?.requestSave()}>Save</button>
<MROfficeEditor bind:editor tokenUrl="/api/mroffice/session" locale="en"
onSave={(saved) => console.log(saved.version)} style="height: 80vh" />
<!-- Or the action, on an element of your own: <div use:mroffice={{ tokenUrl: "/api/mroffice/session" }}></div>
(import { mroffice } from "mroffice-embed/svelte") --><!-- with a bundler: import "mroffice-embed/element"; without one: -->
<script src="https://mroffice.ai/embed/v1/element.js"></script>
<mroffice-editor token-url="/api/mroffice/session" locale="en" toolbar="full" style="height: 80vh"></mroffice-editor>
<script>
const editor = document.querySelector("mroffice-editor");
editor.ui = { toolbar: "full", features: { print: false } }; // objects go in as properties
editor.addEventListener("mroffice-save", (event) => console.log(event.detail.version));
document.querySelector("#save").onclick = () => editor.requestSave();
</script>