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>
  );
}

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));
}
MethodDescription
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.elementThe 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:

  1. Attach the editor to an element in that document.
  2. Call copyEditorStyles(document, other.document).
  3. When the window closes, call the stop function that copyEditorStyles returns.

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.

OptionDefaultDescription
urlabout:blankA same-origin page to open, such as an empty HTML file that your app serves.
namedocx-editorThe window name. Windows with the same name share one browser window, so give each editor a unique name.
featurespopup,width=1000,height=900The window.open features string.
titleNoneThe window title.
stylestrueCopies 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);
}
NeedUse
Document or window of an element you renderhostDocumentOf(element), hostWindowOf(element)
Document or window of the editoreditorHostDocument(editor), editorHostWindow(editor)
Element the editor is mounted ineditorHostElement(editor)
Timers and frames on the editor windowhostTimers(() => element)
Reactive document in React or VueuseEditorHostDocument()
Notification when the editor changes documentseditor.on('hostChange', ({ document }) => ...)
Type checks across windowsisElementNode, 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 onClose runs.
  • Move the content back before the window document unloads. In your own window, do it in pagehide, with flushSync in React.
  • In a custom Vue integration, switch between a Teleport and the inline panel with v-if. Retargeting one Teleport can 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 copyEditorStyles observer 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.

See also