Files
HMDesign/src/components/avatar.stories.tsx
T

123 lines
4.8 KiB
TypeScript

import type { Meta, StoryObj } from "@storybook/react-vite"
import { Avatar, AvatarFallback, AvatarImage } from "@hm-design/ui"
import { StateBoard } from "@/components/story-board"
import { StoryPage, StoryPageHeader, StorySection } from "@/components/story-page"
// Images are inline data URIs so the Story does not depend on the network. "Broken" uses an address that cannot load so the
// Radix fallback shows. Radix renders the image only after it loads.
const photo =
"data:image/svg+xml;utf8," +
encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" width="80" height="80"><rect width="80" height="80" fill="#266651"/><circle cx="40" cy="31" r="14" fill="#bddfd4"/><rect x="14" y="50" width="52" height="40" rx="22" fill="#bddfd4"/></svg>'
)
function Sizes() {
return (
<div className="flex items-end gap-4">
{(["sm", "default", "lg"] as const).map((size) => (
<div key={size} className="grid justify-items-center gap-2">
<Avatar size={size}>
<AvatarImage src={photo} alt="" />
<AvatarFallback>한맥</AvatarFallback>
</Avatar>
<span className="text-xs text-muted-foreground">{size}</span>
</div>
))}
</div>
)
}
// `showCaption={false}` drops the captions on the representative page (they restate the visible fallback text); the verification Story keeps them.
function Fallbacks({ showCaption = true }: { showCaption?: boolean }) {
const items = [
{ label: "한글 두 글자", text: "김한" },
{ label: "영문 두 글자", text: "HM" },
{ label: "한 글자", text: "한" },
{ label: "긴 fallback", text: "ABCDEFGHIJ" },
]
return (
<div className="flex flex-wrap items-end gap-4">
{items.map((i) => (
<div key={i.label} className="grid justify-items-center gap-2">
<Avatar>
<AvatarImage src="/broken-avatar-image.png" alt="" />
<AvatarFallback>{i.text}</AvatarFallback>
</Avatar>
{showCaption ? <span className="text-xs text-muted-foreground">{i.label}</span> : null}
</div>
))}
</div>
)
}
function Group() {
return (
<div className="flex items-center gap-3">
<Avatar>
<AvatarImage src={photo} alt="김한맥" />
<AvatarFallback>김한</AvatarFallback>
</Avatar>
<div className="grid text-sm">
<span className="font-medium">김한맥</span>
<span className="text-muted-foreground">노선 설계팀</span>
</div>
</div>
)
}
const sections = [
{ id: "image", label: "Image", node: <Sizes /> },
{ id: "fallback", label: "Fallback", node: <Fallbacks showCaption={false} /> },
{ id: "composition", label: "Composition", node: <Group /> },
]
const meta = { title: "Components/Data & Display/Avatar", component: Avatar, parameters: { layout: "padded" } } satisfies Meta<typeof Avatar>
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
// (240px 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.
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%,240px),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 / States are merged into Variants (Image, Fallback); Composition stays under Usage. 3 columns on PC.
export const All: Story = {
name: "Avatar",
parameters: { layout: "fullscreen", controls: { disable: true } },
render: () => (
<StoryPage>
<StoryPageHeader title="Avatar" />
<StorySection title="Variants">
<SpecimenGrid ids={["image", "fallback"]} columns={3} />
</StorySection>
<StorySection title="Usage">
<SpecimenGrid ids={["composition"]} columns={3} />
</StorySection>
</StoryPage>
),
}
export const Image: Story = { tags: ["verification"], render: () => <Sizes /> }
export const FallbackError: Story = { tags: ["verification"], name: "Fallback", render: () => <Fallbacks /> }