2021-09-21 12:20:42 +00:00
/ * *
* External dependencies
* /
2021-10-19 14:29:12 +00:00
import { __ } from '@wordpress/i18n' ;
2021-10-25 15:31:22 +00:00
import classnames from 'classnames' ;
2021-10-19 14:29:12 +00:00
import { useState , useEffect } from '@wordpress/element' ;
import Button from '@woocommerce/base-components/button' ;
import { CHECKOUT_URL } from '@woocommerce/block-settings' ;
import { useCheckoutContext } from '@woocommerce/base-context' ;
import { usePositionRelativeToViewport } from '@woocommerce/base-hooks' ;
2021-09-21 12:20:42 +00:00
import { getSetting } from '@woocommerce/settings' ;
/ * *
* Internal dependencies
* /
2021-10-19 14:29:12 +00:00
import './style.scss' ;
2021-09-21 12:20:42 +00:00
2021-10-19 14:29:12 +00:00
/ * *
* Checkout button rendered in the full cart page .
* /
2021-09-21 12:20:42 +00:00
const Block = ( {
checkoutPageId ,
2021-10-25 15:31:22 +00:00
className ,
2021-09-21 12:20:42 +00:00
} : {
checkoutPageId : number ;
2021-10-25 15:31:22 +00:00
className : string ;
2021-09-21 12:20:42 +00:00
} ) : JSX . Element = > {
2021-10-19 14:29:12 +00:00
const link = getSetting ( 'page-' + checkoutPageId , false ) ;
const { isCalculating } = useCheckoutContext ( ) ;
const [
positionReferenceElement ,
positionRelativeToViewport ,
] = usePositionRelativeToViewport ( ) ;
const [ showSpinner , setShowSpinner ] = useState ( false ) ;
useEffect ( ( ) = > {
// Add a listener to remove the spinner on the checkout button, so the saved page snapshot does not
// contain the spinner class. See https://archive.is/lOEW0 for why this is needed for Safari.
if (
typeof global . addEventListener !== 'function' ||
typeof global . removeEventListener !== 'function'
) {
return ;
}
const hideSpinner = ( ) = > {
setShowSpinner ( false ) ;
} ;
global . addEventListener ( 'pageshow' , hideSpinner ) ;
return ( ) = > {
global . removeEventListener ( 'pageshow' , hideSpinner ) ;
} ;
} , [ ] ) ;
const submitContainerContents = (
< Button
className = "wc-block-cart__submit-button"
href = { link || CHECKOUT_URL }
disabled = { isCalculating }
onClick = { ( ) = > setShowSpinner ( true ) }
showSpinner = { showSpinner }
>
{ __ ( 'Proceed to Checkout' , 'woo-gutenberg-products-block' ) }
< / Button >
) ;
2021-09-21 12:20:42 +00:00
return (
2021-10-25 15:31:22 +00:00
< div className = { classnames ( 'wc-block-cart__submit' , className ) } >
2021-10-19 14:29:12 +00:00
{ positionReferenceElement }
{ /* The non-sticky container must always be visible because it gives height to its parent, which is required to calculate when it becomes visible in the viewport. */ }
< div className = "wc-block-cart__submit-container" >
{ submitContainerContents }
< / div >
{ /* If the positionReferenceElement is below the viewport, display the sticky container. */ }
{ positionRelativeToViewport === 'below' && (
< div className = "wc-block-cart__submit-container wc-block-cart__submit-container--sticky" >
{ submitContainerContents }
< / div >
) }
< / div >
2021-09-21 12:20:42 +00:00
) ;
} ;
export default Block ;