Skip to content

Commit c789f32

Browse files
authored
Merge pull request #336 from argos-ci/fix/vitest-screenshot-horizontal-cut-off
fix(storybook): screenshots cut off horizontally in Vitest integration
2 parents 3502fe8 + e89aeab commit c789f32

3 files changed

Lines changed: 102 additions & 21 deletions

File tree

‎packages/storybook/src/vitest-plugin.ts‎

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,56 @@ export const createArgosScreenshotCommand = (
4949
// and we screenshot the iframe's `<body>`. Anything overflowing the iframe box
5050
// is not painted, so the screenshot gets cut. `setViewportSize` grows the iframe
5151
// *before* `argosCSS` (which injects `fitToContent`'s `zoom`) is applied, so it
52-
// can't account for the final content size. Re-fit the iframe height here, after
52+
// can't account for the final content size. Re-fit the iframe here, after
5353
// stabilization has injected `argosCSS`, so the whole content is painted.
5454
const userBeforeScreenshot = options?.beforeScreenshot;
5555
const optionsWithFit: ArgosScreenshotOptions = {
5656
...options,
5757
beforeScreenshot: async (api) => {
5858
await userBeforeScreenshot?.(api);
59-
await ctx.page.evaluate(() => {
60-
const iframe = document.querySelector('iframe[data-vitest="true"]');
59+
await ctx.page.evaluate(
60+
({ fitWidth }) => {
61+
const iframe = document.querySelector('iframe[data-vitest="true"]');
6162

62-
if (
63-
!(iframe instanceof HTMLIFrameElement) ||
64-
!iframe.contentDocument
65-
) {
66-
return;
67-
}
63+
if (
64+
!(iframe instanceof HTMLIFrameElement) ||
65+
!iframe.contentDocument
66+
) {
67+
return;
68+
}
6869

69-
const { body, documentElement } = iframe.contentDocument;
70-
const contentHeight = Math.max(
71-
body.scrollHeight,
72-
body.offsetHeight,
73-
documentElement.scrollHeight,
74-
);
70+
const { body, documentElement } = iframe.contentDocument;
71+
const contentHeight = Math.max(
72+
body.scrollHeight,
73+
body.offsetHeight,
74+
documentElement.scrollHeight,
75+
);
76+
77+
// Only grow, never shrink: the iframe must contain the full content
78+
// so it's painted, but we don't want to collapse an intentionally
79+
// sized viewport.
80+
if (contentHeight > iframe.clientHeight) {
81+
iframe.style.height = `${contentHeight}px`;
82+
}
7583

76-
// Only grow, never shrink: the iframe must contain the full content so
77-
// it's painted, but we don't want to collapse an intentionally sized viewport.
78-
if (contentHeight > iframe.clientHeight) {
79-
iframe.style.height = `${contentHeight}px`;
80-
}
81-
});
84+
// `fitToContent` fits the content in both dimensions, so the iframe
85+
// must also grow horizontally to paint content wider than the
86+
// viewport. Without `fitToContent` we keep the viewport width to
87+
// match Playwright's `fullPage` semantics (full height, viewport
88+
// width).
89+
if (fitWidth) {
90+
const contentWidth = Math.max(
91+
body.scrollWidth,
92+
body.offsetWidth,
93+
documentElement.scrollWidth,
94+
);
95+
if (contentWidth > iframe.clientWidth) {
96+
iframe.style.width = `${contentWidth}px`;
97+
}
98+
}
99+
},
100+
{ fitWidth: Boolean(fitToContent) },
101+
);
82102
},
83103
};
84104

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { WideContent } from "./WideContent";
2+
import type { Meta, StoryObj } from "@storybook/react-vite";
3+
4+
/**
5+
* Regression story for screenshots being cut off *horizontally* when the
6+
* content is wider than the viewport.
7+
*
8+
* With `fitToContent`, the content is fit in both dimensions, so the iframe
9+
* must grow horizontally to paint content wider than the viewport. Otherwise
10+
* the screenshot is left with a large blank area on the right.
11+
*/
12+
const meta = {
13+
title: "Repro/WideContent",
14+
component: WideContent,
15+
parameters: {
16+
layout: "fullscreen",
17+
argos: { modes: null },
18+
},
19+
} satisfies Meta<typeof WideContent>;
20+
21+
export default meta;
22+
type Story = StoryObj<typeof meta>;
23+
24+
export const WideWithFitToContent: Story = {
25+
args: { width: 2400 },
26+
parameters: {
27+
argos: { fitToContent: true, modes: null },
28+
},
29+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from "react";
2+
3+
type WideContentProps = {
4+
/** Intrinsic content width in px, wider than the viewport. */
5+
width?: number;
6+
};
7+
8+
/**
9+
* Renders fixed-width content wider than the viewport, used to reproduce
10+
* screenshots being cut off *horizontally* in the Vitest integration.
11+
*/
12+
export const WideContent = ({ width = 2400 }: WideContentProps) => {
13+
return (
14+
<div style={{ padding: 16, fontFamily: "sans-serif" }}>
15+
<div
16+
style={{
17+
width,
18+
padding: "12px 16px",
19+
border: "2px solid #c00",
20+
borderRadius: 6,
21+
background:
22+
"repeating-linear-gradient(90deg,#fff 0 200px,#f0f0f0 200px 400px)",
23+
whiteSpace: "nowrap",
24+
boxSizing: "border-box",
25+
}}
26+
>
27+
LEFT EDGE ← this box is {width}px wide, wider than the viewport, to test
28+
horizontal clipping → RIGHT EDGE
29+
</div>
30+
</div>
31+
);
32+
};

0 commit comments

Comments
 (0)