Skip to content

Commit

Permalink
fix(ras-acc): hide iframe until loading is complete
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Sep 3, 2024
1 parent ba88a69 commit 443b352
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions src/modal-checkout/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,23 @@ domReady( () => {
const iframe = document.createElement( 'iframe' );
iframe.name = IFRAME_NAME;
iframe.style.height = initialHeight;
iframe.style.visibility = 'hidden';

const iframeResizeObserver = new ResizeObserver( entries => {
if ( ! entries || ! entries.length ) {
return;
}
const contentRect = entries[ 0 ].contentRect;
if ( contentRect ) {
const iframeHeight = contentRect.top + contentRect.bottom + 'px';
const iframeHeight = contentRect.top + contentRect.bottom;
if ( iframeHeight === 0 ) {
// If height is 0, hide iframe content instead of resizing to avoid layout shift.
iframe.style.visibility = 'hidden';
return;
}
// Match iframe and modal content heights to avoid inner iframe scollbar.
modalContent.style.height = iframeHeight;
iframe.style.height = iframeHeight;
modalContent.style.height = iframeHeight + 'px';
iframe.style.height = iframeHeight + 'px';
}
} );
const closeCheckout = () => {
Expand All @@ -64,6 +70,7 @@ domReady( () => {
// Reset iframe and modal content heights.
iframe.src = 'about:blank';
iframe.style.height = initialHeight;
iframe.style.visibility = 'hidden';
modalContent.style.height = initialHeight;
modalContent.removeChild( iframe );
}
Expand Down Expand Up @@ -105,7 +112,7 @@ domReady( () => {

// Ensure we always reset the modal title and width once the modal closes.
if ( shouldCloseModal ) {
setModalWidth();
setModalSize();
setModalTitle( newspackBlocksModal.labels.checkout_modal_title );
}
} else {
Expand Down Expand Up @@ -157,11 +164,11 @@ domReady( () => {
};

/**
* Sets the width of the modal.
* Sets the size of the modal.
*
* @param {string} size Options are 'small' or 'default'. Default is 'default'.
*/
const setModalWidth = ( size = 'default' ) => {
const setModalSize = ( size = 'default' ) => {
const modal = modalCheckout.querySelector( `.${ MODAL_CLASS_PREFIX }` );
if ( ! modal ) {
return;
Expand Down Expand Up @@ -446,29 +453,36 @@ domReady( () => {
}
}
const container = iframe?.contentDocument?.querySelector( `#${ IFRAME_CONTAINER_ID }` );
if ( container ) {
const setModalReady = () => {
iframeResizeObserver.observe( container );
if ( spinner.style.display !== 'none' ) {
spinner.style.display = 'none';
}
if ( iframe.style.visibility !== 'visible' ) {
iframe.style.visibility = 'visible';
}
}
if ( container ) {
if ( container.checkoutComplete ) {
// Update the modal title and width to reflect successful transaction.
setModalWidth( 'small' );
setModalSize( 'small' );
setModalTitle( newspackBlocksModal.labels.thankyou_modal_title );
setModalReady();
a11y.trapFocus( modalCheckout.querySelector( `.${ MODAL_CLASS_PREFIX }` ) );
} else {
// Revert modal title and width default value.
setModalWidth();
setModalSize();
setModalTitle( newspackBlocksModal.labels.checkout_modal_title );
}
if ( container.checkoutReady ) {
spinner.style.display = 'none';
setModalReady();
} else {
container.addEventListener( 'checkout-ready', () => {
spinner.style.display = 'none';
} );
container.addEventListener( 'checkout-ready', () => setModalReady() );
}
// Make sure the iframe has actually loaded something, even if not the expected container.
// This check prevents an issue in Chrome where the 'load' event fired twice and the spinner was hidden too soon.
} else if ( 'about:blank' !== location.href ) {
// Make sure the iframe has actually loaded something, even if not the expected container.
// This check prevents an issue in Chrome where the 'load' event fired twice and the spinner was hidden too soon.
spinner.style.display = 'none';
setModalReady();
}
} );

Expand Down

0 comments on commit 443b352

Please sign in to comment.