Skip to content

Building Blocks ​

createViewerBuildingBlocks is the Svelte counterpart of React's useViewerBuildingBlocks. It builds the full viewer state behind a slide canvas with its chrome turned off, and gives you three components to place yourself:

BlockComponentNotes
Slide canvasBuildingBlocksCanvasThe deck loads here. Selection, inline text, gestures and undo all work.
Compact toolbarBuildingBlocksToolbarHistory, insert, editing, view and navigation controls.
Slides paneBuildingBlocksSlidesPaneThe thumbnail rail; selecting a thumbnail shows that slide.

Call createViewerBuildingBlocks once, synchronously in the script of the component that renders the blocks (it creates the viewer state, which registers effects and Svelte context, so the blocks must be rendered by that component or its descendants), and pass a function returning the current options. Everything it returns is a runes-backed getter, so a template that reads it tracks the editor.

svelte
<script lang="ts">
	import {
		BuildingBlocksCanvas,
		BuildingBlocksSlidesPane,
		BuildingBlocksToolbar,
		createViewerBuildingBlocks,
	} from 'pptx-svelte-viewer';

	let { content }: { content: Uint8Array } = $props();
	let dirty = $state(false);

	const blocks = createViewerBuildingBlocks(() => ({
		content,
		canEdit: true,
		onRequest: (request) => openMyPicker(request),
		onDirtyChange: (value) => (dirty = value),
	}));
</script>

<BuildingBlocksToolbar {blocks} />
<div style="display: flex; height: 600px">
	<div style="width: 200px"><BuildingBlocksSlidesPane {blocks} /></div>
	<div style="flex: 1"><BuildingBlocksCanvas {blocks} /></div>
</div>
<button disabled={!dirty}>Save</button>

The blocks are view-only until canEdit: true, as in every binding, and blocks.state.mode reads preview until then. The options function is read live, so content, canEdit, locale, hiddenActions, fileLimits, onRequest, googleFonts, allowExternalContent (both also lock their Trust Center settings), collaboration and every callback apply at once, without remounting the canvas; limits and initialSlide apply from the next load. blocks.state is { mode, loading, error, slideIndex, slideCount, canUndo, canRedo, dirty, shellState, collaborationState } for your status line, and blocks.viewerState is the full viewer state behind the canvas (its deck, editingApi and exportingApi) for saving, export and everything else. AutoRecover snapshots are off unless you pass autosave: true.

BuildingBlocksToolbar takes an optional include (['history', 'insert']) to render only some groups. BuildingBlocksCanvas takes an optional theme (a ViewerTheme); without it the canvas inherits your tokens.

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. blocks.state also carries the shell:

  • shellState: { canEdit, status, failure, remoteUsers, connectedCount }, the effective edit permission (your canEdit is a ceiling; a viewer role or a room that has not synced yet takes it away, and mode reads preview then) and the connection. Pass it to describeCollaborationShellState for a localised status line; failure names why a session in the error status could not start (collaborationFailureMessageKey maps it to a translation key).
  • collaborationState: the shared CollaborationState (active, roomId, status, synced, people), status: 'off' while not sharing; the value the full viewer's collaboration-change event reports.

Linked pictures and media ​

As in Microsoft PowerPoint, a deck that links pictures or media to the web opens without downloading them. The deck keeps the links (saves write them back), and only what is drawn leaves them out. The canvas does not draw PowerPoint's security bar: blocks.externalContent (a live { barVisible, allowed, enable, close }) says when to show it. Render the exported ExternalContentBar (or your own) with onenable={blocks.externalContent.enable} and onclose={blocks.externalContent.close}. Enable Content redraws the canvas and the slides pane with the linked content, for this viewer only: nothing reloads, and unsaved edits, undo history, the slide on screen, autosave and a collaboration session carry on. A new presentation asks again. The allowExternalContent option (true or false) answers for the user, and then the bar never shows; a change applies at once.

allowed says whether this viewer draws the linked content. A host thumbnail strip passes it as SlideThumbnail's linkedContent (createSlidesPaneProps already carries it as linkedContent), so its thumbnails redraw on Enable Content too.

svelte
{#if blocks.externalContent.barVisible}
	<ExternalContentBar
		onenable={blocks.externalContent.enable}
		onclose={blocks.externalContent.close}
	/>
{/if}

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().

RequestRaised byFinish with
{ kind: 'image' }Picturesactions.insertImage(file)
{ kind: 'media' }Mediaactions.insertMedia(file)
{ kind: 'chart' }Chartactions.chart.insert(kind, options)
{ kind: 'smartArt' }SmartArtactions.smartArt.insert(layout, items)
{ kind: 'equation', existingOmml }Equation, or double-clicking an equationactions.equation.insert / update
{ kind: 'hyperlink' }Link, Ctrl+Kactions.hyperlink.apply / remove
{ kind: 'shortcuts', open }?, Ctrl+/, actions.shortcuts.setOpenrender 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.

svelte
<script lang="ts">
	$effect(() => {
		const { pending } = blocks.actions;
		if (pending?.kind === 'image') {
			pickFile('image/*').then((file) =>
				file ? blocks.actions.insertImage(file) : blocks.actions.cancel(),
			);
		}
	});
</script>

Toolbar groups and hidden controls ​

The toolbar renders <button data-control="..."> elements inside <span data-group="...">, so you can style them, or skip BuildingBlocksToolbar and draw your own buttons from blocks.toolbar (the shared descriptor: groups, controls, labels as dictionary keys, disabled and pressed) and run them with blocks.runToolbarControl(id). hiddenActions removes controls as in the full viewer: undo, redo, insert (every insert control), zoom and navigation.

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.

Each element sits in a <svelte:boundary>, which catches render and effect errors. 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. Event handler errors are not caught, as in React.

Not included ​

The blocks render no dialogs, panes, ribbon, status bar, notes or presentation chrome, and the compact toolbar has no controls for the File and Slide Show dialogs (raise those from your own controls and the actions groups). Broadcast cannot be run from the building blocks. Hosts that need the bundled chrome should use PowerPointViewer.

Released under the Apache-2.0 License.