On this page

chatty-product-card

API chuẩn của element chatty-product-card, sinh từ custom-elements.json của prototype trong guide.

Copy page as MarkdownRead Markdown ↗

Key rules

  • chatty-product-card khai báo 6 attribute, 0 property không có attribute, 1 event, 7 slot, 5 part và 3 CSS custom property.
  • Import 1 module @avada/chatty-ui/elements/product-card.js là define chatty-product-card; chunk chỉ import element nó dùng. Prototype chuẩn: trong guide.
  • Đổi tên attribute, event, slot, part hay CSS custom property là đổi API: diff của custom-elements.json phải có trong 1 MR.
On this page

Overview

A product in the conversation: the image, name, rating and price open the product; the action and footer sit below that open area. Class ChattyProductCard.

Attributes

Name Property Type Default Description
price price string '' Formatted price, e.g. $24.
compare-at-price compareAtPrice string '' Formatted compare-at price; shown struck through before the price.
label label string '' Accessible name of the open area; defaults to its text.
layout layout 'inset' | 'flush' 'inset' inset (AI Mode): padded card with a rounded image. flush (Classic): the image runs edge to edge under the card's own corners and the text sits in a padded block.
selected selected boolean false Border cue for a chosen product; pair it with a pill in the overlay slot.
skeleton skeleton boolean false Placeholder in the same box as a loaded card; hidden from assistive technology.

Properties

Attributes also expose the properties listed in the Property column above.

Not declared in the manifest.

Events

Name Detail type Description
chatty-open CustomEvent<{}> Click, Enter or Space on the open area.

Slots

Name Description
default Product name; clamped to two lines.
media Product image, usually chatty-image with ratio 4 / 5 and fit contain.
overlay Content laid over the image, such as a selected pill.
rating Star rating, usually a read-only chatty-rating.
badge Sale cue beside the price, usually chatty-badge variant="secondary" size="sm".
action Action under the open area, usually an add-to-cart chatty-button.
footer Last row, for an add-to-cart error.

Parts

Name Description
base Card container.
open Area that opens the product (role button).
media Image wrapper.
name Name wrapper.
price Price row.

CSS custom properties

Name Default Description
--chatty-product-card-radius – Card corner radius.
--chatty-product-card-background – Card background.
--chatty-product-card-media-radius – Image corner radius.

Usage

import '@avada/chatty-ui/tokens/tokens.css';
import '@avada/chatty-ui/elements/product-card.js';
import '@avada/chatty-ui/elements/image.js';
import '@avada/chatty-ui/elements/rating.js';
import '@avada/chatty-ui/elements/badge.js';
import '@avada/chatty-ui/elements/button.js';
<chatty-product-card
  style="align-self: stretch"
  price="$24"
  compare-at-price="$32"
  label="View product Canvas tote bag"
>
  <chatty-image
    slot="media"
    src="/fixtures/tote.svg"
    alt=""
    ratio="4 / 5"
    fit="contain"
  ></chatty-image>
  Canvas tote bag
  <span slot="rating" style="display: inline-flex; align-items: center; gap: 4px">
    <chatty-rating
      readonly
      value="4.6"
      label="Rated 4.6 out of 5"
      style="--chatty-rating-size: 16px"
    ></chatty-rating>
    <b>4.6</b> (128)
  </span>
  <chatty-badge slot="badge" variant="secondary" size="sm">−25%</chatty-badge>
  <chatty-button slot="action" size="sm">Add to cart</chatty-button>
</chatty-product-card>
<chatty-product-card
  style="align-self: stretch"
  price="$18"
  selected
  label="View product Ceramic mug"
>
  <chatty-image
    slot="media"
    src="/fixtures/mug.svg"
    alt=""
    ratio="4 / 5"
    fit="contain"
  ></chatty-image>
  <chatty-badge slot="overlay" variant="primary">
    <svg
      width="14"
      height="14"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2.5"
      aria-hidden="true"
      style="vertical-align: -2px"
    >
      <path d="m5 12 5 5 9-10" />
    </svg>
    Selected
  </chatty-badge>
  Ceramic mug
  <chatty-button slot="action" size="sm">Add to cart</chatty-button>
</chatty-product-card>
<chatty-product-card style="align-self: stretch" price="$28" label="View product Wool cap">
  <chatty-image
    slot="media"
    src="/fixtures/cap.svg"
    alt=""
    ratio="4 / 5"
    fit="contain"
  ></chatty-image>
  Wool cap
  <chatty-button slot="action" size="sm" disabled>Sold out</chatty-button>
</chatty-product-card>

Live preview

Component thật dùng cùng story với trang component; dữ liệu mẫu nằm trong frame cục bộ.

Standard
LightDarkDesktopMobile

Default: trạng thái mặc định và nội dung mẫu của element.

Show code
chatty-product-card.element.jsWrap code linesCopy
<chatty-product-card
  style="align-self: stretch"
  price="$24"
  compare-at-price="$32"
  label="View product Canvas tote bag"
>
  <chatty-image
    slot="media"
    src="/fixtures/tote.svg"
    alt=""
    ratio="4 / 5"
    fit="contain"
  ></chatty-image>
  Canvas tote bag
  <span slot="rating" style="display: inline-flex; align-items: center; gap: 4px">
    <chatty-rating
      readonly
      value="4.6"
      label="Rated 4.6 out of 5"
      style="--chatty-rating-size: 16px"
    ></chatty-rating>
    <b>4.6</b> (128)
  </span>
  <chatty-badge slot="badge" variant="secondary" size="sm">−25%</chatty-badge>
  <chatty-button slot="action" size="sm">Add to cart</chatty-button>
</chatty-product-card>
<chatty-product-card
  style="align-self: stretch"
  price="$18"
  selected
  label="View product Ceramic mug"
>
  <chatty-image
    slot="media"
    src="/fixtures/mug.svg"
    alt=""
    ratio="4 / 5"
    fit="contain"
  ></chatty-image>
  <chatty-badge slot="overlay" variant="primary">
    <svg
      width="14"
      height="14"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2.5"
      aria-hidden="true"
      style="vertical-align: -2px"
    >
      <path d="m5 12 5 5 9-10" />
    </svg>
    Selected
  </chatty-badge>
  Ceramic mug
  <chatty-button slot="action" size="sm">Add to cart</chatty-button>
</chatty-product-card>
<chatty-product-card style="align-self: stretch" price="$28" label="View product Wool cap">
  <chatty-image
    slot="media"
    src="/fixtures/cap.svg"
    alt=""
    ratio="4 / 5"
    fit="contain"
  ></chatty-image>
  Wool cap
  <chatty-button slot="action" size="sm" disabled>Sold out</chatty-button>
</chatty-product-card>
Standard
LightDarkDesktopMobile

Các variant hoặc state chính đặt cạnh nhau trong cùng ví dụ.

Show code
chatty-product-card.element.jsWrap code linesCopy
<chatty-product-card
  style="align-self: stretch"
  price="$24"
  label="View product Canvas tote bag"
>
  <chatty-image
    slot="media"
    src="/fixtures/tote.svg"
    alt=""
    ratio="4 / 5"
    fit="contain"
  ></chatty-image>
  Canvas tote bag
  <chatty-button slot="action" size="sm" loading>Adding</chatty-button>
  <span slot="footer" role="alert" style="color: var(--chatty-color-critical); font-size: 14px"
    >Could not add to cart</span
  >
</chatty-product-card>
<chatty-product-card style="align-self: stretch" skeleton></chatty-product-card>

Anatomy

Số trong hình khớp bảng bên dưới. Phần chỉ hiện ở state khác được ghi Not shown; ví dụ không thêm phần tử giả.

Standard
LightDarkDesktopMobile

Anatomy: một instance mặc định riêng, có khoảng trống quanh các phần được đánh số.

Show code
chatty-product-card.element.jsWrap code linesCopy
<chatty-product-card
  style="align-self: stretch"
  price="$24"
  compare-at-price="$32"
  label="View product Canvas tote bag"
>
  <chatty-image
    slot="media"
    src="/fixtures/tote.svg"
    alt=""
    ratio="4 / 5"
    fit="contain"
  ></chatty-image>
  Canvas tote bag
  <span slot="rating" style="display: inline-flex; align-items: center; gap: 4px">
    <chatty-rating
      readonly
      value="4.6"
      label="Rated 4.6 out of 5"
      style="--chatty-rating-size: 16px"
    ></chatty-rating>
    <b>4.6</b> (128)
  </span>
  <chatty-badge slot="badge" variant="secondary" size="sm">−25%</chatty-badge>
  <chatty-button slot="action" size="sm">Add to cart</chatty-button>
</chatty-product-card>
<chatty-product-card
  style="align-self: stretch"
  price="$18"
  selected
  label="View product Ceramic mug"
>
  <chatty-image
    slot="media"
    src="/fixtures/mug.svg"
    alt=""
    ratio="4 / 5"
    fit="contain"
  ></chatty-image>
  <chatty-badge slot="overlay" variant="primary">
    <svg
      width="14"
      height="14"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2.5"
      aria-hidden="true"
      style="vertical-align: -2px"
    >
      <path d="m5 12 5 5 9-10" />
    </svg>
    Selected
  </chatty-badge>
  Ceramic mug
  <chatty-button slot="action" size="sm">Add to cart</chatty-button>
</chatty-product-card>
<chatty-product-card style="align-self: stretch" price="$28" label="View product Wool cap">
  <chatty-image
    slot="media"
    src="/fixtures/cap.svg"
    alt=""
    ratio="4 / 5"
    fit="contain"
  ></chatty-image>
  Wool cap
  <chatty-button slot="action" size="sm" disabled>Sold out</chatty-button>
</chatty-product-card>
Part Element Source
1 CSS part base chatty-ui/custom-elements.json
2 CSS part open chatty-ui/custom-elements.json
3 CSS part media chatty-ui/custom-elements.json
4 CSS part name chatty-ui/custom-elements.json
5 CSS part price chatty-ui/custom-elements.json

Keyboard

Key Action
Enter / Space Open the product; action slots keep their own keyboard behavior.

Source

  • chatty-ui/custom-elements.json