Skip to content

Commit 21c653e

Browse files
authored
Hide Platform Checkout iframe on browser back button (#4365)
* Hide Platform Checkout iframe on browser back button * Add changelog entry
1 parent ced2237 commit 21c653e

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fix
3+
4+
Hide Platform Checkout iframe on browser back button.

client/checkout/platform-checkout/email-input-iframe.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,16 @@ export const handlePlatformCheckoutEmailInput = ( field, api ) => {
164164
iframeWrapper.insertBefore( iframeArrow, null );
165165
iframeWrapper.insertBefore( iframe, null );
166166

167-
const closeIframe = () => {
167+
const closeIframe = ( focus = true ) => {
168168
window.removeEventListener( 'resize', getWindowSize );
169169
window.removeEventListener( 'resize', setPopoverPosition );
170170

171171
iframeWrapper.remove();
172172
iframe.classList.remove( 'open' );
173-
platformCheckoutEmailInput.focus();
173+
174+
if ( focus ) {
175+
platformCheckoutEmailInput.focus();
176+
}
174177

175178
document.body.style.overflow = '';
176179
};
@@ -259,24 +262,39 @@ export const handlePlatformCheckoutEmailInput = ( field, api ) => {
259262

260263
// Prevent show platform checkout iframe if the page comes from
261264
// the back button on platform checkout itself.
262-
const searchParams = new URLSearchParams( window.location.search );
265+
window.addEventListener( 'pageshow', function ( event ) {
266+
// Detect browser back button.
267+
const historyTraversal =
268+
event.persisted ||
269+
( 'undefined' !== typeof performance &&
270+
'back_forward' ===
271+
performance.getEntriesByType( 'navigation' )[ 0 ].type );
263272

264-
if ( 'true' !== searchParams.get( 'skip_platform_checkout' ) ) {
265-
// Check the initial value of the email input and trigger input validation.
266-
if ( validateEmail( platformCheckoutEmailInput.value ) ) {
267-
platformCheckoutLocateUser( platformCheckoutEmailInput.value );
268-
}
269-
} else {
270-
searchParams.delete( 'skip_platform_checkout' );
273+
const searchParams = new URLSearchParams( window.location.search );
274+
275+
if (
276+
! historyTraversal &&
277+
'true' !== searchParams.get( 'skip_platform_checkout' )
278+
) {
279+
// Check the initial value of the email input and trigger input validation.
280+
if ( validateEmail( platformCheckoutEmailInput.value ) ) {
281+
platformCheckoutLocateUser( platformCheckoutEmailInput.value );
282+
}
283+
} else {
284+
searchParams.delete( 'skip_platform_checkout' );
271285

272-
let { pathname } = window.location;
286+
let { pathname } = window.location;
273287

274-
if ( '' !== searchParams.toString() ) {
275-
pathname += '?' + searchParams.toString();
276-
}
288+
if ( '' !== searchParams.toString() ) {
289+
pathname += '?' + searchParams.toString();
290+
}
277291

278-
history.replaceState( null, null, pathname );
279-
}
292+
history.replaceState( null, null, pathname );
293+
294+
// Safari needs to close iframe with this.
295+
closeIframe( false );
296+
}
297+
} );
280298

281299
platformCheckoutEmailInput.addEventListener( 'input', ( e ) => {
282300
const email = e.currentTarget.value;

0 commit comments

Comments
 (0)