Searching Open Sourcery

Type to search Open Sourcery.

Commerce

Cart Panel

Manages Shopify cart data and AJAX, and delegates modal behaviour to dialog-panel. Includes the <cart-item> element for each line, imported from @magic-spells/cart-panel/cart-item.

v2.0.0@magic-spells/cart-panel

$ npm i @magic-spells/cart-panel

GitHubnpm

Usage

<!-- Cart with dialog-panel wrapper -->
<dialog-panel id="cart-dialog">
  <dialog aria-labelledby="cart-title">
    <cart-panel manual>
      <div class="cart-header">
        <h2 id="cart-title">Shopping Cart</h2>
        <button aria-label="Close cart" class="close-button" data-action-hide-cart>
          &times;
        </button>
      </div>
      <div class="cart-body">
        <!-- Cart has items section -->
        <div data-cart-has-items>
          <div class="cart-items" data-content-cart-items>
            <!-- Cart items rendered dynamically -->
          </div>
        </div>

        <!-- Cart is empty section -->
        <div data-cart-is-empty>
          <div class="empty-cart">
            <p>Your cart is empty</p>
            <p>Add some items to get started!</p>
          </div>
        </div>
      </div>
      <div class="cart-footer">
        <div class="cart-total">
          Subtotal: <span data-content-cart-subtotal>$0.00</span>
        </div>

About

A slide-out shopping cart web component. The panel owns the cart data, the Shopify AJAX and the item rendering; @magic-spells/dialog-panel owns the modal.

Entry points

ElementImportStylesheet
<cart-panel> + <cart-item>@magic-spells/cart-panel@magic-spells/cart-panel/css
<cart-panel>@magic-spells/cart-panel/panel@magic-spells/cart-panel/panel/css
<cart-item>@magic-spells/cart-panel/cart-item@magic-spells/cart-panel/cart-item/css

Features

  • Complete cart management - Handles cart data, AJAX requests, and item rendering
  • One import - The root entry registers the panel and the item together; both are also on their own subpaths if you want only one
  • Swappable item element - The panel resolves `` from the custom element registry at render time, so you can substitute your own
  • Two render modes - JS templates by default, or let a Shopify section render the line items: the server renders content, JS renders behavior
  • Optimistic updates - Opt in and quantity changes land on the click, not on the response, with coalescing and stale-response guards behind them
  • Delegates modal to dialog-panel - Works with @magic-spells/dialog-panel for accessible modal behavior

Configuration

AttributeTypeDescription
manualBooleanSkip auto-refresh on connect, require explicit refreshCart()
sectionStringShopify section id that renders the line items. Absent, a JS template renders them — see Render Modes
optimisticBooleanApply quantity changes and removals locally before the server answers — see Optimistic Updates
hide-count-when-emptyBooleanHide every [data-content-cart-count] element on the page while the cart is empty
stateStringReflected attribute: 'has-items' or 'empty'

About, features, usage and options are taken from the package README.