@magic-spells/cart-progress-bar 0.2.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"cart-progress-bar.esm.js","sources":["../src/cart-progress-bar.js"],"sourcesContent":["import './cart-progress-bar.scss';\n\n/**\n * ProgressBar helper class for the visual progress bar element\n */\nclass ProgressBar extends HTMLElement {\n\tconstructor() {\n\t\tsuper();\n\t\tthis.#setupProgressBar();\n\t}\n\n\t#setupProgressBar() {\n\t\tthis.setAttribute('role', 'progressbar');\n\t\tthis.setAttribute('aria-valuemin', '0');\n\t\tthis.setAttribute('aria-valuemax', '100');\n\t\tthis.setAttribute('aria-valuenow', '0');\n\n\t\t// Create the visual progress bar\n\t\tthis.innerHTML = '<div class=\"progress-bar-fill\"></div>';\n\t}\n\n\tsetPercent(percent) {\n\t\tconst clampedPercent = Math.max(0, Math.min(100, percent));\n\t\tthis.style.setProperty('--cart-progress-percent', `${clampedPercent}%`);\n\t\tthis.setAttribute('aria-valuenow', clampedPercent);\n\t}\n}\n\n// Define ProgressBar custom element immediately so it's available for CartProgressBar\ncustomElements.define('progress-bar', ProgressBar);\n\n/**\n * CartProgressBar main component\n */\nclass CartProgressBar extends HTMLElement {\n\t// Private fields\n\t#minAmount = 0;\n\t#currentAmount = 0;\n\t#progressPercent = 0;\n\t#originalAboveMessage = '';\n\t#originalBelowMessage = '';\n\t#progressBar = null;\n\t#messageElement = null;\n\n\t/**\n\t * Define which attributes should be observed for changes\n\t */\n\tstatic get observedAttributes() {\n\t\treturn ['threshold', 'current', 'message-above', 'message-below'];\n\t}\n\n\tconstructor() {\n\t\tsuper();\n\t\tthis.#init();\n\t}\n\n\t#init() {\n\t\t// Read initial attributes\n\t\tthis.#minAmount = parseFloat(this.getAttribute('threshold')) || 0;\n\t\tthis.#currentAmount = parseFloat(this.getAttribute('current')) || 0;\n\n\t\t// Store original message templates\n\t\tthis.#originalAboveMessage = this.getAttribute('message-above') || '';\n\t\tthis.#originalBelowMessage = this.getAttribute('message-below') || '';\n\t}\n\n\tasync connectedCallback() {\n\t\t// ensure the child custom element has been registered\n\t\tawait customElements.whenDefined('progress-bar');\n\n\t\tif (!customElements.get('progress-bar')) {\n\t\t\tthrow new Error('<progress-bar> must be registered before <cart-progress-bar> runs');\n\t\t}\n\n\t\tthis.#render();\n\t\tthis.#updateProgress();\n\t\tthis.#attachListeners();\n\t}\n\n\tattributeChangedCallback(name, oldValue, newValue) {\n\t\tif (oldValue === newValue) return;\n\n\t\tswitch (name) {\n\t\t\tcase 'threshold':\n\t\t\t\tthis.#minAmount = parseFloat(newValue) || 0;\n\t\t\t\tthis.#updateProgress();\n\t\t\t\tbreak;\n\t\t\tcase 'current':\n\t\t\t\tthis.#currentAmount = parseFloat(newValue) || 0;\n\t\t\t\tthis.#updateProgress();\n\t\t\t\tbreak;\n\t\t\tcase 'message-above':\n\t\t\t\tthis.#originalAboveMessage = newValue || '';\n\t\t\t\tthis.#updateMessages();\n\t\t\t\tbreak;\n\t\t\tcase 'message-below':\n\t\t\t\tthis.#originalBelowMessage = newValue || '';\n\t\t\t\tthis.#updateMessages();\n\t\t\t\tbreak;\n\t\t}\n\t}\n\n\t#render() {\n\t\t// Find or create the message element\n\t\tthis.#messageElement =\n\t\t\tthis.querySelector('[data-content-cart-progress-message]') ||\n\t\t\tthis.querySelector('p[data-content-cart-progress-message]');\n\n\t\t// Find existing progress bar (user can add their own)\n\t\tthis.#progressBar = this.querySelector('progress-bar');\n\n\t\t// Create message element if it doesn't exist but we have message templates\n\t\tif (!this.#messageElement && (this.#originalAboveMessage || this.#originalBelowMessage)) {\n\t\t\tthis.#messageElement = document.createElement('p');\n\t\t\tthis.#messageElement.setAttribute('data-content-cart-progress-message', '');\n\t\t\tthis.appendChild(this.#messageElement);\n\t\t}\n\n\t\t// Create progress bar if it doesn't exist - add it at the end (below text message)\n\t\tif (!this.#progressBar) {\n\t\t\t// Ensure progress-bar is defined before creating\n\t\t\tif (customElements.get('progress-bar')) {\n\t\t\t\tthis.#progressBar = document.createElement('progress-bar');\n\t\t\t\tthis.appendChild(this.#progressBar);\n\t\t\t} else {\n\t\t\t\t// Fallback: wait for definition\n\t\t\t\tcustomElements.whenDefined('progress-bar').then(() => {\n\t\t\t\t\tif (!this.#progressBar) {\n\t\t\t\t\t\tthis.#progressBar = document.createElement('progress-bar');\n\t\t\t\t\t\tthis.appendChild(this.#progressBar);\n\t\t\t\t\t\tthis.#updateProgress(); // Update progress after creating\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\t#updateProgress() {\n\t\tif (this.#minAmount === 0) {\n\t\t\tthis.#progressPercent = 100;\n\t\t} else {\n\t\t\tthis.#progressPercent = Math.min(100, (this.#currentAmount / this.#minAmount) * 100);\n\t\t}\n\n\t\t// Update progress bar\n\t\tif (this.#progressBar) {\n\t\t\tthis.#progressBar.setPercent(this.#progressPercent);\n\t\t}\n\n\t\t// Update messages\n\t\tthis.#updateMessages();\n\n\t\t// Update component state\n\t\tthis.#updateComponentState();\n\t}\n\n\t#attachListeners() {\n\t\t// Find the nearest cart-panel component\n\t\tconst cartDialog = this.closest('cart-dialog');\n\n\t\tif (cartDialog) {\n\t\t\t// Listen for cart data changes\n\t\t\tcartDialog.addEventListener('cart-dialog:data-changed', (event) => {\n\t\t\t\tthis.#handleCartDataChange(event);\n\t\t\t});\n\t\t}\n\t}\n\n\t#handleCartDataChange(event) {\n\t\tconst updatedCart = event.detail;\n\n\t\tif (updatedCart) {\n\t\t\t// Use calculated_subtotal if available (handles _ignore_price_in_subtotal logic)\n\t\t\t// Otherwise fall back to total_price for backwards compatibility\n\t\t\tlet currentAmount = 0;\n\n\t\t\tif (typeof updatedCart.calculated_subtotal !== 'undefined') {\n\t\t\t\t// calculated_subtotal is already in dollars, no conversion needed\n\t\t\t\tcurrentAmount = updatedCart.calculated_subtotal / 100;\n\t\t\t} else if (typeof updatedCart.total_price !== 'undefined') {\n\t\t\t\t// Convert from cents to dollars if needed (Shopify typically returns cents)\n\t\t\t\tcurrentAmount = updatedCart.total_price / 100;\n\t\t\t}\n\n\t\t\tthis.setCurrentAmount(currentAmount);\n\t\t}\n\t}\n\n\t#updateMessages() {\n\t\tconst isComplete = this.#currentAmount >= this.#minAmount;\n\t\tconst remainingAmount = Math.max(0, this.#minAmount - this.#currentAmount);\n\n\t\t// Format remaining amount as currency (assuming USD for now)\n\t\tconst formattedAmount = this.#formatCurrency(remainingAmount);\n\n\t\t// Update the single message element\n\t\tif (this.#messageElement) {\n\t\t\tlet messageTemplate;\n\n\t\t\tif (isComplete && this.#originalAboveMessage) {\n\t\t\t\t// Show success message when complete\n\t\t\t\tmessageTemplate = this.#originalAboveMessage;\n\t\t\t} else if (!isComplete) {\n\t\t\t\t// Show progress message when incomplete\n\t\t\t\tmessageTemplate = this.#originalBelowMessage || this.#originalAboveMessage;\n\t\t\t}\n\n\t\t\tif (messageTemplate) {\n\t\t\t\tconst message = messageTemplate.replace('{ amount }', formattedAmount);\n\t\t\t\tthis.#messageElement.textContent = message;\n\t\t\t\tthis.#messageElement.style.display = 'block';\n\t\t\t} else {\n\t\t\t\t// Hide message if no template available for current state\n\t\t\t\tthis.#messageElement.style.display = 'none';\n\t\t\t}\n\t\t}\n\t}\n\n\t#updateComponentState() {\n\t\tconst isComplete = this.#currentAmount >= this.#minAmount;\n\t\tthis.setAttribute('complete', isComplete.toString());\n\t}\n\n\t#formatCurrency(amount) {\n\t\t// Basic currency formatting - could be enhanced with locale/currency options\n\t\treturn new Intl.NumberFormat('en-US', {\n\t\t\tstyle: 'currency',\n\t\t\tcurrency: 'USD',\n\t\t\tminimumFractionDigits: 2,\n\t\t\tmaximumFractionDigits: 2,\n\t\t})\n\t\t\t.format(amount)\n\t\t\t.replace('.00', '');\n\t}\n\n\t/**\n\t * Public API: Set the progress percentage directly\n\t * @param {number} percent - Progress percentage (0-100)\n\t */\n\tsetPercent(percent) {\n\t\tconst clampedPercent = Math.max(0, Math.min(100, percent));\n\t\tthis.#progressPercent = clampedPercent;\n\n\t\tif (this.#progressBar) {\n\t\t\tthis.#progressBar.setPercent(clampedPercent);\n\t\t}\n\n\t\t// Calculate current amount based on percentage\n\t\tthis.#currentAmount = (clampedPercent / 100) * this.#minAmount;\n\t\tthis.setAttribute('current', this.#currentAmount.toString());\n\n\t\tthis.#updateMessages();\n\t\tthis.#updateComponentState();\n\t}\n\n\t/**\n\t * Public API: Set the current cart amount\n\t * @param {number} amount - Current cart amount\n\t */\n\tsetCurrentAmount(amount) {\n\t\tthis.#currentAmount = parseFloat(amount) || 0;\n\t\tthis.setAttribute('current', this.#currentAmount.toString());\n\t\tthis.#updateProgress();\n\t}\n\n\t/**\n\t * Public API: Set the threshold amount for free shipping\n\t * @param {number} amount - Threshold amount for free shipping\n\t */\n\tsetThresholdAmount(amount) {\n\t\tthis.#minAmount = parseFloat(amount) || 0;\n\t\tthis.setAttribute('threshold', this.#minAmount.toString());\n\t\tthis.#updateProgress();\n\t}\n\n\t/**\n\t * Public API: Set the minimum amount for free shipping (deprecated - use setThresholdAmount)\n\t * @param {number} amount - Minimum amount threshold\n\t * @deprecated Use setThresholdAmount instead\n\t */\n\tsetMinAmount(amount) {\n\t\tthis.setThresholdAmount(amount);\n\t}\n\n\t/**\n\t * Public API: Get current progress information\n\t */\n\tgetProgress() {\n\t\treturn {\n\t\t\tcurrentAmount: this.#currentAmount,\n\t\t\tthresholdAmount: this.#minAmount,\n\t\t\tminAmount: this.#minAmount, // backwards compatibility\n\t\t\tremainingAmount: Math.max(0, this.#minAmount - this.#currentAmount),\n\t\t\tpercent: this.#progressPercent,\n\t\t\tisComplete: this.#currentAmount >= this.#minAmount,\n\t\t};\n\t}\n\n\t/**\n\t * Public API: Update message templates\n\t * @param {string} aboveMessage - Message template for above the bar\n\t * @param {string} belowMessage - Message template for below the bar\n\t */\n\tsetMessages(aboveMessage = null, belowMessage = null) {\n\t\tif (aboveMessage !== null) {\n\t\t\tthis.#originalAboveMessage = aboveMessage;\n\t\t\tthis.setAttribute('message-above', aboveMessage);\n\t\t}\n\n\t\tif (belowMessage !== null) {\n\t\t\tthis.#originalBelowMessage = belowMessage;\n\t\t\tthis.setAttribute('message-below', belowMessage);\n\t\t}\n\n\t\tthis.#updateMessages();\n\t}\n\n\t// Getters\n\tget currentAmount() {\n\t\treturn this.#currentAmount;\n\t}\n\tget thresholdAmount() {\n\t\treturn this.#minAmount;\n\t}\n\tget minAmount() {\n\t\treturn this.#minAmount;\n\t} // backwards compatibility\n\tget percent() {\n\t\treturn this.#progressPercent;\n\t}\n\tget isComplete() {\n\t\treturn this.#currentAmount >= this.#minAmount;\n\t}\n}\n\n// Define CartProgressBar custom element\ncustomElements.define('cart-progress-bar', CartProgressBar);\n\n// Export components for external use\nexport { CartProgressBar, ProgressBar };\n"],"names":[],"mappings":"AAEA;AACA;AACA;AACA,MAAM,WAAW,SAAS,WAAW,CAAC;AACtC,CAAC,WAAW,GAAG;AACf,EAAE,KAAK,EAAE,CAAC;AACV,EAAE,IAAI,CAAC,iBAAiB,EAAE,CAAC;AAC3B,EAAE;AACF;AACA,CAAC,iBAAiB,GAAG;AACrB,EAAE,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;AAC3C,EAAE,IAAI,CAAC,YAAY,CAAC,eAAe,EAAE,GAAG,CAAC,CAAC;AAC1C,EAAE,IAAI,CAAC,YAAY,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;AAC5C,EAAE,IAAI,CAAC,YAAY,CAAC,eAAe,EAAE,GAAG,CAAC,CAAC;AAC1C;AACA;AACA,EAAE,IAAI,CAAC,SAAS,GAAG,uCAAuC,CAAC;AAC3D,EAAE;AACF;AACA,CAAC,UAAU,CAAC,OAAO,EAAE;AACrB,EAAE,MAAM,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,C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EAAE;AACF;AACA;AACA;AACA;AACA;AACA;AACA,CAAC,WAAW,CAAC,YAAY,GAAG,IAAI,EAAE,YAAY,GAAG,IAAI,EAAE;AACvD,EAAE,IAAI,YAAY,KAAK,IAAI,EAAE;AAC7B,GAAG,IAAI,CAAC,qBAAqB,GAAG,YAAY,CAAC;AAC7C,GAAG,IAAI,CAAC,YAAY,CAAC,eAAe,EAAE,YAAY,CAAC,CAAC;AACpD,GAAG;AACH;AACA,EAAE,IAAI,YAAY,KAAK,IAAI,EAAE;AAC7B,GAAG,IAAI,CAAC,qBAAqB,GAAG,YAAY,CAAC;AAC7C,GAAG,IAAI,CAAC,YAAY,CAAC,eAAe,EAAE,YAAY,CAAC,CAAC;AACpD,GAAG;AACH;AACA,EAAE,IAAI,CAAC,eAAe,EAAE,CAAC;AACzB,EAAE;AACF;AACA;AACA,CAAC,IAAI,aAAa,GAAG;AACrB,EAAE,OAAO,IAAI,CAAC,cAAc,CAAC;AAC7B,EAAE;AACF,CAAC,IAAI,eAAe,GAAG;AACvB,EAAE,OAAO,IAAI,CAAC,UAAU,CAAC;AACzB,EAAE;AACF,CAAC,IAAI,SAAS,GAAG;AACjB,EAAE,OAAO,IAAI,CAAC,UAAU,CAAC;AACzB,EAAE;AACF,CAAC,IAAI,OAAO,GAAG;AACf,EAAE,OAAO,IAAI,CAAC,gBAAgB,CAAC;AAC/B,EAAE;AACF,CAAC,IAAI,UAAU,GAAG;AAClB,EAAE,OAAO,IAAI,CAAC,cAAc,IAAI,IAAI,CAAC,UAAU,CAAC;AAChD,EAAE;AACF,CAAC;AACD;AACA;AACA,cAAc,CAAC,MAAM,CAAC,mBAAmB,EAAE,eAAe,CAAC;;;;"}
1
+ {"version":3,"file":"cart-progress-bar.esm.js","sources":["../src/cart-progress-bar.js"],"sourcesContent":["import './cart-progress-bar.scss';\n\n/**\n * ProgressBar helper class for the visual progress bar element\n */\nclass ProgressBar extends HTMLElement {\n\tconstructor() {\n\t\tsuper();\n\t\tconst _ = this;\n\t\t_.setAttribute('role', 'progressbar');\n\t\t_.setAttribute('aria-valuemin', '0');\n\t\t_.setAttribute('aria-valuemax', '100');\n\t\t_.setAttribute('aria-valuenow', '0');\n\t\t_.innerHTML = '<div class=\"progress-bar-fill\"></div>';\n\t}\n\n\tsetPercent(percent) {\n\t\tconst p = Math.max(0, Math.min(100, percent));\n\t\tthis.style.setProperty('--cart-progress-percent', `${p}%`);\n\t\tthis.setAttribute('aria-valuenow', p);\n\t}\n}\n\n// Define ProgressBar custom element immediately so it's available for CartProgressBar\ncustomElements.define('progress-bar', ProgressBar);\n\n/**\n * CartProgressBar main component\n */\nclass CartProgressBar extends HTMLElement {\n\t// Private fields\n\t#threshold = 0;\n\t#current = 0;\n\t#percent = 0;\n\t#msgAbove = '';\n\t#msgBelow = '';\n\t#bar = null;\n\t#msgEl = null;\n\t#moneyFmt = null;\n\t#debounce = null;\n\n\tstatic get observedAttributes() {\n\t\treturn ['threshold', 'current', 'message-above', 'message-below', 'money-format'];\n\t}\n\n\tconstructor() {\n\t\tsuper();\n\t\tconst _ = this;\n\t\t_.#threshold = parseFloat(_.getAttribute('threshold')) || 0;\n\t\t_.#current = parseFloat(_.getAttribute('current')) || 0;\n\t\t_.#msgAbove = _.getAttribute('message-above') || '';\n\t\t_.#msgBelow = _.getAttribute('message-below') || '';\n\t\t_.#moneyFmt = _.getAttribute('money-format');\n\t}\n\n\tasync connectedCallback() {\n\t\tawait customElements.whenDefined('progress-bar');\n\t\tthis.#render();\n\t\tthis.#updateProgress();\n\t\tthis.#attachListeners();\n\t}\n\n\tdisconnectedCallback() {\n\t\tif (this.#debounce) clearTimeout(this.#debounce);\n\t}\n\n\tattributeChangedCallback(name, oldVal, newVal) {\n\t\tif (oldVal === newVal) return;\n\t\tconst _ = this;\n\n\t\tswitch (name) {\n\t\t\tcase 'threshold':\n\t\t\t\t_.#threshold = parseFloat(newVal) || 0;\n\t\t\t\t_.#updateProgress();\n\t\t\t\tbreak;\n\t\t\tcase 'current':\n\t\t\t\t_.#current = parseFloat(newVal) || 0;\n\t\t\t\t_.#updateProgress();\n\t\t\t\tbreak;\n\t\t\tcase 'message-above':\n\t\t\t\t_.#msgAbove = newVal || '';\n\t\t\t\t_.#updateMessages();\n\t\t\t\tbreak;\n\t\t\tcase 'message-below':\n\t\t\t\t_.#msgBelow = newVal || '';\n\t\t\t\t_.#updateMessages();\n\t\t\t\tbreak;\n\t\t\tcase 'money-format':\n\t\t\t\t_.#moneyFmt = newVal;\n\t\t\t\t_.#updateMessages();\n\t\t\t\tbreak;\n\t\t}\n\t}\n\n\t#render() {\n\t\tconst _ = this;\n\t\t// Find existing elements or create them\n\t\t_.#msgEl = _.querySelector('[data-content-cart-progress-message]');\n\t\t_.#bar = _.querySelector('progress-bar');\n\n\t\t// Create message element if needed\n\t\tif (!_.#msgEl && (_.#msgAbove || _.#msgBelow)) {\n\t\t\t_.#msgEl = document.createElement('p');\n\t\t\t_.#msgEl.setAttribute('data-content-cart-progress-message', '');\n\t\t\t_.appendChild(_.#msgEl);\n\t\t}\n\n\t\t// Create progress bar if needed\n\t\tif (!_.#bar) {\n\t\t\t_.#bar = document.createElement('progress-bar');\n\t\t\t_.appendChild(_.#bar);\n\t\t}\n\t}\n\n\t#updateProgress() {\n\t\tconst _ = this;\n\t\tconst converted = _.#converted();\n\t\t_.#percent = converted === 0 ? 100 : Math.min(100, (_.#current / converted) * 100);\n\t\tif (_.#bar) _.#bar.setPercent(_.#percent);\n\t\t_.#updateState(converted);\n\t}\n\n\t#attachListeners() {\n\t\tconst _ = this;\n\t\tconst panel = _.closest('cart-panel');\n\t\tif (panel) {\n\t\t\tpanel.addEventListener('cart-panel:data-changed', (e) => _.#onCartChange(e));\n\t\t}\n\t}\n\n\t#onCartChange(event) {\n\t\tconst _ = this;\n\t\tconst cart = event.detail;\n\t\tif (!cart) return;\n\n\t\tif (_.#debounce) clearTimeout(_.#debounce);\n\t\t_.#debounce = setTimeout(() => {\n\t\t\t_.#debounce = null;\n\t\t\t// Use calculated_subtotal if available, else total_price (both in cents)\n\t\t\tconst amt = (cart.calculated_subtotal ?? cart.total_price ?? 0) / 100;\n\t\t\t_.setCurrentAmount(amt);\n\t\t}, 100);\n\t}\n\n\t#updateState(converted) {\n\t\tconst _ = this;\n\t\tconst complete = _.#current >= converted;\n\t\tconst remaining = Math.max(0, converted - _.#current);\n\t\tconst formatted = _.#fmtMoney(remaining);\n\n\t\t// Update message\n\t\tif (_.#msgEl) {\n\t\t\tlet tpl = complete ? _.#msgAbove : (_.#msgBelow || _.#msgAbove);\n\t\t\tif (tpl) {\n\t\t\t\t_.#msgEl.textContent = tpl.replace(/\\[\\s*amount\\s*\\]/g, formatted);\n\t\t\t\t_.#msgEl.style.display = 'block';\n\t\t\t} else {\n\t\t\t\t_.#msgEl.style.display = 'none';\n\t\t\t}\n\t\t}\n\n\t\t// Update complete attribute\n\t\t_.setAttribute('complete', complete.toString());\n\t}\n\n\t#fmtMoney(amt) {\n\t\tconst fmt = this.#moneyFmt;\n\t\tif (!fmt) return amt.toFixed(2).replace(/\\.00$/, '');\n\n\t\tconst fixed = amt.toFixed(2);\n\t\tconst noDecimals = Math.round(amt).toString();\n\t\tconst withComma = fixed.replace('.', ',');\n\t\tconst noDecWithComma = noDecimals.replace(/\\B(?=(\\d{3})+(?!\\d))/g, ',');\n\n\t\treturn fmt\n\t\t\t.replace(/\\{\\{\\s*amount_no_decimals_with_comma_separator\\s*\\}\\}/g, noDecWithComma)\n\t\t\t.replace(/\\{\\{\\s*amount_with_comma_separator\\s*\\}\\}/g, withComma)\n\t\t\t.replace(/\\{\\{\\s*amount_no_decimals\\s*\\}\\}/g, noDecimals)\n\t\t\t.replace(/\\{\\{\\s*amount\\s*\\}\\}/g, fixed);\n\t}\n\n\t#converted() {\n\t\tconst rate = parseFloat(window.Shopify?.currency?.rate) || 1;\n\t\treturn this.#threshold * rate;\n\t}\n\n\t#updateMessages() {\n\t\tthis.#updateState(this.#converted());\n\t}\n\n\t// Public API\n\tsetPercent(pct) {\n\t\tconst _ = this;\n\t\tconst p = Math.max(0, Math.min(100, pct));\n\t\t_.#percent = p;\n\t\tif (_.#bar) _.#bar.setPercent(p);\n\t\t// Use converted threshold for multi-currency consistency\n\t\t_.#current = (p / 100) * _.#converted();\n\t\t_.setAttribute('current', _.#current.toString());\n\t\t_.#updateState(_.#converted());\n\t}\n\n\tsetCurrentAmount(amt) {\n\t\tconst _ = this;\n\t\t_.#current = parseFloat(amt) || 0;\n\t\t_.setAttribute('current', _.#current.toString());\n\t\t_.#updateProgress();\n\t}\n\n\tsetThresholdAmount(amt) {\n\t\tconst _ = this;\n\t\t_.#threshold = parseFloat(amt) || 0;\n\t\t_.setAttribute('threshold', _.#threshold.toString());\n\t\t_.#updateProgress();\n\t}\n\n\t/** @deprecated Use setThresholdAmount */\n\tsetMinAmount(amt) {\n\t\tthis.setThresholdAmount(amt);\n\t}\n\n\tgetProgress() {\n\t\tconst _ = this;\n\t\tconst converted = _.#converted();\n\t\treturn {\n\t\t\tcurrentAmount: _.#current,\n\t\t\tthresholdAmount: _.#threshold,\n\t\t\tconvertedThreshold: converted,\n\t\t\tminAmount: _.#threshold,\n\t\t\tremainingAmount: Math.max(0, converted - _.#current),\n\t\t\tpercent: _.#percent,\n\t\t\tisComplete: _.#current >= converted,\n\t\t\tcurrencyRate: parseFloat(window.Shopify?.currency?.rate) || 1,\n\t\t};\n\t}\n\n\tsetMessages(above = null, below = null) {\n\t\tconst _ = this;\n\t\tif (above !== null) {\n\t\t\t_.#msgAbove = above;\n\t\t\t_.setAttribute('message-above', above);\n\t\t}\n\t\tif (below !== null) {\n\t\t\t_.#msgBelow = below;\n\t\t\t_.setAttribute('message-below', below);\n\t\t}\n\t\t_.#updateMessages();\n\t}\n\n\t// Getters (with backwards compatibility)\n\tget currentAmount() { return this.#current; }\n\tget thresholdAmount() { return this.#threshold; }\n\tget minAmount() { return this.#threshold; }\n\tget percent() { return this.#percent; }\n\tget isComplete() { return this.#current >= this.#converted(); }\n}\n\n// Define CartProgressBar custom element\ncustomElements.define('cart-progress-bar', CartProgressBar);\n\n// Export components for external use\nexport { CartProgressBar, ProgressBar };\n"],"names":[],"mappings":"AAEA;AACA;AACA;AACA,MAAM,WAAW,SAAS,WAAW,CAAC;AACtC,CAAC,WAAW,GAAG;AACf,EAAE,KAAK,EAAE,CAAC;AACV,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC;AACjB,EAAE,CAAC,CAAC,YAAY,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;AACxC,EAAE,CAAC,CAAC,YAAY,CAAC,eAAe,EAAE,GAAG,CAAC,CAAC;AACvC,EAAE,CAAC,CAAC,YAAY,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;AACzC,EAAE,CAAC,CAAC,YAAY,CAAC,eAAe,EAAE,GAAG,CAAC,CAAC;AACvC,EAAE,CAAC,CAAC,SAAS,GAAG,uCAAuC,CAAC;AACxD,EAAE;AACF;AACA,CAAC,UAAU,CAAC,OAAO,EAAE;AACrB,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;AAChD,EAAE,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,yBAAyB,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7D,EAAE,IAAI,CAAC,YAAY,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC;AACxC,EAAE;AACF,CAAC;AACD;AACA;AACA,cAAc,CAAC,MAAM,CAAC,cAAc,EAAE,WAAW,CAAC,CAAC;AACnD;AACA;AACA;AACA;AACA,MAAM,eAAe,SAAS,WAAW,CAAC;AAC1C;AACA,CAAC,UAAU,GAAG,CAAC,CAAC;AAChB,CAAC,QAAQ,GAAG,CAAC,CAAC;AACd,CAAC,QAAQ,GAAG,CAAC,CAAC;AACd,CAAC,SAAS,GAAG,EAAE,CAAC;AAChB,CAAC,SAAS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AAI,KAAK,KAAK,IAAI,EAAE;AACtB,GAAG,CAAC,CAAC,SAAS,GAAG,KAAK,CAAC;AACvB,GAAG,CAAC,CAAC,YAAY,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;AAC1C,GAAG;AACH,EAAE,IAAI,KAAK,KAAK,IAAI,EAAE;AACtB,GAAG,CAAC,CAAC,SAAS,GAAG,KAAK,CAAC;AACvB,GAAG,CAAC,CAAC,YAAY,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;AAC1C,GAAG;AACH,EAAE,CAAC,CAAC,eAAe,EAAE,CAAC;AACtB,EAAE;AACF;AACA;AACA,CAAC,IAAI,aAAa,GAAG,EAAE,OAAO,IAAI,CAAC,QAAQ,CAAC,EAAE;AAC9C,CAAC,IAAI,eAAe,GAAG,EAAE,OAAO,IAAI,CAAC,UAAU,CAAC,EAAE;AAClD,CAAC,IAAI,SAAS,GAAG,EAAE,OAAO,IAAI,CAAC,UAAU,CAAC,EAAE;AAC5C,CAAC,IAAI,OAAO,GAAG,EAAE,OAAO,IAAI,CAAC,QAAQ,CAAC,EAAE;AACxC,CAAC,IAAI,UAAU,GAAG,EAAE,OAAO,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC,EAAE;AAChE,CAAC;AACD;AACA;AACA,cAAc,CAAC,MAAM,CAAC,mBAAmB,EAAE,eAAe,CAAC;;;;"}
@@ -10,23 +10,18 @@
10
10
  class ProgressBar extends HTMLElement {
11
11
  constructor() {
12
12
  super();
13
- this.#setupProgressBar();
14
- }
15
-
16
- #setupProgressBar() {
17
- this.setAttribute('role', 'progressbar');
18
- this.setAttribute('aria-valuemin', '0');
19
- this.setAttribute('aria-valuemax', '100');
20
- this.setAttribute('aria-valuenow', '0');
21
-
22
- // Create the visual progress bar
23
- this.innerHTML = '<div class="progress-bar-fill"></div>';
13
+ const _ = this;
14
+ _.setAttribute('role', 'progressbar');
15
+ _.setAttribute('aria-valuemin', '0');
16
+ _.setAttribute('aria-valuemax', '100');
17
+ _.setAttribute('aria-valuenow', '0');
18
+ _.innerHTML = '<div class="progress-bar-fill"></div>';
24
19
  }
25
20
 
26
21
  setPercent(percent) {
27
- const clampedPercent = Math.max(0, Math.min(100, percent));
28
- this.style.setProperty('--cart-progress-percent', `${clampedPercent}%`);
29
- this.setAttribute('aria-valuenow', clampedPercent);
22
+ const p = Math.max(0, Math.min(100, percent));
23
+ this.style.setProperty('--cart-progress-percent', `${p}%`);
24
+ this.setAttribute('aria-valuenow', p);
30
25
  }
31
26
  }
32
27
 
@@ -38,303 +33,230 @@
38
33
  */
39
34
  class CartProgressBar extends HTMLElement {
40
35
  // Private fields
41
- #minAmount = 0;
42
- #currentAmount = 0;
43
- #progressPercent = 0;
44
- #originalAboveMessage = '';
45
- #originalBelowMessage = '';
46
- #progressBar = null;
47
- #messageElement = null;
48
-
49
- /**
50
- * Define which attributes should be observed for changes
51
- */
36
+ #threshold = 0;
37
+ #current = 0;
38
+ #percent = 0;
39
+ #msgAbove = '';
40
+ #msgBelow = '';
41
+ #bar = null;
42
+ #msgEl = null;
43
+ #moneyFmt = null;
44
+ #debounce = null;
45
+
52
46
  static get observedAttributes() {
53
- return ['threshold', 'current', 'message-above', 'message-below'];
47
+ return ['threshold', 'current', 'message-above', 'message-below', 'money-format'];
54
48
  }
55
49
 
56
50
  constructor() {
57
51
  super();
58
- this.#init();
59
- }
60
-
61
- #init() {
62
- // Read initial attributes
63
- this.#minAmount = parseFloat(this.getAttribute('threshold')) || 0;
64
- this.#currentAmount = parseFloat(this.getAttribute('current')) || 0;
65
-
66
- // Store original message templates
67
- this.#originalAboveMessage = this.getAttribute('message-above') || '';
68
- this.#originalBelowMessage = this.getAttribute('message-below') || '';
52
+ const _ = this;
53
+ _.#threshold = parseFloat(_.getAttribute('threshold')) || 0;
54
+ _.#current = parseFloat(_.getAttribute('current')) || 0;
55
+ _.#msgAbove = _.getAttribute('message-above') || '';
56
+ _.#msgBelow = _.getAttribute('message-below') || '';
57
+ _.#moneyFmt = _.getAttribute('money-format');
69
58
  }
70
59
 
71
60
  async connectedCallback() {
72
- // ensure the child custom element has been registered
73
61
  await customElements.whenDefined('progress-bar');
74
-
75
- if (!customElements.get('progress-bar')) {
76
- throw new Error('<progress-bar> must be registered before <cart-progress-bar> runs');
77
- }
78
-
79
62
  this.#render();
80
63
  this.#updateProgress();
81
64
  this.#attachListeners();
82
65
  }
83
66
 
84
- attributeChangedCallback(name, oldValue, newValue) {
85
- if (oldValue === newValue) return;
67
+ disconnectedCallback() {
68
+ if (this.#debounce) clearTimeout(this.#debounce);
69
+ }
70
+
71
+ attributeChangedCallback(name, oldVal, newVal) {
72
+ if (oldVal === newVal) return;
73
+ const _ = this;
86
74
 
87
75
  switch (name) {
88
76
  case 'threshold':
89
- this.#minAmount = parseFloat(newValue) || 0;
90
- this.#updateProgress();
77
+ _.#threshold = parseFloat(newVal) || 0;
78
+ _.#updateProgress();
91
79
  break;
92
80
  case 'current':
93
- this.#currentAmount = parseFloat(newValue) || 0;
94
- this.#updateProgress();
81
+ _.#current = parseFloat(newVal) || 0;
82
+ _.#updateProgress();
95
83
  break;
96
84
  case 'message-above':
97
- this.#originalAboveMessage = newValue || '';
98
- this.#updateMessages();
85
+ _.#msgAbove = newVal || '';
86
+ _.#updateMessages();
99
87
  break;
100
88
  case 'message-below':
101
- this.#originalBelowMessage = newValue || '';
102
- this.#updateMessages();
89
+ _.#msgBelow = newVal || '';
90
+ _.#updateMessages();
91
+ break;
92
+ case 'money-format':
93
+ _.#moneyFmt = newVal;
94
+ _.#updateMessages();
103
95
  break;
104
96
  }
105
97
  }
106
98
 
107
99
  #render() {
108
- // Find or create the message element
109
- this.#messageElement =
110
- this.querySelector('[data-content-cart-progress-message]') ||
111
- this.querySelector('p[data-content-cart-progress-message]');
112
-
113
- // Find existing progress bar (user can add their own)
114
- this.#progressBar = this.querySelector('progress-bar');
115
-
116
- // Create message element if it doesn't exist but we have message templates
117
- if (!this.#messageElement && (this.#originalAboveMessage || this.#originalBelowMessage)) {
118
- this.#messageElement = document.createElement('p');
119
- this.#messageElement.setAttribute('data-content-cart-progress-message', '');
120
- this.appendChild(this.#messageElement);
100
+ const _ = this;
101
+ // Find existing elements or create them
102
+ _.#msgEl = _.querySelector('[data-content-cart-progress-message]');
103
+ _.#bar = _.querySelector('progress-bar');
104
+
105
+ // Create message element if needed
106
+ if (!_.#msgEl && (_.#msgAbove || _.#msgBelow)) {
107
+ _.#msgEl = document.createElement('p');
108
+ _.#msgEl.setAttribute('data-content-cart-progress-message', '');
109
+ _.appendChild(_.#msgEl);
121
110
  }
122
111
 
123
- // Create progress bar if it doesn't exist - add it at the end (below text message)
124
- if (!this.#progressBar) {
125
- // Ensure progress-bar is defined before creating
126
- if (customElements.get('progress-bar')) {
127
- this.#progressBar = document.createElement('progress-bar');
128
- this.appendChild(this.#progressBar);
129
- } else {
130
- // Fallback: wait for definition
131
- customElements.whenDefined('progress-bar').then(() => {
132
- if (!this.#progressBar) {
133
- this.#progressBar = document.createElement('progress-bar');
134
- this.appendChild(this.#progressBar);
135
- this.#updateProgress(); // Update progress after creating
136
- }
137
- });
138
- }
112
+ // Create progress bar if needed
113
+ if (!_.#bar) {
114
+ _.#bar = document.createElement('progress-bar');
115
+ _.appendChild(_.#bar);
139
116
  }
140
117
  }
141
118
 
142
119
  #updateProgress() {
143
- if (this.#minAmount === 0) {
144
- this.#progressPercent = 100;
145
- } else {
146
- this.#progressPercent = Math.min(100, (this.#currentAmount / this.#minAmount) * 100);
147
- }
148
-
149
- // Update progress bar
150
- if (this.#progressBar) {
151
- this.#progressBar.setPercent(this.#progressPercent);
152
- }
153
-
154
- // Update messages
155
- this.#updateMessages();
156
-
157
- // Update component state
158
- this.#updateComponentState();
120
+ const _ = this;
121
+ const converted = _.#converted();
122
+ _.#percent = converted === 0 ? 100 : Math.min(100, (_.#current / converted) * 100);
123
+ if (_.#bar) _.#bar.setPercent(_.#percent);
124
+ _.#updateState(converted);
159
125
  }
160
126
 
161
127
  #attachListeners() {
162
- // Find the nearest cart-panel component
163
- const cartDialog = this.closest('cart-dialog');
164
-
165
- if (cartDialog) {
166
- // Listen for cart data changes
167
- cartDialog.addEventListener('cart-dialog:data-changed', (event) => {
168
- this.#handleCartDataChange(event);
169
- });
128
+ const _ = this;
129
+ const panel = _.closest('cart-panel');
130
+ if (panel) {
131
+ panel.addEventListener('cart-panel:data-changed', (e) => _.#onCartChange(e));
170
132
  }
171
133
  }
172
134
 
173
- #handleCartDataChange(event) {
174
- const updatedCart = event.detail;
175
-
176
- if (updatedCart) {
177
- // Use calculated_subtotal if available (handles _ignore_price_in_subtotal logic)
178
- // Otherwise fall back to total_price for backwards compatibility
179
- let currentAmount = 0;
180
-
181
- if (typeof updatedCart.calculated_subtotal !== 'undefined') {
182
- // calculated_subtotal is already in dollars, no conversion needed
183
- currentAmount = updatedCart.calculated_subtotal / 100;
184
- } else if (typeof updatedCart.total_price !== 'undefined') {
185
- // Convert from cents to dollars if needed (Shopify typically returns cents)
186
- currentAmount = updatedCart.total_price / 100;
187
- }
188
-
189
- this.setCurrentAmount(currentAmount);
190
- }
135
+ #onCartChange(event) {
136
+ const _ = this;
137
+ const cart = event.detail;
138
+ if (!cart) return;
139
+
140
+ if (_.#debounce) clearTimeout(_.#debounce);
141
+ _.#debounce = setTimeout(() => {
142
+ _.#debounce = null;
143
+ // Use calculated_subtotal if available, else total_price (both in cents)
144
+ const amt = (cart.calculated_subtotal ?? cart.total_price ?? 0) / 100;
145
+ _.setCurrentAmount(amt);
146
+ }, 100);
191
147
  }
192
148
 
193
- #updateMessages() {
194
- const isComplete = this.#currentAmount >= this.#minAmount;
195
- const remainingAmount = Math.max(0, this.#minAmount - this.#currentAmount);
196
-
197
- // Format remaining amount as currency (assuming USD for now)
198
- const formattedAmount = this.#formatCurrency(remainingAmount);
199
-
200
- // Update the single message element
201
- if (this.#messageElement) {
202
- let messageTemplate;
203
-
204
- if (isComplete && this.#originalAboveMessage) {
205
- // Show success message when complete
206
- messageTemplate = this.#originalAboveMessage;
207
- } else if (!isComplete) {
208
- // Show progress message when incomplete
209
- messageTemplate = this.#originalBelowMessage || this.#originalAboveMessage;
210
- }
211
-
212
- if (messageTemplate) {
213
- const message = messageTemplate.replace('{ amount }', formattedAmount);
214
- this.#messageElement.textContent = message;
215
- this.#messageElement.style.display = 'block';
149
+ #updateState(converted) {
150
+ const _ = this;
151
+ const complete = _.#current >= converted;
152
+ const remaining = Math.max(0, converted - _.#current);
153
+ const formatted = _.#fmtMoney(remaining);
154
+
155
+ // Update message
156
+ if (_.#msgEl) {
157
+ let tpl = complete ? _.#msgAbove : (_.#msgBelow || _.#msgAbove);
158
+ if (tpl) {
159
+ _.#msgEl.textContent = tpl.replace(/\[\s*amount\s*\]/g, formatted);
160
+ _.#msgEl.style.display = 'block';
216
161
  } else {
217
- // Hide message if no template available for current state
218
- this.#messageElement.style.display = 'none';
162
+ _.#msgEl.style.display = 'none';
219
163
  }
220
164
  }
221
- }
222
165
 
223
- #updateComponentState() {
224
- const isComplete = this.#currentAmount >= this.#minAmount;
225
- this.setAttribute('complete', isComplete.toString());
166
+ // Update complete attribute
167
+ _.setAttribute('complete', complete.toString());
226
168
  }
227
169
 
228
- #formatCurrency(amount) {
229
- // Basic currency formatting - could be enhanced with locale/currency options
230
- return new Intl.NumberFormat('en-US', {
231
- style: 'currency',
232
- currency: 'USD',
233
- minimumFractionDigits: 2,
234
- maximumFractionDigits: 2,
235
- })
236
- .format(amount)
237
- .replace('.00', '');
238
- }
170
+ #fmtMoney(amt) {
171
+ const fmt = this.#moneyFmt;
172
+ if (!fmt) return amt.toFixed(2).replace(/\.00$/, '');
239
173
 
240
- /**
241
- * Public API: Set the progress percentage directly
242
- * @param {number} percent - Progress percentage (0-100)
243
- */
244
- setPercent(percent) {
245
- const clampedPercent = Math.max(0, Math.min(100, percent));
246
- this.#progressPercent = clampedPercent;
174
+ const fixed = amt.toFixed(2);
175
+ const noDecimals = Math.round(amt).toString();
176
+ const withComma = fixed.replace('.', ',');
177
+ const noDecWithComma = noDecimals.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
247
178
 
248
- if (this.#progressBar) {
249
- this.#progressBar.setPercent(clampedPercent);
250
- }
179
+ return fmt
180
+ .replace(/\{\{\s*amount_no_decimals_with_comma_separator\s*\}\}/g, noDecWithComma)
181
+ .replace(/\{\{\s*amount_with_comma_separator\s*\}\}/g, withComma)
182
+ .replace(/\{\{\s*amount_no_decimals\s*\}\}/g, noDecimals)
183
+ .replace(/\{\{\s*amount\s*\}\}/g, fixed);
184
+ }
185
+
186
+ #converted() {
187
+ const rate = parseFloat(window.Shopify?.currency?.rate) || 1;
188
+ return this.#threshold * rate;
189
+ }
251
190
 
252
- // Calculate current amount based on percentage
253
- this.#currentAmount = (clampedPercent / 100) * this.#minAmount;
254
- this.setAttribute('current', this.#currentAmount.toString());
191
+ #updateMessages() {
192
+ this.#updateState(this.#converted());
193
+ }
255
194
 
256
- this.#updateMessages();
257
- this.#updateComponentState();
195
+ // Public API
196
+ setPercent(pct) {
197
+ const _ = this;
198
+ const p = Math.max(0, Math.min(100, pct));
199
+ _.#percent = p;
200
+ if (_.#bar) _.#bar.setPercent(p);
201
+ // Use converted threshold for multi-currency consistency
202
+ _.#current = (p / 100) * _.#converted();
203
+ _.setAttribute('current', _.#current.toString());
204
+ _.#updateState(_.#converted());
258
205
  }
259
206
 
260
- /**
261
- * Public API: Set the current cart amount
262
- * @param {number} amount - Current cart amount
263
- */
264
- setCurrentAmount(amount) {
265
- this.#currentAmount = parseFloat(amount) || 0;
266
- this.setAttribute('current', this.#currentAmount.toString());
267
- this.#updateProgress();
207
+ setCurrentAmount(amt) {
208
+ const _ = this;
209
+ _.#current = parseFloat(amt) || 0;
210
+ _.setAttribute('current', _.#current.toString());
211
+ _.#updateProgress();
268
212
  }
269
213
 
270
- /**
271
- * Public API: Set the threshold amount for free shipping
272
- * @param {number} amount - Threshold amount for free shipping
273
- */
274
- setThresholdAmount(amount) {
275
- this.#minAmount = parseFloat(amount) || 0;
276
- this.setAttribute('threshold', this.#minAmount.toString());
277
- this.#updateProgress();
214
+ setThresholdAmount(amt) {
215
+ const _ = this;
216
+ _.#threshold = parseFloat(amt) || 0;
217
+ _.setAttribute('threshold', _.#threshold.toString());
218
+ _.#updateProgress();
278
219
  }
279
220
 
280
- /**
281
- * Public API: Set the minimum amount for free shipping (deprecated - use setThresholdAmount)
282
- * @param {number} amount - Minimum amount threshold
283
- * @deprecated Use setThresholdAmount instead
284
- */
285
- setMinAmount(amount) {
286
- this.setThresholdAmount(amount);
221
+ /** @deprecated Use setThresholdAmount */
222
+ setMinAmount(amt) {
223
+ this.setThresholdAmount(amt);
287
224
  }
288
225
 
289
- /**
290
- * Public API: Get current progress information
291
- */
292
226
  getProgress() {
227
+ const _ = this;
228
+ const converted = _.#converted();
293
229
  return {
294
- currentAmount: this.#currentAmount,
295
- thresholdAmount: this.#minAmount,
296
- minAmount: this.#minAmount, // backwards compatibility
297
- remainingAmount: Math.max(0, this.#minAmount - this.#currentAmount),
298
- percent: this.#progressPercent,
299
- isComplete: this.#currentAmount >= this.#minAmount,
230
+ currentAmount: _.#current,
231
+ thresholdAmount: _.#threshold,
232
+ convertedThreshold: converted,
233
+ minAmount: _.#threshold,
234
+ remainingAmount: Math.max(0, converted - _.#current),
235
+ percent: _.#percent,
236
+ isComplete: _.#current >= converted,
237
+ currencyRate: parseFloat(window.Shopify?.currency?.rate) || 1,
300
238
  };
301
239
  }
302
240
 
303
- /**
304
- * Public API: Update message templates
305
- * @param {string} aboveMessage - Message template for above the bar
306
- * @param {string} belowMessage - Message template for below the bar
307
- */
308
- setMessages(aboveMessage = null, belowMessage = null) {
309
- if (aboveMessage !== null) {
310
- this.#originalAboveMessage = aboveMessage;
311
- this.setAttribute('message-above', aboveMessage);
241
+ setMessages(above = null, below = null) {
242
+ const _ = this;
243
+ if (above !== null) {
244
+ _.#msgAbove = above;
245
+ _.setAttribute('message-above', above);
312
246
  }
313
-
314
- if (belowMessage !== null) {
315
- this.#originalBelowMessage = belowMessage;
316
- this.setAttribute('message-below', belowMessage);
247
+ if (below !== null) {
248
+ _.#msgBelow = below;
249
+ _.setAttribute('message-below', below);
317
250
  }
318
-
319
- this.#updateMessages();
251
+ _.#updateMessages();
320
252
  }
321
253
 
322
- // Getters
323
- get currentAmount() {
324
- return this.#currentAmount;
325
- }
326
- get thresholdAmount() {
327
- return this.#minAmount;
328
- }
329
- get minAmount() {
330
- return this.#minAmount;
331
- } // backwards compatibility
332
- get percent() {
333
- return this.#progressPercent;
334
- }
335
- get isComplete() {
336
- return this.#currentAmount >= this.#minAmount;
337
- }
254
+ // Getters (with backwards compatibility)
255
+ get currentAmount() { return this.#current; }
256
+ get thresholdAmount() { return this.#threshold; }
257
+ get minAmount() { return this.#threshold; }
258
+ get percent() { return this.#percent; }
259
+ get isComplete() { return this.#current >= this.#converted(); }
338
260
  }
339
261
 
340
262
  // Define CartProgressBar custom element