For the complete documentation index, see llms.txt. Markdown variants are available by appending .md to any URL or sending an Accept: text/markdown header. An agent skill is available at /.well-known/agent-skills/site-skill.md.
Shadcn Labs/

Add to Cart

The product image flies into the cart button, the badge count rolls up and the button label morphs to "Added". A React component built on View Transitions, installed with the shadcn CLI.

Studio Goods

Subtotal $0

Aurora Lamp

$89

Field Notebook

$24

Ceramic Mug

$32

Installation

$ pnpm dlx shadcn@latest add @transition-kit/add-to-cart

Usage

import {
  AddToCartButton,
  CartButton,
  CartProvider,
  useCart,
} from "@/components/transitions/add-to-cart";
const ProductCard = ({ product }) => {
  const imageRef = React.useRef<HTMLImageElement>(null);
  return (
    <div>
      <img ref={imageRef} src={product.image} alt={product.name} />
      <AddToCartButton
        productId={product.id}
        src={product.image}
        source={imageRef}
      />
    </div>
  );
};
 
<CartProvider>
  <CartButton />
  <ProductCard product={product} />
</CartProvider>;
const { items, count, add, remove, clear } = useCart();

API Reference

CartProvider

PropTypeDefaultDescription
itemsCartItem[]-Controlled cart contents, where CartItem is { id: string; quantity: number }.
defaultItemsCartItem[][]Initial cart contents when uncontrolled.
onItemsChange(items: CartItem[]) => void-Called with the new items after each change.

AddToCartButton

PropTypeDefaultDescription
productIdstring-Id added to the cart on click.
quantitynumber1Amount added per click.
imageReactNode-Custom artwork that flies into the cart; without image or src, the item is added with no flight.
srcstring-Image URL used to build the flying <img> when image is not given.
sourceRefObject<HTMLElement | null>the buttonElement the product image flies from.
childrenReactNode"Add to cart"Idle button label.
addedLabelReactNode"Added"Label shown after adding.
resetAfternumber1500Milliseconds before the label returns to its idle text.

useCart

FieldTypeDescription
itemsCartItem[]Current cart contents.
countnumberTotal quantity across items.
add(id: string, quantity?: number) => voidAdds a quantity of an item; the badge rolls with no flight.
remove(id: string) => voidRemoves the item with the given id; the badge rolls with no flight.
clear() => voidEmpties the cart; the badge rolls with no flight.