Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit b508cc6

Browse files
committed
Refactor textAlign property of Product Button so it uses flex rather than text-align
1 parent 03193d2 commit b508cc6

File tree

3 files changed

+19
-45
lines changed

3 files changed

+19
-45
lines changed

assets/js/atomic/blocks/product-elements/button/block.tsx

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,10 @@ import type {
2727
AddToCartButtonPlaceholderAttributes,
2828
} from './types';
2929

30-
/**
31-
* Product Button Block Component.
32-
*
33-
* @param {Object} props Incoming props.
34-
* @param {Object} [props.product] Product.
35-
* @param {Object} [props.style] Object contains CSS Styles.
36-
* @param {string} [props.className] String contains CSS class.
37-
* @param {Object} [props.textAlign] Text alignment.
38-
*
39-
* @return {*} The component.
40-
*/
4130
const AddToCartButton = ( {
4231
product,
4332
className,
4433
style,
45-
textAlign,
4634
}: AddToCartButtonAttributes ): JSX.Element => {
4735
const {
4836
id,
@@ -117,9 +105,6 @@ const AddToCartButton = ( {
117105
{
118106
loading: addingToCart,
119107
added: addedToCart,
120-
},
121-
{
122-
[ `has-text-align-${ textAlign }` ]: textAlign,
123108
}
124109
) }
125110
style={ style }
@@ -129,15 +114,6 @@ const AddToCartButton = ( {
129114
);
130115
};
131116

132-
/**
133-
* Product Button Block Component.
134-
*
135-
* @param {Object} props Incoming props.
136-
* @param {Object} [props.style] Object contains CSS Styles.
137-
* @param {string} [props.className] String contains CSS class.
138-
*
139-
* @return {*} The component.
140-
*/
141117
const AddToCartButtonPlaceholder = ( {
142118
className,
143119
style,
@@ -158,14 +134,6 @@ const AddToCartButtonPlaceholder = ( {
158134
);
159135
};
160136

161-
/**
162-
* Product Button Block Component.
163-
*
164-
* @param {Object} props Incoming props.
165-
* @param {string} [props.className] CSS Class name for the component.
166-
* @param {string} [props.textAlign] Text alignment.
167-
* @return {*} The component.
168-
*/
169137
export const Block = ( props: BlockAttributes ): JSX.Element => {
170138
const { className, textAlign } = props;
171139
const styleProps = useStyleProps( props );
@@ -181,9 +149,7 @@ export const Block = ( props: BlockAttributes ): JSX.Element => {
181149
{
182150
[ `${ parentClassName }__product-add-to-cart` ]:
183151
parentClassName,
184-
},
185-
{
186-
[ `has-text-align-${ textAlign }` ]: textAlign,
152+
[ `align-${ textAlign }` ]: textAlign,
187153
}
188154
) }
189155
>

assets/js/atomic/blocks/product-elements/button/style.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@
5555
}
5656
}
5757

58+
&.align-left {
59+
justify-content: left;
60+
}
61+
62+
&.align-right {
63+
justify-content: right;
64+
}
65+
5866
.wc-block-components-product-button__button {
5967
border-style: none;
6068
display: inline-flex;

src/BlockTypes/ProductButton.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ protected function render( $attributes, $content, $block ) {
8484
__( '%s in cart', 'woo-gutenberg-products-block' ),
8585
$number_of_items_in_cart
8686
) : $product->add_to_cart_text();
87-
$cart_redirect_after_add = get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes';
88-
$ajax_add_to_cart_enabled = get_option( 'woocommerce_enable_ajax_add_to_cart' ) === 'yes';
89-
$is_ajax_button = $ajax_add_to_cart_enabled && ! $cart_redirect_after_add && $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock();
90-
$html_element = $is_ajax_button ? 'button' : 'a';
91-
$styles_and_classes = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
92-
$text_align_styles_and_classes = StyleAttributesUtils::get_text_align_class_and_style( $attributes );
93-
$classname = $attributes['className'] ?? '';
94-
$custom_width_classes = isset( $attributes['width'] ) ? 'has-custom-width wp-block-button__width-' . $attributes['width'] : '';
95-
$html_classes = implode(
87+
$cart_redirect_after_add = get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes';
88+
$ajax_add_to_cart_enabled = get_option( 'woocommerce_enable_ajax_add_to_cart' ) === 'yes';
89+
$is_ajax_button = $ajax_add_to_cart_enabled && ! $cart_redirect_after_add && $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock();
90+
$html_element = $is_ajax_button ? 'button' : 'a';
91+
$styles_and_classes = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
92+
$classname = $attributes['className'] ?? '';
93+
$custom_width_classes = isset( $attributes['width'] ) ? 'has-custom-width wp-block-button__width-' . $attributes['width'] : '';
94+
$custom_align_classes = isset( $attributes['textAlign'] ) ? 'align-' . $attributes['textAlign'] : '';
95+
$html_classes = implode(
9696
' ',
9797
array_filter(
9898
array(
@@ -208,7 +208,7 @@ class="{button_classes}"
208208
</div>',
209209
array(
210210
'{classes}' => esc_attr( $text_align_styles_and_classes['class'] ?? '' ),
211-
'{custom_classes}' => esc_attr( $classname . ' ' . $custom_width_classes ),
211+
'{custom_classes}' => esc_attr( $classname . ' ' . $custom_width_classes . ' ' . $custom_align_classes ),
212212
'{html_element}' => $html_element,
213213
'{add_to_cart_url}' => esc_url( $product->add_to_cart_url() ),
214214
'{button_classes}' => isset( $args['class'] ) ? esc_attr( $args['class'] ) : '',

0 commit comments

Comments
 (0)