写一个 story 模拟三星 Fold 折叠态切换并自动重排布局
解读
面试官想考察三件事:
- 能否用 Storybook 的 viewport 插件 精确模拟三星 Fold 的 双屏分辨率(折叠 586×882、展开 1536×2152)。
- 能否在 story 里 动态切换两种尺寸 并触发组件的 响应式重排逻辑(CSS Grid/Flex 或容器查询)。
- 是否熟悉 Storybook 8 的交互式测试能力(play 函数 + @storybook/addon-interactions),让“折叠/展开”动作可 自动回放,而不仅是静态展示。
在国内面试场景下,组件库往往要适配 鸿蒙折叠屏、荣耀 Magic V 等多款国产折叠机,因此必须证明方案 可泛化,而非硬编码三星参数。
知识点
- viewport 预设:在
.storybook/preview.js注册自定义设备,精确到 DPR 2.4 与 圆角安全区。 - useEffect + ResizeObserver:在 story 内部监听 iframe 尺寸变化,触发组件重新渲染,避免仅靠 CSS 媒体查询。
- addon-interactions:利用
userEvent.click与expect(canvas.getByTestId)断言 布局列数 从 2→1→2 的切换。 - CSS 容器查询:使用
@container (width <= 586px)实现 组件级响应,而非全局断点,符合 微前端 场景。 - Storybook 8 的 parameters.viewports.defaultOrientation,可一次性把 “折叠态”设为初始视角,方便设计走查。
答案
- 在
.storybook/preview.js注册国产折叠屏预设
import { MINIMAL_VIEWPORTS } from '@storybook/addon-viewport';
const FOLD_DEVICES = {
samsungFoldFolded: {
name: '三星Fold 折叠态',
styles: { width: '586px', height: '882px' },
type: 'mobile',
deviceScaleFactor: 2.4,
},
samsungFoldUnfolded: {
name: '三星Fold 展开态',
styles: { width: '1536px', height: '2152px' },
type: 'tablet',
deviceScaleFactor: 2.4,
},
};
export const parameters = {
viewport: {
viewports: { ...MINIMAL_VIEWPORTS, ...FOLD_DEVICES },
defaultViewport: 'samsungFoldFolded',
},
};
- 编写可重排的业务组件
AdaptiveGrid.tsx
import { useState, useEffect, useRef } from 'react';
export default function AdaptiveGrid({ items }: { items: string[] }) {
const ref = useRef<HTMLDivElement>(null);
const [cols, setCols] = useState(1);
useEffect(() => {
const ro = new ResizeObserver(([entry]) => {
const width = entry.borderBoxSize[0].inlineSize;
setCols(width <= 586 ? 1 : 2);
});
if (ref.current) ro.observe(ref.current);
return () => ro.disconnect();
}, []);
return (
<div
ref={ref}
style={{
display: 'grid',
gridTemplateColumns: `repeat(${cols}, 1fr)`,
gap: 16,
}}
>
{items.map((t) => (
<div key={t} style={{ background: '#e2e8f0', padding: 24 }}>
{t}
</div>
))}
</div>
);
}
- 创建
AdaptiveGrid.stories.tsx
import type { Meta, StoryObj } from '@storybook/react';
import { within, expect } from '@storybook/test';
import AdaptiveGrid from './AdaptiveGrid';
const meta: Meta<typeof AdaptiveGrid> = {
title: 'Example/AdaptiveGrid',
component: AdaptiveGrid,
parameters: {
viewport: { defaultViewport: 'samsungFoldFolded' },
},
};
export default meta;
type Story = StoryObj<typeof meta>;
export const Folded: Story = {
args: { items: ['A', 'B', 'C', 'D'] },
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
// 折叠态只有 1 列
await expect(canvas.getByText('A').parentElement?.parentElement).toHaveStyle({
gridTemplateColumns: 'repeat(1, 1fr)',
});
},
};
export const Unfolded: Story = {
args: { items: ['A', 'B', 'C', 'D'] },
parameters: {
viewport: { defaultViewport: 'samsungFoldUnfolded' },
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
// 展开态变为 2 列
await expect(canvas.getByText('A').parentElement?.parentElement).toHaveStyle({
gridTemplateColumns: 'repeat(2, 1fr)',
});
},
};
// 交互式切换演示
export const ToggleFold: Story = {
args: { items: ['A', 'B', 'C', 'D'] },
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement);
await step('初始折叠', async () => {
await expect(canvas.getByText('A').parentElement?.parentElement).toHaveStyle({
gridTemplateColumns: 'repeat(1, 1fr)',
});
});
await step('切到展开', async () => {
// 通过 channel 通知 viewport 插件切换
window.__STORYBOOK_VIEWPORT__.updateViewport('samsungFoldUnfolded');
await canvas.findByText('C'); // 等待重排完成
await expect(canvas.getByText('A').parentElement?.parentElement).toHaveStyle({
gridTemplateColumns: 'repeat(2, 1fr)',
});
});
},
};
- 运行
pnpm storybook后,在设计、产品、测试三方评审时,可直接点击 “ToggleFold” story 的 Replay 按钮,自动回放折叠→展开→重排全过程,无需真机。
拓展思考
- 若组件库需同时适配 荣耀 Magic V3(外屏 1066×2508,内屏 1800×2296),只需在预设里再新增两条记录,零业务代码改动。
- 对 Flutter 混合项目,可把同一思路搬到 Storybook-flutter 社区插件,用
MediaQuery模拟折叠,保持 设计 token 一致。 - 在 CI 阶段 用
test-runner截取两种视口下的 DOM 快照,通过 reg-cli 做像素级回归,防止响应式布局被意外破坏。 - 若需 性能基线,可在
play函数里调用performance.measure(),把 重排耗时 上报到 阿里云 SLS,实现 可观测性闭环。