Add new buttonAttributes API to style express checkout buttons coherently (#47899)

* Expose buttonAttributes to the express payment methods

* Add size and label attributes to the express checkout area

* Remove defaultHeight

* default button Label

* Remove the button label attribute

* Remove px from height

* Change large button height to 55px

* Load express checkout block with attributes

* Add toggle and borderRadius controls and remove getting border radius from the theme

* Remove extra border radius text

* Only pass buttonAttributes if toggled on

* Move express payment block attribute logic into a Provider

* Tidy up editor grid and parse attributes into context on frontend

* Add px to border-radius input

* Express payment methods not selectable

* Add a test

* lint fixes

* default button height is 48 not 4

* Add changefile(s) from automation for the following project(s): woocommerce-blocks

* Update docs

* Add tests for express payment methods

* Center images within the express payment area in the editor

* Apply the buttonAttributes to the li container in the editor regardless of showButtonStyles

* Fix style issue

* fix linting

* fix lint again

* Update manifest

* Update docs manifest

* Resize images in editor

* lint fix

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Alex Florisca 2024-08-14 11:02:58 +01:00 committed by GitHub
parent 9995b51a5a
commit 006fbc6714
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 627 additions and 31 deletions

View File

@ -139,23 +139,24 @@ The options you feed the configuration instance are the same as those for expres
A big part of the payment method integration is the interface that is exposed for payment methods to use via props when the node provided is cloned and rendered on block mount. While all the props are listed below, you can find more details about what the props reference, their types etc via the [typedefs described in this file](https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce-blocks/assets/js/types/type-defs/payment-method-interface.ts).
| Property | Type | Description | Values |
| ------------------------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `activePaymentMethod` | String | The slug of the current active payment method in the checkout. | - |
| `billing` | Object | Contains everything related to billing. | `billingAddress`, `cartTotal`, `currency`, `cartTotalItems`, `displayPricesIncludingTax`, `appliedCoupons`, `customerId` |
| `cartData` | Object | Data exposed from the cart including items, fees, and any registered extension data. Note that this data should be treated as immutable (should not be modified/mutated) or it will result in errors in your application. | `cartItems`, `cartFees`, `extensions` |
| `checkoutStatus` | Object | The current checkout status exposed as various boolean state. | `isCalculating`, `isComplete`, `isIdle`, `isProcessing` |
| Property | Type | Description | Values |
| ------------------------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `activePaymentMethod` | String | The slug of the current active payment method in the checkout. | - |
| `billing` | Object | Contains everything related to billing. | `billingAddress`, `cartTotal`, `currency`, `cartTotalItems`, `displayPricesIncludingTax`, `appliedCoupons`, `customerId` |
| `cartData` | Object | Data exposed from the cart including items, fees, and any registered extension data. Note that this data should be treated as immutable (should not be modified/mutated) or it will result in errors in your application. | `cartItems`, `cartFees`, `extensions` |
| `checkoutStatus` | Object | The current checkout status exposed as various boolean state. | `isCalculating`, `isComplete`, `isIdle`, `isProcessing` |
| `components` | Object | It exposes React components that can be implemented by your payment method for various common interface elements used by payment methods. | <!-- markdownlint-disable-line no-inline-html --><ul><li>`ValidationInputError`: a container for holding validation errors which typically you'll include after any inputs.</li><li>[`PaymentMethodLabel`](https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/e089ae17043fa525e8397d605f0f470959f2ae95/assets/js/payment-method-extensions/payment-methods/paypal/index.js#L37-L40): use this component for the payment method label, including an optional icon.</li><li>`PaymentMethodIcons`: a React component used for displaying payment method icons.</li><li>- `LoadingMask`: a wrapper component that handles displaying a loading state when the isLoading prop is true. Exposes the [LoadingMask component](https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/c9074a4941919987dbad16a80f358b960336a09d/assets/js/base/components/loading-mask/index.js)</li></ul> |
| `emitResponse` | Object | Contains some constants that can be helpful when using the event emitter. Read the _[Emitting Events](https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/e267cd96a4329a4eeef816b2ef627e113ebb72a5/docs/extensibility/checkout-flow-and-events.md#emitting-events)_ section for more details. | <!-- markdownlint-disable-line no-inline-html --><ul><li>`noticeContexts`: This is an object containing properties referencing areas where notices can be targeted in the checkout. The object has the following properties: <ul><li>`PAYMENTS`: This is a reference to the notice area in the payment methods step.</li><li>`EXPRESS_PAYMENTS`: This is a reference to the notice area in the express payment methods step.</li></ul></li><li>`responseTypes`: This is an object containing properties referencing the various response types that can be returned by observers for some event emitters. It makes it easier for autocompleting the types and avoiding typos due to human error. The types are `SUCCESS`, `FAIL`, `ERROR`. The values for these types also correspond to the [payment status types](https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/34e17c3622637dbe8b02fac47b5c9b9ebf9e3596/src/Payments/PaymentResult.php#L21) from the [checkout endpoint response from the server](https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/34e17c3622637dbe8b02fac47b5c9b9ebf9e3596/src/RestApi/StoreApi/Schemas/CheckoutSchema.php#L103-L113).</li></ul> |
| `eventRegistration` | object | Contains all the checkout event emitter registration functions. These are functions the payment method can register observers on to interact with various points in the checkout flow (see [this doc](./checkout-flow-and-events.md) for more info). | `onCheckoutValidation`, `onCheckoutSuccess`, `onCheckoutFail`, `onPaymentSetup`, `onShippingRateSuccess`, `onShippingRateFail`, `onShippingRateSelectSuccess`, `onShippingRateSelectFail` |
| `onClick` | Function | **Provided to express payment methods** that should be triggered when the payment method button is clicked (which will signal to checkout the payment method has taken over payment processing) | - |
| `onClose` | Function | **Provided to express payment methods** that should be triggered when the express payment method modal closes and control is returned to checkout. | - |
| `onSubmit` | Function | Submits the checkout and begins processing | - |
| `paymentStatus` | Object | Various payment status helpers. Note, your payment method does not have to handle setting this status client side. Checkout will handle this via the responses your payment method gives from observers registered to [checkout event emitters](./checkout-flow-and-events.md). | `isPristine`, `isStarted`, `isProcessing`, `isFinished`, `hasError`, `hasFailed`, `isSuccessful` (see below for explanation) |
| `setExpressPaymentError` | Function | Receives a string and allows express payment methods to set an error notice for the express payment area on demand. This can be necessary because some express payment method processing might happen outside of checkout events. | - |
| `shippingData` | Object | Contains all shipping related data (outside of the shipping status). | `shippingRates`, `shippingRatesLoading`, `selectedRates`, `setSelectedRates`, `isSelectingRate`, `shippingAddress`, `setShippingAddress`, and `needsShipping` |
| `eventRegistration` | object | Contains all the checkout event emitter registration functions. These are functions the payment method can register observers on to interact with various points in the checkout flow (see [this doc](./checkout-flow-and-events.md) for more info). | `onCheckoutValidation`, `onCheckoutSuccess`, `onCheckoutFail`, `onPaymentSetup`, `onShippingRateSuccess`, `onShippingRateFail`, `onShippingRateSelectSuccess`, `onShippingRateSelectFail` |
| `onClick` | Function | **Provided to express payment methods** that should be triggered when the payment method button is clicked (which will signal to checkout the payment method has taken over payment processing) | - |
| `onClose` | Function | **Provided to express payment methods** that should be triggered when the express payment method modal closes and control is returned to checkout. | - |
| `onSubmit` | Function | Submits the checkout and begins processing | - |
| `buttonAttributes` | Object | Styles set by the merchant that should be respected by all express payment buttons | `height, borderRadius, darkMode` |
| `paymentStatus` | Object | Various payment status helpers. Note, your payment method does not have to handle setting this status client side. Checkout will handle this via the responses your payment method gives from observers registered to [checkout event emitters](./checkout-flow-and-events.md). | `isPristine`, `isStarted`, `isProcessing`, `isFinished`, `hasError`, `hasFailed`, `isSuccessful`(see below for explanation) |
| `setExpressPaymentError` | Function | Receives a string and allows express payment methods to set an error notice for the express payment area on demand. This can be necessary because some express payment method processing might happen outside of checkout events. | - |
| `shippingData` | Object | Contains all shipping related data (outside of the shipping status). | `shippingRates`, `shippingRatesLoading`, `selectedRates`, `setSelectedRates`, `isSelectingRate`, `shippingAddress`, `setShippingAddress`, and `needsShipping` |
| `shippingStatus` | Object | Various shipping status helpers. | <!-- markdownlint-disable-line no-inline-html --><ul><li>`shippingErrorStatus`: an object with various error statuses that might exist for shipping</li><li>`shippingErrorTypes`: an object containing all the possible types for shipping error status</li></ul> |
| `shouldSavePayment` | Boolean | Indicates whether or not the shopper has selected to save their payment method details (for payment methods that support saved payments). True if selected, false otherwise. Defaults to false. | - |
| `shouldSavePayment` | Boolean | Indicates whether or not the shopper has selected to save their payment method details (for payment methods that support saved payments). True if selected, false otherwise. Defaults to false. | - |
- `isPristine`: This is true when the current payment status is `PRISTINE`.
- `isStarted`: This is true when the current payment status is `EXPRESS_STARTED`.
@ -167,6 +168,33 @@ A big part of the payment method integration is the interface that is exposed fo
Any registered `savedTokenComponent` node will also receive a `token` prop which includes the id for the selected saved token in case your payment method needs to use it for some internal logic. However, keep in mind, this is just the id representing this token in the database (and the value of the radio input the shopper checked), not the actual customer payment token (since processing using that usually happens on the server for security).
### Button Attributes for Express Payment Methods
This API provides a way to synchronise the look and feel of the express payment buttons for a coherent shopper experience. Express Payment Methods must prefer the values provided in the `buttonAttributes`, and use it's own configuration settings as backup when the buttons are rendered somewhere other than the Cart or Checkout block.
For example, in your button component, you would do something like this:
```js
// Get your extension specific settings and set defaults if not available
let {
theme = 'dark',
borderRadius = '4',
height = '48',
} = getButtonSettingsFromConfig();
// In a cart & checkout block context, we receive `buttonAttributes` as a prop which overwrite the extension specific settings
if ( typeof buttonAttributes !== 'undefined' ) {
height = buttonAttributes.height;
borderRadius = buttonAttributes.borderRadius;
theme = buttonAttributes.darkMode ? 'light' : 'dark';
}
...
return <button style={height: `${height}px`, borderRadius: `${borderRadius}px`} data-theme={theme} />
```
Note the `height` and `borderRadius` properties should always be respected. The `darkMode` is an optional property that may be used by an extension as a clue to help identify what colour button would be best rendered, given the user's theme.
## Server Side Integration
### Processing Payment

View File

@ -180,7 +180,7 @@
"menu_title": "Payment Method Integration",
"tags": "reference",
"edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/docs/cart-and-checkout-blocks/checkout-payment-methods/payment-method-integration.md",
"hash": "f60acaaea4a6ac4adf637bc7069c966e01db089f9dfaa937def91165a71a4255",
"hash": "dd8caf9a8f79bb0806887bb41f64f9e32da5542e1c4cabdf0057c7e17f4208b5",
"url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/docs/cart-and-checkout-blocks/checkout-payment-methods/payment-method-integration.md",
"id": "c9a763b6976ecf03aeb961577c17c31f1ac7c420",
"links": {
@ -1016,7 +1016,7 @@
"menu_title": "DOM Events",
"tags": "how-to",
"edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/docs/product-collection-block/dom-events.md",
"hash": "78bce4ab5b5e902232b5ff73fd7a7c197e4f4417a490ccb45c9a27400d003787",
"hash": "fbad20bc55cc569161e80478c0789db3c34cf35513e669554af36db1de967a26",
"url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/docs/product-collection-block/dom-events.md",
"id": "c8d247b91472740075871e6b57a9583d893ac650"
}
@ -1706,5 +1706,5 @@
"categories": []
}
],
"hash": "402a928bfa0399c09151423796f98caeb8f99a70f8a4cf009db6703ec7949468"
"hash": "7c588f43c6f82a8f916b4ab0ff8046539685db971ad9c6ccb26da9a8dfef0fa4"
}

View File

@ -15,16 +15,31 @@ import {
import { useEditorContext } from '@woocommerce/base-context';
import deprecated from '@wordpress/deprecated';
import { useDispatch, useSelect } from '@wordpress/data';
import { useCheckoutBlockContext } from '@woocommerce/blocks/checkout/context';
/**
* Internal dependencies
*/
import PaymentMethodErrorBoundary from './payment-method-error-boundary';
import { STORE_KEY as PAYMENT_STORE_KEY } from '../../../data/payment/constants';
import { useExpressCheckoutContext } from '../../checkout/inner-blocks/checkout-express-payment-block/context';
const ExpressPaymentMethods = () => {
const { isEditor } = useEditorContext();
const { hasDarkControls } = useCheckoutBlockContext();
const { showButtonStyles, buttonHeight, buttonBorderRadius } =
useExpressCheckoutContext();
// API for passing styles to express payment buttons
const buttonAttributes = showButtonStyles
? {
height: buttonHeight,
borderRadius: buttonBorderRadius,
darkMode: hasDarkControls,
}
: undefined;
const { activePaymentMethod, paymentMethodData } = useSelect(
( select ) => {
const store = select( PAYMENT_STORE_KEY );
@ -127,6 +142,20 @@ const ExpressPaymentMethods = () => {
[ __internalSetExpressPaymentError, onExpressPaymentError ]
);
// In the editor, we apply styles to the button containers to show the changes of the height and border-radius controls,
// which would be passed to the payment APIs on the front-end
const stylesForButtonContainers = isEditor
? {
height: `${ showButtonStyles ? buttonHeight : '48' }px`,
borderRadius: `${
showButtonStyles ? buttonBorderRadius : '4'
}px`,
pointerEvents: 'none',
userSelect: 'none',
ariaDisabled: true,
}
: {};
/**
* @todo Find a way to Memoize Express Payment Method Content
*
@ -142,7 +171,11 @@ const ExpressPaymentMethods = () => {
? paymentMethod.edit
: paymentMethod.content;
return isValidElement( expressPaymentMethod ) ? (
<li key={ id } id={ `express-payment-method-${ id }` }>
<li
key={ id }
id={ `express-payment-method-${ id }` }
style={ stylesForButtonContainers }
>
{ cloneElement( expressPaymentMethod, {
...paymentMethodInterface,
onClick: onExpressPaymentClick( id ),
@ -150,6 +183,7 @@ const ExpressPaymentMethods = () => {
onError: onExpressPaymentError,
setExpressPaymentError:
deprecatedSetExpressPaymentError,
buttonAttributes,
} ) }
</li>
) : null;

View File

@ -1,2 +1,2 @@
export { default as CartExpressPayment } from './cart-express-payment.js';
export { default as CheckoutExpressPayment } from './checkout-express-payment.js';
export { default as CheckoutExpressPayment } from './checkout-express-payment';

View File

@ -14,10 +14,11 @@ $border-width: 1px;
> li {
margin: 0;
width: 100%;
overflow: hidden;
> img {
width: 100%;
height: 48px;
height: auto;
}
}
}

View File

@ -0,0 +1,3 @@
// This needs to be defined in a separate file because we are mocking an import.
// The only way to do this is to define the mock and import it BEFORE the module being mocked.
export default jest.fn( () => ( { isEditor: false } ) );

View File

@ -0,0 +1,188 @@
// This is the shape of the API exposed to the express payment methods via props
// Note that this is a public API!
export const getExpectedExpressPaymentProps = ( name: string ) => ( {
activePaymentMethod: undefined,
billing: {
appliedCoupons: [],
billingAddress: {
address_1: '',
address_2: '',
city: '',
company: '',
country: '',
first_name: '',
last_name: '',
phone: '',
postcode: '',
state: '',
},
billingData: {
address_1: '',
address_2: '',
city: '',
company: '',
country: '',
first_name: '',
last_name: '',
phone: '',
postcode: '',
state: '',
},
cartTotal: {
label: 'Total',
value: 0,
},
cartTotalItems: [
{
key: 'total_items',
label: 'Subtotal:',
value: 0,
valueWithTax: 0,
},
{
key: 'total_fees',
label: 'Fees:',
value: 0,
valueWithTax: 0,
},
{
key: 'total_discount',
label: 'Discount:',
value: 0,
valueWithTax: 0,
},
{
key: 'total_tax',
label: 'Taxes:',
value: 0,
valueWithTax: 0,
},
{
key: 'total_shipping',
label: 'Shipping:',
value: 0,
valueWithTax: 0,
},
],
currency: {
code: 'USD',
decimalSeparator: '.',
minorUnit: 2,
prefix: '&#36;',
suffix: '',
symbol: '&#36;',
thousandSeparator: ',',
},
customerId: 1,
displayPricesIncludingTax: false,
},
buttonAttributes: {
borderRadius: '4',
darkMode: false,
height: '48',
},
cartData: {
cartFees: [],
cartItems: [],
extensions: {},
},
checkoutStatus: {
isCalculating: false,
isComplete: false,
isIdle: true,
isProcessing: false,
},
components: {
LoadingMask: expect.any( Function ),
PaymentMethodIcons: expect.any( Function ),
PaymentMethodLabel: expect.any( Function ),
ValidationInputError: expect.any( Function ),
},
emitResponse: {
noticeContexts: {
BILLING_ADDRESS: 'wc/checkout/billing-address',
CART: 'wc/cart',
CHECKOUT: 'wc/checkout',
CHECKOUT_ACTIONS: 'wc/checkout/checkout-actions',
CONTACT_INFORMATION: 'wc/checkout/contact-information',
EXPRESS_PAYMENTS: 'wc/checkout/express-payments',
ORDER_INFORMATION: 'wc/checkout/additional-information',
PAYMENTS: 'wc/checkout/payments',
SHIPPING_ADDRESS: 'wc/checkout/shipping-address',
SHIPPING_METHODS: 'wc/checkout/shipping-methods',
},
responseTypes: {
ERROR: 'error',
FAIL: 'failure',
SUCCESS: 'success',
},
},
eventRegistration: {
onCheckoutAfterProcessingWithError: expect.any( Function ),
onCheckoutAfterProcessingWithSuccess: expect.any( Function ),
onCheckoutBeforeProcessing: expect.any( Function ),
onCheckoutFail: expect.any( Function ),
onCheckoutSuccess: expect.any( Function ),
onCheckoutValidation: expect.any( Function ),
onCheckoutValidationBeforeProcessing: expect.any( Function ),
onPaymentProcessing: expect.any( Function ),
onPaymentSetup: expect.any( Function ),
onShippingRateFail: expect.any( Function ),
onShippingRateSelectFail: expect.any( Function ),
onShippingRateSelectSuccess: expect.any( Function ),
onShippingRateSuccess: expect.any( Function ),
},
name,
onClick: expect.any( Function ),
onClose: expect.any( Function ),
onError: expect.any( Function ),
onSubmit: expect.any( Function ),
paymentStatus: {
hasError: false,
hasFailed: false,
isDoingExpressPayment: false,
isFinished: false,
isIdle: true,
isPristine: true,
isProcessing: false,
isReady: false,
isStarted: false,
isSuccessful: false,
},
setExpressPaymentError: expect.any( Function ),
shippingData: {
isSelectingRate: false,
needsShipping: true,
selectedRates: {},
setSelectedRates: expect.any( Function ),
setShippingAddress: expect.any( Function ),
shippingAddress: {
address_1: '',
address_2: '',
city: '',
company: '',
country: '',
first_name: '',
last_name: '',
phone: '',
postcode: '',
state: '',
},
shippingRates: [],
shippingRatesLoading: false,
},
shippingStatus: {
shippingErrorStatus: {
hasError: false,
hasInvalidAddress: false,
isPristine: true,
isValid: false,
},
shippingErrorTypes: {
INVALID_ADDRESS: 'invalid_address',
NONE: 'none',
UNKNOWN: 'unknown_error',
},
},
shouldSavePayment: false,
} );

View File

@ -0,0 +1,122 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
import { PAYMENT_STORE_KEY } from '@woocommerce/block-data';
import {
registerExpressPaymentMethod,
__experimentalDeRegisterExpressPaymentMethod,
} from '@woocommerce/blocks-registry';
import { dispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import mockEditorContext from './__mocks__/editor-context';
import { getExpectedExpressPaymentProps } from './__mocks__/express-payment-props';
import ExpressPaymentMethods from '../express-payment-methods';
jest.mock( '@woocommerce/base-context', () => ( {
useEditorContext: mockEditorContext,
} ) );
const mockExpressPaymentMethodNames = [ 'paypal', 'google pay', 'apple pay' ];
const MockExpressButton = jest.fn( ( { name } ) => (
<div className="boo">{ `${ name } button` }</div>
) );
const MockEditorExpressButton = jest.fn( ( { name } ) => (
<div>{ `${ name } preview` }</div>
) );
const registerMockExpressPaymentMethods = () => {
mockExpressPaymentMethodNames.forEach( ( name ) => {
registerExpressPaymentMethod( {
name,
paymentMethodId: name,
content: <MockExpressButton name={ name } />,
edit: <MockEditorExpressButton name={ name } />,
canMakePayment: () => true,
supports: {
features: [ 'products' ],
},
} );
} );
dispatch( PAYMENT_STORE_KEY ).__internalUpdateAvailablePaymentMethods();
};
const deregisterMockExpressPaymentMethods = () => {
mockExpressPaymentMethodNames.forEach( ( name ) => {
__experimentalDeRegisterExpressPaymentMethod( name );
} );
};
describe( 'Express payment methods', () => {
afterAll( () => {
jest.restoreAllMocks();
} );
describe( 'No payment methods available', () => {
it( 'should display no registered payment methods', () => {
render( <ExpressPaymentMethods /> );
const noPaymentMethods = screen.queryAllByText(
/No registered Payment Methods/
);
expect( noPaymentMethods.length ).toEqual( 1 );
} );
} );
describe( 'Payment methods available', () => {
beforeAll( () => {
registerMockExpressPaymentMethods();
} );
afterAll( () => {
deregisterMockExpressPaymentMethods();
} );
describe( 'In a frontend context', () => {
it( 'should display the element provided by paymentMethods.content', () => {
render( <ExpressPaymentMethods /> );
mockExpressPaymentMethodNames.forEach( ( name ) => {
const btn = screen.getByText( `${ name } button` );
expect( btn ).toBeVisible();
} );
} );
it( 'should pass the correct properties to the rendered element', () => {
render( <ExpressPaymentMethods /> );
mockExpressPaymentMethodNames.forEach( ( name ) => {
expect( MockExpressButton ).toHaveBeenCalledWith(
getExpectedExpressPaymentProps( name ),
{}
);
} );
// Expect some deprecation warnings
expect( console ).toHaveWarnedWith(
'isPristine is deprecated since version 9.6.0. Please use isIdle instead. See: https://github.com/woocommerce/woocommerce-blocks/pull/8110'
);
} );
} );
describe( 'In an editor context', () => {
beforeEach( () => {
mockEditorContext.mockImplementation( () => ( {
isEditor: true,
} ) );
} );
it( 'should display the element provided by paymentMethods.edit', () => {
render( <ExpressPaymentMethods /> );
mockExpressPaymentMethodNames.forEach( ( name ) => {
const btn = screen.getByText( `${ name } preview` );
expect( btn ).toBeVisible();
} );
} );
it( 'should pass the correct properties to the rendered element', () => {
render( <ExpressPaymentMethods /> );
mockExpressPaymentMethodNames.forEach( ( name ) => {
expect( MockEditorExpressButton ).toHaveBeenCalledWith(
getExpectedExpressPaymentProps( name ),
{}
);
} );
} );
} );
} );
} );

View File

@ -32,3 +32,20 @@
display: block;
}
}
// Center images rendered in place of buttons in the editor
.wc-block-components-express-payment {
.wc-block-components-express-payment__event-buttons {
> li {
display: flex;
align-items: center;
justify-content: center;
> img {
width: 100%;
height: 100%;
object-fit: cover;
}
}
}
}

View File

@ -65,6 +65,7 @@ const Checkout = ( {
requireApartmentField,
showPhoneField,
requirePhoneField,
hasDarkControls,
showFormStepNumbers,
} = attributes;
@ -98,6 +99,7 @@ const Checkout = ( {
requireApartmentField,
showPhoneField,
requirePhoneField,
hasDarkControls,
showFormStepNumbers,
} as Attributes
}

View File

@ -18,6 +18,7 @@ export type CheckoutBlockContextProps = {
showReturnToCart: boolean;
cartPageId: number;
showRateAfterTaxName: boolean;
hasDarkControls: boolean;
showFormStepNumbers: boolean;
};
@ -38,6 +39,7 @@ export const CheckoutBlockContext: React.Context< CheckoutBlockContextProps > =
showReturnToCart: true,
cartPageId: 0,
showRateAfterTaxName: false,
hasDarkControls: false,
showFormStepNumbers: false,
} );

View File

@ -13,6 +13,18 @@
"lock": false
},
"attributes": {
"showButtonStyles": {
"type": "boolean",
"default": true
},
"buttonHeight": {
"type": "string",
"default": "48"
},
"buttonBorderRadius": {
"type": "string",
"default": "4"
},
"className": {
"type": "string",
"default": ""

View File

@ -10,7 +10,6 @@ import { CheckoutExpressPayment } from '../../../cart-checkout-shared/payment-me
const Block = ( { className }: { className?: string } ): JSX.Element | null => {
const { cartNeedsPayment } = useStoreCart();
if ( ! cartNeedsPayment ) {
return null;
}

View File

@ -0,0 +1,21 @@
/**
* External dependencies
*/
import { useContext, createContext } from '@wordpress/element';
type ExpressCheckoutContextProps = {
showButtonStyles: boolean;
buttonHeight: string;
buttonBorderRadius: string;
};
export const ExpressCheckoutContext: React.Context< ExpressCheckoutContextProps > =
createContext< ExpressCheckoutContextProps >( {
showButtonStyles: true,
buttonHeight: '48',
buttonBorderRadius: '4',
} );
export const useExpressCheckoutContext = () => {
return useContext( ExpressCheckoutContext );
};

View File

@ -1,8 +1,18 @@
/**
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import {
PanelBody,
RadioControl,
ToggleControl,
Notice,
TextControl,
} from '@wordpress/components';
import ExternalLinkCard from '@woocommerce/editor-components/external-link-card';
import { ADMIN_URL } from '@woocommerce/settings';
import { useExpressPaymentMethods } from '@woocommerce/base-context/hooks';
import { __ } from '@wordpress/i18n';
import clsx from 'clsx';
/**
@ -10,17 +20,15 @@ import clsx from 'clsx';
*/
import Block from './block';
import './editor.scss';
import { ExpressCheckoutAttributes } from './types';
import { ExpressCheckoutContext } from './context';
export const Edit = ( {
attributes,
setAttributes,
}: {
attributes: {
className?: string;
lock: {
move: boolean;
remove: boolean;
};
};
attributes: ExpressCheckoutAttributes;
setAttributes: ( attributes: Record< string, unknown > ) => undefined;
} ): JSX.Element | null => {
const { paymentMethods, isInitialized } = useExpressPaymentMethods();
const hasExpressPaymentMethods = Object.keys( paymentMethods ).length > 0;
@ -39,9 +47,88 @@ export const Edit = ( {
return null;
}
const { buttonHeight, buttonBorderRadius, showButtonStyles } = attributes;
const buttonStyleControls = (
<>
<RadioControl
label={ __( 'Button Size', 'woocommerce' ) }
selected={ buttonHeight }
options={ [
{ label: 'Small (40px)', value: '40' },
{ label: 'Medium (48px)', value: '48' },
{ label: 'Large (55px)', value: '55' },
] }
onChange={ ( newValue: string ) =>
setAttributes( { buttonHeight: newValue } )
}
/>
<div className="border-radius-control-container">
<TextControl
label={ __( 'Button Border Radius', 'woocommerce' ) }
value={ buttonBorderRadius }
onChange={ ( newValue: string ) =>
setAttributes( {
buttonBorderRadius: newValue,
} )
}
/>
<span className="border-radius-control-px">px</span>
</div>
</>
);
const showControls = () => {
if ( showButtonStyles ) {
return buttonStyleControls;
}
return (
<Notice
status="info"
isDismissible={ false }
className="show-button-styles-notice"
>
<p className="wc-block-checkout__controls-text">
{ __(
'You can change the appearance of individual buttons in the respective payment extension settings page',
'woocommerce'
) }
</p>
<ExternalLinkCard
href={ `${ ADMIN_URL }admin.php?page=wc-settings&tab=checkout` }
title="Payment Settings"
/>
</Notice>
);
};
return (
<div { ...blockProps }>
<Block />
<InspectorControls>
<PanelBody title={ __( 'Button Settings', 'woocommerce' ) }>
<p className="wc-block-checkout__controls-text">
{ __(
'These settings will override the plugin specific styles for these buttons',
'woocommerce'
) }
</p>
<ToggleControl
label={ __( 'Express Button Styles', 'woocommerce' ) }
checked={ showButtonStyles }
onChange={ () =>
setAttributes( {
showButtonStyles: ! showButtonStyles,
} )
}
/>
{ showControls() }
</PanelBody>
</InspectorControls>
<ExpressCheckoutContext.Provider
value={ { showButtonStyles, buttonHeight, buttonBorderRadius } }
>
<Block />
</ExpressCheckoutContext.Provider>
</div>
);
};

View File

@ -27,3 +27,37 @@
margin: 0 0 1em;
}
}
.show-button-styles-notice {
/* stylelint-disable scss/operator-no-unspaced */
margin: 0 -$gap;
/* stylelint-enable scss/operator-no-unspaced */
}
.border-radius-control-container {
position: relative;
}
.border-radius-control-px {
position: absolute;
top: 29px;
right: 12px;
color: var(--wp-components-color-accent, var(--wp-admin-theme-color, #3858e9));
}
// Center images rendered in place of buttons in the editor
.wc-block-components-express-payment {
.wc-block-components-express-payment__event-buttons {
> li {
display: flex;
align-items: center;
justify-content: center;
> img {
width: 100%;
height: 100%;
object-fit: cover;
}
}
}
}

View File

@ -0,0 +1,32 @@
/**
* External dependencies
*/
import { getValidBlockAttributes } from '@woocommerce/base-utils';
/**
* Internal dependencies
*/
import Block from './block';
import { ExpressCheckoutContext } from './context';
import metadata from './block.json';
import { ExpressCheckoutAttributes } from './types';
const FrontendBlock = ( attributes: ExpressCheckoutAttributes ) => {
const validAttributes = getValidBlockAttributes(
metadata.attributes,
attributes
);
const { showButtonStyles, buttonHeight, buttonBorderRadius } =
validAttributes;
return (
<ExpressCheckoutContext.Provider
value={ { showButtonStyles, buttonHeight, buttonBorderRadius } }
>
<Block />
</ExpressCheckoutContext.Provider>
);
};
export default FrontendBlock;

View File

@ -0,0 +1,10 @@
export type ExpressCheckoutAttributes = {
className?: string;
buttonHeight: string;
showButtonStyles: boolean;
buttonBorderRadius: string;
lock: {
move: boolean;
remove: boolean;
};
};

View File

@ -33,7 +33,7 @@ registerCheckoutBlock( {
component: lazy(
() =>
import(
/* webpackChunkName: "checkout-blocks/express-payment" */ './checkout-express-payment-block/block'
/* webpackChunkName: "checkout-blocks/express-payment" */ './checkout-express-payment-block/frontend'
)
),
} );

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Adds unified styles for the express checkout block