1- // Importing the package registers the <shopify-checkout> custom element
2- // as a side effect — once the component implementation lands. Today the
3- // package only exports `VERSION`, so the element below renders as an
4- // unknown HTML element and the playground produces no events at runtime.
5- import "../src" ;
1+ // Registers `<shopify-checkout>` (same entry as published `@shopify/checkout-kit`).
2+ import "@shopify/checkout-kit" ;
3+ import type { ShopifyCheckout } from "@shopify/checkout-kit" ;
64
75import "./styles.css" ;
86
@@ -40,11 +38,10 @@ const buyNowButton = $<HTMLButtonElement>("#buy-now");
4038const buyHint = $ < HTMLParagraphElement > ( "#buy-hint" ) ;
4139
4240const stateNodes = {
43- cart : $ < HTMLElement > ( "#state-cart" ) ,
44- locale : $ < HTMLElement > ( "#state-locale" ) ,
45- orderConfirmation : $ < HTMLElement > ( "#state-order-confirmation" ) ,
41+ checkout : $ < HTMLElement > ( "#state-checkout" ) ,
4642 error : $ < HTMLElement > ( "#state-error" ) ,
47- sessionId : $ < HTMLElement > ( "#state-session-id" ) ,
43+ target : $ < HTMLElement > ( "#state-target" ) ,
44+ debug : $ < HTMLElement > ( "#state-debug" ) ,
4845} ;
4946
5047// ───── Mount the component (off-layout) ───────────────────────────────────
@@ -55,9 +52,11 @@ const stateNodes = {
5552// called, so we attach it to <body> and leave the storefront panel free for
5653// the merchant's product UI.
5754
58- const checkout = document . createElement ( "shopify-checkout" ) ;
55+ const checkout = document . createElement ( "shopify-checkout" ) as ShopifyCheckout ;
5956document . body . append ( checkout ) ;
6057
58+ const checkoutEl : HTMLElement = checkout ;
59+
6160// ───── Form ↔ attributes ──────────────────────────────────────────────────
6261
6362function setStringAttribute ( el : HTMLElement , name : string , value : FormDataEntryValue | null ) : void {
@@ -72,6 +71,11 @@ function syncAttributes(): void {
7271 const data = new FormData ( form ) ;
7372 setStringAttribute ( checkout , "src" , data . get ( "src" ) ) ;
7473 setStringAttribute ( checkout , "target" , data . get ( "target" ) ) ;
74+ if ( data . has ( "debug" ) ) {
75+ checkout . setAttribute ( "debug" , "" ) ;
76+ } else {
77+ checkout . removeAttribute ( "debug" ) ;
78+ }
7579
7680 refreshBuyButton ( data . get ( "src" ) ) ;
7781}
@@ -123,24 +127,22 @@ for (const swatch of swatches) {
123127
124128// ───── Event log ──────────────────────────────────────────────────────────
125129
130+ /** Dispatched by `ShopifyCheckout` (see `src/checkout.ts`). */
126131const EVENT_TYPES = [
127- "checkout :start" ,
128- "checkout :complete" ,
132+ "ec :start" ,
133+ "ec :complete" ,
129134 "checkout:close" ,
130- "checkout:error" ,
131- "checkout:addressChangeStart" ,
132- "checkout:paymentMethodChangeStart" ,
133- "checkout:submitStart" ,
135+ "ec:error" ,
136+ "ec:lineItemsChange" ,
137+ "ec:buyerChange" ,
138+ "ec:totalsChange" ,
139+ "ec:messagesChange" ,
134140] as const ;
135141
136- const RESPONDABLE_EVENTS = new Set < string > ( [
137- "checkout:addressChangeStart" ,
138- "checkout:paymentMethodChangeStart" ,
139- "checkout:submitStart" ,
140- ] ) ;
142+ const RESPONDABLE_EVENTS = new Set < string > ( [ ] ) ;
141143
142144for ( const type of EVENT_TYPES ) {
143- checkout . addEventListener ( type , ( ) => {
145+ checkoutEl . addEventListener ( type , ( ) => {
144146 appendLog ( type ) ;
145147 refreshState ( ) ;
146148 } ) ;
@@ -152,20 +154,18 @@ clearLogButton.addEventListener("click", () => {
152154
153155function snapshotState ( ) : Record < string , unknown > {
154156 return {
155- cart : checkout . cart ,
156- locale : checkout . locale ,
157- orderConfirmation : checkout . orderConfirmation ,
157+ checkout : checkout . checkout ,
158158 error : checkout . error ,
159- sessionId : checkout . sessionId ,
159+ target : checkout . target ,
160+ debug : checkout . debug ,
160161 } ;
161162}
162163
163164function refreshState ( ) : void {
164- stateNodes . cart . textContent = formatValue ( checkout . cart ) ;
165- stateNodes . locale . textContent = formatValue ( checkout . locale ) ;
166- stateNodes . orderConfirmation . textContent = formatValue ( checkout . orderConfirmation ) ;
165+ stateNodes . checkout . textContent = formatValue ( checkout . checkout ) ;
167166 stateNodes . error . textContent = formatValue ( checkout . error ) ;
168- stateNodes . sessionId . textContent = formatValue ( checkout . sessionId ) ;
167+ stateNodes . target . textContent = formatValue ( checkout . target ) ;
168+ stateNodes . debug . textContent = formatValue ( checkout . debug ) ;
169169}
170170
171171function appendLog ( type : string ) : void {
0 commit comments