Building Blocks
createViewerBuildingBlocks is the vanilla counterpart of React's useViewerBuildingBlocks. It runs the full viewer engine behind a slide canvas with its chrome turned off, and lets you place the pieces yourself:
| Block | Mount with | Notes |
|---|---|---|
| Slide canvas | mountCanvas(host) | The deck loads here. Selection, inline text, gestures and undo all work. |
| Compact toolbar | mountToolbar(host, groups?) | History, insert, editing, view and navigation controls. |
| Slides pane | mountSlidesPane(host) | The thumbnail rail; selecting a thumbnail shows that slide. |
Each mount... returns its disposer; destroy() disposes whatever is still mounted.
import { createViewerBuildingBlocks } from 'pptx-vanilla-viewer';
import 'pptx-vanilla-viewer/styles.css';
const blocks = createViewerBuildingBlocks({
content: bytes,
canEdit: true,
onRequest: (request) => openMyPicker(request),
onDirtyChange: (dirty) => (saveButton.disabled = !dirty),
});
blocks.mountToolbar(document.querySelector('#toolbar')!);
blocks.mountSlidesPane(document.querySelector('#slides')!);
blocks.mountCanvas(document.querySelector('#canvas')!);The blocks are view-only until canEdit: true, as in every binding, and mode reads preview until then. update(options) applies content, canEdit, locale, theme, customization, googleFonts, allowExternalContent, collaboration and every callback to the mounted canvas at once, keeping the open deck, and options the viewer reads when it uses them (listConversionScope, limits on the next load) follow too. fonts, registry, availableThemes and availableLocales apply the next time the canvas mounts. subscribe(listener) calls listener after every change, and getState() returns { mode, loading, error, slideIndex, slideCount, canUndo, canRedo, dirty, shellState, collaborationState } for your status line (see below). blocks.viewer is the full viewer instance behind the canvas (null until it mounts), for saving, export and everything else.
Collaboration and shell state
collaboration takes the same configuration as the full viewer, including a host-owned Yjs session (externalSession). Setting it joins the room, a new configuration object rejoins, and clearing it leaves; the open deck, its edits and the canvas stay. Remote cursors and selections draw on the canvas. getState() also carries the shell:
shellState:{ canEdit, status, failure, remoteUsers, connectedCount }, the effective edit permission (yourcanEditis a ceiling; aviewerrole or a room that has not synced yet takes it away, andmodereadspreviewthen) and the connection. Pass it todescribeCollaborationShellStatefor a localised status line;failurenames why a session in theerrorstatus could not start (collaborationFailureMessageKeymaps it to a translation key).collaborationState: the sharedCollaborationState(active,roomId,status,synced,people),status: 'off'while not sharing; the value the full viewer's collaboration-change event reports.
blocks.viewer.startCollaboration and stopCollaboration still work; prefer the option, so update and the viewer never disagree about the room.
Linked pictures and media
As in Microsoft PowerPoint, a deck that links pictures or media to the web opens without downloading them. The canvas does not draw PowerPoint's security bar. blocks.mountExternalContentBar(host) mounts it into host (it shows only while needed and returns its disposer), or draw your own from blocks.externalContent ({ barVisible, allowed, enable, close }, read again after subscribe fires): enable() draws the linked content without reloading (edits, the unsaved state, the slide on screen and undo history stay; collaborators decide for themselves), and close() hides the bar. A shell that draws slides itself passes allowed as the linkedContent option of renderSlideStage, renderSlideThumbnail or createSlidesPane. A new presentation asks again. The allowExternalContent option (true or false) answers for the user, and then the bar never shows.
blocks.mountExternalContentBar(document.querySelector('#security-bar')!);Host-owned workflows
The blocks mount no file inputs and no dialogs. A toolbar control or a canvas gesture that needs one raises a request instead: onRequest(request) is called and blocks.actions.pending holds it until you finish it with the matching operation or call actions.cancel().
| Request | Raised by | Finish with |
|---|---|---|
{ kind: 'image' } | Pictures | actions.insertImage(file) |
{ kind: 'media' } | Media | actions.insertMedia(file) |
{ kind: 'chart' } | Chart | actions.chart.insert(kind, options) |
{ kind: 'smartArt' } | SmartArt | actions.smartArt.insert(layout, items) |
{ kind: 'equation', existingOmml } | Equation, or double-clicking an equation | actions.equation.insert / update |
{ kind: 'hyperlink' } | Link, Ctrl+K, the element menu's Link entry | actions.hyperlink.apply / remove |
{ kind: 'shortcuts', open } | ?, Ctrl+/, actions.shortcuts.setOpen | render actions.shortcuts.catalogue |
Ctrl+F and Ctrl+H on the canvas open your find bar (actions.findReplace.open); render it from actions.findReplace (query, options, matches, next, previous, replaceCurrent, replaceAll). The canvas marks the matched text itself, as the full viewer and React do: each match is a <mark data-pptx-find-match> (current for the current match, match for the others) inside the run, decided by the shared findTextHighlightsByElement / highlightFindMatches.
The file and slide show dialogs are groups on actions too, with the same shape as React's: passwordProtection, documentProperties, fontEmbedding, digitalSignatures, setUpSlideShow, customShows, broadcast (reports that it is not supported) and shortcuts. Render your own UI from their state and apply through them; a host raises them from its own controls. Layout apply and Reset are actions.layout.apply(path) and actions.layout.reset().
Every operation returns { ok: true } or { ok: false, code } and runs the viewer's own insert, find and layout code, so history and onDirtyChange behave as in the full viewer. actions.availability says which operations can run now and why not (read-only, protected, no-selection, ...). The actions object has the same shape in every binding; see React's building blocks for each operation.
blocks.subscribe(() => {
const { pending } = blocks.actions;
if (pending?.kind === 'image') {
pickFile('image/*').then((file) =>
file ? blocks.actions.insertImage(file) : blocks.actions.cancel(),
);
}
});Toolbar groups and hidden controls
mountToolbar(host, ['history', 'insert']) renders only those groups. hiddenActions removes controls as in the full viewer: undo, redo, insert (every insert control), zoom and navigation. Controls are plain <button data-control="..."> elements inside <span data-group="...">, so you can style them or replace the toolbar with your own buttons that call actions and blocks.viewer.
Element render errors
A slide element whose renderer throws is left out on its own: its neighbours, the slide and the viewer stay, on the canvas, the thumbnails, the slide show, the reading view and the previews. The document is not changed. Pass onElementRenderError (in the createViewerBuildingBlocks options; it reaches the canvas and slides pane blocks) to hear about it; the full viewer takes the same option. It receives the error and an ElementRenderErrorContext (elementId, elementType and, where the surface knows it, slideId; both types are exported). Without it the viewer calls console.error(error, context), which is also the fallback when the callback throws, so a failing reporter never takes the viewer down.
The stage builds each element (group children included) inside a try/catch, and a stage rebuild is assembled before the old one is removed, so a rebuild that fails for another reason keeps the last canvas on screen. A surface remembers its failed elements, so an unchanged one is not retried or reported again on every repaint. Replacing the failed element object (even with the same id) or changing its id renders it again; re-rendering the same failed object does not retry. Layout and template previews in the ribbon are contained the same way and report to console.error. createSlidesPane and renderSlideThumbnail take onElementRenderError too.
Not included
The blocks render no dialogs, panes, ribbon, status bar, notes or presentation chrome (no Share dialog: join a room through collaboration). Broadcast cannot be run from the building blocks (actions.unmountedDialogActions). Hosts that need the bundled chrome should use createPptxViewer.