102 lines
3.9 KiB
TypeScript
102 lines
3.9 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 { STORY_KICKER, 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>
|
|
)
|
|
}
|
|
|
|
function Fallbacks() {
|
|
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>
|
|
<span className="text-xs text-muted-foreground">{i.label}</span>
|
|
</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", note: "이미지가 로드되면 fallback은 나타나지 않습니다", node: <Sizes /> },
|
|
{ id: "fallback", label: "Fallback (image error)", note: "이미지 로드 실패 시 initials", node: <Fallbacks /> },
|
|
{ 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>
|
|
|
|
const board = (...ids: string[]) => (
|
|
<StateBoard min={320} states={sections.filter((s) => ids.includes(s.id)).map((s) => ({ id: s.id, label: s.label, note: s.note, children: s.node }))} />
|
|
)
|
|
|
|
// Representative Story: the common Story page; the sections group the same specimens.
|
|
export const All: Story = {
|
|
name: "Avatar",
|
|
parameters: { layout: "fullscreen", controls: { disable: true } },
|
|
render: () => (
|
|
<StoryPage>
|
|
<StoryPageHeader
|
|
kicker={STORY_KICKER.components}
|
|
title="Avatar"
|
|
description="사용자를 나타내는 원형 이미지입니다. 이미지를 불러오지 못하면 fallback 글자를 표시합니다."
|
|
/>
|
|
<StorySection title="Overview">{board("image")}</StorySection>
|
|
<StorySection title="States">{board("fallback")}</StorySection>
|
|
<StorySection title="Usage" description="이름·소속과 함께 쓰는 소비 조합입니다.">{board("composition")}</StorySection>
|
|
</StoryPage>
|
|
),
|
|
}
|
|
|
|
export const Image: Story = { tags: ["verification"], render: () => <Sizes /> }
|
|
export const FallbackError: Story = { tags: ["verification"], name: "Fallback", render: () => <Fallbacks /> }
|