Skip to content

Commit

Permalink
Dedupe quick add modal option value ids (Shopify#3504)
Browse files Browse the repository at this point in the history
* Deduplicate the option value IDs after option value selection changes in quick add modal
  • Loading branch information
lhoffbeck authored and Nathan Scheele committed Jul 1, 2024
1 parent 6a74d7e commit 6af2411
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
19 changes: 3 additions & 16 deletions assets/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,14 @@ class SectionId {
}

class HTMLUpdateUtility {
#preProcessCallbacks = [];
#postProcessCallbacks = [];

constructor() {}

addPreProcessCallback(callback) {
this.#preProcessCallbacks.push(callback);
}

addPostProcessCallback(callback) {
this.#postProcessCallbacks.push(callback);
}

/**
* Used to swap an HTML node with a new node.
* The new node is inserted as a previous sibling to the old node, the old node is hidden, and then the old node is removed.
*
* The function currently uses a double buffer approach, but this should be replaced by a view transition once it is more widely supported https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API
*/
viewTransition(oldNode, newContent) {
this.#preProcessCallbacks.forEach((callback) => callback(newContent));
static viewTransition(oldNode, newContent, preProcessCallbacks = [], postProcessCallbacks = []) {
preProcessCallbacks?.forEach((callback) => callback(newContent));

const newNodeWrapper = document.createElement('div');
HTMLUpdateUtility.setInnerHTML(newNodeWrapper, newContent.outerHTML);
Expand All @@ -62,7 +49,7 @@ class HTMLUpdateUtility {
oldNode.parentNode.insertBefore(newNode, oldNode);
oldNode.style.display = 'none';

this.#postProcessCallbacks.forEach((callback) => callback(newNode));
postProcessCallbacks?.forEach((callback) => callback(newNode));

setTimeout(() => oldNode.remove(), 500);
}
Expand Down
32 changes: 24 additions & 8 deletions assets/product-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ if (!customElements.get('product-info')) {
quantityForm = undefined;
onVariantChangeUnsubscriber = undefined;
cartUpdateUnsubscriber = undefined;
swapProductUtility = undefined;
abortController = undefined;
pendingRequestUrl = null;
preProcessHtmlCallbacks = [];
postProcessHtmlCallbacks = [];

constructor() {
super();
Expand All @@ -29,7 +30,7 @@ if (!customElements.get('product-info')) {
}

addPreProcessCallback(callback) {
this.swapProductUtility.addPreProcessCallback(callback);
this.preProcessHtmlCallbacks.push(callback);
}

initQuantityHandlers() {
Expand All @@ -50,11 +51,10 @@ if (!customElements.get('product-info')) {
}

initializeProductSwapUtility() {
this.swapProductUtility = new HTMLUpdateUtility();
this.swapProductUtility.addPreProcessCallback((html) =>
this.preProcessHtmlCallbacks.push((html) =>
html.querySelectorAll('.scroll-trigger').forEach((element) => element.classList.add('scroll-trigger--cancel'))
);
this.swapProductUtility.addPostProcessCallback((newNode) => {
this.postProcessHtmlCallbacks.push((newNode) => {
window?.Shopify?.PaymentButton?.init();
window?.ProductModel?.loadShopifyXR();
publish(PUB_SUB_EVENTS.sectionRefreshed, {
Expand Down Expand Up @@ -106,11 +106,21 @@ if (!customElements.get('product-info')) {
// If we are in an embedded context (quick add, featured product, etc), only swap product info.
// Otherwise, refresh the entire page content and sibling sections.
if (this.dataset.updateUrl === 'false') {
this.swapProductUtility.viewTransition(this, html.querySelector('product-info'));
HTMLUpdateUtility.viewTransition(
this,
html.querySelector('product-info'),
this.preProcessHtmlCallbacks,
this.postProcessHtmlCallbacks
);
} else {
document.querySelector('head title').innerHTML = html.querySelector('head title').innerHTML;

this.swapProductUtility.viewTransition(document.querySelector('main'), html.querySelector('main'));
HTMLUpdateUtility.viewTransition(
document.querySelector('main'),
html.querySelector('main'),
this.preProcessHtmlCallbacks,
this.postProcessHtmlCallbacks
);
}
};
}
Expand Down Expand Up @@ -159,7 +169,13 @@ if (!customElements.get('product-info')) {

updateOptionValues(html) {
const variantSelects = html.querySelector('variant-selects');
if (variantSelects) this.variantSelectors.innerHTML = variantSelects.innerHTML;
if (variantSelects) {
HTMLUpdateUtility.viewTransition(
this.variantSelectors,
variantSelects,
this.preProcessHtmlCallbacks,
);
}
}

handleUpdateProductInfo(productUrl) {
Expand Down
2 changes: 1 addition & 1 deletion assets/quick-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ if (!customElements.get('quick-add-modal')) {

updateImageSizes(productElement) {
const product = productElement.querySelector('.product');
const desktopColumns = product.classList.contains('product--columns');
const desktopColumns = product?.classList.contains('product--columns');
if (!desktopColumns) return;

const mediaImages = product.querySelectorAll('.product__media img');
Expand Down

0 comments on commit 6af2411

Please sign in to comment.