Files
HMDesign/src/components/aspect-ratio.stories.tsx
T

105 lines
4.8 KiB
TypeScript

import type { Meta, StoryObj } from "@storybook/react-vite"
import { AspectRatio, Card, CardContent } from "@hm-design/ui"
import { StateBoard } from "@/components/story-board"
import { StoryPage, StoryPageHeader, StorySection } from "@/components/story-page"
// AspectRatio is a layout primitive: it keeps the ratio and nothing else. The image styling (object-fit, radius) in these Stories is
// the consumer's composition on the child, not part of AspectRatio. Images are inline data URIs (no network). The Card example only
// shows how a consumer can compose it; AspectRatio does not depend on Card.
const svg = (label: string, fill: string) =>
"data:image/svg+xml;utf8," +
encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" width="480" height="320" viewBox="0 0 480 320"><rect width="480" height="320" fill="${fill}"/><circle cx="360" cy="90" r="40" fill="#bddfd4"/><path d="M0 320 L150 150 L260 260 L340 190 L480 320 Z" fill="#205241"/><text x="24" y="44" font-family="sans-serif" font-size="26" fill="#ffffff">${label}</text></svg>`
)
function Image({ ratio, label }: { ratio: number; label: string }) {
return (
<div style={{ width: "100%", maxWidth: 320 }}>
<AspectRatio ratio={ratio} data-testid={`ratio-${label}`}>
<img src={svg(label, "#266651")} alt={`${label} 예시 이미지`} className="size-full rounded-md object-cover" />
</AspectRatio>
</div>
)
}
function Placeholder() {
return (
<div style={{ width: "100%", maxWidth: 320 }}>
<AspectRatio ratio={16 / 9} data-testid="ratio-video">
<div className="grid size-full place-items-center rounded-md border bg-muted text-sm text-muted-foreground">영상 영역 16:9</div>
</AspectRatio>
</div>
)
}
function InCard() {
return (
<Card style={{ width: "100%", maxWidth: 320 }}>
<CardContent>
<AspectRatio ratio={4 / 3} data-testid="ratio-card">
<img src={svg("Card 4:3", "#3e3523")} alt="카드 안 4:3 이미지" className="size-full rounded-md object-cover" />
</AspectRatio>
</CardContent>
</Card>
)
}
const sections = [
{ id: "r169", label: "16:9", node: <Image ratio={16 / 9} label="16:9" /> },
{ id: "r43", label: "4:3", node: <Image ratio={4 / 3} label="4:3" /> },
{ id: "r11", label: "1:1", node: <Image ratio={1} label="1:1" /> },
{ id: "video", label: "Video", node: <Placeholder /> },
{ id: "card", label: "Card", node: <InCard /> },
]
const meta = { title: "Components/Data & Display/Aspect Ratio", component: AspectRatio, parameters: { layout: "padded" } } satisfies Meta<typeof AspectRatio>
export default meta
type Story = StoryObj<typeof meta>
// Representative-page layout (Storybook presentation only): equal columns on PC (iframe width 1000px and up), fewer below that
// (200px minimum). Each specimen sits in its own single-column StateBoard (min larger than any column), so every subtitle and divider
// has the width of its column; a section with fewer specimens than columns leaves the remaining columns empty.
// The Card example spans two columns (about its previous width); the ratio boxes fill their column by design.
const specimenColumns = {
2: "min-[1000px]:grid-cols-2",
3: "min-[1000px]:grid-cols-3",
4: "min-[1000px]:grid-cols-4",
} as const
function SpecimenGrid({ ids, columns, spans = {} }: { ids: string[]; columns: 2 | 3 | 4; spans?: Record<string, 2> }) {
return (
<div className={`grid items-start gap-6 [grid-template-columns:repeat(auto-fill,minmax(min(100%,200px),1fr))] ${specimenColumns[columns]}`}>
{ids.map((id) => {
const s = sections.find((section) => section.id === id)!
return (
<div key={id} className={spans[id] ? "min-[1000px]:col-span-2" : undefined}>
<StateBoard min={10000} states={[{ id: s.id, label: s.label, children: s.node }]} />
</div>
)
})}
</div>
)
}
// Representative Story: the common Story page; the sections group the same specimens.
// Representative Story: Overview (16:9) is merged into Variants (16:9, 4:3, 1:1, Video), 4 columns on PC; Card stays under Usage.
export const All: Story = {
name: "Aspect Ratio",
parameters: { layout: "fullscreen", controls: { disable: true } },
render: () => (
<StoryPage>
<StoryPageHeader title="Aspect Ratio" />
<StorySection title="Variants">
<SpecimenGrid ids={["r169", "r43", "r11", "video"]} columns={4} />
</StorySection>
<StorySection title="Usage">
<SpecimenGrid ids={["card"]} columns={4} spans={{ card: 2 }} />
</StorySection>
</StoryPage>
),
}
export const Wide: Story = { tags: ["verification"], name: "16:9", render: () => <Image ratio={16 / 9} label="16:9" /> }
export const Square: Story = { tags: ["verification"], name: "1:1", render: () => <Image ratio={1} label="1:1" /> }