Skip to content

About

Accessible full-window pan and zoom for inline SVG diagrams in Backstage TechDocs and MkDocs sites

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

SVG viewer icon Backstage TechDocs & MkDocs SVG viewer

GitHub release npm stable version npm snapshot version jsDelivr stable jsDelivr snapshot

Click an inline SVG diagram to open it in a full-window, accessible viewer. The viewer has zoom, mouse and touch panning, direction buttons and an optional browser fullscreen mode.

Try the live Backstage demo to explore the diagrams, zoom and pan controls, and page navigation.

The viewer works with inline <svg> elements on any HTML page, including SVGs that mkdocs-inline-select-svg-plugin and similar plugins embed in the page. The viewer uses the original inline SVG, so embedded links, text, gradients, markers, IDs and event listeners stay available. The viewer does not rasterise the SVG or change it into an <img>.

There are three ways to use the viewer:

  • Backstage TechDocs: Install the package and register the addon. See Legacy frontend or New frontend system.
  • MkDocs site outside Backstage: Load the self-contained svg-viewer.js from a CDN with extra_javascript. You do not need a JavaScript toolchain. See Standalone MkDocs site.
  • Standalone HTML page: Load svg-viewer.js with a <script> tag. You do not need MkDocs, Backstage or React. See Standalone HTML page.

Backstage

Light Dark
Backstage light Backstage dark

MkDocs

Light Dark
MkDocs light MkDocs dark

SVG viewer

Light Dark
SVG viewer light SVG viewer dark

Behaviour

  • To open the viewer, click an unlinked area of the diagram, or click its Expand diagram button.
  • When you click a link, the link opens, not the viewer. Links keep their destinations, targets, query strings, fragments and modifier-click behaviour.
  • TechDocs resolves and routes only links that use href. Many exporters write links that have only the legacy xlink:href. For these links, the addon adds an href with the resolved URL. TechDocs then navigates in the app and does not reload the page. When the addon unmounts, it removes the attributes that it added.
  • If TechDocs routes a link that you click in the viewer, the viewer closes so that you can see the destination. This includes a #fragment link on the same page.
  • A mouse or touch drag pans the diagram, also when the drag starts on a linked node. When you release a drag, the link does not open. A normal click or keyboard activation opens the link.
  • The wheel, the trackpad and a two-finger pinch zoom around the pointer or the pinch centre.
  • Keyboard: +/− zoom, arrow keys pan, Shift+arrow pans farther, 0/Home fits the diagram, and Escape closes the viewer. Labelled buttons are also available for all essential operations.
  • The viewer fills the browser viewport immediately. A separate Fullscreen button uses the browser Fullscreen API when the browser supports it. The viewer does not need this API or permission to open.
  • The native <dialog> element keeps focus in the viewer and makes the page behind it inert. When the viewer closes, it restores the SVG to the page and returns keyboard focus to the expand button.
  • The viewer follows the TechDocs or Material for MkDocs colour palette, including light and dark themes. Colours defined in the SVG stay unchanged. Pages without these theme variables use the default light viewer palette.
  • If a diagram is hard to read in the current theme, select White theme in the viewer toolbar. This restores the original light palette and white canvas. Select Page theme to follow the host palette again and clear the saved override. Your browser remembers this choice for all diagrams on the same website, including after reloads and browser restarts. This preference is separate from the website's theme and preserves the SVG artwork, zoom and pan. If browser storage is unavailable, the choice applies only to the open viewer.
  • The viewer skips small icons, nested SVG viewports, Material theme icon wrappers and Mermaid diagrams (.mermaid, or an SVG id that starts with mermaid). The Mermaid addon has its own zoom controls.
  • A MutationObserver finds diagrams that navigation or client rendering adds to the page.
  • All processing stays in the page. The viewer has no services and no telemetry.

Compatibility

The implementation is type-checked against these exact Backstage packages:

Package Version
@backstage/plugin-techdocs-react 1.4.0
@backstage/core-plugin-api 1.12.11
@backstage/frontend-plugin-api (new frontend only) 0.18.2
React 18.3.1

Use the versions that align with your Backstage release. The /alpha entry point uses the alpha AddonBlueprint API of Backstage. This API can change between releases. Older Backstage versions can work with the legacy entry point, but we did not test them. The peer range does not include React 19, because the tested TechDocs package declares support for React 17 and 18 only.

All peer dependencies are optional. Thus you can use the /viewer entry point without React or Backstage packages. A Backstage app already has these packages.

The package targets Node 24 LTS (engines.node >=24). Build your Backstage app on Node 24 or later. On older Node versions, Yarn 1 refuses to install the package.

The viewer needs a modern browser that supports native dialog, Pointer Events, ResizeObserver and shadow DOM. Chromium browser tests cover real native SVG navigation, mouse, keyboard and touch. The local Backstage example tests the addon in a real TechDocs reader. Before you roll out the addon, test it in your own TechDocs instance too.

Install

From the root of your Backstage app, run:

yarn --cwd packages/app add @mwz/techdocs-mkdocs-svg-viewer

Then register the addon for the legacy frontend or the new frontend system. The package includes ESM, CommonJS and TypeScript declarations.

To try unreleased changes from main, install a snapshot with the next tag:

yarn --cwd packages/app add @mwz/techdocs-mkdocs-svg-viewer@next

When a change to the package source merges to main, CI stages a snapshot, for example 0.2.1-main.42.gabc1234. The snapshot is available after a maintainer approves it. Snapshots are not stable releases.

Legacy frontend

Add SvgViewer to the existing TechDocsAddons registry in your reader route:

import { TechDocsReaderPage } from '@backstage/plugin-techdocs';
import { TechDocsAddons } from '@backstage/plugin-techdocs-react';
import { SvgViewer } from '@mwz/techdocs-mkdocs-svg-viewer';

<Route path="/docs/:namespace/:kind/:name/*" element={<TechDocsReaderPage />}>
  <TechDocsAddons>
    <SvgViewer />
    {/* Keep any other existing addons here. */}
  </TechDocsAddons>
</Route>;

If you use the entity documentation tab, also register the addon there:

import { EntityTechdocsContent } from '@backstage/plugin-techdocs';

<EntityTechdocsContent>
  <TechDocsAddons>
    <SvgViewer />
  </TechDocsAddons>
</EntityTechdocsContent>;

New frontend system

Add the module to the existing features list of your app:

import { createApp } from '@backstage/frontend-defaults';
import { techDocsSvgViewerModule } from '@mwz/techdocs-mkdocs-svg-viewer/alpha';

const app = createApp({
  features: [
    // Your existing plugins/modules, including TechDocs.
    techDocsSvgViewerModule,
  ],
});

For custom options, use createTechDocsSvgViewerModule({ maxZoom: 32 }) instead of the default module.

Register the viewer only one time for each TechDocs surface. Do not use the legacy addon and the new frontend module on the same reader.

The package declares itself as a TechDocs module (frontend-plugin-module for techdocs). It exports the default module from ./alpha. If your app uses feature discovery (app.packages in app-config.yaml), discovery installs the module automatically. In this case, do not also add the module to features.

To use custom options with discovery, exclude this package from discovery. Then add createTechDocsSvgViewerModule(...) to features. The discovery configuration can change between releases. Read the Backstage feature discovery documentation for your release.

MkDocs configuration

Keep your existing inline plugin configuration, for example:

plugins:
  - techdocs-core
  - inline-select-svg:
      pattern: '[.]inline[.]svg$'
![Space observatory](observatory.inline.svg)

The Python plugin runs when MkDocs builds the documentation. This addon runs in the Backstage frontend. You do not need to add anything to each SVG. The inline plugin uses the SVG <title>, not the Markdown alt text. A meaningful title gives better labels for the button and the viewer.

All examples use the same space observatory diagram with 30 nodes on a 3100 × 2060 canvas. The science processing page has a smaller diagram with links back to the overview. Both diagrams have links to page sections. They exercise relative href and legacy xlink:href links, query strings, fragments, non-zero viewBox origins and marker references. The overview also has a gradient and an external link.

The diagrams show a simplified fictional space mission. The overview notes explain the scope and link to the NASA and STScI sources behind the model.

The SVG sources are in examples/mkdocs/docs. After you edit them, run pnpm examples:sync to update the standalone HTML copies. These copies work when opened directly from disk. pnpm check detects copies that need an update. The small regression fixtures live in tests/fixtures.

Standalone HTML page

The viewer works on a plain HTML page with inline SVGs anywhere in <body>. The general svg selector and script configuration below require version 0.3.0 or later. The 0.2.0 script requires the documentation containers.

After the development setup, run pnpm build. Copy dist/svg-viewer.js beside your HTML file. Save this example as index.html and open it in a browser:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>SVG viewer example</title>
    <script defer src="./svg-viewer.js"></script>
  </head>
  <body>
    <svg
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 300 120"
      width="300"
      height="120"
    >
      <title>Example diagram</title>
      <rect width="300" height="120" fill="#edf4ff" />
      <text x="150" y="65" text-anchor="middle">Click to expand</text>
    </svg>
  </body>
</html>

Click the diagram or its Expand diagram button to open the viewer. The script includes the viewer styles and installs itself after the HTML loads.

The default selector is svg. You do not need a particular container or CSS class. The SVG must be inline HTML, not an <img src="diagram.svg">, <object> or <iframe>. The viewer skips SVGs smaller than 80 × 40 CSS pixels. Add data-svg-viewer="on" to a small SVG to include it, or data-svg-viewer="off" to exclude a diagram.

The standalone example uses the script built from this checkout. After the development setup, run pnpm example:standalone:dev to build the script and open the example. You can also run pnpm build and open the HTML file directly without a server.

Configure the standalone script

Set window.svgViewerOptions before loading svg-viewer.js. You can use any option in Configuration. For example, this configuration selects only SVGs with class="diagram" and changes the size and zoom limits:

<script>
  window.svgViewerOptions = {
    selector: 'svg.diagram',
    minWidth: 40,
    minHeight: 20,
    minZoom: 0.5,
    maxZoom: 32,
  };
</script>
<script defer src="./svg-viewer.js"></script>

Options that you omit keep their defaults. The script reads the configuration once when it installs. Later changes to the object do not change the viewer. Call window.disposeSvgViewer() to remove it. For component lifecycle control, use the JavaScript import below.

Use with a JavaScript bundler

If your site bundles JavaScript, install the package:

npm install @mwz/techdocs-mkdocs-svg-viewer

Import the /viewer entry point after the page container exists. It does not need React or Backstage packages. Pass your options directly to installSvgViewer:

import { installSvgViewer } from '@mwz/techdocs-mkdocs-svg-viewer/viewer';

const dispose = installSvgViewer(document.body, {
  selector: 'svg.diagram',
  maxZoom: 32,
});

The selector searches within the container passed as the first argument. The viewer also finds matching SVGs added to that container later. From version 0.3.0, an omitted selector defaults to svg. With version 0.2.0, set the selector explicitly to search outside documentation containers.

If an existing direct call needs the previous selection, pass selector: '.md-content__inner svg, article svg'. Both Backstage addon entry points keep this selector by default and ignore window.svgViewerOptions.

Call dispose() when you remove the page or component to remove the viewer and its listeners. Use either this import or the automatic script on a page, not both. The JavaScript import does not read window.svgViewerOptions.

Standalone MkDocs site (outside Backstage)

The viewer also works on a MkDocs site that you host without Backstage, for example the output of mkdocs build or techdocs-cli generate. The package contains one self-contained script, dist/svg-viewer.js. The docs repository does not need a JavaScript toolchain.

  1. Add the script to mkdocs.yml. The jsDelivr CDN serves the script from the npm package:

    plugins:
      - search
      - inline-select-svg:
          pattern: '[.]inline[.]svg$'
    extra_javascript:
      - https://cdn.jsdelivr.net/npm/@mwz/techdocs-mkdocs-svg-viewer@0.3.0/dist/svg-viewer.js

    Keep an exact version in the URL. Then a new release does not change your site until you update the URL.

  2. Preview the site with mkdocs serve.

  3. Click a diagram.

To test the latest approved snapshot, replace the stable URL above with the next URL:

extra_javascript:
  - https://cdn.jsdelivr.net/npm/@mwz/techdocs-mkdocs-svg-viewer@next/dist/svg-viewer.js

Snapshots become available on jsDelivr after a maintainer approves them on npm. The next URL changes as snapshots are approved, but CDN caching can delay an update. For repeatable validation, replace next with the exact published snapshot version. Keep the pinned stable version for production sites.

If your site cannot load scripts from a CDN (for example, because of a Content-Security-Policy or an offline build), use a local copy of the script:

  1. Download svg-viewer.js from the URL above, or from a GitHub release.
  2. Commit the script as docs/javascripts/svg-viewer.js.
  3. In extra_javascript, use javascripts/svg-viewer.js.

The script installs the viewer on document.body with the default options. Material instant navigation replaces the page content but keeps <body>. Thus the viewer also works on diagrams on later pages. The browser handles href and xlink:href links natively, so the script does not change links. If the page loads the script two times, the second load has no effect. To remove the viewer, call window.disposeSvgViewer().

For Material themes, set theme.palette.scheme to default for light mode or slate for dark mode. Do not load techdocs-core in the standalone configuration: it clears the palette. Backstage adds that plugin during generation and supplies its own theme.

You can use the same mkdocs.yml for Backstage. TechDocs sanitises the HTML and removes <script> tags. Thus the script does not run in Backstage, and the addon handles the diagrams. In your Backstage instance, make sure that each diagram has only one Expand diagram button.

examples/mkdocs uses this setup. To run it, use pnpm example:mkdocs:dev from the repository root. This command builds the script, copies it into the example, and serves the site with MkDocs through Poetry. For more information, see examples/mkdocs/README.md.

With the script built from this checkout, you can also use script configuration in MkDocs. Put the window.svgViewerOptions assignment in docs/javascripts/svg-viewer-options.js. List that file before the viewer script:

extra_javascript:
  - javascripts/svg-viewer-options.js
  - javascripts/svg-viewer.js

Configuration

<SvgViewer
  selector=".md-content__inner svg, article svg"
  minWidth={80}
  minHeight={40}
  minZoom={0.25}
  maxZoom={16}
  resolveXlinkHref
/>
Option Default Meaning
selector svg (standalone), .md-content__inner svg, article svg (addon) CSS selector within the supplied container or TechDocs shadow root
minWidth, minHeight 80, 40 Minimum rendered size in CSS pixels. The viewer skips smaller SVGs as icons
minZoom, maxZoom 0.25, 16 Zoom limits relative to Fit. 1 is 100% of Fit, not the native pixel size
resolveXlinkHref true (addon), false (standalone) Add a resolved href to links that have only xlink:href

To open a small SVG in the viewer, add data-svg-viewer="on" to the SVG. This attribute overrides the size limits. To exclude an SVG, add data-svg-viewer="off" to the SVG or to an element that contains it.

If an SVG is inside an HTML link, the viewer adds a separate expand button outside that link. The link around the whole image stays available on the page. Links inside the SVG stay available in the viewer.

Expand button layout

The expand button takes its own line and centres beneath the rendered diagram, including diagrams narrower than their content container. The viewer measures the diagram and updates the button position when the diagram or its container resizes. It does not move or wrap the SVG.

The default layout rule has low CSS specificity. Host styles can override its position without !important, even if they load before the viewer styles. For example, align the button to the left:

.tdsv-expand {
  margin-inline: 0 auto;
}

Use margin-inline: auto 0 to align it to the right. In MkDocs, load this rule through extra_css. In Backstage, add the rule inside the TechDocs shadow root, for example through a custom styles addon. Styles in the outer app cannot select elements inside that shadow root.

Rendering and lifecycle notes

The viewer temporarily moves the original SVG into a native dialog in the same document or shadow root. Because only one node exists, there are no duplicate IDs. References between <defs>, gradients, markers and <use> also continue to work.

Before the move, the viewer records the presentation styles from a temporary copy of the SVG in the same position. The copy is not under the pointer, so the viewer does not record hover styles that were active at the click. After the move, the viewer sets inline only the values that changed because the SVG left its original ancestors, for example inherited fonts and colours. When the viewer closes, it restores the original inline styles and position.

The viewer does not parse or inject fetched SVG or HTML. Only the demo parses its checked-in, trusted fixture.

The viewer keeps existing viewBox values, including negative origins. If an SVG has no viewBox, the viewer gives it a temporary viewBox from its rendered viewport. The viewer is mainly for fixed-size diagrams. Some SVGs have geometry that depends on dynamic ancestor CSS, animations or interactive form controls. These SVGs can need a custom selector, or data-svg-viewer="off".

Hover styles and other interactive styles in the same shadow root also apply in the viewer, for example styles from a custom styles addon. An !important rule always applies. A rule without !important applies to a property only if the viewer did not set that property inline on the element. Rules that depend on the original ancestors, such as .md-typeset svg a:hover, do not match in the viewer.

This addon does not repair existing problems in the source, such as duplicate IDs between different SVGs.

TechDocs stays responsible for sanitisation and routing. Relative URLs must already be correct in the rendered document. After inlining, the viewer cannot find the source path of the original .svg file.

TechDocs attaches a click listener to each link before addons mount. The viewer moves the original nodes, so these listeners stay attached in the viewer. The addon resolves xlink:href values in the same way that TechDocs resolves href: relative to the page URL, with a trailing / unless the path ends in .html. If your app customises routing, test the links with your customisations.

The page behind the viewer is inert. The viewer restores the SVG when it closes, on route or hash navigation, when the content is removed, or when the addon unmounts. A link with target="_blank" opens a new tab, and the viewer stays open in the original tab. Browser-native fullscreen is optional. If the browser denies it, the full-window viewer still works.

Development

mise install
pnpm install --frozen-lockfile
pnpm exec playwright install --with-deps chromium
pnpm check
pnpm format:check
pnpm dev

Open the Vite URL that the terminal shows. The demo has controls to replace the page and to mount and unmount the addon, for lifecycle tests. To simulate the link handling of TechDocs, add ?techdocs to the URL.

Use the theme button to switch between light and dark mode. The page and viewer follow your choice, which is saved in this browser. New visits use light mode until you select a different theme.

/dev/standalone.html loads the built dist/svg-viewer.js on a plain MkDocs-style page with the same theme switcher. Before you open this page, run pnpm build.

pnpm check runs TypeScript, geometry tests, production builds and Playwright integration tests. CI runs the same checks on Node 24 and on the current Node release. When a check fails, CI archives the browser traces. Browser tests intercept example link destinations locally.

See CONTRIBUTING.md for pull requests and releases.

Run the examples

All example commands use example:<name>:<action>. After the development setup, run these commands from the repository root. Use dev to launch an example and test to run its tests:

Example Launch Test
Standalone HTML pnpm example:standalone:dev pnpm example:standalone:test
MkDocs pnpm example:mkdocs:dev pnpm example:mkdocs:test
Backstage pnpm example:backstage:dev pnpm example:backstage:test
Static Backstage pnpm example:backstage-demo:dev pnpm example:backstage-demo:test

The standalone command builds the script and opens http://127.0.0.1:5174/examples/standalone/. Restart it after changes to the viewer source. MkDocs installs its Python dependencies and serves on port 8000. Before the first Backstage launch, run pnpm example:backstage:setup. Press Ctrl+C to stop a running example.

The standalone and MkDocs tests use Chromium. The MkDocs tests also check the generated HTML and need the Python dependencies installed by example:mkdocs:dev or example:backstage:setup. The Backstage tests start the app and test the viewer in a real TechDocs reader. Navigation tests cover both diagrams, page sections, query strings, browser history and refreshes.

Run the included Backstage app

The static Backstage demo runs the real TechDocs reader and SVG addon without a backend. After the Backstage setup below, run pnpm example:backstage-demo:dev to preview it. The GitHub Pages workflow builds and tests the demo, then deploys pushes to main.

The Backstage example uses Backstage 1.56.2 with the new frontend system. It loads the plugin from this checkout and registers examples/mkdocs as TechDocs. It uses automatic guest access, SQLite and local MkDocs builds. You do not need Docker, PostgreSQL or external credentials.

After mise install and pnpm install --frozen-lockfile, run:

pnpm example:backstage:setup
pnpm example:backstage:dev

Open http://localhost:3000/. The app enters as a guest automatically and opens the example documentation. After a plugin change, stop the app and run pnpm example:backstage:dev again. Run pnpm example:backstage:check and pnpm example:backstage:test to test the integration. See the example README for prerequisites and details.

Test changes in your own Backstage app

Add this checkout to your Backstage app as a folder-based file: dependency. This works with Yarn 1 (Classic) and Yarn 2–4. Yarn copies the folder into the node_modules of the app. Thus the peer dependencies of the addon (React and the Backstage packages) resolve to the copies in the app.

CAUTION: Do not use a portal: or link: dependency. If you do, TechDocs fails with TypeError: Cannot read properties of null (reading 'useContext').

Both of these dependency types create a symlink to this checkout. The Backstage bundler follows the symlink, so the addon imports react and @backstage/* from the node_modules of this checkout. That folder contains the development dependencies of the addon. As a result, the page has two copies of React and of the Backstage API context.

The commands below assume sibling checkouts named backstage and techdocs-mkdocs-svg-viewer. If your layout is different, change the paths.

  1. In the addon checkout, install the dependencies and build the package:

    cd techdocs-mkdocs-svg-viewer
    mise install
    pnpm install --frozen-lockfile
    pnpm build
  2. From backstage/packages/app, add the local package:

    yarn add '@mwz/techdocs-mkdocs-svg-viewer@file:../../../techdocs-mkdocs-svg-viewer'

    The relative path starts at packages/app, not at the root of the Backstage repository.

  3. Register the addon. Use the instructions for the legacy frontend or the new frontend system.

  4. Start Backstage with your usual development command.

The installed copy does not change when you rebuild the addon. After each change, do these steps:

  1. In the addon checkout, run pnpm build.
  2. In the Backstage app, run yarn install to copy the package again.
  3. Restart Backstage.

pnpm dev runs the standalone demo. It does not replace these steps.

Keep the local dependency and its lockfile changes out of shared Backstage commits. CI and other developers usually do not have your sibling checkout. When you finish:

  • If the app used the package before this test, restore the previous dependency. Then run yarn install.
  • If you added the package only for this test, run yarn remove @mwz/techdocs-mkdocs-svg-viewer from packages/app. Then remove the addon registration.

The file: copy includes the whole checkout, not only the published files. To test the exact contents of the published package, pack a tarball and add the tarball instead:

# In the addon checkout:
pnpm pack --out ../techdocs-mkdocs-svg-viewer.tgz

# From the root of your Backstage app. Adjust the path to the tarball:
yarn --cwd packages/app add file:../../../techdocs-mkdocs-svg-viewer.tgz

Upstream integration references

License

Copyright 2026 mwz. Licensed under the Apache License, Version 2.0.

About

Accessible full-window pan and zoom for inline SVG diagrams in Backstage TechDocs and MkDocs sites

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages