Taster

진단부품톤 고르기
부품 갤러리 / Product card

Product card

Commerce

A single still hero, category entries as worlds, best-seller rails with a four by five product grid and a consultation entry, in a light editorial tone.

내 사이트에서 가져오기

색을 고르면 아래 부품이 그 색으로 바뀝니다.

실제로 돌아가는 부품입니다. 눌러 보고 입력해 보세요.

내 앱에 넣기

  1. 아직 npm 패키지가 없어서 Taster 리포를 받아 그 안의 명령을 씁니다. 내 앱 폴더가 들어 있는 폴더에서 한 번만 받습니다.

    git clone https://github.com/makim-414/taster.git && npm ci --prefix taster/packages/engine
  2. 내 앱 폴더에서 이 부품을 넣습니다. 부품 파일과 토큰 파일이 src/components/taster 에 들어갑니다.

    node ../taster/packages/engine/cli.mjs add commerce/product-card --from https://taster-six.vercel.app --dir src/components/taster
  3. 부품은 Tailwind 클래스로 그리고, Taster 프리셋이 그 클래스를 토큰에 연결합니다. Tailwind CSS 를 설치하고, tokens.css 를 앱 루트에서 한 번 불러온 뒤, 앱 루트에 이 설정을 둡니다. 프리셋이 없으면 빌드는 되어도 부품이 스타일 없이 나옵니다.

    // tailwind.config.cjs
    module.exports = {
      presets: [require('./src/components/taster/tailwind-preset.cjs')],
      content: ['./index.html', './src/**/*.{ts,tsx}'],
    };

    Tailwind v3 는 이 파일을 그대로 읽습니다. v4 는 Tailwind 를 불러오는 src/ 의 스타일시트 맨 위에 이 두 줄을 넣습니다.

    @import "tailwindcss";
    @config "../tailwind.config.cjs";

    tokens.css 는 :root 에 --bg, --text, --border, --accent 를 정의합니다. 내 앱의 스타일시트가 같은 이름을 뒤에서 다시 정의하면 그쪽 색이 이깁니다. 새 Vite 앱의 스타터 CSS 처럼 그런 변수가 있으면 지우거나 이름을 바꾸세요.

언제 쓰나요

  • Editorial storefront homeA single still hero, category entries as worlds, best-seller rails with a four by five product grid and a consultation entry, in a light editorial tone.
  • Media tile cardThe result, product or article image is the tile; the caption is thin and sits below or on a bottom scrim. Hub cards and article cards follow the same shape.

대신 없애 주는 AI 티

  • The predictable section order (LAY-04)
  • Drawn mockup instead of the real thing (AC-17)
  • Stretched image (CMP-12)
  • Three icon cards in a row (LAY-02)

정보

설치할 패키지
tailwindcss
라이선스
MIT
코드 보기
import * as React from 'react'
import { cn } from '../blocks/_cn'
import { safeHref } from '../blocks/_url'

// Product card: one product tile for a grid or a row. A photo at one ratio (a second photo shows on
// hover and on keyboard focus where supplied), the name, one descriptor line, the price and, when there is
// one, a real previous price struck through with its wording for screen readers. A short status line such
// as "Only 3 left" or "Back in May" is plain text, not a tinted pill (AC-04).
// The whole tile is one link; the image is not a separate tab stop. No hover lift, no quick-add overlay
// that hides the photo on touch, no star row unless the caller passes a real rating with its count.
// Follows patterns/editorial-storefront-home and patterns/media-tile-card. Use ProductGrid for a set: it
// keeps one ratio and lets the text set each row's height.
// Original code, MIT.

export interface Product {
  id: string
  href: string
  name: string
  /** One line, for example the size, the material or the variant count. */
  descriptor?: string
  /** Formatted price, for example "$48". */
  price: string
  /** A real previous price, formatted. */
  was?: string
  image: { src: string; alt: string }
  /** Second photo for hover and focus. */
  hoverImage?: { src: string; alt?: string }
  status?: string
  /** A real rating, for example { value: 4.6, count: 128 }. */
  rating?: { value: number; count: number }
  /** Language of the name and descriptor when it differs from the page, for example "ko". */
  lang?: string
}

export interface ProductCardProps extends React.HTMLAttributes<HTMLElement> {
  product: Product
  ratio?: '4/5' | '1/1' | '3/4'
  wasLabel?: string
  nowLabel?: string
}

const KEEP = 'break-keep [overflow-wrap:break-word]'
const RATIO = { '4/5': 'aspect-[4/5]', '1/1': 'aspect-square', '3/4': 'aspect-[3/4]' } as const

export function ProductCard({ product: p, ratio = '4/5', wasLabel = 'Was', nowLabel = 'Now', className, ...props }: ProductCardProps) {
  return (
    <article lang={p.lang} className={cn('min-w-0', className)} {...props}>
      <a href={safeHref(p.href)} className="group block rounded-md outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-4 focus-visible:ring-offset-[color:var(--bg)]">
        <div className={cn('relative overflow-hidden rounded-md bg-bg-sunken', RATIO[ratio])}>
          <img src={p.image.src} alt={p.image.alt} loading="lazy" className="absolute inset-0 h-full w-full object-cover" />
          {p.hoverImage ? (
            <img
              src={p.hoverImage.src}
              alt=""
              aria-hidden
              loading="lazy"
              className="absolute inset-0 h-full w-full object-cover opacity-0 transition-opacity duration-dropdown ease-out group-hover:opacity-100 group-focus-visible:opacity-100"
            />
          ) : null}
        </div>
        <div className="mt-3 flex items-start justify-between gap-3">
          <div className="min-w-0">
            <h3 className={cn('text-body font-medium text-text', KEEP)}>{p.name}</h3>
            {p.descriptor ? <p className={cn('mt-0.5 text-label text-text-muted', KEEP)}>{p.descriptor}</p> : null}
          </div>
          <p className="shrink-0 text-right text-body tabular-nums text-text">
            {p.was ? (
              <>
                <span className="sr-only">{nowLabel} </span>
                {p.price}
                <span className="block text-label text-text-subtle line-through">
                  <span className="sr-only">{wasLabel} </span>
                  {p.was}
                </span>
              </>
            ) : (
              p.price
            )}
          </p>
        </div>
        {p.rating || p.status ? (
          <p className="mt-1 flex flex-wrap gap-x-3 text-label tabular-nums text-text-muted">
            {p.rating ? (
              <span>
                {p.rating.value.toFixed(1)} out of 5, {p.rating.count.toLocaleString('en-US')} reviews
              </span>
            ) : null}
            {p.status ? <span className="text-text">{p.status}</span> : null}
          </p>
        ) : null}
      </a>
    </article>
  )
}

export interface ProductGridProps extends React.HTMLAttributes<HTMLDivElement> {
  products: Product[]
  ratio?: ProductCardProps['ratio']
  columns?: 2 | 3 | 4
}

export function ProductGrid({ products, ratio, columns = 4, className, ...props }: ProductGridProps) {
  return (
    <div
      className={cn('grid grid-cols-2 gap-x-4 gap-y-10 sm:gap-x-6', columns >= 3 && 'lg:grid-cols-3', columns === 4 && 'xl:grid-cols-4', className)}
      {...props}
    >
      {products.map((p) => (
        <ProductCard key={p.id} product={p} ratio={ratio} />
      ))}
    </div>
  )
}