Taster

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

Cart

Commerce

Line items, a free-shipping progress line, order summary and a stepped checkout.

내 사이트에서 가져오기

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

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

내 앱에 넣기

  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/cart --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 처럼 그런 변수가 있으면 지우거나 이름을 바꾸세요.

언제 쓰나요

  • Cart and checkoutLine items, a free-shipping progress line, order summary and a stepped checkout.

정보

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

// Cart: line items on the left and the order summary on the right, stacked on phones. A line has a 72px
// image, the name, its option text, a quantity stepper, the line price and a plain "Remove" text button.
// The summary lists subtotal, discount (only when applied), shipping, tax and the total, which is the
// largest figure on the screen, then the one solid checkout button. Every fee the caller knows is shown
// here, before checkout. An optional line above the list says how far the cart is from free shipping.
// Controlled: the caller owns the lines and gets onQuantity and onRemove, so it works with any backend.
// Follows patterns/cart-and-checkout: no upsell popup, no fake secure-payment badges, no green circles.
// Original code, MIT.

export interface CartLine {
  id: string
  name: string
  /** For example "30 ml, Unscented". */
  options?: string
  unitPrice: number
  quantity: number
  image: { src: string; alt: string }
  href?: string
  /** Language of the name and options when it differs from the page. */
  lang?: string
}

export interface CartProps extends React.HTMLAttributes<HTMLDivElement> {
  lines: CartLine[]
  onQuantity: (id: string, quantity: number) => void
  onRemove: (id: string) => void
  /** Shipping cost, or null when it is calculated at the next step (say so in shippingNote). */
  shipping: number | null
  shippingNote?: string
  /** Tax amount, or null when it is included in prices (say so in taxNote). */
  tax?: number | null
  taxNote?: string
  discount?: { label: string; amount: number }
  freeShippingAt?: number
  format?: (n: number) => string
  checkout: { label: string; href: string }
  maxQuantity?: number
  emptyMessage?: React.ReactNode
}

const KEEP = 'break-keep [overflow-wrap:break-word]'
const usd = (n: number) => new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(n)
const STEP =
  'inline-flex h-11 w-11 items-center justify-center text-text outline-none transition-colors duration-press ease-out hover:bg-bg-sunken focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-40 desk:h-9 desk:w-9'

export function Cart({
  lines,
  onQuantity,
  onRemove,
  shipping,
  shippingNote,
  tax = null,
  taxNote,
  discount,
  freeShippingAt,
  format = usd,
  checkout,
  maxQuantity = 10,
  emptyMessage = 'Your cart is empty.',
  className,
  ...props
}: CartProps) {
  const subtotal = lines.reduce((s, l) => s + l.unitPrice * l.quantity, 0)
  const total = subtotal - (discount?.amount ?? 0) + (shipping ?? 0) + (tax ?? 0)
  const left = freeShippingAt !== undefined ? Math.max(0, freeShippingAt - subtotal) : null
  const uid = React.useId()

  if (!lines.length) {
    return (
      <div className={cn('py-12', className)} {...props}>
        <p className={cn('text-body text-text-muted', KEEP)}>{emptyMessage}</p>
      </div>
    )
  }

  return (
    <div className={cn('grid gap-10 lg:grid-cols-[minmax(0,7fr)_minmax(0,4fr)] lg:gap-12', className)} {...props}>
      <section aria-labelledby={`${uid}-items`} className="min-w-0">
        <h2 id={`${uid}-items`} className="sr-only">
          Items
        </h2>
        {left !== null && freeShippingAt ? (
          <div className="mb-6">
            <p className={cn('text-body text-text', KEEP)}>
              {left > 0 ? `${format(left)} more for free shipping.` : 'Shipping is free for this order.'}
            </p>
            <div
              role="progressbar"
              aria-label="Progress to free shipping"
              aria-valuemin={0}
              aria-valuemax={freeShippingAt}
              aria-valuenow={Math.min(subtotal, freeShippingAt)}
              className="mt-2 h-1 overflow-hidden rounded-pill bg-[color:var(--border)]"
            >
              <div className="h-full bg-text transition-[width] duration-modal ease-out" style={{ width: `${Math.min(100, (subtotal / freeShippingAt) * 100)}%` }} />
            </div>
          </div>
        ) : null}
        <ul className="m-0 list-none border-t border-border p-0">
          {lines.map((l) => (
            <li key={l.id} lang={l.lang} className="grid grid-cols-[72px_minmax(0,1fr)] gap-4 border-b border-border py-5">
              <img src={l.image.src} alt={l.image.alt} width={72} height={72} className="h-[72px] w-[72px] rounded-sm bg-bg-sunken object-cover" />
              <div className="grid min-w-0 gap-3 sm:grid-cols-[minmax(0,1fr)_auto] sm:items-start">
                <div className="min-w-0">
                  <p className={cn('text-body font-medium text-text', KEEP)}>
                    {l.href ? (
                      <a href={safeHref(l.href)} className="rounded-sm outline-none hover:underline focus-visible:ring-2 focus-visible:ring-ring">
                        {l.name}
                      </a>
                    ) : (
                      l.name
                    )}
                  </p>
                  {l.options ? <p className={cn('mt-0.5 text-label text-text-muted', KEEP)}>{l.options}</p> : null}
                  <p className="mt-0.5 text-label tabular-nums text-text-subtle">{format(l.unitPrice)} each</p>
                </div>
                <p className="text-body font-medium tabular-nums text-text sm:text-right">{format(l.unitPrice * l.quantity)}</p>
                <div className="flex flex-wrap items-center gap-4 sm:col-span-2">
                  <div role="group" aria-label={`Quantity of ${l.name}`} className="inline-flex items-center rounded-md border border-border">
                    <button type="button" className={cn(STEP, 'rounded-l-md')} onClick={() => onQuantity(l.id, l.quantity - 1)} disabled={l.quantity <= 1} aria-label="Decrease">
                      <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden>
                        <path d="M5 12h14" />
                      </svg>
                    </button>
                    <output className="w-9 text-center text-body tabular-nums text-text">{l.quantity}</output>
                    <button type="button" className={cn(STEP, 'rounded-r-md')} onClick={() => onQuantity(l.id, l.quantity + 1)} disabled={l.quantity >= maxQuantity} aria-label="Increase">
                      <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden>
                        <path d="M12 5v14M5 12h14" />
                      </svg>
                    </button>
                  </div>
                  <button
                    type="button"
                    onClick={() => onRemove(l.id)}
                    className="inline-flex h-11 items-center rounded-sm text-label text-text-muted underline underline-offset-4 outline-none hover:text-text focus-visible:ring-2 focus-visible:ring-ring desk:h-8"
                  >
                    Remove<span className="sr-only"> {l.name}</span>
                  </button>
                </div>
              </div>
            </li>
          ))}
        </ul>
      </section>

      <section aria-labelledby={`${uid}-summary`} className="h-fit min-w-0 rounded-xl bg-bg-sunken p-6 lg:sticky lg:top-8">
        <h2 id={`${uid}-summary`} className="text-title font-semibold text-text">
          Order summary
        </h2>
        <dl className="m-0 mt-4 grid gap-2 text-body">
          <div className="flex justify-between gap-4">
            <dt className="text-text-muted">Subtotal</dt>
            <dd className="m-0 tabular-nums text-text">{format(subtotal)}</dd>
          </div>
          {discount ? (
            <div className="flex justify-between gap-4">
              <dt className={cn('text-text-muted', KEEP)}>{discount.label}</dt>
              <dd className="m-0 tabular-nums text-text">-{format(discount.amount)}</dd>
            </div>
          ) : null}
          <div className="flex justify-between gap-4">
            <dt className="text-text-muted">Shipping</dt>
            <dd className={cn('m-0 text-right tabular-nums text-text', KEEP)}>{shipping === null ? shippingNote ?? 'At the next step' : shipping === 0 ? 'Free' : format(shipping)}</dd>
          </div>
          <div className="flex justify-between gap-4">
            <dt className="text-text-muted">Tax</dt>
            <dd className={cn('m-0 text-right tabular-nums text-text', KEEP)}>{tax === null ? taxNote ?? 'Included' : format(tax)}</dd>
          </div>
          <div className="mt-2 flex items-baseline justify-between gap-4 border-t border-border pt-4">
            <dt className="font-medium text-text">Total</dt>
            <dd className="m-0 text-h2 font-semibold tabular-nums text-text">{format(total)}</dd>
          </div>
        </dl>
        <a
          href={safeHref(checkout.href)}
          className="mt-6 inline-flex h-12 w-full items-center justify-center rounded-md bg-accent px-6 text-title font-medium text-accent-fg outline-none transition-[opacity,transform] duration-press ease-out hover:opacity-90 active:scale-press focus-visible:ring-2 focus-visible:ring-ring"
        >
          {checkout.label}
        </a>
      </section>
    </div>
  )
}