Pop-out windows and iframes
Show the editor in a pop-out window or same-origin iframe, and move it between windows without losing edits or undo history.
A moved editor keeps its document, undo history, selection, scroll position, and collaboration session. Keyboard focus follows it.
Move the editor into a window
Wrap the parts to move in the window component. Keep the root outside it, so the editor instance stays in the main window.
import { useState } from 'react';
import { DocxEditor } from '@docx-editor.dev/react';
export function Editor({ bytes }: { bytes: Uint8Array }) {
const [popped, setPopped] = useState(false);
return (
<DocxEditor.Root document={bytes}>
<button onClick={() => setPopped(!popped)}>{popped ? 'Bring back' : 'Pop out'}</button>
<DocxEditor.Window open={popped} onClose={() => setPopped(false)}>
<DocxEditor.Toolbar />
<DocxEditor.Viewport style={{ height: '100%' }}>
<DocxEditor.Content />
</DocxEditor.Viewport>
</DocxEditor.Window>
</DocxEditor.Root>
);
}<script setup lang="ts">
import { ref } from 'vue';
import {
DocxEditorContent,
DocxEditorRoot,
DocxEditorToolbar,
DocxEditorViewport,
DocxEditorWindow,
} from '@docx-editor.dev/vue';
defineProps<{ bytes: Uint8Array }>();
const popped = ref(false);
</script>
<template>
<DocxEditorRoot :document="bytes">
<button @click="popped = !popped">
{{ popped ? 'Bring back' : 'Pop out' }}
</button>
<DocxEditorWindow v-model:open="popped">
<DocxEditorToolbar />
<DocxEditorViewport style="height: 100%">
<DocxEditorContent />
</DocxEditorViewport>
</DocxEditorWindow>
</DocxEditorRoot>
</template>When the user closes the window, the content moves back before the window document unloads. React then calls onClose; Vue sets open to false. For a runnable app, see the pop-out window example.
Do not wrap the packaged <DocxEditor> component in a window. Unmounting it destroys its instance and undo history. Use the composition API instead.
Use a window or iframe you own
Without an adapter, open the window with openEditorWindow and move the editor with attach.
import { createDocxEditor, openEditorWindow } from '@docx-editor.dev/core/editor';
declare const bytes: Uint8Array;
const home = document.getElementById('editor')!;
const editor = createDocxEditor({ document: bytes, container: home });
async function popOut() {
const popup = await openEditorWindow({ title: 'Contract' });
if (!popup) return; // The browser blocked the window.
editor.attach(popup.element);
popup.onClose(() => editor.attach(home));
}| Method | Description |
|---|---|
editor.attach(element, options?) | Moves the open document into element, in any same-origin window. Pass { focus: false } to keep focus where it is. |
editor.detach() | Parks the editor. It keeps memory, saves, runs commands, and receives collaboration edits. |
editor.destroy() | Releases the editor and its memory. |
popup.element | The mount point that fills the window. |
popup.onClose(listener) | Runs once when the window goes away, while its document still exists. |
popup.close() | Closes the window and runs the onClose listeners. |
For a same-origin iframe or a window that your layout system opens:
- Attach the editor to an element in that document.
- Call
copyEditorStyles(document, other.document). - When the window closes, call the stop function that
copyEditorStylesreturns.
With an adapter, render the content into that element with a portal. For more information, see Editor mount modes.
Configure the window
DocxEditor.Window, DocxEditorWindow, and openEditorWindow take the same options.
| Option | Default | Description |
|---|---|---|
url | about:blank | A same-origin page to open, such as an empty HTML file that your app serves. |
name | docx-editor | The window name. Windows with the same name share one browser window, so give each editor a unique name. |
features | popup,width=1000,height=900 | The window.open features string. |
title | None | The window title. |
styles | true | Copies your page stylesheets and keeps them current. Pass false to style the window yourself, or a function that copies them and returns a stop function. |
Build controls for another window
Custom chrome must resolve its document and window from the editor, not from script globals.
import type { Editor } from '@docx-editor.dev/core';
import { editorHostWindow } from '@docx-editor.dev/core/editor';
async function copyText(editor: Editor, text: string) {
await editorHostWindow(editor).navigator.clipboard.writeText(text);
}| Need | Use |
|---|---|
| Document or window of an element you render | hostDocumentOf(element), hostWindowOf(element) |
| Document or window of the editor | editorHostDocument(editor), editorHostWindow(editor) |
| Element the editor is mounted in | editorHostElement(editor) |
| Timers and frames on the editor window | hostTimers(() => element) |
| Reactive document in React or Vue | useEditorHostDocument() |
| Notification when the editor changes documents | editor.on('hostChange', ({ document }) => ...) |
| Type checks across windows | isElementNode, isHTMLElementNode, isKeyboardEvent, isPointerEvent |
The hooks come from the adapter packages. The other helpers come from @docx-editor.dev/core/editor. node instanceof HTMLElement is false for an element of another window.
Limits
- The destination must be same-origin. The browser blocks rendering into a cross-origin page.
- Open windows from a user gesture. If the browser blocks the window, the content stays in place and
onCloseruns. - Move the content back before the window document unloads. In your own window, do it in
pagehide, withflushSyncin React. - In a custom Vue integration, switch between a
Teleportand the inline panel withv-if. Retargeting oneTeleportcan leave open popovers listening to the old window. - Popovers and menus close when the content remounts in another window.
openEditorWindow()closes the pop-out when the opener page unloads.- Style copying skips rules inserted into a
<link>stylesheet and cross-origin stylesheets. - Stop the
copyEditorStylesobserver when the destination closes. load()on a detached editor replaces the kept document.- The context menu Paste command needs the clipboard-read permission. Keyboard paste does not.