Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions packages/storybook/src/vitest-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,56 @@ export const createArgosScreenshotCommand = (
// and we screenshot the iframe's `<body>`. Anything overflowing the iframe box
// is not painted, so the screenshot gets cut. `setViewportSize` grows the iframe
// *before* `argosCSS` (which injects `fitToContent`'s `zoom`) is applied, so it
// can't account for the final content size. Re-fit the iframe height here, after
// can't account for the final content size. Re-fit the iframe here, after
// stabilization has injected `argosCSS`, so the whole content is painted.
const userBeforeScreenshot = options?.beforeScreenshot;
const optionsWithFit: ArgosScreenshotOptions = {
...options,
beforeScreenshot: async (api) => {
await userBeforeScreenshot?.(api);
await ctx.page.evaluate(() => {
const iframe = document.querySelector('iframe[data-vitest="true"]');
await ctx.page.evaluate(
({ fitWidth }) => {
const iframe = document.querySelector('iframe[data-vitest="true"]');

if (
!(iframe instanceof HTMLIFrameElement) ||
!iframe.contentDocument
) {
return;
}
if (
!(iframe instanceof HTMLIFrameElement) ||
!iframe.contentDocument
) {
return;
}

const { body, documentElement } = iframe.contentDocument;
const contentHeight = Math.max(
body.scrollHeight,
body.offsetHeight,
documentElement.scrollHeight,
);
const { body, documentElement } = iframe.contentDocument;
const contentHeight = Math.max(
body.scrollHeight,
body.offsetHeight,
documentElement.scrollHeight,
);

// Only grow, never shrink: the iframe must contain the full content
// so it's painted, but we don't want to collapse an intentionally
// sized viewport.
if (contentHeight > iframe.clientHeight) {
iframe.style.height = `${contentHeight}px`;
}

// Only grow, never shrink: the iframe must contain the full content so
// it's painted, but we don't want to collapse an intentionally sized viewport.
if (contentHeight > iframe.clientHeight) {
iframe.style.height = `${contentHeight}px`;
}
});
// `fitToContent` fits the content in both dimensions, so the iframe
// must also grow horizontally to paint content wider than the
// viewport. Without `fitToContent` we keep the viewport width to
// match Playwright's `fullPage` semantics (full height, viewport
// width).
if (fitWidth) {
const contentWidth = Math.max(
body.scrollWidth,
body.offsetWidth,
documentElement.scrollWidth,
);
if (contentWidth > iframe.clientWidth) {
iframe.style.width = `${contentWidth}px`;
}
}
},
{ fitWidth: Boolean(fitToContent) },
);
},
};

Expand Down
29 changes: 29 additions & 0 deletions packages/storybook/stories/WideContent.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { WideContent } from "./WideContent";
import type { Meta, StoryObj } from "@storybook/react-vite";

/**
* Regression story for screenshots being cut off *horizontally* when the
* content is wider than the viewport.
*
* With `fitToContent`, the content is fit in both dimensions, so the iframe
* must grow horizontally to paint content wider than the viewport. Otherwise
* the screenshot is left with a large blank area on the right.
*/
const meta = {
title: "Repro/WideContent",
component: WideContent,
parameters: {
layout: "fullscreen",
argos: { modes: null },
},
} satisfies Meta<typeof WideContent>;

export default meta;
type Story = StoryObj<typeof meta>;

export const WideWithFitToContent: Story = {
args: { width: 2400 },
parameters: {
argos: { fitToContent: true, modes: null },
},
};
32 changes: 32 additions & 0 deletions packages/storybook/stories/WideContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from "react";

type WideContentProps = {
/** Intrinsic content width in px, wider than the viewport. */
width?: number;
};

/**
* Renders fixed-width content wider than the viewport, used to reproduce
* screenshots being cut off *horizontally* in the Vitest integration.
*/
export const WideContent = ({ width = 2400 }: WideContentProps) => {
return (
<div style={{ padding: 16, fontFamily: "sans-serif" }}>
<div
style={{
width,
padding: "12px 16px",
border: "2px solid #c00",
borderRadius: 6,
background:
"repeating-linear-gradient(90deg,#fff 0 200px,#f0f0f0 200px 400px)",
whiteSpace: "nowrap",
boxSizing: "border-box",
}}
>
LEFT EDGE ← this box is {width}px wide, wider than the viewport, to test
horizontal clipping → RIGHT EDGE
</div>
</div>
);
};
Loading