shopping-cart
An animated shopping-cart icon for React that scales up slightly and bounces twice on hover using motion/react. It can also be controlled imperatively through a ref with startAnimation and stopAnimation.
shadcn add · no workarounds · theme neutral (none shipped)Installation
Requires @heroicons-animated in the registries of your components.json (shadcn adds official directory namespaces automatically).
Usage
import { ShoppingCartIcon } from "@/components/ui/shopping-cart";export function Demo() { return <ShoppingCartIcon className="text-foreground" />;}- Add a cart icon to e-commerce headers, product cards, or checkout buttons that should respond to hover with a playful motion.
- Show cart activity in navigation bars where a subtle, icon-only animation adds feedback without extra copy.
- Trigger the cart animation programmatically (for example after an add-to-cart action) by using the exposed ref handle.
- Replace a static cart icon in a design system that already uses motion/react and Tailwind-style className utilities.
Examples
Hover animation (uncontrolled)
The default behavior: the icon animates on mouse enter and returns to rest on mouse leave. Color is set through className because the SVG uses currentColor.
import { ShoppingCartIcon } from "@/components/ui/shopping-cart";export default function HeaderCart() { return ( <a href="/cart" aria-label="Open cart" className="inline-flex items-center text-zinc-900 dark:text-zinc-100"> <ShoppingCartIcon size={24} /> </a> );}Imperative control via ref
Attaching the ref switches the icon to controlled mode: its built-in hover animation is disabled and the parent calls startAnimation and stopAnimation. Here the cart bounces when the user clicks an add-to-cart button.
import { useRef } from "react";import { ShoppingCartIcon, type ShoppingCartIconHandle } from "@/components/ui/shopping-cart";export default function AddToCartButton() { const cartRef = useRef<ShoppingCartIconHandle>(null); return ( <button type="button" onClick={() => { cartRef.current?.startAnimation(); window.setTimeout(() => cartRef.current?.stopAnimation(), 1200); }} className="inline-flex items-center gap-2 rounded-md border px-4 py-2" > <ShoppingCartIcon ref={cartRef} size={20} /> Add to cart </button> );}Custom size and hover callbacks
Passing onMouseEnter and onMouseLeave together with the ref lets the parent react to hover while the component skips its own animation. Extra HTMLDivElement attributes such as aria-hidden are forwarded to the wrapper div.
import { useRef } from "react";import { ShoppingCartIcon, type ShoppingCartIconHandle } from "@/components/ui/shopping-cart";export default function ProductCard() { const cartRef = useRef<ShoppingCartIconHandle>(null); return ( <div className="rounded-xl border p-4"> <ShoppingCartIcon ref={cartRef} size={32} className="cursor-pointer text-blue-600" aria-hidden="true" onMouseEnter={() => cartRef.current?.startAnimation()} onMouseLeave={() => cartRef.current?.stopAnimation()} /> </div> );}API reference
Accessibility
- The component renders no text or title. When the icon is the only content of a link or button, add aria-label (for example aria-label="Open cart") on the interactive parent.
- If the icon is decorative next to visible text, pass aria-hidden="true" so screen readers skip it.
- The animation is a short scale and vertical bounce that plays once per hover. Respect reduced-motion preferences by disabling or shortening the animation in your app if needed, for example by not calling startAnimation when the user prefers reduced motion.
- The SVG uses currentColor, so keep sufficient contrast between the icon color and its background (WCAG 1.4.11 non-text contrast of 3:1).
- Keyboard users do not trigger hover. If the animation conveys state, also trigger startAnimation on focus or on the relevant state change.
Docs written by anthropic:claude-haiku-5-5 from the registry source.
Use with Coding Agent
Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.
Use the "shopping-cart" component (heroicons-animated/shopping-cart) from its shadcn registry.1. Install it with: npx shadcn@latest add @heroicons-animated/shopping-cart2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: size, className, onMouseEnter, onMouseLeave, ref, ...props.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { ShoppingCartIcon } from "@/components/ui/shopping-cart";export function Demo() { return <ShoppingCartIcon className="text-foreground" />;}```Files & dependencies
- shopping-cart.tsx