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

هر ستون یک کالا با عکس، قیمت، امتیاز و افزودن به سبد؛ ردیف‌های مشخصات با تیک و خط؛ «فقط تفاوت‌ها» با سوییچ و حذفِ هر ستون تا حداقل دو کالا.

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

این بلاک از ۱۰ آیتم دیگ استفاده می‌کند: add-to-cartbadgebuttoncontrol-sizeiconslabelpersianratingswitchtable

پکیج‌منیجر پیش‌فرض Node.jsنصب سریع با لینک سخت و دیسک مشترکYarn نسخهٔ ۲ به بالا (Berry)رانتایم و پکیج‌منیجر Bun
npx digdesign@latest add https://docs.digdesign.ir/r/add-to-cart.json https://docs.digdesign.ir/r/badge.json https://docs.digdesign.ir/r/button.json https://docs.digdesign.ir/r/control-size.json https://docs.digdesign.ir/r/icons.json https://docs.digdesign.ir/r/label.json https://docs.digdesign.ir/r/persian.json https://docs.digdesign.ir/r/rating.json https://docs.digdesign.ir/r/switch.json https://docs.digdesign.ir/r/table.json
pnpm dlx digdesign@latest add https://docs.digdesign.ir/r/add-to-cart.json https://docs.digdesign.ir/r/badge.json https://docs.digdesign.ir/r/button.json https://docs.digdesign.ir/r/control-size.json https://docs.digdesign.ir/r/icons.json https://docs.digdesign.ir/r/label.json https://docs.digdesign.ir/r/persian.json https://docs.digdesign.ir/r/rating.json https://docs.digdesign.ir/r/switch.json https://docs.digdesign.ir/r/table.json
yarn dlx digdesign@latest add https://docs.digdesign.ir/r/add-to-cart.json https://docs.digdesign.ir/r/badge.json https://docs.digdesign.ir/r/button.json https://docs.digdesign.ir/r/control-size.json https://docs.digdesign.ir/r/icons.json https://docs.digdesign.ir/r/label.json https://docs.digdesign.ir/r/persian.json https://docs.digdesign.ir/r/rating.json https://docs.digdesign.ir/r/switch.json https://docs.digdesign.ir/r/table.json
bunx --bun digdesign@latest add https://docs.digdesign.ir/r/add-to-cart.json https://docs.digdesign.ir/r/badge.json https://docs.digdesign.ir/r/button.json https://docs.digdesign.ir/r/control-size.json https://docs.digdesign.ir/r/icons.json https://docs.digdesign.ir/r/label.json https://docs.digdesign.ir/r/persian.json https://docs.digdesign.ir/r/rating.json https://docs.digdesign.ir/r/switch.json https://docs.digdesign.ir/r/table.json
پکیج‌منیجر پیش‌فرض Node.jsنصب سریع با لینک سخت و دیسک مشترکYarn نسخهٔ ۲ به بالا (Berry)رانتایم و پکیج‌منیجر Bun
npx digdesign@latest add add-to-cart badge button control-size label persian rating switch table --framework vue
pnpm dlx digdesign@latest add add-to-cart badge button control-size label persian rating switch table --framework vue
yarn dlx digdesign@latest add add-to-cart badge button control-size label persian rating switch table --framework vue
bunx --bun digdesign@latest add add-to-cart badge button control-size label persian rating switch table --framework vue
پکیج‌منیجر پیش‌فرض Node.jsنصب سریع با لینک سخت و دیسک مشترکYarn نسخهٔ ۲ به بالا (Berry)رانتایم و پکیج‌منیجر Bun
npx digdesign@latest add button --framework svelte
pnpm dlx digdesign@latest add button --framework svelte
yarn dlx digdesign@latest add button --framework svelte
bunx --bun digdesign@latest add button --framework svelte
پکیج‌منیجر پیش‌فرض Node.jsنصب سریع با لینک سخت و دیسک مشترکYarn نسخهٔ ۲ به بالا (Berry)رانتایم و پکیج‌منیجر Bun
npx digdesign@latest add add-to-cart badge button control-size icons label persian rating switch table --framework angular
pnpm dlx digdesign@latest add add-to-cart badge button control-size icons label persian rating switch table --framework angular
yarn dlx digdesign@latest add add-to-cart badge button control-size icons label persian rating switch table --framework angular
bunx --bun digdesign@latest add add-to-cart badge button control-size icons label persian rating switch table --framework angular
پکیج‌منیجر پیش‌فرض Node.jsنصب سریع با لینک سخت و دیسک مشترکYarn نسخهٔ ۲ به بالا (Berry)رانتایم و پکیج‌منیجر Bun
npx digdesign@latest add add-to-cart badge button control-size label persian rating switch table --framework android
pnpm dlx digdesign@latest add add-to-cart badge button control-size label persian rating switch table --framework android
yarn dlx digdesign@latest add add-to-cart badge button control-size label persian rating switch table --framework android
bunx --bun digdesign@latest add add-to-cart badge button control-size label persian rating switch table --framework android

۲. کد صفحه

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

product-compare.tsx
"use client";

import * as React from "react";

import { CheckIcon, MinusIcon, XIcon } from "@/components/icons";
import { Badge } from "@/components/ui/badge";
import { AddToCart } from "@/components/ui/add-to-cart";
import { Button } from "@/components/ui/button";
import { ControlSizeProvider } from "@/components/ui/control-size";
import { Label } from "@/components/ui/label";
import { Rating } from "@/components/ui/rating";
import { Switch } from "@/components/ui/switch";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import { formatCurrency, toFaDigits } from "@/lib/persian";
import { stockImages, type StockImageName } from "@/lib/stock-images";

type Value = string | boolean;

const PRODUCTS: {
  id: string;
  title: string;
  image: StockImageName;
  price: number;
  rating: number;
  best?: boolean;
  specs: Record<string, Value>;
}[] = [
  {
    id: "a",
    title: "هدفونِ بی‌سیمِ دیگ پرو",
    image: "headphonesLight",
    price: 3_490_000,
    rating: 4.8,
    best: true,
    specs: {
      "حذفِ نویزِ فعال": true,
      شارژدهی: "۳۰ ساعت",
      بلوتوث: "۵٫۳",
      وزن: "۲۵۰ گرم",
      "میکروفونِ تماس": true,
      گارانتی: "۱۸ ماه",
    },
  },
  {
    id: "b",
    title: "هدفونِ نوا لایت",
    image: "headphonesBeige",
    price: 2_190_000,
    rating: 4.3,
    specs: {
      "حذفِ نویزِ فعال": true,
      شارژدهی: "۲۴ ساعت",
      بلوتوث: "۵٫۲",
      وزن: "۲۲۰ گرم",
      "میکروفونِ تماس": true,
      گارانتی: "۱۲ ماه",
    },
  },
  {
    id: "c",
    title: "هدفونِ سیم‌دارِ استودیو",
    image: "headphonesWhite",
    price: 940_000,
    rating: 4.1,
    specs: {
      "حذفِ نویزِ فعال": false,
      شارژدهی: "بی‌نیاز به شارژ",
      بلوتوث: false,
      وزن: "۱۸۰ گرم",
      "میکروفونِ تماس": false,
      گارانتی: "۶ ماه",
    },
  },
];

function Mark({ value }: { value: Value }) {
  if (typeof value === "string")
    return <span className="text-sm">{value}</span>;
  return value ? (
    <CheckIcon aria-label="دارد" className="mx-auto size-5 text-success-subtle-foreground" />
  ) : (
    <MinusIcon
      aria-label="ندارد"
      className="mx-auto size-5 text-muted-foreground"
    />
  );
}

/**
 * مقایسهٔ محصول: هر ستون یک کالا با عکس، قیمت، امتیاز و افزودن به سبد؛ ردیف‌های
 * مشخصات با تیک و خط؛ «فقط تفاوت‌ها» ردیف‌های یکسان را پنهان می‌کند و حذفِ هر ستون.
 */
export default function ProductCompareBlock() {
  const [products, setProducts] = React.useState(PRODUCTS);
  const [diffOnly, setDiffOnly] = React.useState(false);
  const specs = Object.keys(PRODUCTS[0].specs).filter(
    (spec) =>
      !diffOnly ||
      new Set(products.map((product) => String(product.specs[spec]))).size > 1,
  );

  return (
    <ControlSizeProvider size="xl" asChild>
      <main className="mx-auto flex w-full max-w-6xl flex-col gap-8 p-4 sm:p-6">
        <header className="flex flex-wrap items-end justify-between gap-x-4 gap-y-8">
          <div className="flex flex-col gap-1">
            <h1 className="text-2xl font-bold">مقایسهٔ کالاها</h1>
            <p className="text-muted-foreground">
              {toFaDigits(products.length)} کالا کنارِ هم
            </p>
          </div>
          <div className="flex items-center gap-3">
            <Label htmlFor="compare-diff">فقط تفاوت‌ها</Label>
            <Switch
              id="compare-diff"
              checked={diffOnly}
              onCheckedChange={setDiffOnly}
            />
          </div>
        </header>

        <div className="overflow-x-auto">
          {/* در موبایل ستونِ نامِ مشخصه حذف و نامش ردیفی بالای مقدارها می‌شود؛ دو کالا کنارِ هم دیده می‌شوند. */}
          {/* ستون‌ها پهنای ثابت دارند؛ با حذفِ یک کالا بقیه کش نمی‌آیند و جدول فقط باریک‌تر می‌شود. */}
          <Table layout="fixed" className="w-max">
            <TableHeader>
              <TableRow className="hover:bg-card">
                <TableHead className="hidden w-40 sm:table-cell">
                  <span className="sr-only">مشخصه</span>
                </TableHead>
                {products.map((product) => (
                  <TableHead key={product.id} className="h-auto w-40 border-s border-border py-4 align-top sm:w-52">
                    {/* هر ستون همان ردیف‌ها را با ارتفاعِ ثابت دارد (عکس، عنوانِ دوخطی، جای برچسب، امتیاز، قیمت، دکمه) تا داده‌ها در همهٔ ستون‌ها هم‌تراز بمانند. */}
                    <div className="relative flex flex-col gap-2">
                      <ControlSizeProvider size="default" mobileSize="sm" asChild>
                        <Button
                          variant="outline"
                          size="icon"
                          aria-label={`حذفِ ${product.title} از مقایسه`}
                          onClick={() => setProducts((list) => list.filter((item) => item.id !== product.id))}
                          disabled={products.length <= 2}
                          className="absolute end-2 top-2 z-10 bg-background"
                        >
                          <XIcon />
                        </Button>
                      </ControlSizeProvider>
                      {/* eslint-disable-next-line @next/next/no-img-element -- عکس از کتابخانهٔ تصویرِ دیگ */}
                      <img src={stockImages[product.image].src} alt={stockImages[product.image].alt} className="aspect-square w-full rounded-lg bg-muted object-cover" />
                      <span className="mt-1 line-clamp-2 min-h-[2lh] text-sm leading-snug font-bold text-foreground">{product.title}</span>
                      <span className="flex h-6 items-center">{product.best && <Badge color="violet">پیشنهادِ ما</Badge>}</span>
                      <Rating readOnly defaultValue={product.rating} size="sm" aria-label={`امتیازِ ${toFaDigits(product.rating)}`} />
                      <span className="text-base font-bold text-foreground tabular-nums sm:text-lg">{formatCurrency(product.price)}</span>
                      <ControlSizeProvider size="lg" mobileSize="default" asChild>
                        <AddToCart fullWidth label="افزودن" variant={product.best ? "default" : "outline"} itemName={product.title} />
                      </ControlSizeProvider>
                    </div>
                  </TableHead>
                ))}
              </TableRow>
            </TableHeader>
            <TableBody>
              {specs.map((spec) => (
                <React.Fragment key={spec}>
                  <TableRow className="hover:bg-card sm:hidden">
                    <TableCell
                      colSpan={products.length}
                      className="bg-subtle py-2 text-xs font-medium text-muted-foreground"
                    >
                      {spec}
                    </TableCell>
                  </TableRow>
                  <TableRow>
                    <TableCell className="hidden text-sm text-muted-foreground sm:table-cell">
                      {spec}
                    </TableCell>
                    {products.map((product) => (
                      <TableCell key={product.id} className="w-40 border-s border-border text-center sm:w-52">
                        <Mark value={product.specs[spec]} />
                      </TableCell>
                    ))}
                  </TableRow>
                </React.Fragment>
              ))}
              {specs.length === 0 && (
                <TableRow>
                  <TableCell
                    colSpan={products.length + 1}
                    className="py-8 text-center text-muted-foreground"
                  >
                    این کالاها در همهٔ مشخصات یکسان‌اند.
                  </TableCell>
                </TableRow>
              )}
            </TableBody>
          </Table>
        </div>
      </main>
    </ControlSizeProvider>
  );
}
product-compare.vue
<script setup lang="ts">
import { computed, ref } from "vue"

import AddToCart from "@/components/ui/AddToCart.vue"
import Badge from "@/components/ui/Badge.vue"
import Button from "@/components/ui/Button.vue"
import ControlSize from "@/components/ui/ControlSize.vue"
import Label from "@/components/ui/Label.vue"
import Rating from "@/components/ui/Rating.vue"
import Switch from "@/components/ui/Switch.vue"
import Table from "@/components/ui/Table.vue"
import TableBody from "@/components/ui/TableBody.vue"
import TableCell from "@/components/ui/TableCell.vue"
import TableHead from "@/components/ui/TableHead.vue"
import TableHeader from "@/components/ui/TableHeader.vue"
import TableRow from "@/components/ui/TableRow.vue"
import { DigIcon } from "@/lib/icons"
import { formatCurrency, toFaDigits } from "@/lib/persian"

// تصویرهای کتابخانهٔ تصویرِ دیگ (همان فایل‌های stock-images نسخهٔ React).
const stockImages = {
  headphonesLight: { src: "https://docs.digdesign.ir/images/headphones-light.webp", alt: "هدفونِ بی‌سیمِ مشکی روی میزِ سفید" },
  headphonesBeige: { src: "https://docs.digdesign.ir/images/headphones-beige.webp", alt: "هدفونِ کرم و طوسی روی زمینهٔ سفید" },
  headphonesWhite: { src: "https://docs.digdesign.ir/images/headphones-white.webp", alt: "هدفونِ سفیدِ سیم‌دار روی زمینهٔ یاسی" },
}
type StockImageName = keyof typeof stockImages

type Value = string | boolean

const PRODUCTS: { id: string; title: string; image: StockImageName; price: number; rating: number; best?: boolean; specs: Record<string, Value> }[] = [
  {
    id: "a",
    title: "هدفونِ بی‌سیمِ دیگ پرو",
    image: "headphonesLight",
    price: 3_490_000,
    rating: 4.8,
    best: true,
    specs: { "حذفِ نویزِ فعال": true, شارژدهی: "۳۰ ساعت", بلوتوث: "۵٫۳", وزن: "۲۵۰ گرم", "میکروفونِ تماس": true, گارانتی: "۱۸ ماه" },
  },
  {
    id: "b",
    title: "هدفونِ نوا لایت",
    image: "headphonesBeige",
    price: 2_190_000,
    rating: 4.3,
    specs: { "حذفِ نویزِ فعال": true, شارژدهی: "۲۴ ساعت", بلوتوث: "۵٫۲", وزن: "۲۲۰ گرم", "میکروفونِ تماس": true, گارانتی: "۱۲ ماه" },
  },
  {
    id: "c",
    title: "هدفونِ سیم‌دارِ استودیو",
    image: "headphonesWhite",
    price: 940_000,
    rating: 4.1,
    specs: { "حذفِ نویزِ فعال": false, شارژدهی: "بی‌نیاز به شارژ", بلوتوث: false, وزن: "۱۸۰ گرم", "میکروفونِ تماس": false, گارانتی: "۶ ماه" },
  },
]

/**
 * مقایسهٔ محصول: هر ستون یک کالا با عکس، قیمت، امتیاز و افزودن به سبد؛ ردیف‌های
 * مشخصات با تیک و خط؛ «فقط تفاوت‌ها» ردیف‌های یکسان را پنهان می‌کند و حذفِ هر ستون.
 */
const products = ref(PRODUCTS)
const diffOnly = ref(false)
const specs = computed(() =>
  Object.keys(PRODUCTS[0].specs).filter((spec) => !diffOnly.value || new Set(products.value.map((product) => String(product.specs[spec]))).size > 1)
)
</script>

<template>
  <ControlSize size="xl" as-child>
    <main class="mx-auto flex w-full max-w-6xl flex-col gap-8 p-4 sm:p-6">
      <header class="flex flex-wrap items-end justify-between gap-x-4 gap-y-8">
        <div class="flex flex-col gap-1">
          <h1 class="text-2xl font-bold">مقایسهٔ کالاها</h1>
          <p class="text-muted-foreground">{{ toFaDigits(products.length) }} کالا کنارِ هم</p>
        </div>
        <div class="flex items-center gap-3">
          <Label for="compare-diff">فقط تفاوت‌ها</Label>
          <Switch id="compare-diff" v-model:checked="diffOnly" />
        </div>
      </header>

      <div class="overflow-x-auto">
        <!-- در موبایل ستونِ نامِ مشخصه حذف و نامش ردیفی بالای مقدارها می‌شود؛ دو کالا کنارِ هم دیده می‌شوند. -->
        <!-- ستون‌ها پهنای ثابت دارند؛ با حذفِ یک کالا بقیه کش نمی‌آیند و جدول فقط باریک‌تر می‌شود. -->
        <Table layout="fixed" class="w-max">
          <TableHeader>
            <TableRow class="hover:bg-card">
              <TableHead class="hidden w-40 sm:table-cell">
                <span class="sr-only">مشخصه</span>
              </TableHead>
              <TableHead v-for="product in products" :key="product.id" class="h-auto w-40 border-s border-border py-4 align-top sm:w-52">
                <!-- هر ستون همان ردیف‌ها را با ارتفاعِ ثابت دارد (عکس، عنوانِ دوخطی، جای برچسب، امتیاز، قیمت، دکمه) تا داده‌ها در همهٔ ستون‌ها هم‌تراز بمانند. -->
                <div class="relative flex flex-col gap-2">
                  <ControlSize size="default" mobile-size="sm" as-child>
                    <Button
                      variant="outline"
                      size="icon"
                      :aria-label="`حذفِ ${product.title} از مقایسه`"
                      :disabled="products.length <= 2"
                      class="absolute end-2 top-2 z-10 bg-background"
                      @click="products = products.filter((item) => item.id !== product.id)"
                    >
                      <DigIcon name="x" />
                    </Button>
                  </ControlSize>
                  <img :src="stockImages[product.image].src" :alt="stockImages[product.image].alt" class="aspect-square w-full rounded-lg bg-muted object-cover" />
                  <span class="mt-1 line-clamp-2 min-h-[2lh] text-sm leading-snug font-bold text-foreground">{{ product.title }}</span>
                  <span class="flex h-6 items-center"><Badge v-if="product.best" color="violet">پیشنهادِ ما</Badge></span>
                  <Rating read-only :default-value="product.rating" size="sm" :aria-label="`امتیازِ ${toFaDigits(product.rating)}`" />
                  <span class="text-base font-bold text-foreground tabular-nums sm:text-lg">{{ formatCurrency(product.price) }}</span>
                  <ControlSize size="lg" mobile-size="default" as-child>
                    <AddToCart full-width label="افزودن" :variant="product.best ? 'default' : 'outline'" :item-name="product.title" />
                  </ControlSize>
                </div>
              </TableHead>
            </TableRow>
          </TableHeader>
          <TableBody>
            <template v-for="spec in specs" :key="spec">
              <TableRow class="hover:bg-card sm:hidden">
                <TableCell :colspan="products.length" class="bg-subtle py-2 text-xs font-medium text-muted-foreground">{{ spec }}</TableCell>
              </TableRow>
              <TableRow>
                <TableCell class="hidden text-sm text-muted-foreground sm:table-cell">{{ spec }}</TableCell>
                <TableCell v-for="product in products" :key="product.id" class="w-40 border-s border-border text-center sm:w-52">
                  <span v-if="typeof product.specs[spec] === 'string'" class="text-sm">{{ product.specs[spec] }}</span>
                  <DigIcon v-else-if="product.specs[spec]" name="check" aria-label="دارد" class="mx-auto size-5 text-success-subtle-foreground" />
                  <DigIcon v-else name="minus" aria-label="ندارد" class="mx-auto size-5 text-muted-foreground" />
                </TableCell>
              </TableRow>
            </template>
            <TableRow v-if="specs.length === 0">
              <TableCell :colspan="products.length + 1" class="py-8 text-center text-muted-foreground">این کالاها در همهٔ مشخصات یکسان‌اند.</TableCell>
            </TableRow>
          </TableBody>
        </Table>
      </div>
    </main>
  </ControlSize>
</template>

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

این موارد هنوز برای Svelte آماده نیستند: add-to-cartbadgecontrol-sizelabelpersianratingswitchtableآیکون checkآیکون minusآیکون x

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

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

import { DigIcon } from "@/lib/icons"
import { formatCurrency, toFaDigits } from "@/lib/persian"
import { DigAddToCart } from "@/components/ui/add-to-cart"
import { DigBadge } from "@/components/ui/badge"
import { DigButton } from "@/components/ui/button"
import { DigControlSize } from "@/components/ui/control-size"
import { DigLabel } from "@/components/ui/label"
import { DigRating } from "@/components/ui/rating"
import { DigSwitch } from "@/components/ui/switch"
import {
  DigTable,
  DigTableBody,
  DigTableCell,
  DigTableHead,
  DigTableHeader,
  DigTableRow,
} from "@/components/ui/table"

const stockImages = {
  headphonesLight: {
    src: "https://docs.digdesign.ir/images/headphones-light.webp",
    alt: "هدفونِ بی‌سیمِ مشکی روی میزِ سفید",
  },
  headphonesBeige: {
    src: "https://docs.digdesign.ir/images/headphones-beige.webp",
    alt: "هدفونِ کرم و طوسی روی زمینهٔ سفید",
  },
  headphonesWhite: {
    src: "https://docs.digdesign.ir/images/headphones-white.webp",
    alt: "هدفونِ سفیدِ سیم‌دار روی زمینهٔ یاسی",
  },
}
type StockImageName = keyof typeof stockImages

type Value = string | boolean

const PRODUCTS: {
  id: string
  title: string
  image: StockImageName
  price: number
  rating: number
  best?: boolean
  specs: Record<string, Value>
}[] = [
  {
    id: "a",
    title: "هدفونِ بی‌سیمِ دیگ پرو",
    image: "headphonesLight",
    price: 3_490_000,
    rating: 4.8,
    best: true,
    specs: {
      "حذفِ نویزِ فعال": true,
      شارژدهی: "۳۰ ساعت",
      بلوتوث: "۵٫۳",
      وزن: "۲۵۰ گرم",
      "میکروفونِ تماس": true,
      گارانتی: "۱۸ ماه",
    },
  },
  {
    id: "b",
    title: "هدفونِ نوا لایت",
    image: "headphonesBeige",
    price: 2_190_000,
    rating: 4.3,
    specs: {
      "حذفِ نویزِ فعال": true,
      شارژدهی: "۲۴ ساعت",
      بلوتوث: "۵٫۲",
      وزن: "۲۲۰ گرم",
      "میکروفونِ تماس": true,
      گارانتی: "۱۲ ماه",
    },
  },
  {
    id: "c",
    title: "هدفونِ سیم‌دارِ استودیو",
    image: "headphonesWhite",
    price: 940_000,
    rating: 4.1,
    specs: {
      "حذفِ نویزِ فعال": false,
      شارژدهی: "بی‌نیاز به شارژ",
      بلوتوث: false,
      وزن: "۱۸۰ گرم",
      "میکروفونِ تماس": false,
      گارانتی: "۶ ماه",
    },
  },
]

@Component({
  selector: "dig-block-product-compare",
  imports: [
    DigAddToCart,
    DigBadge,
    DigButton,
    DigControlSize,
    DigIcon,
    DigLabel,
    DigRating,
    DigSwitch,
    DigTable,
    DigTableBody,
    DigTableCell,
    DigTableHead,
    DigTableHeader,
    DigTableRow,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <main
      digControlSize="xl"
      class="mx-auto flex w-full max-w-6xl flex-col gap-8 p-4 sm:p-6"
    >
      <header class="flex flex-wrap items-end justify-between gap-x-4 gap-y-8">
        <div class="flex flex-col gap-1">
          <h1 class="text-2xl font-bold">مقایسهٔ کالاها</h1>
          <p class="text-muted-foreground">
            {{ toFaDigits(products().length) }} کالا کنارِ هم
          </p>
        </div>
        <div class="flex items-center gap-3">
          <label digLabel for="compare-diff">فقط تفاوت‌ها</label>
          <button
            digSwitch
            type="button"
            id="compare-diff"
            [checked]="diffOnly()"
            (checkedChange)="diffOnly.set($event)"
          ></button>
        </div>
      </header>

      <div class="overflow-x-auto">
        <!-- در موبایل ستونِ نامِ مشخصه حذف و نامش ردیفی بالای مقدارها می‌شود؛ دو کالا کنارِ هم دیده می‌شوند. -->
        <!-- ستون‌ها پهنای ثابت دارند؛ با حذفِ یک کالا بقیه کش نمی‌آیند و جدول فقط باریک‌تر می‌شود. -->
        <dig-table layout="fixed" class="w-max">
          <thead digTableHeader>
            <tr digTableRow class="hover:bg-card">
              <th digTableHead class="hidden w-40 sm:table-cell">
                <span class="sr-only">مشخصه</span>
              </th>
              @for (product of products(); track product.id) {
                <th
                  digTableHead
                  class="h-auto w-40 border-s border-border py-4 align-top sm:w-52"
                >
                  <!-- هر ستون همان ردیف‌ها را با ارتفاعِ ثابت دارد (عکس، عنوانِ دوخطی، جای برچسب، امتیاز، قیمت، دکمه) تا داده‌ها در همهٔ ستون‌ها هم‌تراز بمانند. -->
                  <div class="relative flex flex-col gap-2">
                    <button
                      digButton
                      type="button"
                      mobileSize="sm"
                      digControlSize="default"
                      variant="outline"
                      size="icon"
                      [attr.aria-label]="'حذفِ ' + product.title + ' از مقایسه'"
                      [disabled]="products().length <= 2"
                      class="absolute end-2 top-2 z-10 bg-background"
                      (click)="removeProduct(product.id)"
                    >
                      <svg digIcon="x"></svg>
                    </button>
                    <img
                      [src]="stockImages[product.image].src"
                      [alt]="stockImages[product.image].alt"
                      class="aspect-square w-full rounded-lg bg-muted object-cover"
                    />
                    <span
                      class="mt-1 line-clamp-2 min-h-[2lh] text-sm leading-snug font-bold text-foreground"
                      >{{ product.title }}</span
                    >
                    <span class="flex h-6 items-center">
                      @if (product.best) {
                        <span digBadge color="violet">پیشنهادِ ما</span>
                      }
                    </span>
                    <div
                      digRating
                      readOnly
                      [value]="product.rating"
                      size="sm"
                      [attr.aria-label]="
                        'امتیازِ ' + toFaDigits(product.rating)
                      "
                    ></div>
                    <span
                      class="text-base font-bold text-foreground tabular-nums sm:text-lg"
                      >{{ formatCurrency(product.price) }}</span
                    >
                    <div digControlSize="lg" mobileSize="default">
                      <div
                        digAddToCart
                        fullWidth
                        label="افزودن"
                        [variant]="product.best ? 'default' : 'outline'"
                        [itemName]="product.title"
                      ></div>
                    </div>
                  </div>
                </th>
              }
            </tr>
          </thead>
          <tbody digTableBody>
            @for (spec of specs(); track spec) {
              <tr digTableRow class="hover:bg-card sm:hidden">
                <td
                  digTableCell
                  [colSpan]="products().length"
                  class="bg-subtle py-2 text-xs font-medium text-muted-foreground"
                >
                  {{ spec }}
                </td>
              </tr>
              <tr digTableRow>
                <td
                  digTableCell
                  class="hidden text-sm text-muted-foreground sm:table-cell"
                >
                  {{ spec }}
                </td>
                @for (product of products(); track product.id) {
                  <td
                    digTableCell
                    class="w-40 border-s border-border text-center sm:w-52"
                  >
                    @if (typeof product.specs[spec] === "string") {
                      <span class="text-sm">{{ product.specs[spec] }}</span>
                    } @else if (product.specs[spec]) {
                      <svg
                        digIcon="check"
                        aria-label="دارد"
                        class="mx-auto size-5 text-success-subtle-foreground"
                      ></svg>
                    } @else {
                      <svg
                        digIcon="minus"
                        aria-label="ندارد"
                        class="mx-auto size-5 text-muted-foreground"
                      ></svg>
                    }
                  </td>
                }
              </tr>
            }
            @if (specs().length === 0) {
              <tr digTableRow>
                <td
                  digTableCell
                  [colSpan]="products().length + 1"
                  class="py-8 text-center text-muted-foreground"
                >
                  این کالاها در همهٔ مشخصات یکسان‌اند.
                </td>
              </tr>
            }
          </tbody>
        </dig-table>
      </div>
    </main>
  `,
})
export class ProductCompareBlock {
  readonly stockImages = stockImages
  readonly formatCurrency = formatCurrency
  readonly toFaDigits = toFaDigits

  readonly products = signal(PRODUCTS)
  readonly diffOnly = signal(false)
  readonly specs = computed(() =>
    Object.keys(PRODUCTS[0].specs).filter(
      (spec) =>
        !this.diffOnly() ||
        new Set(this.products().map((product) => String(product.specs[spec])))
          .size > 1
    )
  )

  removeProduct(id: string) {
    this.products.update((products) =>
      products.length > 2 ? products.filter((item) => item.id !== id) : products
    )
  }
}

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

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

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