CartControl

Cart control that syncs with a datasource and exposes pending vs verified state.

Import
the javascript logo
import { CartControl } from "@schukai/monster/source/components/form/cart-control.mjs";
Source
the git logo
Package
Since
1.0.0
Load updated cart

Purpose and scope

Cart control that syncs with a datasource and exposes pending vs verified state.

This page documents the <monster-cart-control> custom element. Use the Usage examples for a working starting point and the API tab for the complete public contract.

When to use it

Use this module when its focused responsibility matches the surrounding workflow. Prefer its public options, properties, methods and events over reaching into rendered internals.

Cart Summary

Renders item count and total from local cart data using the public mapping options.

Javascript

import "@schukai/monster/source/components/form/cart-control.mjs";

const cart = document.getElementById("cart-summary");
cart.setOption("mapping.totalTemplate", "total");
cart.setOption("mapping.currencyTemplate", "currency");
cart.setOption("data", {
  items: [{sku: "MONSTER-1", qty: 2}, {sku: "MONSTER-2", qty: 1}],
  total: 87.5,
  currency: "EUR",
});
cart.refresh();

HTML

<monster-cart-control id="cart-summary"></monster-cart-control>

Stylesheet

/** no additional stylesheet is defined **/
Open in playground

Verified Cart Updates

Applies a verified cart snapshot and consumes the component's update event.

Waiting for a cart update.

Javascript

import "@schukai/monster/source/components/form/cart-control.mjs";

const cart = document.getElementById("cart-updates");
const status = document.getElementById("cart-updates-status");
cart.setOption("mapping.totalTemplate", "total");
cart.setOption("mapping.currencyTemplate", "currency");
cart.addEventListener("monster-cart-control-update", event => {
  status.textContent = `${event.detail.count} items, ${event.detail.total} ${event.detail.currency}`;
});

document.getElementById("cart-updates-refresh").addEventListener("click", () => {
  cart.setOption("data", {
    items: [{sku: "MONSTER-1", qty: 4}],
    total: 116,
    currency: "EUR",
  });
  cart.refresh();
});

HTML

<section style="display:grid;gap:var(--monster-space-3)">
  <monster-cart-control id="cart-updates"></monster-cart-control>
  <button id="cart-updates-refresh" type="button">Apply verified cart</button>
  <output id="cart-updates-status">Waiting for a cart update.</output>
</section>

Stylesheet

/** no additional stylesheet is defined **/
Open in playground

Component Design

monster-cart-control separates the cart summary from transport state. It renders item count and total while reporting idle, pending, verified, and error states.

Data and Interaction

Supply verified cart snapshots through the public data and mapping options. Use addOrChange() only when a write datasource is configured, and consume the public lifecycle events for user feedback.

Customizing With Parts

You can adapt the visual appearance through exported parts instead of modifying the internal template. This reference is generated from the current component source.

The component exposes these part names:

  • control: Outer wrapper of the component.
  • count: Element representing the count.
  • status: Live component status region.
  • summary: Summary region.
  • total: Rendered total value.

HTML Structure

<monster-cart-control></monster-cart-control>

JavaScript Initialization

const element = document.createElement('monster-cart-control');
document.body.appendChild(element);

Exported

CartControl

Derived from

CustomControl

Options

The Options listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the CustomControl.

Option
Type
Default
Description
templates
object
Template definitions
templates.main
string
undefined
Main template
labels
object
undefined
Labels
datasource
object
Datasource configuration
datasource.selector
string
Selector for datasource
datasource.rest
object
Rest datasource config
cart
object
Cart write configuration
mapping
object
Response mapping
state
object
Control state
actions
object
Callback actions

  • since
  • deprecated

Properties and Attributes

The Properties and Attributes listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits Properties and Attributes from these parent classes. If you cannot find a specific Properties and Attributes in this list, we recommend consulting the documentation of the CustomControl.

  • data-monster-options: Sets the configuration options for the collapse component when used as an HTML attribute.
  • data-monster-option-[name]: Sets the value of the configuration option [name] for the collapse component when used as an HTML attribute.

Methods

The methods listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the CustomControl.

Behavioral methods

addOrChange(payload)
Parameters
  • payload {object}: payload
Add or change an item in the cart.
refresh()
Returns
  • {CartControl}
Re-apply mapped data from options or datasource.

Static methods

[instanceSymbol]()
No description available.
getCSSStyleSheet()
No description available.
getTag()
No description available.

Lifecycle methods

Lifecycle methods are called by the environment and are usually not intended to be called directly.

[assembleMethodSymbol]()
No description available.

Events

The component emits the following events:

  • monster-cart-control-pending
  • monster-cart-control-verified
  • monster-cart-control-error
  • monster-cart-control-update

For more information on how to handle events, see the mdn documentation.

The current width of the area is too small to display the content correctly.