Shot

Storybook addon: a toolbar button that copies the current story's canvas to the clipboard as a PNG, without Storybook's chrome.

View on GithubNew to Storybook?Get started

Storybook Shot

npm Test

I don't know about you, but I spend an unreasonable amount of time inside Storybook tweaking components and screens. And when I need to share an ongoing experiment with someone, nothing beats a good old screenshot pasted in Slack.

I could use a screen shooting tool, but Storybook's toolbar is right there, so I thought it would be nice to have a one-click way to copy what I'm seeing to the clipboard.

Thus: Storybook Shot, a Storybook toolbar button that copies the current story's canvas to the clipboard as a PNG, with none of Storybook's own interface in it: just what the story draws.

  • Click: copy the PNG to the clipboard.
  • Alt/Option-click: download it instead.
  • Shift-click: leave the page background out (transparent PNG).

Works with Storybook 8, 9, 10 and 11, with any framework (it reads the rendered DOM, not your components).

The camera button in Storybook's toolbar, next to a story showing a rubber duck's performance review

One click puts this on the clipboard, at 2x, ready to paste into a doc, a chat or a design tool:

The PNG it copies: the duck's card alone, cropped with its shadow

Install

npm install --save-dev storybook-shot
# or, from a local checkout:
npm install --save-dev /path/to/storybook-shot

Then add it to .storybook/main.(js|ts):

export default {
  addons: ["storybook-shot"],
};

Restart Storybook: the manager (the UI around the canvas) is only rebuilt at startup.

What Ends up in the Image

  • The story's content, cropped tight, plus a margin (16px; none for layout: "fullscreen"), or further where a box-shadow reaches further, so shadows are not cut off.
  • Anything the story portalled into <body> (dialogs, popovers, toasts), at the position it has on screen. A modal comes with its overlay.
  • The page background (the backgrounds addon's colour, or the preview's), unless Shift is held.
  • Web fonts the preview loaded.
  • At least 2x: the screen's pixel ratio, or 2 if that is lower.

Storybook's loaders, error screens and highlight layers are left out. The button only shows on story pages, not on Docs pages.

Per-Story Options

Under the shot parameter, on a story, a component's default export, or globally in preview:

export const Card = {
  parameters: {
    shot: {
      margin: 24,         // CSS px around the content (default 16, 0 for fullscreen)
      scale: 3,           // pixel density (default: the screen's, at least 2)
      transparent: true,  // always leave the background out
      disable: true,      // hide the button for this story
    },
  },
};

How It Works

  • Only in the manager. The button runs in Storybook's manager and reads the preview iframe's document directly, so the clipboard write happens inside the click (which browsers require). The preview must be same-origin with the manager: true for storybook dev and a static build, not for a composed Storybook (refs) served from another origin.
  • No Storybook imports. React, the manager API, the components and the icons are read from the globals the manager publishes (__REACT__, __STORYBOOK_API__, __STORYBOOK_COMPONENTS__, __STORYBOOK_ICONS__). Their names are the same in 8 to 11, while the import paths changed between versions, so one file serves them all. From 10 the button is Button with its own tooltip; on 8 and 9 it is IconButton with a title (11 removed IconButton).
  • No build step. Storybook bundles an addon's manager entry itself, so the package ships plain ES modules.
  • Rendering is modern-screenshot (MIT): the preview's <html> is cloned into an SVG foreignObject and drawn on a canvas, then cropped.

Limits

  • It is a re-render of the DOM, not a screenshot of pixels: what the browser cannot serialise (cross-origin images without CORS, cross-origin iframes, some <video>) comes out blank. Canvases are copied as they are.
  • Clipboard images need a secure context (localhost or https) and a browser that takes a promise in ClipboardItem (Chrome, Edge, Safari and Firefox). Where the copy is refused, Alt-click downloads the PNG instead.
  • Crop bounds come from the root's direct children (and their single-child wrappers' shadows): content that overflows its parent without being in its box can fall outside.

Contributing

How the tests work and how releases are made: CONTRIBUTING.md. Changes are listed in CHANGELOG.md.

License

MIT. See LICENSE.

Made by
  • bomberstudios
    bomberstudios
Work with
    Angular
    Ember
    HTML
    Preact
    React
    Svelte
    Vue
    Web Components
Tags