This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Mini cart] Make the title customizable (#8905)
* Create the counter block and the header pattern * Change the pattern to an inner block * Allow customizing item counter block * Rename block * Allow removing the items counter block * Add padding controls * Preload new blocks * Add title block back from the inserter * Move h2 tag to the parent title * Align items at the bottom * Unify styles * Add title class in the editor * Prevent removing title blocks * Disallow unlocking and inserter * Disallowing moving blocks
- Loading branch information
Showing
24 changed files
with
444 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 26 additions & 15 deletions
41
assets/js/blocks/mini-cart/mini-cart-contents/inner-blocks/mini-cart-title-block/block.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 24 additions & 11 deletions
35
assets/js/blocks/mini-cart/mini-cart-contents/inner-blocks/mini-cart-title-block/edit.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,36 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Block from './block'; | ||
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor'; | ||
|
||
export const Edit = (): JSX.Element => { | ||
const blockProps = useBlockProps(); | ||
const blockProps = useBlockProps( { | ||
className: 'wc-block-mini-cart__title', | ||
} ); | ||
|
||
const TEMPLATE = [ | ||
[ 'woocommerce/mini-cart-title-label-block', {} ], | ||
[ 'woocommerce/mini-cart-title-items-counter-block', {} ], | ||
]; | ||
|
||
return ( | ||
<div { ...blockProps }> | ||
<Block /> | ||
</div> | ||
<h2 { ...blockProps }> | ||
<InnerBlocks | ||
allowedBlocks={ [ | ||
'woocommerce/mini-cart-title-label-block', | ||
'woocommerce/mini-cart-title-items-counter-block', | ||
] } | ||
template={ TEMPLATE } | ||
templateLock="all" | ||
/> | ||
</h2> | ||
); | ||
}; | ||
|
||
export const Save = (): JSX.Element => { | ||
return <div { ...useBlockProps.save() }></div>; | ||
return ( | ||
<div { ...useBlockProps.save() }> | ||
<InnerBlocks.Content /> | ||
</div> | ||
); | ||
}; |
29 changes: 29 additions & 0 deletions
29
.../mini-cart/mini-cart-contents/inner-blocks/mini-cart-title-items-counter-block/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "woocommerce/mini-cart-title-items-counter-block", | ||
"version": "1.0.0", | ||
"title": "Mini Cart Title Items Counter", | ||
"description": "Block that displays the items counter part of the Mini Cart Title block.", | ||
"category": "woocommerce", | ||
"supports": { | ||
"align": false, | ||
"html": false, | ||
"multiple": false, | ||
"reusable": false, | ||
"inserter": false, | ||
"lock": false, | ||
"color": { | ||
"text": true, | ||
"background": true | ||
}, | ||
"typography": { | ||
"fontSize": true | ||
}, | ||
"spacing": { | ||
"padding": true | ||
} | ||
}, | ||
"parent": [ "woocommerce/mini-cart-title-block" ], | ||
"textdomain": "woo-gutenberg-products-block", | ||
"apiVersion": 2, | ||
"$schema": "https://schemas.wp.org/trunk/block.json" | ||
} |
50 changes: 50 additions & 0 deletions
50
...s/mini-cart/mini-cart-contents/inner-blocks/mini-cart-title-items-counter-block/block.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useStoreCart } from '@woocommerce/base-context'; | ||
import classNames from 'classnames'; | ||
import { _n, sprintf } from '@wordpress/i18n'; | ||
import { | ||
useColorProps, | ||
useSpacingProps, | ||
useTypographyProps, | ||
} from '@woocommerce/base-hooks'; | ||
|
||
type Props = { | ||
className?: string; | ||
}; | ||
|
||
const Block = ( props: Props ): JSX.Element => { | ||
const { cartItemsCount } = useStoreCart(); | ||
const colorProps = useColorProps( props ); | ||
const typographyProps = useTypographyProps( props ); | ||
const spacingProps = useSpacingProps( props ); | ||
|
||
return ( | ||
<span | ||
className={ classNames( | ||
props.className, | ||
colorProps.className, | ||
typographyProps.className | ||
) } | ||
style={ { | ||
...colorProps.style, | ||
...typographyProps.style, | ||
...spacingProps.style, | ||
} } | ||
> | ||
{ sprintf( | ||
/* translators: %d is the count of items in the cart. */ | ||
_n( | ||
'(%d item)', | ||
'(%d items)', | ||
cartItemsCount, | ||
'woo-gutenberg-products-block' | ||
), | ||
cartItemsCount | ||
) } | ||
</span> | ||
); | ||
}; | ||
|
||
export default Block; |
30 changes: 30 additions & 0 deletions
30
...ks/mini-cart/mini-cart-contents/inner-blocks/mini-cart-title-items-counter-block/edit.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
import { _n, sprintf } from '@wordpress/i18n'; | ||
import { useStoreCart } from '@woocommerce/base-context'; | ||
|
||
export const Edit = (): JSX.Element => { | ||
const blockProps = useBlockProps(); | ||
const { cartItemsCount } = useStoreCart(); | ||
|
||
return ( | ||
<span { ...blockProps }> | ||
{ sprintf( | ||
/* translators: %d is the count of items in the cart. */ | ||
_n( | ||
'(%d item)', | ||
'(%d items)', | ||
cartItemsCount, | ||
'woo-gutenberg-products-block' | ||
), | ||
cartItemsCount | ||
) } | ||
</span> | ||
); | ||
}; | ||
|
||
export const Save = (): JSX.Element => { | ||
return <div { ...useBlockProps.save() }></div>; | ||
}; |
26 changes: 26 additions & 0 deletions
26
...s/mini-cart/mini-cart-contents/inner-blocks/mini-cart-title-items-counter-block/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Icon, heading } from '@wordpress/icons'; | ||
import { registerBlockType } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Edit, Save } from './edit'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore -- TypeScript expects some required properties which we already | ||
// registered in PHP. | ||
registerBlockType( 'woocommerce/mini-cart-title-items-counter-block', { | ||
icon: { | ||
src: ( | ||
<Icon | ||
icon={ heading } | ||
className="wc-block-editor-components-block-icon" | ||
/> | ||
), | ||
}, | ||
edit: Edit, | ||
save: Save, | ||
} ); |
11 changes: 11 additions & 0 deletions
11
...ocks/mini-cart/mini-cart-contents/inner-blocks/mini-cart-title-label-block/attributes.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { defaultYourCartLabel } from './constants'; | ||
|
||
export default { | ||
label: { | ||
type: 'string', | ||
default: defaultYourCartLabel, | ||
}, | ||
}; |
34 changes: 34 additions & 0 deletions
34
...s/blocks/mini-cart/mini-cart-contents/inner-blocks/mini-cart-title-label-block/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "woocommerce/mini-cart-title-label-block", | ||
"version": "1.0.0", | ||
"title": "Mini Cart Title Label", | ||
"description": "Block that displays the 'Your cart' part of the Mini Cart Title block.", | ||
"category": "woocommerce", | ||
"supports": { | ||
"align": false, | ||
"html": false, | ||
"multiple": false, | ||
"reusable": false, | ||
"inserter": false, | ||
"lock": false, | ||
"color": { | ||
"text": true, | ||
"background": true | ||
}, | ||
"typography": { | ||
"fontSize": true | ||
}, | ||
"spacing": { | ||
"padding": true | ||
} | ||
}, | ||
"attributes": { | ||
"label": { | ||
"type": "string" | ||
} | ||
}, | ||
"parent": [ "woocommerce/mini-cart-title-block" ], | ||
"textdomain": "woo-gutenberg-products-block", | ||
"apiVersion": 2, | ||
"$schema": "https://schemas.wp.org/trunk/block.json" | ||
} |
44 changes: 44 additions & 0 deletions
44
...js/blocks/mini-cart/mini-cart-contents/inner-blocks/mini-cart-title-label-block/block.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { | ||
useColorProps, | ||
useSpacingProps, | ||
useTypographyProps, | ||
} from '@woocommerce/base-hooks'; | ||
import classNames from 'classnames'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { defaultYourCartLabel } from './constants'; | ||
|
||
type Props = { | ||
label?: string; | ||
className?: string; | ||
}; | ||
|
||
const Block = ( props: Props ): JSX.Element => { | ||
const colorProps = useColorProps( props ); | ||
const typographyProps = useTypographyProps( props ); | ||
const spacingProps = useSpacingProps( props ); | ||
|
||
return ( | ||
<span | ||
className={ classNames( | ||
props.className, | ||
colorProps.className, | ||
typographyProps.className | ||
) } | ||
style={ { | ||
...colorProps.style, | ||
...typographyProps.style, | ||
...spacingProps.style, | ||
} } | ||
> | ||
{ props.label || defaultYourCartLabel } | ||
</span> | ||
); | ||
}; | ||
|
||
export default Block; |
Oops, something went wrong.