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

Commit 0f09ffe

Browse files
committed
A bunch of styles support
- switch to use spans instead of <sup> tag. - always render separator (but hide from visuals) - proper support for style (typography, colors etc).
1 parent 863faed commit 0f09ffe

File tree

8 files changed

+60
-12
lines changed

8 files changed

+60
-12
lines changed

assets/js/atomic/blocks/product-elements/price-v2/block.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"woocommerce/isDescendentOfSingleProductTemplate":
2222
"isDescendentOfSingleProductTemplate",
2323
"woocommerce/isDescendentOfSingleProductBlock":
24-
"woocommerce/isDescendentOfSingleProductBlock"
24+
"isDescendentOfSingleProductBlock",
25+
"woocommerce/withSuperScriptStyle": "withSuperScriptStyle"
2526
},
2627
"attributes": {
2728
"isDescendentOfSingleProductTemplate": {
@@ -35,6 +36,10 @@
3536
"productId": {
3637
"type": "number",
3738
"default": 0
39+
},
40+
"withSuperScriptStyle": {
41+
"type": "boolean",
42+
"default": false
3843
}
3944
},
4045
"styles": [

assets/js/atomic/blocks/product-elements/price-v2/edit.tsx

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
* External dependencies
33
*/
44
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor';
5-
import { useEffect } from '@wordpress/element';
5+
import { useEffect, useMemo } from '@wordpress/element';
66
import { useSelect } from '@wordpress/data';
77
import {
88
InnerBlockLayoutContextProvider,
99
useInnerBlockLayoutContext,
1010
ProductDataContextProvider,
1111
} from '@woocommerce/shared-context';
1212
import { useStoreProducts } from '@woocommerce/base-context/hooks';
13+
import { useStyleProps } from '@woocommerce/base-hooks';
1314

1415
/**
1516
* Internal dependencies
@@ -20,6 +21,7 @@ import { TEMPLATE } from './template';
2021
interface Attributes {
2122
isDescendentOfSingleProductBlock: boolean;
2223
isDescendentOfSingleProductTemplate: boolean;
24+
withSuperScriptStyle: boolean;
2325
productId?: number;
2426
}
2527

@@ -40,6 +42,14 @@ interface ContextProviderProps extends Props {
4042

4143
type ProductIdProps = Partial< ContextProviderProps > & { productId: number };
4244

45+
const deriveSuperScriptFromClass = ( className: string ): boolean => {
46+
if ( className ) {
47+
const classList = className.split( ' ' );
48+
return classList.includes( 'is-style-price-super' );
49+
}
50+
return false;
51+
};
52+
4353
const ProviderFromAPI = ( {
4454
productId,
4555
children,
@@ -71,7 +81,7 @@ const DerivedProductDataContextProvider = ( {
7181
attributes,
7282
setAttributes,
7383
children,
74-
}: ContextProviderProps ): JSX.Element => {
84+
}: ContextProviderProps & { withSuperScript: boolean } ): JSX.Element => {
7585
const { queryId, postId } = context;
7686
const { productId } = attributes;
7787
const isDescendentOfQueryLoop = Number.isFinite( queryId );
@@ -112,13 +122,15 @@ const EditBlock = ( {
112122
context,
113123
attributes,
114124
setAttributes,
115-
}: Props ): JSX.Element => {
125+
withSuperScript,
126+
}: Props & { withSuperScript: boolean } ): JSX.Element => {
116127
const { parentClassName } = useInnerBlockLayoutContext();
117128
return (
118129
<DerivedProductDataContextProvider
119130
context={ context }
120131
attributes={ attributes }
121132
setAttributes={ setAttributes }
133+
withSuperScript={ withSuperScript }
122134
>
123135
<div className={ parentClassName }>
124136
<InnerBlocks
@@ -131,15 +143,31 @@ const EditBlock = ( {
131143
);
132144
};
133145

134-
const Edit = ( props: Props ): JSX.Element => {
135-
const blockProps = useBlockProps();
146+
const Edit = ( {
147+
setAttributes,
148+
attributes,
149+
...props
150+
}: Props ): JSX.Element => {
151+
const { style, className } = useStyleProps( attributes );
152+
const blockProps = useBlockProps( { style, className } );
153+
const withSuperScript = useMemo(
154+
() => deriveSuperScriptFromClass( blockProps.className ),
155+
[ blockProps.className ]
156+
);
157+
useEffect( () => {
158+
setAttributes( { withSuperScriptStyle: withSuperScript } );
159+
}, [ withSuperScript, setAttributes ] );
136160
return (
137161
<div { ...blockProps }>
138162
<InnerBlockLayoutContextProvider
139163
parentName={ 'woocommerce/price-block' }
140164
parentClassName={ 'wc-block-price-element' }
141165
>
142-
<EditBlock { ...props } />
166+
<EditBlock
167+
{ ...props }
168+
attributes={ attributes }
169+
setAttributes={ setAttributes }
170+
/>
143171
</InnerBlockLayoutContextProvider>
144172
</div>
145173
);

assets/js/atomic/blocks/product-elements/price-v2/inner-blocks/current-price/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"description": "Display the current price for the product",
77
"category": "woocommerce",
88
"keywords": [ "WooCommerce" ],
9+
"usesContext": ["woocommerce/withSuperScriptStyle"],
910
"textdomain": "woo-gutenberg-products-block",
1011
"apiVersion": 2,
1112
"$schema": "https://schemas.wp.org/trunk/block.json"

assets/js/atomic/blocks/product-elements/price-v2/inner-blocks/current-price/block.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const Block = ( {
2525
const { className: stylesClassName, style } = useStyleProps( attributes );
2626
const { parentClassName } = useInnerBlockLayoutContext();
2727
const wrapperClassName = classnames(
28-
'wc-block-product-price__original',
2928
className,
3029
{
3130
[ `${ parentClassName }__product-price` ]: parentClassName,
@@ -38,7 +37,7 @@ const Block = ( {
3837

3938
const pricePreview = '5000';
4039
const priceClassName = classnames( {
41-
[ `${ parentClassName }__product-original-price__value` ]:
40+
[ `${ parentClassName }__product-current-price__value` ]:
4241
parentClassName,
4342
} );
4443

@@ -49,7 +48,7 @@ const Block = ( {
4948
priceStyle={ style }
5049
priceClassName={ priceClassName }
5150
currency={ currency }
52-
withSuperScript={ true }
51+
withSuperScript={ context[ 'woocommerce/withSuperScriptStyle' ] }
5352
price={
5453
isDescendentOfSingleProductTemplate ? pricePreview : rawPrice
5554
}

assets/js/atomic/blocks/product-elements/price-v2/inner-blocks/original-price/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"description": "Display the original price for the product",
77
"category": "woocommerce",
88
"keywords": [ "WooCommerce" ],
9+
"usesContext": ["woocommerce/withSuperScriptStyle"],
910
"textdomain": "woo-gutenberg-products-block",
1011
"apiVersion": 2,
1112
"$schema": "https://schemas.wp.org/trunk/block.json"

assets/js/atomic/blocks/product-elements/price-v2/inner-blocks/original-price/block.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const Block = ( {
2626
const { className: stylesClassName, style } = useStyleProps( attributes );
2727
const { parentClassName } = useInnerBlockLayoutContext();
2828
const wrapperClassName = classnames(
29-
'wc-block-product-price__original',
3029
className,
3130
{
3231
[ `${ parentClassName }__product-price` ]: parentClassName,
@@ -50,6 +49,7 @@ const Block = ( {
5049
priceStyle={ style }
5150
priceClassName={ priceClassName }
5251
currency={ currency }
52+
withSuperScript={ context[ 'woocommerce/withSuperScriptStyle' ] }
5353
price={
5454
isDescendentOfSingleProductTemplate ? pricePreview : rawPrice
5555
}

assets/js/base/components/formatted-monetary-amount/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ const applySuperscript =
4444
return (
4545
<>
4646
<>{ matches[ 1 ] }</>
47-
<sup>{ matches[ 3 ] }</sup>
47+
<span className="formatted-money-amount-superscript">
48+
<span className="formatted-money-amount-separator">
49+
${ currency.decimalSeparator }
50+
</span>
51+
{ matches[ 3 ] }
52+
</span>
4853
</>
4954
);
5055
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
.wc-block-components-formatted-money-amount {
22
white-space: nowrap;
33
}
4+
5+
.formatted-money-amount-superscript {
6+
vertical-align: super;
7+
font-size: .5em;
8+
}
9+
10+
.formatted-money-amount-separator {
11+
@include visually-hidden();
12+
}

0 commit comments

Comments
 (0)