1280px
Install
One command installs the files and packages this item needs.
TerminalTerminalTerminalbash
npx shadcn@latest add https://dsui.vercel.app/r/item-card.jsonFiles
components/arc/item-card/item-card.module.csscomponents/arc/item-card/item-card.tsx
Registry items it pulls in
- v2.0.3Card title up to 3 lines, summary up to 3 lines, and the small line (kind and date) fixed to one line
- v2.0.2When a drawn picture paints its own ground, the box's grey ground no longer shows through as a pale line along the rounded edge.
- v2.0.1Keeps the card short in a phone's narrow two columns: the title shows up to 4 lines, the summary 2, the picture grows no taller than 9.5rem and the padding is tighter. A long word wraps inside its column.
- v2.0.0Rebuilt as a masonry card: a small line (kind, date), the title and the summary are always shown, and the picture runs to the card's bottom edge. The hover roll of the title is gone, the muted and ink tones are new, and split now draws as basic. Added the ratio, footer and linkProps props.
- v1.2.0art 속성 추가(썸네일 파일 대신 인라인 SVG 등을 그릴 수 있어요)
- v1.1.0Added the split tone with a separate cover.
- v1.0.0First release.
item-card.module.cssitem-card.module.cssitem-card.module.csscss
/* A card of a masonry: the small line, the title, a line of summary, and the picture flush with the bottom edge. The card is as tall as its content,
so cards of different content stagger when set in columns. Pointing at it lifts it; pressing it sinks it. */
.wrap { position: relative; display: flex; min-width: 0; }
.card {
--bg: var(--surface); --fg: var(--foreground); --sub: var(--text-secondary); --ground: var(--surface-muted);
--shadow-rest: var(--shadow-resting); --shadow-lift: var(--shadow-raised); --ring: var(--accent);
--count-bg: var(--foreground); --count-fg: var(--surface);
position: relative; isolation: isolate; display: flex; min-width: 0; flex: 1 1 auto; flex-direction: column; overflow: hidden;
border-radius: var(--radius-surface); background: var(--bg); color: var(--fg); box-shadow: var(--shadow-rest);
text-decoration: none; -webkit-tap-highlight-color: transparent;
transition: transform var(--duration-spring) var(--ease-spring), box-shadow var(--duration-fast) var(--ease-standard);
}
/* Where the browser can draw a rounder corner (a superellipse, as on a phone's app icons), the card takes it. */
@supports (corner-shape: superellipse(1.5)) { .card { corner-shape: superellipse(1.5); border-radius: calc(var(--radius-surface) * 1.2); } }
.muted { --bg: var(--surface-sunken); --ground: var(--surface); --shadow-rest: var(--shadow-resting); }
.dark { --bg: var(--inverse); --fg: var(--inverse-foreground); --sub: color-mix(in srgb, var(--inverse-foreground) 70%, transparent); --count-bg: var(--surface); --count-fg: var(--foreground); --ring: color-mix(in srgb, var(--accent) 50%, transparent); }
.ink { --bg: var(--foreground); --fg: var(--background); --sub: color-mix(in srgb, var(--background) 72%, transparent); --count-bg: var(--background); --count-fg: var(--foreground); }
.accent { --bg: var(--accent); --fg: var(--accent-foreground); --sub: color-mix(in srgb, var(--accent-foreground) 78%, transparent); --shadow-rest: var(--shadow-accent-resting); --shadow-lift: var(--shadow-accent-raised); --count-bg: var(--accent-foreground); --count-fg: var(--accent); }
.highlight { --bg: var(--highlight); --fg: var(--highlight-foreground); --sub: color-mix(in srgb, var(--highlight-foreground) 72%, transparent); --shadow-rest: var(--shadow-highlight-resting); --shadow-lift: var(--shadow-highlight-raised); --count-bg: var(--highlight-foreground); --count-fg: var(--highlight); }
/* The coloured faces cannot take the accent for an active comment count (it would vanish), so it keeps the card's own colour. */
.accent, .highlight, .ink, .dark, .cover { --stat-active: currentColor; }
.cover { --bg: var(--inverse); --fg: #fff; --sub: rgba(255, 255, 255, .82); --count-bg: var(--surface); --count-fg: var(--foreground); min-height: 15rem; }
.card:focus-visible { outline: 4px solid var(--ring); outline-offset: 0; }
/* In dark mode the grey card takes the lighter grey and its picture the near-black one; the "ink" card is the light one there and its picture takes the lighter grey.
Same guards as the rest of the site's dark rules. */
:global(:root[data-theme="dark"]) .muted, :global(:root.dark) .muted { --bg: var(--surface-raised); --ground: var(--surface-sunken); }
:global(:root[data-theme="dark"]) .ink, :global(:root.dark) .ink { --ground: var(--surface-raised); }
@media (prefers-color-scheme: dark) {
:global(:root:not([data-theme="light"]):not([data-theme="dark"]):not(.light):not(.dark)) .muted { --bg: var(--surface-raised); --ground: var(--surface-sunken); }
:global(:root:not([data-theme="light"]):not([data-theme="dark"]):not(.light):not(.dark)) .ink { --ground: var(--surface-raised); }
}
.body { position: relative; z-index: 1; display: flex; min-width: 0; flex-direction: column; gap: var(--space-2); padding: var(--space-5) var(--space-5) var(--space-5); }
.hasShot .body { padding-bottom: 0; }
.cover .body { flex: 1 1 auto; justify-content: flex-end; }
/* The small line (kind and date) is always one line: the kind gives way with an ellipsis, the date stays whole. */
.meta { display: flex; min-width: 0; flex-wrap: nowrap; align-items: center; gap: var(--space-1) var(--space-2); overflow: hidden; color: var(--sub); font-size: var(--text-sm); font-weight: 500; font-variant-numeric: tabular-nums; white-space: nowrap; }
.label { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.date { flex: none; min-width: 0; white-space: nowrap; }
.title { display: -webkit-box; overflow: hidden; margin: 0; -webkit-box-orient: vertical; -webkit-line-clamp: 3; font-size: var(--card-title, clamp(17px, 4.6vw, 22px)); font-weight: 800; line-height: 1.2; letter-spacing: -.03em; text-wrap: balance; word-break: keep-all; }
.cover .title { text-shadow: 0 .1em .5em rgba(0, 0, 0, .35); }
.summary { display: -webkit-box; overflow: hidden; -webkit-box-orient: vertical; -webkit-line-clamp: var(--summary-lines, 3); color: var(--sub); font-size: var(--text-base); line-height: 1.4; word-break: keep-all; text-wrap: pretty; }
/* A long unbroken word (a product name, an address) breaks rather than push the card wider than its column. */
.meta, .title, .summary { overflow-wrap: anywhere; }
/* On a phone the columns are narrow (about 170px), so the card keeps itself short: a smaller title, a shorter summary, tighter padding, and a picture that never grows taller than 9.5rem. */
@media (max-width: 39.99rem) {
.card { --summary-lines: 2; }
.body { gap: var(--space-1); padding: var(--space-4) var(--space-4) var(--space-4); }
.title { font-size: var(--card-title, clamp(15px, 4.4vw, 18px)); }
.meta { font-size: var(--text-xs); }
.summary { font-size: var(--text-sm); line-height: 1.35; }
.card .shot { max-height: 9.5rem; margin: var(--space-3) var(--space-2) 0; }
}
/* Anything the caller puts under the words, such as the counts that load later, takes the card's quiet colour. */
.body > :global([data-card-stats]) { margin-top: var(--space-1); color: inherit; opacity: .8; font-size: var(--text-xs); }
/* The picture: inset from the sides, flush with the bottom, its top corners rounded. The card clips its bottom. */
.shot { position: relative; display: block; flex: none; aspect-ratio: var(--shot-ratio, 4 / 3); margin: var(--space-4) var(--space-3) 0; overflow: hidden; border-radius: calc(var(--radius-surface) - var(--space-3)) calc(var(--radius-surface) - var(--space-3)) 0 0; background: var(--ground); }
/* A drawn picture paints its own ground. The box's grey behind it would show as a pale hairline along the rounded edge (the picture's edge is anti-aliased there), so the box has none. */
.shotArt { background: transparent; }
.image, .video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.art { position: absolute; inset: 0; display: block; transition: transform var(--duration-considered) var(--ease-standard); }
.art > svg { display: block; width: 100%; height: 100%; }
.image { transition: transform var(--duration-considered) var(--ease-standard); }
.video { opacity: 0; transition: opacity var(--duration-fast) var(--ease-standard), transform var(--duration-considered) var(--ease-standard); }
.video[data-playing="true"] { opacity: 1; }
.darkOnly { display: none; }
:global(:root[data-theme="dark"]) .lightOnly, :global(:root.dark) .lightOnly { display: none; }
:global(:root[data-theme="dark"]) .darkOnly, :global(:root.dark) .darkOnly { display: block; }
@media (prefers-color-scheme: dark) {
:global(:root:not([data-theme="light"])) .lightOnly { display: none; }
:global(:root:not([data-theme="light"])) .darkOnly { display: block; }
}
/* A cover: the picture fills the card and is dimmed under the words, so they stay readable. */
.media { position: absolute; inset: 0; z-index: -1; overflow: hidden; border-radius: inherit; background: var(--inverse); }
.media::after { content: ""; position: absolute; inset: 0; background: linear-gradient(180deg, rgba(0, 0, 0, .05) 25%, rgba(0, 0, 0, .62)); transition: opacity var(--duration-standard) var(--ease-standard); }
/* Outside the link: a number is not a destination, and inside it would be read as part of the item's name. */
.count { position: absolute; z-index: 2; inset-block-start: var(--space-4); inset-inline-end: var(--space-4); display: inline-flex; align-items: center; gap: var(--space-1); border-radius: var(--radius-pill); padding: var(--space-1) var(--space-2); background: color-mix(in srgb, var(--count-bg) 86%, transparent); color: var(--count-fg); font-size: var(--text-xs); font-weight: var(--label-weight); pointer-events: none; }
.wrap:has(.cover) .count { --count-bg: var(--surface); --count-fg: var(--foreground); }
.wrap:has(.accent) .count { --count-bg: var(--accent-foreground); --count-fg: var(--accent); }
.wrap:has(.highlight) .count { --count-bg: var(--highlight-foreground); --count-fg: var(--highlight); }
.wrap:has(.dark) .count, .wrap:has(.ink) .count { --count-bg: var(--surface); --count-fg: var(--foreground); }
.countIcon { display: inline-flex; flex: none; }
@media (hover: hover) and (pointer: fine) {
.card:hover { transform: scale(1.025); box-shadow: var(--shadow-lift); }
.card:hover .image, .card:hover .art, .card:hover .video { transform: scale(1.04); }
}
.card:active { transform: scale(.965); box-shadow: var(--shadow-rest); }
@media (prefers-reduced-motion: reduce) {
.card, .media::after, .image, .art, .video { transition: none; }
.card:hover, .card:active { transform: none; }
.card:hover .image, .card:hover .art, .card:hover .video { transform: none; }
}
item-card.tsxitem-card.tsxitem-card.tsxtsx
"use client";
import { useCallback, useEffect, useId, useRef, useState } from "react";
import type { CSSProperties, ReactNode } from "react";
import { ArcLink } from "../lib/arc-provider";
import { playUiSound } from "../sound/play";
import styles from "./item-card.module.css";
import { DecorParticles, useDecorEdgeClass } from "../decor";
import { prefersReducedMotion } from "../lib/use-reduced-motion";
/**
* The face of the card: `basic` is a raised white card, `muted` the page's own grey one, `dark` and `ink` the two dark ones (`dark` stays dark in both
* themes, `ink` is the opposite of the page), `accent` and `highlight` the brand colours, `cover` a card the picture fills edge to edge.
* `split` is the name this card had when its picture sat in a box of its own; it now draws as `basic`.
*/
export type ItemCardTone = "basic" | "muted" | "dark" | "ink" | "accent" | "highlight" | "cover" | "split";
export interface ItemCardProps {
href: string;
title: string;
/** A line under the title, up to four lines of it. Left out, the card is the title and its picture. */
summary?: string;
/** The face of the card. `cover` paints the picture edge to edge with the words over it; without a picture it takes `fallbackTone`. */
tone?: ItemCardTone;
/** The face a `cover` takes when its picture is missing or fails to load. */
fallbackTone?: Exclude<ItemCardTone, "cover">;
/** The picture of the card, one per theme. Give both when the picture is a screenshot of the item. */
thumbnail?: { light: string; dark?: string };
/** A picture drawn in place of the thumbnail files, such as inline SVG that reads the page's theme variables. It fills the picture box and wins over `thumbnail`. */
art?: ReactNode;
/** The shape of the picture box under the words (a CSS `aspect-ratio`, `4 / 3` by default). Varying it from card to card is what gives a masonry its rhythm. */
ratio?: string;
/** A clip over the picture: plays on hover and on keyboard focus, and never when the reader asked for less motion. */
video?: string;
/** What kind of thing it is, the first part of the small line above the title. */
label?: ReactNode;
/** The second part of that line, usually a date. */
date?: ReactNode;
/** More inside the link, under the words (counts that load later, say). Left to the caller's own styling. */
footer?: ReactNode;
/** A count shown in the corner, e.g. likes. `label` names what is counted, for the reader and for assistive tech. */
count?: { value: number; label: string; icon?: ReactNode };
/** The language of the title and summary, when they differ from the page. */
lang?: string;
/** More attributes for the link, such as `data-icon-surface`. */
linkProps?: Record<`data-${string}`, string | undefined>;
className?: string;
/** Lands on the link, for tests. */
"data-testid"?: string;
}
/**
* A card of a masonry: the small line (kind and date), the title, a line of summary and the picture under them, flush with the card's bottom edge.
* Cards differ in height (with or without a summary or a picture, and the picture's `ratio`), so set side by side in columns they stagger.
* Pointing at one lifts it a little; pressing it sinks it back.
*
* The count sits outside the link, because a number is not a destination and would otherwise be read as part of the item's name.
*/
export function ItemCard({ href, title, summary, tone = "basic", fallbackTone = "dark", thumbnail, art, ratio, video, label, date, footer, count, lang, linkProps, className, "data-testid": testId }: ItemCardProps) {
const id = useId();
const edgeClass = useDecorEdgeClass();
const [failed, setFailed] = useState(false);
const [playing, setPlaying] = useState(false);
const videoRef = useRef<HTMLVideoElement>(null);
const images = useRef<(HTMLImageElement | null)[]>([]);
// An image that failed before hydration never fires React's onError, so they are checked once on mount.
useEffect(() => {
if (images.current.some(img => img && img.complete && img.naturalWidth === 0)) setFailed(true);
}, []);
const start = useCallback(() => {
if (!video || prefersReducedMotion()) return;
setPlaying(true);
videoRef.current?.play().catch(() => setPlaying(false));
}, [video]);
const stop = useCallback(() => {
setPlaying(false);
const el = videoRef.current;
if (el) { el.pause(); el.currentTime = 0; }
}, []);
const pictured = !!art || (!!thumbnail && !failed);
const cover = tone === "cover" && pictured;
const face = tone === "cover" ? (pictured ? "cover" : fallbackTone) : tone === "split" ? "basic" : tone;
const shot = pictured && !cover;
const picture = (
<>
{art ? <span className={styles.art}>{art}</span> : thumbnail ? (
<>
{/* eslint-disable-next-line @next/next/no-img-element -- generated thumbnails that may be missing; next/image cannot report that. */}
<img ref={el => { images.current[0] = el; }} src={thumbnail.light} alt="" loading="lazy" onError={() => setFailed(true)} className={`${styles.image} ${styles.lightOnly}`} />
{thumbnail.dark ? (
/* eslint-disable-next-line @next/next/no-img-element -- as above */
<img ref={el => { images.current[1] = el; }} src={thumbnail.dark} alt="" loading="lazy" onError={() => setFailed(true)} className={`${styles.image} ${styles.darkOnly}`} />
) : null}
</>
) : null}
{video ? <video ref={videoRef} muted loop playsInline preload="none" src={video} className={styles.video} data-playing={playing ? "true" : undefined} /> : null}
</>
);
return (
<div className={[styles.wrap, className].filter(Boolean).join(" ")} data-item-card="">
<ArcLink
href={href}
data-testid={testId}
aria-labelledby={`${id}-title`}
aria-describedby={summary ? `${id}-summary` : undefined}
className={[styles.card, styles[face], shot ? styles.hasShot : undefined, edgeClass].filter(Boolean).join(" ")}
style={ratio ? ({ "--shot-ratio": ratio } as CSSProperties) : undefined}
onMouseEnter={start}
onMouseLeave={stop}
onFocus={start}
onBlur={stop}
onClick={() => playUiSound("tick")}
{...linkProps}
>
<DecorParticles slot="card" />
{cover ? <span aria-hidden="true" className={styles.media}>{picture}</span> : null}
<span className={styles.body} data-part="body">
{label || date ? (
<span className={styles.meta} data-part="meta">
{label ? <span className={styles.label}>{label}</span> : null}
{label && date ? <span aria-hidden="true">·</span> : null}
{date ? <span className={styles.date}>{date}</span> : null}
</span>
) : null}
<span id={`${id}-title`} lang={lang} className={styles.title} data-part="title">{title}</span>
{summary && !cover ? <span id={`${id}-summary`} lang={lang} className={styles.summary} data-part="summary">{summary}</span> : null}
{footer}
</span>
{shot ? <span aria-hidden="true" className={[styles.shot, art ? styles.shotArt : undefined].filter(Boolean).join(" ")} data-part="shot">{picture}</span> : null}
</ArcLink>
{count && count.value > 0 ? (
<span className={styles.count}>
{count.icon ? <span className={styles.countIcon} aria-hidden="true">{count.icon}</span> : null}
<span>{`${count.label} ${count.value}`}</span>
</span>
) : null}
</div>
);
}
ItemCard.displayName = "ItemCard";
export default ItemCard;