Beauty Affairs HK

Cart events

Every event the mini-cart emits or listens for, with payloads, plus the custom elements that make up the drawer.

src/entrypoints/mini-cart/internals/core.ts defines every event name in the CartEvents constant and every payload type in the CartEventDetailMap interface. A new event needs an entry in both.

All events are CustomEvents dispatched on window. Every payload that CartEventBus.emit sends also has an external boolean, which is true when code outside the mini-cart's internals started the action.

Commands you can dispatch

Dispatch these to change the cart from any code on the page. They get the same request queueing and stale-key recovery as the drawer's own controls.

EventPayload
mini-cart:getnone
mini-cart:add{ items: [{ id, quantity, properties }], sections? }
mini-cart:change{ id | line, quantity, properties?, sections? }
mini-cart:update{ note?, attributes?, discount?, sections? } or { updates: { [line]: qty } }
mini-cart:clearnone
window.dispatchEvent(
  new CustomEvent('mini-cart:add', {
    detail: { items: [{ id: 123456789, quantity: 1 }] },
  }),
);

Lifecycle events you can listen for

EventPayload
cart:add:startThe add payload
cart:add:success{ payload, cart }
cart:add:error{ payload, error }
cart:change:startThe change payload
cart:change:success{ payload, cart }
cart:change:error{ payload, error }
cart:update:startThe update payload
cart:update:success{ payload, cart }
cart:update:error{ payload, error }
cart:clear:startnone
cart:clear:success{ cart? }. The cart is optional because the interceptor can emit this
cart:clear:error{ error }

Change notifications

EventPayloadNotes
cart:changed{ cart, reason? }Emitted for every change, including writes by other apps
cart:changed:debounced{ cart, reason? }The same event, debounced. Use this one for rendering

reason is one of add, change, update, clear or get.

Render from cart:changed:debounced. A burst of quantity changes emits cart:changed many times, and rendering on each one makes the drawer flicker.

Request and transport events

EventPayload
cart:request{ endpoint, method, payload? }
cart:response{ endpoint, method, cart }
cart:error{ endpoint, method, error }
cart:fetch:request{ url, method, init? }
cart:fetch:response{ url, method, status, data }
cart:fetch:error{ url, method, error }

The three cart:fetch:* events are off by default. A comment in the interceptor explains that other apps send enough cart requests to make these events noise. Turn them on only while debugging.

UI events

EventPayloadDirection
mini-cart:ui:opennoneAsk the drawer to open
mini-cart:ui:closenoneAsk the drawer to close
mini-cart:ui:update{ action }Ask for an arbitrary UI action
mini-cart:ui:openednoneThe drawer has opened
mini-cart:ui:closednoneThe drawer has closed

open and close are requests. opened and closed report what happened. To react to the drawer, listen for opened and closed.

Readiness

EventPayload
mini-cart:readynone

The mini-cart emits this once its core has initialised. Code that needs the mini-cart should wait for this event and not assume the mini-cart exists at DOMContentLoaded.

Custom elements

TagFile
mini-cartelements/mini-cart.ts
mini-cart-headerelements/mini-cart-header.ts
mini-cart-line-itemselements/mini-cart-line-items.ts
mini-cart-footerelements/mini-cart-footer.ts
mini-cart-summaryelements/mini-cart-summary.ts
mini-cart-progress-barelements/mini-cart-progress-bar.ts
mini-cart-recommendationselements/mini-cart-recommendations.ts
mini-cart-gifts-accordionelements/mini-cart-gifts-accordion.ts
mini-cart-discount-codeelements/mini-cart-discount-code.ts
mini-cart-clear-cartelements/mini-cart-clear-cart.ts

All paths are relative to src/entrypoints/mini-cart/.

Four more custom elements exist outside the mini-cart. The homepage redesign defines product-card-form, products-carousel and tabbed-carousels, and the quick add modal defines select-options.

Other globals the mini-cart exposes

GlobalPurpose
window.addCheckoutEventChain(fn)Register an async step that runs over the cart at checkout. Returns false when there is no footer element

On this page