بلاک‌ها · فروشگاهی
ری‌اکت ۱۹ و Next.js با پیاده‌سازی دسترس‌پذیری داخلی دیگویو ۳ با Composition APISvelte ۵ با runesانگولار با signals و دایرکتیوهای standaloneجتپک کامپوز با KotliniOS — هنوز پورت نشده

اقلام سبد با تغییر تعداد و حذف، کد تخفیف و خلاصهٔ مبلغ.

۱. نصب کامپوننت‌ها

این بلاک از ۱۱ آیتم دیگ استفاده می‌کند: add-to-cartbottom-action-barbuttoncontrol-sizeempty-stateiconspersianproduct-cardprogresstext-fieldtoast

پکیج‌منیجر پیش‌فرض Node.jsنصب سریع با لینک سخت و دیسک مشترکYarn نسخهٔ ۲ به بالا (Berry)رانتایم و پکیج‌منیجر Bun
npx digdesign@latest add https://docs.digdesign.ir/r/add-to-cart.json https://docs.digdesign.ir/r/bottom-action-bar.json https://docs.digdesign.ir/r/button.json https://docs.digdesign.ir/r/control-size.json https://docs.digdesign.ir/r/empty-state.json https://docs.digdesign.ir/r/icons.json https://docs.digdesign.ir/r/persian.json https://docs.digdesign.ir/r/product-card.json https://docs.digdesign.ir/r/progress.json https://docs.digdesign.ir/r/text-field.json https://docs.digdesign.ir/r/toast.json
pnpm dlx digdesign@latest add https://docs.digdesign.ir/r/add-to-cart.json https://docs.digdesign.ir/r/bottom-action-bar.json https://docs.digdesign.ir/r/button.json https://docs.digdesign.ir/r/control-size.json https://docs.digdesign.ir/r/empty-state.json https://docs.digdesign.ir/r/icons.json https://docs.digdesign.ir/r/persian.json https://docs.digdesign.ir/r/product-card.json https://docs.digdesign.ir/r/progress.json https://docs.digdesign.ir/r/text-field.json https://docs.digdesign.ir/r/toast.json
yarn dlx digdesign@latest add https://docs.digdesign.ir/r/add-to-cart.json https://docs.digdesign.ir/r/bottom-action-bar.json https://docs.digdesign.ir/r/button.json https://docs.digdesign.ir/r/control-size.json https://docs.digdesign.ir/r/empty-state.json https://docs.digdesign.ir/r/icons.json https://docs.digdesign.ir/r/persian.json https://docs.digdesign.ir/r/product-card.json https://docs.digdesign.ir/r/progress.json https://docs.digdesign.ir/r/text-field.json https://docs.digdesign.ir/r/toast.json
bunx --bun digdesign@latest add https://docs.digdesign.ir/r/add-to-cart.json https://docs.digdesign.ir/r/bottom-action-bar.json https://docs.digdesign.ir/r/button.json https://docs.digdesign.ir/r/control-size.json https://docs.digdesign.ir/r/empty-state.json https://docs.digdesign.ir/r/icons.json https://docs.digdesign.ir/r/persian.json https://docs.digdesign.ir/r/product-card.json https://docs.digdesign.ir/r/progress.json https://docs.digdesign.ir/r/text-field.json https://docs.digdesign.ir/r/toast.json
پکیج‌منیجر پیش‌فرض Node.jsنصب سریع با لینک سخت و دیسک مشترکYarn نسخهٔ ۲ به بالا (Berry)رانتایم و پکیج‌منیجر Bun
npx digdesign@latest add add-to-cart bottom-action-bar button control-size empty-state persian product-card progress text-field toast --framework vue
pnpm dlx digdesign@latest add add-to-cart bottom-action-bar button control-size empty-state persian product-card progress text-field toast --framework vue
yarn dlx digdesign@latest add add-to-cart bottom-action-bar button control-size empty-state persian product-card progress text-field toast --framework vue
bunx --bun digdesign@latest add add-to-cart bottom-action-bar button control-size empty-state persian product-card progress text-field toast --framework vue
پکیج‌منیجر پیش‌فرض Node.jsنصب سریع با لینک سخت و دیسک مشترکYarn نسخهٔ ۲ به بالا (Berry)رانتایم و پکیج‌منیجر Bun
npx digdesign@latest add bottom-action-bar button --framework svelte
pnpm dlx digdesign@latest add bottom-action-bar button --framework svelte
yarn dlx digdesign@latest add bottom-action-bar button --framework svelte
bunx --bun digdesign@latest add bottom-action-bar button --framework svelte
پکیج‌منیجر پیش‌فرض Node.jsنصب سریع با لینک سخت و دیسک مشترکYarn نسخهٔ ۲ به بالا (Berry)رانتایم و پکیج‌منیجر Bun
npx digdesign@latest add add-to-cart bottom-action-bar button control-size empty-state icons input persian product-card progress text-field toast --framework angular
pnpm dlx digdesign@latest add add-to-cart bottom-action-bar button control-size empty-state icons input persian product-card progress text-field toast --framework angular
yarn dlx digdesign@latest add add-to-cart bottom-action-bar button control-size empty-state icons input persian product-card progress text-field toast --framework angular
bunx --bun digdesign@latest add add-to-cart bottom-action-bar button control-size empty-state icons input persian product-card progress text-field toast --framework angular
پکیج‌منیجر پیش‌فرض Node.jsنصب سریع با لینک سخت و دیسک مشترکYarn نسخهٔ ۲ به بالا (Berry)رانتایم و پکیج‌منیجر Bun
npx digdesign@latest add add-to-cart bottom-action-bar button control-size empty-state persian product-card progress text-field toast --framework android
pnpm dlx digdesign@latest add add-to-cart bottom-action-bar button control-size empty-state persian product-card progress text-field toast --framework android
yarn dlx digdesign@latest add add-to-cart bottom-action-bar button control-size empty-state persian product-card progress text-field toast --framework android
bunx --bun digdesign@latest add add-to-cart bottom-action-bar button control-size empty-state persian product-card progress text-field toast --framework android

۲. کد صفحه

کد را در صفحهٔ پروژه‌تان بگذارید (React: app/<route>/page.tsx) و متن‌ها، داده و هندلرها را با محصول خودتان عوض کنید. با سوییچر فریم‌ورک کدِ همین صفحه در فریم‌ورک دیگر را ببینید.

cart.tsx
"use client"

import * as React from "react"

import { CheckCircleIcon, ShieldCheckIcon, ShoppingCartIcon, TruckIcon } from "@/components/icons"
import { AddToCart } from "@/components/ui/add-to-cart"
import { BottomActionBar, BottomActionBarSummary } from "@/components/ui/bottom-action-bar"
import { Button } from "@/components/ui/button"
import { ControlSizeProvider } from "@/components/ui/control-size"
import { EmptyState, EmptyStateActions, EmptyStateDescription, EmptyStateTitle } from "@/components/ui/empty-state"
import { ProductCardPrice } from "@/components/ui/product-card"
import { Progress } from "@/components/ui/progress"
import { TextField } from "@/components/ui/text-field"
import { toast } from "@/components/ui/toast"
import { formatCurrency, formatNumber, toFaDigits } from "@/lib/persian"
import { stockImages, type StockImageName } from "@/lib/stock-images"

type Item = { id: string; title: string; variant: string; swatch?: string; image: StockImageName; price: number; oldPrice?: number; quantity: number }

const INITIAL: Item[] = [
  { id: "1", title: "گلدانِ سرامیکی دوقلو", variant: "کرم", swatch: "var(--dig-amber-100)", image: "productVase", price: 1_290_000, oldPrice: 1_520_000, quantity: 1 },
  { id: "2", title: "دفترِ یادداشتِ جلدسخت", variant: "A5 · خط‌دار", image: "coverNotebook", price: 180_000, quantity: 2 },
  { id: "3", title: "ست لیوانِ کاغذیِ برندار", variant: "۱۲ عدد", image: "projectCafeBrand", price: 420_000, quantity: 1 },
]
const SHIPPING = 45_000
const FREE_SHIPPING = 5_000_000

export default function CartBlock() {
  const [items, setItems] = React.useState(INITIAL)
  const [discount, setDiscount] = React.useState(0)
  const subtotal = items.reduce((sum, item) => sum + item.price * item.quantity, 0)
  const count = items.reduce((sum, item) => sum + item.quantity, 0)
  const shipping = subtotal >= FREE_SHIPPING ? 0 : SHIPPING
  const remaining = Math.max(0, FREE_SHIPPING - subtotal)

  const update = (id: string, quantity: number) =>
    setItems((current) => current.map((item) => (item.id === id ? { ...item, quantity } : item)))
  const remove = (item: Item) => {
    const index = items.findIndex((row) => row.id === item.id)
    setItems((current) => current.filter((row) => row.id !== item.id))
    toast(`«${item.title}» از سبد حذف شد.`, {
      action: { label: "برگردان", onClick: () => setItems((current) => (current.some((row) => row.id === item.id) ? current : [...current.slice(0, index), { ...item, quantity: 1 }, ...current.slice(index)])) },
    })
  }
  const [couponOpen, setCouponOpen] = React.useState(false)
  const total = subtotal - discount + shipping

  if (items.length === 0) {
    return (
      <main className="mx-auto w-full max-w-3xl p-4 sm:p-6">
        <EmptyState variant="bordered" size="lg">
          <ShoppingCartIcon aria-hidden className="size-10 text-placeholder" />
          <EmptyStateTitle>سبد خرید شما خالی است</EmptyStateTitle>
          <EmptyStateDescription>محصولی که به سبد اضافه کنید این‌جا نمایش داده می‌شود.</EmptyStateDescription>
          <EmptyStateActions>
            <Button onClick={() => setItems(INITIAL)}>مشاهدهٔ محصولات</Button>
          </EmptyStateActions>
        </EmptyState>
      </main>
    )
  }

  return (
    <ControlSizeProvider size="xl" asChild>
      <main className="mx-auto flex w-full max-w-5xl flex-col gap-6 p-4 sm:p-6">
        <header className="flex flex-col gap-1">
          <h1 className="text-2xl font-bold">سبد خرید</h1>
          <p className="text-sm text-muted-foreground">{toFaDigits(count)} کالا در سبد شما</p>
        </header>

        <ControlSizeProvider size="xl">
          <div className="grid grid-cols-1 items-start gap-6 lg:grid-cols-[minmax(0,1fr)_22rem]">
            <section aria-label="کالاهای سبد" className="flex flex-col gap-3">
              {/* پیشرفت تا ارسالِ رایگان: انگیزهٔ افزودنِ کالا، همان‌جا که کاربر تصمیم می‌گیرد. */}
              <div className="flex flex-col gap-2.5 rounded-xl border border-border bg-card p-4">
                <div className="flex items-center gap-2 text-sm">
                  {remaining > 0 ? (
                    <TruckIcon aria-hidden className="size-5 text-muted-foreground" />
                  ) : (
                    <CheckCircleIcon aria-hidden className="size-5 text-success-subtle-foreground" />
                  )}
                  {remaining > 0 ? (
                    <span>
                      <strong className="font-semibold">{formatCurrency(remaining)}</strong> تا ارسالِ رایگان
                    </span>
                  ) : (
                    <span className="font-medium text-success-subtle-foreground">ارسالِ این سفارش رایگان است</span>
                  )}
                </div>
                <Progress
                  value={Math.min(100, (subtotal / FREE_SHIPPING) * 100)}
                  color={remaining > 0 ? "primary" : "success"}
                  size="sm"
                  aria-label="پیشرفت تا ارسال رایگان"
                />
              </div>

              {/* هر کالا یک ردیفِ فشرده: عکس، نام و گونه، قیمت و شمارنده در همان ردیف؛ تعدادِ ۱ سطل نشان می‌دهد و حذف برگشت‌پذیر است. */}
              <ul className="flex flex-col divide-y divide-border rounded-xl border border-border bg-card">
                {items.map((item) => (
                  <li key={item.id} className="flex gap-3 p-3 sm:gap-4 sm:p-4">
                    {/* eslint-disable-next-line @next/next/no-img-element -- عکس از کتابخانهٔ تصویرِ دیگ */}
                    <img src={stockImages[item.image].src} alt="" className="size-16 shrink-0 rounded-lg object-cover sm:size-24" />
                    <div className="flex min-w-0 flex-1 flex-col gap-1">
                      <a href="#" className="line-clamp-2 leading-snug font-semibold hover:underline">
                        {item.title}
                      </a>
                      <span className="flex items-center gap-1.5 text-xs text-muted-foreground">
                        {item.swatch && <span aria-hidden className="size-3 rounded-full border border-border" style={{ background: item.swatch }} />}
                        {item.variant}
                      </span>
                      <div className="mt-auto flex flex-wrap items-end justify-between gap-x-3 gap-y-2 pt-1">
                        <span className="flex flex-col gap-0.5">
                          <ProductCardPrice
                            amount={formatCurrency(item.price * item.quantity)}
                            originalAmount={item.oldPrice ? formatCurrency(item.oldPrice * item.quantity) : undefined}
                            discount={item.oldPrice ? `٪${toFaDigits(Math.round((1 - item.price / item.oldPrice) * 100))}` : undefined}
                            className="pt-0 [--product-card-price:var(--text-base)]"
                          />
                          {item.quantity > 1 && <span className="text-xs text-muted-foreground tabular-nums">هر عدد {formatCurrency(item.price)}</span>}
                        </span>
                        <ControlSizeProvider size="lg" mobileSize="default" asChild>
                          <AddToCart
                            label="افزودن"
                            itemName={item.title}
                            max={10}
                            quantity={item.quantity}
                            onQuantityChange={(quantity) => (quantity === 0 ? remove(item) : update(item.id, quantity))}
                          />
                        </ControlSizeProvider>
                      </div>
                    </div>
                  </li>
                ))}
              </ul>
            </section>

            <aside aria-labelledby="summary-title" className="flex flex-col gap-4 rounded-xl border border-border bg-card p-4 sm:p-5 lg:sticky lg:top-6">
              <h2 id="summary-title" className="text-lg font-semibold">
                خلاصهٔ سفارش
              </h2>
              {/* کد تخفیف پنهان ولی پیداشدنی؛ فیلد و دکمه‌اش ۴۰ پیکسل، یک پله زیرِ دکمهٔ پرداخت. */}
              {couponOpen ? (
                <ControlSizeProvider size="lg" asChild>
                  <form
                    className="flex items-end gap-2"
                    onSubmit={(event) => {
                      event.preventDefault()
                      setDiscount(Math.round(subtotal * 0.1))
                      toast.success("کد تخفیف اعمال شد.")
                    }}
                  >
                    <TextField label="کد تخفیف" name="coupon" dir="ltr" autoComplete="off" autoFocus className="flex-1" />
                    <Button type="submit" variant="outline">
                      اعمال
                    </Button>
                  </form>
                </ControlSizeProvider>
              ) : (
                <ControlSizeProvider size="sm" asChild>
                  <Button variant="ghost" onClick={() => setCouponOpen(true)} className="-ms-3 w-fit text-brand-secondary">
                    کد تخفیف داری؟
                  </Button>
                </ControlSizeProvider>
              )}
              <dl className="flex flex-col gap-3 text-sm">
                <div className="flex justify-between gap-2">
                  <dt className="text-muted-foreground">جمع کالاها ({toFaDigits(count)})</dt>
                  <dd>{formatCurrency(subtotal)}</dd>
                </div>
                {discount > 0 && (
                  <div className="flex justify-between gap-2 text-success-subtle-foreground">
                    <dt>تخفیف</dt>
                    <dd>
                      <bdi dir="ltr">−{formatNumber(discount)}</bdi> تومان
                    </dd>
                  </div>
                )}
                <div className="flex justify-between gap-2">
                  <dt className="text-muted-foreground">هزینهٔ ارسال</dt>
                  <dd>{shipping === 0 ? <span className="font-medium text-success-subtle-foreground">رایگان</span> : formatCurrency(shipping)}</dd>
                </div>
                <div className="mt-2 flex items-baseline justify-between gap-2 border-t border-border pt-4">
                  <dt className="font-semibold">مبلغ قابل پرداخت</dt>
                  <dd className="text-lg font-bold tabular-nums">{formatCurrency(total)}</dd>
                </div>
              </dl>
              {/* در موبایل دکمهٔ پرداخت در نوارِ پایینِ صفحه است (دسترسِ شست)؛ این‌جا فقط از sm به بالا. */}
              <div className="mt-4 hidden flex-col gap-3 sm:flex">
                <Button fullWidth>
                  ادامه و پرداخت
                </Button>
                <p className="flex items-center justify-center gap-1.5 text-xs text-muted-foreground">
                  <ShieldCheckIcon aria-hidden className="size-4" />
                  پرداختِ امن از درگاهِ شاپرک
                </p>
              </div>
            </aside>
          </div>
        </ControlSizeProvider>

        {/* نوارِ پایینِ موبایل: مبلغِ نهایی و اقدامِ اصلی همیشه در دسترسِ شست. */}
        <BottomActionBar hideFrom="sm" className="-mx-4 -mb-4">
          <BottomActionBarSummary label="مبلغِ قابلِ پرداخت" value={formatCurrency(total)} />
          <Button>ادامه و پرداخت</Button>
        </BottomActionBar>
      </main>
    </ControlSizeProvider>
  )
}
cart.vue
<script setup lang="ts">
import { computed, ref } from "vue"

import AddToCart from "@/components/ui/AddToCart.vue"
import BottomActionBar from "@/components/ui/BottomActionBar.vue"
import BottomActionBarSummary from "@/components/ui/BottomActionBarSummary.vue"
import Button from "@/components/ui/Button.vue"
import ControlSize from "@/components/ui/ControlSize.vue"
import EmptyState from "@/components/ui/EmptyState.vue"
import EmptyStateActions from "@/components/ui/EmptyStateActions.vue"
import EmptyStateDescription from "@/components/ui/EmptyStateDescription.vue"
import EmptyStateTitle from "@/components/ui/EmptyStateTitle.vue"
import ProductCardPrice from "@/components/ui/ProductCardPrice.vue"
import Progress from "@/components/ui/Progress.vue"
import TextField from "@/components/ui/TextField.vue"
import { toast } from "@/components/ui/Toast.vue"
import { DigIcon } from "@/lib/icons"
import { formatCurrency, formatNumber, toFaDigits } from "@/lib/persian"

// تصویرهای کتابخانهٔ تصویرِ دیگ (همان فایل‌های stock-images نسخهٔ React).
const stockImages = {
  productVase: { src: "https://docs.digdesign.ir/images/product-vase.webp", alt: "دو گلدانِ سرامیکیِ سفید کنارِ پارچهٔ کرم" },
  coverNotebook: { src: "https://docs.digdesign.ir/images/cover-notebook.webp", alt: "دفترچه، خودکار، صفحه‌کلید و عینک روی میزِ سفید" },
  projectCafeBrand: { src: "https://docs.digdesign.ir/images/project-cafe-brand.webp", alt: "لیوان‌های کاغذیِ قهوه با درِ سیاه و سفید" },
}
type StockImageName = keyof typeof stockImages

type Item = { id: string; title: string; variant: string; swatch?: string; image: StockImageName; price: number; oldPrice?: number; quantity: number }

const INITIAL: Item[] = [
  { id: "1", title: "گلدانِ سرامیکی دوقلو", variant: "کرم", swatch: "var(--dig-amber-100)", image: "productVase", price: 1_290_000, oldPrice: 1_520_000, quantity: 1 },
  { id: "2", title: "دفترِ یادداشتِ جلدسخت", variant: "A5 · خط‌دار", image: "coverNotebook", price: 180_000, quantity: 2 },
  { id: "3", title: "ست لیوانِ کاغذیِ برندار", variant: "۱۲ عدد", image: "projectCafeBrand", price: 420_000, quantity: 1 },
]
const SHIPPING = 45_000
const FREE_SHIPPING = 5_000_000

const items = ref<Item[]>(INITIAL)
const discount = ref(0)
const couponOpen = ref(false)
const subtotal = computed(() => items.value.reduce((sum, item) => sum + item.price * item.quantity, 0))
const count = computed(() => items.value.reduce((sum, item) => sum + item.quantity, 0))
const shipping = computed(() => (subtotal.value >= FREE_SHIPPING ? 0 : SHIPPING))
const remaining = computed(() => Math.max(0, FREE_SHIPPING - subtotal.value))
const total = computed(() => subtotal.value - discount.value + shipping.value)

function update(id: string, quantity: number) {
  items.value = items.value.map((item) => (item.id === id ? { ...item, quantity } : item))
}
function remove(item: Item) {
  const index = items.value.findIndex((row) => row.id === item.id)
  items.value = items.value.filter((row) => row.id !== item.id)
  toast(`«${item.title}» از سبد حذف شد.`, {
    action: {
      label: "برگردان",
      onClick: () => {
        items.value = items.value.some((row) => row.id === item.id) ? items.value : [...items.value.slice(0, index), { ...item, quantity: 1 }, ...items.value.slice(index)]
      },
    },
  })
}
function onQuantity(item: Item, quantity: number) {
  if (quantity === 0) remove(item)
  else update(item.id, quantity)
}
function applyCoupon() {
  discount.value = Math.round(subtotal.value * 0.1)
  toast.success("کد تخفیف اعمال شد.")
}
</script>

<template>
  <main v-if="items.length === 0" class="mx-auto w-full max-w-3xl p-4 sm:p-6">
    <EmptyState variant="bordered" size="lg">
      <DigIcon name="shopping-cart" aria-hidden="true" class="size-10 text-placeholder" />
      <EmptyStateTitle>سبد خرید شما خالی است</EmptyStateTitle>
      <EmptyStateDescription>محصولی که به سبد اضافه کنید این‌جا نمایش داده می‌شود.</EmptyStateDescription>
      <EmptyStateActions>
        <Button @click="items = INITIAL">مشاهدهٔ محصولات</Button>
      </EmptyStateActions>
    </EmptyState>
  </main>

  <ControlSize v-else size="xl" as-child>
    <main class="mx-auto flex w-full max-w-5xl flex-col gap-6 p-4 sm:p-6">
      <header class="flex flex-col gap-1">
        <h1 class="text-2xl font-bold">سبد خرید</h1>
        <p class="text-sm text-muted-foreground">{{ toFaDigits(count) }} کالا در سبد شما</p>
      </header>

      <ControlSize size="xl">
        <div class="grid grid-cols-1 items-start gap-6 lg:grid-cols-[minmax(0,1fr)_22rem]">
          <section aria-label="کالاهای سبد" class="flex flex-col gap-3">
            <!-- پیشرفت تا ارسالِ رایگان: انگیزهٔ افزودنِ کالا، همان‌جا که کاربر تصمیم می‌گیرد. -->
            <div class="flex flex-col gap-2.5 rounded-xl border border-border bg-card p-4">
              <div class="flex items-center gap-2 text-sm">
                <DigIcon v-if="remaining > 0" name="truck" aria-hidden="true" class="size-5 text-muted-foreground" />
                <DigIcon v-else name="check-circle" aria-hidden="true" class="size-5 text-success-subtle-foreground" />
                <span v-if="remaining > 0"><strong class="font-semibold">{{ formatCurrency(remaining) }}</strong> تا ارسالِ رایگان</span>
                <span v-else class="font-medium text-success-subtle-foreground">ارسالِ این سفارش رایگان است</span>
              </div>
              <Progress :value="Math.min(100, (subtotal / FREE_SHIPPING) * 100)" :color="remaining > 0 ? 'primary' : 'success'" size="sm" aria-label="پیشرفت تا ارسال رایگان" />
            </div>

            <!-- هر کالا یک ردیفِ فشرده: عکس، نام و گونه، قیمت و شمارنده در همان ردیف؛ تعدادِ ۱ سطل نشان می‌دهد و حذف برگشت‌پذیر است. -->
            <ul class="flex flex-col divide-y divide-border rounded-xl border border-border bg-card">
              <li v-for="item in items" :key="item.id" class="flex gap-3 p-3 sm:gap-4 sm:p-4">
                <img :src="stockImages[item.image].src" alt="" class="size-16 shrink-0 rounded-lg object-cover sm:size-24" />
                <div class="flex min-w-0 flex-1 flex-col gap-1">
                  <a href="#" class="line-clamp-2 leading-snug font-semibold hover:underline">{{ item.title }}</a>
                  <span class="flex items-center gap-1.5 text-xs text-muted-foreground">
                    <span v-if="item.swatch" aria-hidden="true" class="size-3 rounded-full border border-border" :style="{ background: item.swatch }" />
                    {{ item.variant }}
                  </span>
                  <div class="mt-auto flex flex-wrap items-end justify-between gap-x-3 gap-y-2 pt-1">
                    <span class="flex flex-col gap-0.5">
                      <ProductCardPrice
                        :amount="formatCurrency(item.price * item.quantity)"
                        :original-amount="item.oldPrice ? formatCurrency(item.oldPrice * item.quantity) : undefined"
                        :discount="item.oldPrice ? `٪${toFaDigits(Math.round((1 - item.price / item.oldPrice) * 100))}` : undefined"
                        class="pt-0 [--product-card-price:var(--text-base)]"
                      />
                      <span v-if="item.quantity > 1" class="text-xs text-muted-foreground tabular-nums">هر عدد {{ formatCurrency(item.price) }}</span>
                    </span>
                    <ControlSize size="lg" mobile-size="default" as-child>
                      <AddToCart label="افزودن" :item-name="item.title" :max="10" :quantity="item.quantity" @update:quantity="(quantity) => onQuantity(item, quantity)" />
                    </ControlSize>
                  </div>
                </div>
              </li>
            </ul>
          </section>

          <aside aria-labelledby="summary-title" class="flex flex-col gap-4 rounded-xl border border-border bg-card p-4 sm:p-5 lg:sticky lg:top-6">
            <h2 id="summary-title" class="text-lg font-semibold">خلاصهٔ سفارش</h2>
            <!-- کد تخفیف پنهان ولی پیداشدنی؛ فیلد و دکمه‌اش ۴۰ پیکسل، یک پله زیرِ دکمهٔ پرداخت. -->
            <ControlSize v-if="couponOpen" size="lg" as-child>
              <form class="flex items-end gap-2" @submit.prevent="applyCoupon">
                <TextField label="کد تخفیف" name="coupon" dir="ltr" autocomplete="off" autofocus class="flex-1" />
                <Button type="submit" variant="outline">اعمال</Button>
              </form>
            </ControlSize>
            <ControlSize v-else size="sm" as-child>
              <Button variant="ghost" class="-ms-3 w-fit text-brand-secondary" @click="couponOpen = true">کد تخفیف داری؟</Button>
            </ControlSize>
            <dl class="flex flex-col gap-3 text-sm">
              <div class="flex justify-between gap-2">
                <dt class="text-muted-foreground">جمع کالاها ({{ toFaDigits(count) }})</dt>
                <dd>{{ formatCurrency(subtotal) }}</dd>
              </div>
              <div v-if="discount > 0" class="flex justify-between gap-2 text-success-subtle-foreground">
                <dt>تخفیف</dt>
                <dd><bdi dir="ltr">−{{ formatNumber(discount) }}</bdi> تومان</dd>
              </div>
              <div class="flex justify-between gap-2">
                <dt class="text-muted-foreground">هزینهٔ ارسال</dt>
                <dd>
                  <span v-if="shipping === 0" class="font-medium text-success-subtle-foreground">رایگان</span>
                  <template v-else>{{ formatCurrency(shipping) }}</template>
                </dd>
              </div>
              <div class="mt-2 flex items-baseline justify-between gap-2 border-t border-border pt-4">
                <dt class="font-semibold">مبلغ قابل پرداخت</dt>
                <dd class="text-lg font-bold tabular-nums">{{ formatCurrency(total) }}</dd>
              </div>
            </dl>
            <!-- در موبایل دکمهٔ پرداخت در نوارِ پایینِ صفحه است (دسترسِ شست)؛ این‌جا فقط از sm به بالا. -->
            <div class="mt-4 hidden flex-col gap-3 sm:flex">
              <Button full-width>ادامه و پرداخت</Button>
              <p class="flex items-center justify-center gap-1.5 text-xs text-muted-foreground">
                <DigIcon name="shield-check" aria-hidden="true" class="size-4" />
                پرداختِ امن از درگاهِ شاپرک
              </p>
            </div>
          </aside>
        </div>
      </ControlSize>

      <!-- نوارِ پایینِ موبایل: مبلغِ نهایی و اقدامِ اصلی همیشه در دسترسِ شست. -->
      <BottomActionBar hide-from="sm" class="-mx-4 -mb-4">
        <BottomActionBarSummary label="مبلغِ قابلِ پرداخت" :value="formatCurrency(total)" />
        <Button>ادامه و پرداخت</Button>
      </BottomActionBar>
    </main>
  </ControlSize>
</template>

کدِ این بلاک هنوز برای Svelte نوشته نشده است.

این موارد هنوز برای Svelte آماده نیستند: add-to-cartcontrol-sizeempty-statepersianproduct-cardprogresstext-fieldtoastآیکون check-circleآیکون shield-checkآیکون shopping-cartآیکون truck

کدِ React همین بلاک مرجع است. با MCP دیگ و get_block با framework="svelte" ایجنت آن را با کامپوننت‌های Svelte می‌سازد.

cart.ts
import {
  ChangeDetectionStrategy,
  Component,
  computed,
  signal,
} from "@angular/core"

import { DigIcon } from "@/lib/icons"
import { formatCurrency, formatNumber, toFaDigits } from "@/lib/persian"
import { DigAddToCart } from "@/components/ui/add-to-cart"
import { DigBottomActionBar } from "@/components/ui/bottom-action-bar"
import { DigButton } from "@/components/ui/button"
import { DigControlSize } from "@/components/ui/control-size"
import {
  DigEmptyState,
  DigEmptyStateActions,
  DigEmptyStateDescription,
  DigEmptyStateTitle,
} from "@/components/ui/empty-state"
import { DigInput, DigInputField } from "@/components/ui/input"
import { DigProductCardPrice } from "@/components/ui/product-card"
import { DigProgress } from "@/components/ui/progress"
import { DigTextField } from "@/components/ui/text-field"
import { toast } from "@/components/ui/toast"

const stockImages = {
  productVase: {
    src: "https://docs.digdesign.ir/images/product-vase.webp",
    alt: "دو گلدانِ سرامیکیِ سفید کنارِ پارچهٔ کرم",
  },
  coverNotebook: {
    src: "https://docs.digdesign.ir/images/cover-notebook.webp",
    alt: "دفترچه، خودکار، صفحه‌کلید و عینک روی میزِ سفید",
  },
  projectCafeBrand: {
    src: "https://docs.digdesign.ir/images/project-cafe-brand.webp",
    alt: "لیوان‌های کاغذیِ قهوه با درِ سیاه و سفید",
  },
}
type StockImageName = keyof typeof stockImages

type Item = {
  id: string
  title: string
  variant: string
  swatch?: string
  image: StockImageName
  price: number
  oldPrice?: number
  quantity: number
}

const INITIAL: Item[] = [
  {
    id: "1",
    title: "گلدانِ سرامیکی دوقلو",
    variant: "کرم",
    swatch: "var(--dig-amber-100)",
    image: "productVase",
    price: 1_290_000,
    oldPrice: 1_520_000,
    quantity: 1,
  },
  {
    id: "2",
    title: "دفترِ یادداشتِ جلدسخت",
    variant: "A5 · خط‌دار",
    image: "coverNotebook",
    price: 180_000,
    quantity: 2,
  },
  {
    id: "3",
    title: "ست لیوانِ کاغذیِ برندار",
    variant: "۱۲ عدد",
    image: "projectCafeBrand",
    price: 420_000,
    quantity: 1,
  },
]
const SHIPPING = 45_000
const FREE_SHIPPING = 5_000_000

@Component({
  selector: "dig-block-cart",
  imports: [
    DigAddToCart,
    DigBottomActionBar,
    DigButton,
    DigControlSize,
    DigEmptyState,
    DigEmptyStateActions,
    DigEmptyStateDescription,
    DigEmptyStateTitle,
    DigIcon,
    DigInput,
    DigInputField,
    DigProductCardPrice,
    DigProgress,
    DigTextField,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    @if (items().length === 0) {
      <main class="mx-auto w-full max-w-3xl p-4 sm:p-6">
        <div digEmptyState variant="bordered" size="lg">
          <svg
            digIcon="shopping-cart"
            aria-hidden="true"
            class="size-10 text-placeholder"
          ></svg>
          <h3 digEmptyStateTitle>سبد خرید شما خالی است</h3>
          <p digEmptyStateDescription>
            محصولی که به سبد اضافه کنید این‌جا نمایش داده می‌شود.
          </p>
          <div digEmptyStateActions>
            <button digButton type="button" (click)="resetCart()">
              مشاهدهٔ محصولات
            </button>
          </div>
        </div>
      </main>

    } @else {
      <main
        digControlSize="xl"
        class="mx-auto flex w-full max-w-5xl flex-col gap-6 p-4 sm:p-6"
      >
        <header class="flex flex-col gap-1">
          <h1 class="text-2xl font-bold">سبد خرید</h1>
          <p class="text-sm text-muted-foreground">
            {{ toFaDigits(count()) }} کالا در سبد شما
          </p>
        </header>

        <div digControlSize="xl">
          <div
            class="grid grid-cols-1 items-start gap-6 lg:grid-cols-[minmax(0,1fr)_22rem]"
          >
            <section aria-label="کالاهای سبد" class="flex flex-col gap-3">
              <!-- پیشرفت تا ارسالِ رایگان: انگیزهٔ افزودنِ کالا، همان‌جا که کاربر تصمیم می‌گیرد. -->
              <div
                class="flex flex-col gap-2.5 rounded-xl border border-border bg-card p-4"
              >
                <div class="flex items-center gap-2 text-sm">
                  @if (remaining() > 0) {
                    <svg
                      digIcon="truck"
                      aria-hidden="true"
                      class="size-5 text-muted-foreground"
                    ></svg>
                  } @else {
                    <svg
                      digIcon="check-circle"
                      aria-hidden="true"
                      class="size-5 text-success-subtle-foreground"
                    ></svg>
                  }
                  @if (remaining() > 0) {
                    <span
                      ><strong class="font-semibold">{{
                        formatCurrency(remaining())
                      }}</strong>
                      تا ارسالِ رایگان</span
                    >
                  } @else {
                    <span class="font-medium text-success-subtle-foreground"
                      >ارسالِ این سفارش رایگان است</span
                    >
                  }
                </div>
                <div
                  digProgress
                  [value]="math.min(100, (subtotal() / FREE_SHIPPING) * 100)"
                  [color]="remaining() > 0 ? 'primary' : 'success'"
                  size="sm"
                  aria-label="پیشرفت تا ارسال رایگان"
                ></div>
              </div>

              <!-- هر کالا یک ردیفِ فشرده: عکس، نام و گونه، قیمت و شمارنده در همان ردیف؛ تعدادِ ۱ سطل نشان می‌دهد و حذف برگشت‌پذیر است. -->
              <ul
                class="flex flex-col divide-y divide-border rounded-xl border border-border bg-card"
              >
                @for (item of items(); track item.id) {
                  <li class="flex gap-3 p-3 sm:gap-4 sm:p-4">
                    <img
                      [src]="stockImages[item.image].src"
                      alt=""
                      class="size-16 shrink-0 rounded-lg object-cover sm:size-24"
                    />
                    <div class="flex min-w-0 flex-1 flex-col gap-1">
                      <a
                        href="#"
                        class="line-clamp-2 leading-snug font-semibold hover:underline"
                        >{{ item.title }}</a
                      >
                      <span
                        class="flex items-center gap-1.5 text-xs text-muted-foreground"
                      >
                        @if (item.swatch) {
                          <span
                            aria-hidden="true"
                            class="size-3 rounded-full border border-border"
                            [style.background]="item.swatch"
                          ></span>
                        }
                        {{ item.variant }}
                      </span>
                      <div
                        class="mt-auto flex flex-wrap items-end justify-between gap-x-3 gap-y-2 pt-1"
                      >
                        <span class="flex flex-col gap-0.5">
                          <div
                            digProductCardPrice
                            [amount]="
                              formatCurrency(item.price * item.quantity)
                            "
                            [originalAmount]="
                              item.oldPrice
                                ? formatCurrency(item.oldPrice * item.quantity)
                                : undefined
                            "
                            [discount]="
                              item.oldPrice
                                ? \`٪\${toFaDigits(math.round((1 - item.price / item.oldPrice) * 100))}\`
                                : undefined
                            "
                            class="pt-0 [--product-card-price:var(--text-base)]"
                          ></div>
                          @if (item.quantity > 1) {
                            <span
                              class="text-xs text-muted-foreground tabular-nums"
                              >هر عدد {{ formatCurrency(item.price) }}</span
                            >
                          }
                        </span>
                        <div digControlSize="lg" mobileSize="default">
                          <div
                            digAddToCart
                            label="افزودن"
                            [itemName]="item.title"
                            [max]="10"
                            [quantity]="item.quantity"
                            (quantityChange)="onQuantity(item, $event ?? 0)"
                          ></div>
                        </div>
                      </div>
                    </div>
                  </li>
                }
              </ul>
            </section>

            <aside
              aria-labelledby="summary-title"
              class="flex flex-col gap-4 rounded-xl border border-border bg-card p-4 sm:p-5 lg:sticky lg:top-6"
            >
              <h2 id="summary-title" class="text-lg font-semibold">
                خلاصهٔ سفارش
              </h2>
              <!-- کد تخفیف پنهان ولی پیداشدنی؛ فیلد و دکمه‌اش ۴۰ پیکسل، یک پله زیرِ دکمهٔ پرداخت. -->
              @if (couponOpen()) {
                <form
                  digControlSize="lg"
                  class="flex items-end gap-2"
                  (submit)="$event.preventDefault(); applyCoupon($event)"
                >
                  <div digTextField label="کد تخفیف" required class="flex-1">
                    <dig-input
                      ><input
                        digInput
                        name="coupon"
                        dir="ltr"
                        autocomplete="off"
                        autofocus
                    /></dig-input>
                  </div>
                  <button digButton type="submit" variant="outline">
                    اعمال
                  </button>
                </form>
              } @else {
                <button
                  digButton
                  type="button"
                  digControlSize="sm"
                  variant="ghost"
                  class="-ms-3 w-fit text-brand-secondary"
                  (click)="couponOpen.set(true)"
                >
                  کد تخفیف داری؟
                </button>
              }
              <dl class="flex flex-col gap-3 text-sm">
                <div class="flex justify-between gap-2">
                  <dt class="text-muted-foreground">
                    جمع کالاها ({{ toFaDigits(count()) }})
                  </dt>
                  <dd>{{ formatCurrency(subtotal()) }}</dd>
                </div>
                @if (discount() > 0) {
                  <div
                    class="flex justify-between gap-2 text-success-subtle-foreground"
                  >
                    <dt>تخفیف</dt>
                    <dd>
                      <bdi dir="ltr">−{{ formatNumber(discount()) }}</bdi> تومان
                    </dd>
                  </div>
                }
                <div class="flex justify-between gap-2">
                  <dt class="text-muted-foreground">هزینهٔ ارسال</dt>
                  <dd>
                    @if (shipping() === 0) {
                      <span class="font-medium text-success-subtle-foreground"
                        >رایگان</span
                      >
                    } @else {
                      {{ formatCurrency(shipping()) }}
                    }
                  </dd>
                </div>
                <div
                  class="mt-2 flex items-baseline justify-between gap-2 border-t border-border pt-4"
                >
                  <dt class="font-semibold">مبلغ قابل پرداخت</dt>
                  <dd class="text-lg font-bold tabular-nums">
                    {{ formatCurrency(total()) }}
                  </dd>
                </div>
              </dl>
              <!-- در موبایل دکمهٔ پرداخت در نوارِ پایینِ صفحه است (دسترسِ شست)؛ این‌جا فقط از sm به بالا. -->
              <div class="mt-4 hidden flex-col gap-3 sm:flex">
                <button digButton type="button" fullWidth>
                  ادامه و پرداخت
                </button>
                <p
                  class="flex items-center justify-center gap-1.5 text-xs text-muted-foreground"
                >
                  <svg
                    digIcon="shield-check"
                    aria-hidden="true"
                    class="size-4"
                  ></svg>
                  پرداختِ امن از درگاهِ شاپرک
                </p>
              </div>
            </aside>
          </div>
        </div>

        <!-- نوارِ پایینِ موبایل: مبلغِ نهایی و اقدامِ اصلی همیشه در دسترسِ شست. -->
        <dig-bottom-action-bar
          hideFrom="sm"
          class="-mx-4 -mb-4"
          label="مبلغِ قابلِ پرداخت"
          [value]="formatCurrency(total())"
        >
          <button digButton type="button">ادامه و پرداخت</button>
        </dig-bottom-action-bar>
      </main>
    }
  `,
})
export class CartBlock {
  readonly stockImages = stockImages
  readonly FREE_SHIPPING = FREE_SHIPPING
  readonly formatCurrency = formatCurrency
  readonly formatNumber = formatNumber
  readonly toFaDigits = toFaDigits
  readonly math = Math

  readonly items = signal<Item[]>(INITIAL)
  readonly couponOpen = signal(false)
  private readonly couponApplied = signal(false)
  readonly subtotal = computed(() =>
    this.items().reduce((sum, item) => sum + item.price * item.quantity, 0)
  )
  readonly count = computed(() =>
    this.items().reduce((sum, item) => sum + item.quantity, 0)
  )
  readonly shipping = computed(() =>
    this.subtotal() >= FREE_SHIPPING || !this.count() ? 0 : SHIPPING
  )
  readonly remaining = computed(() =>
    Math.max(0, FREE_SHIPPING - this.subtotal())
  )
  readonly discount = computed(() =>
    this.couponApplied() ? Math.round(this.subtotal() * 0.1) : 0
  )
  readonly total = computed(
    () => this.subtotal() - this.discount() + this.shipping()
  )

  onQuantity(item: Item, quantity: number) {
    if (quantity > 0) {
      this.items.update((rows) =>
        rows.map((row) => (row.id === item.id ? { ...row, quantity } : row))
      )
      return
    }
    const index = this.items().findIndex((row) => row.id === item.id)
    if (index < 0) return
    this.items.update((rows) => rows.filter((row) => row.id !== item.id))
    toast(`«${item.title}» از سبد حذف شد.`, {
      action: {
        label: "برگردان",
        onClick: () =>
          this.items.update((rows) =>
            rows.some((row) => row.id === item.id)
              ? rows
              : [
                  ...rows.slice(0, index),
                  { ...item, quantity: 1 },
                  ...rows.slice(index),
                ]
          ),
      },
    })
  }

  applyCoupon(event: Event) {
    const form = event.currentTarget as HTMLFormElement
    if (!form.reportValidity()) return
    this.couponApplied.set(true)
    toast.success("کد تخفیف اعمال شد.")
  }

  resetCart() {
    this.items.set(INITIAL)
    this.couponApplied.set(false)
    this.couponOpen.set(false)
  }
}

کدِ این بلاک هنوز برای Android نوشته نشده است.

همهٔ کامپوننت‌های لازم برای Android آماده‌اند؛ با MCP می‌توانید همین صفحه را از روی نسخهٔ React بسازید.

کدِ React همین بلاک مرجع است. با MCP دیگ و get_block با framework="android" ایجنت آن را با کامپوننت‌های Android می‌سازد.