From 5c0fb4c88779057cbc7f08e6d7777233b16affb7 Mon Sep 17 00:00:00 2001
From: Andrew Serong <14988353+andrewserong@users.noreply.github.com>
Date: Wed, 15 Sep 2021 16:55:38 +1000
Subject: [PATCH] Try setting a blockGap label using a context string, update
fallback label to Block spacing
---
packages/block-editor/src/hooks/dimensions.js | 7 ++++--
packages/block-editor/src/hooks/gap.js | 25 ++++++++++++++++---
packages/block-library/src/columns/index.js | 5 ++++
.../components/sidebar/dimensions-panel.js | 22 ++++++++++++++--
4 files changed, 52 insertions(+), 7 deletions(-)
diff --git a/packages/block-editor/src/hooks/dimensions.js b/packages/block-editor/src/hooks/dimensions.js
index 9e38b6c6f0cab6..eb91311bf9faf4 100644
--- a/packages/block-editor/src/hooks/dimensions.js
+++ b/packages/block-editor/src/hooks/dimensions.js
@@ -12,6 +12,7 @@ import { getBlockSupport } from '@wordpress/blocks';
import InspectorControls from '../components/inspector-controls';
import {
GapEdit,
+ getGapLabel,
hasGapSupport,
hasGapValue,
resetGap,
@@ -59,6 +60,8 @@ export function DimensionsPanel( props ) {
'__experimentalDefaultControls',
] );
+ const blockGapLabel = getGapLabel( props );
+
const createResetAllFilter = ( attribute ) => ( newAttributes ) => ( {
...newAttributes,
style: {
@@ -100,13 +103,13 @@ export function DimensionsPanel( props ) {
hasGapValue( props ) }
- label={ __( 'Block gap' ) }
+ label={ blockGapLabel }
onDeselect={ () => resetGap( props ) }
resetAllFilter={ createResetAllFilter( 'blockGap' ) }
isShownByDefault={ defaultSpacingControls?.blockGap }
panelId={ props.clientId }
>
-
+
) }
diff --git a/packages/block-editor/src/hooks/gap.js b/packages/block-editor/src/hooks/gap.js
index d29a63774fbb64..4ded3f0e2f4eb1 100644
--- a/packages/block-editor/src/hooks/gap.js
+++ b/packages/block-editor/src/hooks/gap.js
@@ -3,7 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { Platform } from '@wordpress/element';
-import { getBlockSupport } from '@wordpress/blocks';
+import { getBlockSupport, getBlockType } from '@wordpress/blocks';
import {
__experimentalUseCustomUnits as useCustomUnits,
__experimentalUnitControl as UnitControl,
@@ -60,6 +60,24 @@ export function resetGap( { attributes = {}, setAttributes } ) {
} );
}
+/**
+ * Get the label for the gap control, if handled by the block's
+ * `__experimentalLabel` function via the `blockGap` context string.
+ *
+ * Falls back to a default string.
+ *
+ * @param {Object} props Block props.
+ * @param {Object} props.name Block's name.
+ * @param {Object} props.attributes Block's attributes.
+ * @return {string} The label for the gap control.
+ */
+export function getGapLabel( { name, attributes } ) {
+ const { __experimentalLabel: getLabel } = getBlockType( name );
+ const label = getLabel && getLabel( attributes, { context: 'blockGap' } );
+
+ return label || __( 'Block spacing' );
+}
+
/**
* Custom hook that checks if gap settings have been disabled.
*
@@ -80,8 +98,9 @@ export function useIsGapDisabled( { name: blockName } = {} ) {
*/
export function GapEdit( props ) {
const {
- clientId,
attributes: { style },
+ clientId,
+ label,
setAttributes,
} = props;
@@ -132,7 +151,7 @@ export function GapEdit( props ) {
web: (
<>
{
setStyle( name, '--wp--style--block-gap', newGapValue );
@@ -181,12 +199,12 @@ export default function DimensionsPanel( { context, getStyle, setStyle } ) {