From b0cb09cc2c3c5be35868a9b11d8500304ddb220d Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Tue, 4 May 2021 14:54:31 +1000
Subject: [PATCH 01/22] Rename panel to border
---
packages/block-editor/src/hooks/border.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/packages/block-editor/src/hooks/border.js b/packages/block-editor/src/hooks/border.js
index d14633975a88a..a2efb898ddd49 100644
--- a/packages/block-editor/src/hooks/border.js
+++ b/packages/block-editor/src/hooks/border.js
@@ -46,7 +46,7 @@ export function BorderPanel( props ) {
{ isStyleSupported && }
From a20bdea04096751b26578d8ad5e5b916ac0844d1 Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Tue, 4 May 2021 14:57:19 +1000
Subject: [PATCH 02/22] Remove repetitious border copy in labels
---
.../src/components/border-style-control/index.js | 2 +-
packages/block-editor/src/hooks/border-color.js | 2 +-
packages/block-editor/src/hooks/border-radius.js | 2 +-
packages/block-editor/src/hooks/border-width.js | 2 +-
packages/edit-site/src/components/sidebar/border-panel.js | 6 +++---
5 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/packages/block-editor/src/components/border-style-control/index.js b/packages/block-editor/src/components/border-style-control/index.js
index c07e3962438e2..dfd277af8cbc7 100644
--- a/packages/block-editor/src/components/border-style-control/index.js
+++ b/packages/block-editor/src/components/border-style-control/index.js
@@ -50,7 +50,7 @@ export default function BorderStyleControl( { onChange, value } ) {
);
From 1231dac4031c0c8357ee8562eddfcf2fb8d0a61f Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Tue, 4 May 2021 15:37:29 +1000
Subject: [PATCH 04/22] Change border style to standard select control
---
.../components/border-style-control/index.js | 46 ++++---------------
1 file changed, 9 insertions(+), 37 deletions(-)
diff --git a/packages/block-editor/src/components/border-style-control/index.js b/packages/block-editor/src/components/border-style-control/index.js
index dfd277af8cbc7..eb85edb281cd0 100644
--- a/packages/block-editor/src/components/border-style-control/index.js
+++ b/packages/block-editor/src/components/border-style-control/index.js
@@ -1,37 +1,15 @@
/**
* WordPress dependencies
*/
-import { CustomSelectControl } from '@wordpress/components';
+import { SelectControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
-const DEFAULT_STYLE = {
- key: 'default',
- name: __( 'Default' ),
- style: { borderStyle: undefined },
-};
-
const BORDER_STYLES = [
- DEFAULT_STYLE,
- {
- key: 'none',
- name: __( 'None' ),
- style: { borderStyle: 'none' },
- },
- {
- key: 'solid',
- name: __( 'Solid' ),
- style: { borderStyle: 'solid' },
- },
- {
- key: 'dashed',
- name: __( 'Dashed' ),
- style: { borderStyle: 'dashed' },
- },
- {
- key: 'dotted',
- name: __( 'Dotted' ),
- style: { borderStyle: 'dotted' },
- },
+ { label: __( 'Default' ), value: undefined },
+ { label: __( 'None' ), value: 'none' },
+ { label: __( 'Solid' ), value: 'solid' },
+ { label: __( 'Dashed' ), value: 'dashed' },
+ { label: __( 'Dotted' ), value: 'dotted' },
];
/**
@@ -44,20 +22,14 @@ const BORDER_STYLES = [
* @return {WPElement} Custom border style select control.
*/
export default function BorderStyleControl( { onChange, value } ) {
- const style = BORDER_STYLES.find( ( option ) => option.key === value );
-
return (
);
From 8ed4bf11b1a6037cb82010a5ed9a327f1920679b Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Wed, 5 May 2021 16:00:06 +1000
Subject: [PATCH 05/22] Rearrange controls to match CSS order
---
packages/block-editor/src/hooks/border.js | 8 ++++++--
packages/block-editor/src/hooks/border.scss | 9 +++++++++
2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/packages/block-editor/src/hooks/border.js b/packages/block-editor/src/hooks/border.js
index f3d8e5b1a95d5..64872c5c470f0 100644
--- a/packages/block-editor/src/hooks/border.js
+++ b/packages/block-editor/src/hooks/border.js
@@ -49,8 +49,12 @@ export function BorderPanel( props ) {
title={ __( 'Border' ) }
initialOpen={ false }
>
- { isWidthSupported && }
- { isStyleSupported && }
+ { ( isWidthSupported || isStyleSupported ) && (
+
+ { isWidthSupported && }
+ { isStyleSupported && }
+
+ ) }
{ isColorSupported && }
{ isRadiusSupported && }
diff --git a/packages/block-editor/src/hooks/border.scss b/packages/block-editor/src/hooks/border.scss
index 61d75c5945463..6c859252afc34 100644
--- a/packages/block-editor/src/hooks/border.scss
+++ b/packages/block-editor/src/hooks/border.scss
@@ -1,4 +1,13 @@
.block-editor-hooks__border-controls {
+ .block-editor-hooks__border-controls-row {
+ display: flex;
+ justify-content: space-between;
+
+ > * {
+ width: calc(50% - #{ $grid-unit-10 });
+ }
+ }
+
.components-unit-control-wrapper {
margin-bottom: $grid-unit-30;
From 6500f689a3a1d228d1d24126d5f326e8e5884ce6 Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Wed, 5 May 2021 16:01:16 +1000
Subject: [PATCH 06/22] Use segmented control for border style control
---
.../components/border-style-control/icons.js | 30 ++++++++++++++
.../components/border-style-control/index.js | 41 +++++++++++++------
.../border-style-control/style.scss | 21 ++++++----
3 files changed, 70 insertions(+), 22 deletions(-)
create mode 100644 packages/block-editor/src/components/border-style-control/icons.js
diff --git a/packages/block-editor/src/components/border-style-control/icons.js b/packages/block-editor/src/components/border-style-control/icons.js
new file mode 100644
index 0000000000000..8af50413999a0
--- /dev/null
+++ b/packages/block-editor/src/components/border-style-control/icons.js
@@ -0,0 +1,30 @@
+/**
+ * WordPress dependencies
+ */
+import { Path, SVG } from '@wordpress/components';
+
+export const solidIcon = (
+
+);
+
+export const dashedIcon = (
+
+);
+
+export const dottedIcon = (
+
+);
diff --git a/packages/block-editor/src/components/border-style-control/index.js b/packages/block-editor/src/components/border-style-control/index.js
index eb85edb281cd0..b3fd1d8fe6a9a 100644
--- a/packages/block-editor/src/components/border-style-control/index.js
+++ b/packages/block-editor/src/components/border-style-control/index.js
@@ -1,15 +1,18 @@
/**
* WordPress dependencies
*/
-import { SelectControl } from '@wordpress/components';
+import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
+/**
+ * Internal dependencies
+ */
+import { dashedIcon, dottedIcon, solidIcon } from './icons';
+
const BORDER_STYLES = [
- { label: __( 'Default' ), value: undefined },
- { label: __( 'None' ), value: 'none' },
- { label: __( 'Solid' ), value: 'solid' },
- { label: __( 'Dashed' ), value: 'dashed' },
- { label: __( 'Dotted' ), value: 'dotted' },
+ { label: __( 'Solid' ), icon: solidIcon, value: 'solid' },
+ { label: __( 'Dashed' ), icon: dashedIcon, value: 'dashed' },
+ { label: __( 'Dotted' ), icon: dottedIcon, value: 'dotted' },
];
/**
@@ -24,13 +27,25 @@ const BORDER_STYLES = [
export default function BorderStyleControl( { onChange, value } ) {
return (
);
}
diff --git a/packages/block-editor/src/components/border-style-control/style.scss b/packages/block-editor/src/components/border-style-control/style.scss
index 827f31bca718c..737103be1cd24 100644
--- a/packages/block-editor/src/components/border-style-control/style.scss
+++ b/packages/block-editor/src/components/border-style-control/style.scss
@@ -1,14 +1,17 @@
-.components-border-style-control__select {
- margin-bottom: 24px;
-
- button {
- width: 100%;
+.components-border-style-control {
+ legend {
+ padding-bottom: $grid-unit-05;
}
- ul {
- li,
- li:last-child {
- margin: 6px;
+ .components-border-style-control__buttons {
+ display: inline-flex;
+ margin-bottom: $grid-unit-30;
+
+ .components-button.has-icon {
+ min-width: 30px;
+ height: 30px;
+ padding: 3px;
+ margin-right: $grid-unit-05;
}
}
}
From 9f01170ce9a0501bd6ca41cc0d22a493d9227663 Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Wed, 5 May 2021 16:15:12 +1000
Subject: [PATCH 07/22] Fix doc block for border style control
---
.../src/components/border-style-control/index.js | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/packages/block-editor/src/components/border-style-control/index.js b/packages/block-editor/src/components/border-style-control/index.js
index b3fd1d8fe6a9a..ee3a936bc3291 100644
--- a/packages/block-editor/src/components/border-style-control/index.js
+++ b/packages/block-editor/src/components/border-style-control/index.js
@@ -18,11 +18,11 @@ const BORDER_STYLES = [
/**
* Control to display border style options.
*
- * @param {Object} props Component props.
- * @param {Object} props.onChange Handler for changing border style selection.
- * @param {Object} props.value Currently selected border style value.
+ * @param {Object} props Component props.
+ * @param {Function} props.onChange Handler for changing border style selection.
+ * @param {string} props.value Currently selected border style value.
*
- * @return {WPElement} Custom border style select control.
+ * @return {WPElement} Custom border style segmented control.
*/
export default function BorderStyleControl( { onChange, value } ) {
return (
From 665d006213e5d5ac9895e2610a7d07899335e36a Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Wed, 5 May 2021 20:55:01 +1000
Subject: [PATCH 08/22] Overhaul border radius control
Allows individual control over corner radii.
---
.../all-input-control.js | 37 +++++
.../components/border-radius-control/index.js | 93 +++++++++++
.../border-radius-control/input-controls.js | 60 +++++++
.../border-radius-control/linked-button.js | 27 ++++
.../border-radius-control/style.scss | 47 ++++++
.../components/border-radius-control/utils.js | 151 ++++++++++++++++++
packages/block-editor/src/components/index.js | 1 +
.../block-editor/src/hooks/border-radius.js | 38 +----
packages/block-editor/src/style.scss | 1 +
9 files changed, 420 insertions(+), 35 deletions(-)
create mode 100644 packages/block-editor/src/components/border-radius-control/all-input-control.js
create mode 100644 packages/block-editor/src/components/border-radius-control/index.js
create mode 100644 packages/block-editor/src/components/border-radius-control/input-controls.js
create mode 100644 packages/block-editor/src/components/border-radius-control/linked-button.js
create mode 100644 packages/block-editor/src/components/border-radius-control/style.scss
create mode 100644 packages/block-editor/src/components/border-radius-control/utils.js
diff --git a/packages/block-editor/src/components/border-radius-control/all-input-control.js b/packages/block-editor/src/components/border-radius-control/all-input-control.js
new file mode 100644
index 0000000000000..7fe5bc57d3442
--- /dev/null
+++ b/packages/block-editor/src/components/border-radius-control/all-input-control.js
@@ -0,0 +1,37 @@
+/**
+ * External dependencies
+ */
+import { noop } from 'lodash';
+
+/**
+ * WordPress dependencies
+ */
+import { __experimentalUnitControl as UnitControl } from '@wordpress/components';
+import { __ } from '@wordpress/i18n';
+
+/**
+ * Internal dependencies
+ */
+import { getAllValue, isValuesMixed, isValuesDefined } from './utils';
+
+export default function AllInputControl( {
+ onChange = noop,
+ values,
+ ...props
+} ) {
+ const allValue = getAllValue( values );
+ const hasValues = isValuesDefined( values );
+ const isMixed = hasValues && isValuesMixed( values );
+ const allPlaceholder = isMixed ? __( 'Mixed' ) : null;
+
+ return (
+
+ );
+}
diff --git a/packages/block-editor/src/components/border-radius-control/index.js b/packages/block-editor/src/components/border-radius-control/index.js
new file mode 100644
index 0000000000000..a7e202ff8ab94
--- /dev/null
+++ b/packages/block-editor/src/components/border-radius-control/index.js
@@ -0,0 +1,93 @@
+/**
+ * WordPress dependencies
+ */
+import {
+ RangeControl,
+ __experimentalUseCustomUnits as useCustomUnits,
+} from '@wordpress/components';
+import { useState } from '@wordpress/element';
+import { __ } from '@wordpress/i18n';
+
+/**
+ * Internal dependencies
+ */
+import AllInputControl from './all-input-control';
+import InputControls from './input-controls';
+import LinkedButton from './linked-button';
+import {
+ DEFAULT_VALUES,
+ getAllValue,
+ getAllUnit,
+ isValuesDefined,
+ isValuesMixed,
+ parseUnit,
+} from './utils';
+
+export const MIN_BORDER_RADIUS_VALUE = 0;
+
+/**
+ * Control to display border radius options.
+ *
+ * @param {Object} props Component props.
+ * @param {Function} props.onChange Callback to handle onChange.
+ * @param {Object} props.values Border radius values.
+ *
+ * @return {WPElement} Custom border radius control.
+ */
+export default function BorderRadiusControl( { onChange, values } ) {
+ const [ isLinked, setIsLinked ] = useState(
+ ! isValuesDefined( values ) || ! isValuesMixed( values )
+ );
+
+ const units = useCustomUnits( {
+ availableUnits: [ 'px', 'em', 'rem' ],
+ } );
+
+ const [ allValue ] = parseUnit( getAllValue( values ) );
+ const unit = getAllUnit( values );
+ const step = unit === 'px' ? 1 : 0.25;
+
+ const toggleLinked = () => setIsLinked( ! isLinked );
+
+ const handleSliderChange = ( next ) => {
+ onChange( next !== undefined ? `${ next }${ unit }` : undefined );
+ };
+
+ return (
+
+ );
+}
diff --git a/packages/block-editor/src/components/border-radius-control/input-controls.js b/packages/block-editor/src/components/border-radius-control/input-controls.js
new file mode 100644
index 0000000000000..74fb62db36192
--- /dev/null
+++ b/packages/block-editor/src/components/border-radius-control/input-controls.js
@@ -0,0 +1,60 @@
+/**
+ * External dependencies
+ */
+import { noop } from 'lodash';
+
+/**
+ * WordPress dependencies
+ */
+import { __experimentalUnitControl as UnitControl } from '@wordpress/components';
+
+export default function BoxInputControls( {
+ onChange = noop,
+ values: valuesProp,
+ ...props
+} ) {
+ const createHandleOnChange = ( corner ) => ( next ) => {
+ onChange( { ...values, [ corner ]: next } );
+ };
+
+ // For backwards compatibility, handle possible flat string value.
+ const values =
+ typeof valuesProp !== 'string'
+ ? valuesProp
+ : {
+ topLeft: valuesProp,
+ topRight: valuesProp,
+ bottomLeft: valuesProp,
+ bottomRight: valuesProp,
+ };
+
+ return (
+
+
+
+
+
+
+ );
+}
diff --git a/packages/block-editor/src/components/border-radius-control/linked-button.js b/packages/block-editor/src/components/border-radius-control/linked-button.js
new file mode 100644
index 0000000000000..c4c7527bb9d89
--- /dev/null
+++ b/packages/block-editor/src/components/border-radius-control/linked-button.js
@@ -0,0 +1,27 @@
+/**
+ * WordPress dependencies
+ */
+import { Button, Tooltip } from '@wordpress/components';
+import { link, linkOff } from '@wordpress/icons';
+import { __ } from '@wordpress/i18n';
+
+export default function LinkedButton( { isLinked, ...props } ) {
+ const label = isLinked ? __( 'Unlink Radii' ) : __( 'Link Radii' );
+
+ return (
+
+
+
+
+
+ );
+}
diff --git a/packages/block-editor/src/components/border-radius-control/style.scss b/packages/block-editor/src/components/border-radius-control/style.scss
new file mode 100644
index 0000000000000..a45f89a8c0ac3
--- /dev/null
+++ b/packages/block-editor/src/components/border-radius-control/style.scss
@@ -0,0 +1,47 @@
+.components-border-radius-control {
+ margin-bottom: $grid-unit-15;
+
+ legend {
+ padding-bottom: $grid-unit-05;
+ }
+
+ .components-border-radius-control__wrapper {
+ display: flex;
+ justify-content: space-between;
+ align-items: flex-start;
+
+ > .components-unit-control-wrapper {
+ width: calc(50% - 26px);
+ margin-bottom: 0;
+ }
+
+ .components-range-control {
+ width: calc(50% - 26px);
+ margin-bottom: 0;
+
+ .components-base-control__field {
+ margin-bottom: 0;
+ }
+
+ .components-range-control__wrapper {
+ margin-right: 10px;
+ }
+ }
+
+ > span {
+ flex: 0 0 auto;
+ }
+ }
+
+ .components-border-radius-control__input-controls-wrapper {
+ display: flex;
+ width: 70%;
+ flex-wrap: wrap;
+
+ .components-unit-control-wrapper {
+ width: calc(50% - #{ $grid-unit-10 });
+ margin-bottom: $grid-unit-10;
+ margin-right: $grid-unit-10;
+ }
+ }
+}
diff --git a/packages/block-editor/src/components/border-radius-control/utils.js b/packages/block-editor/src/components/border-radius-control/utils.js
new file mode 100644
index 0000000000000..18fed86442e26
--- /dev/null
+++ b/packages/block-editor/src/components/border-radius-control/utils.js
@@ -0,0 +1,151 @@
+/**
+ * External dependencies
+ */
+import { isEmpty, isNumber } from 'lodash';
+
+/**
+ * WordPress dependencies
+ */
+import { __ } from '@wordpress/i18n';
+
+export const LABELS = {
+ all: __( 'All' ),
+ topLeft: __( 'Top left' ),
+ topRight: __( 'Top right' ),
+ bottomLeft: __( 'Bottom left' ),
+ bottomRight: __( 'Bottom right' ),
+ mixed: __( 'Mixed' ),
+};
+
+export const DEFAULT_VALUES = {
+ topLeft: null,
+ topRight: null,
+ bottomLeft: null,
+ bottomRight: null,
+};
+
+const CSS_UNITS = [ 'px', 'em', 'rem' ];
+
+/**
+ * Parses a number and unit from a value.
+ *
+ * @param {string} initialValue Value to parse
+ * @return {Array} The extracted number and unit.
+ */
+export function parseUnit( initialValue ) {
+ const value = String( initialValue ).trim();
+
+ let num = parseFloat( value, 10 );
+ num = isNaN( num ) ? '' : num;
+
+ const unitMatch = value.match( /[\d.\-\+]*\s*(.*)/ )[ 1 ]?.toLowerCase();
+ const unit = CSS_UNITS.includes( unitMatch ) ? unitMatch : undefined;
+
+ return [ num, unit ];
+}
+
+/**
+ * Gets an items with the most occurrence within an array
+ * https://stackoverflow.com/a/20762713
+ *
+ * @param {Array} arr Array of items to check.
+ * @return {any} The item with the most occurrences.
+ */
+function mode( arr ) {
+ return arr
+ .sort(
+ ( a, b ) =>
+ arr.filter( ( v ) => v === a ).length -
+ arr.filter( ( v ) => v === b ).length
+ )
+ .pop();
+}
+
+/**
+ * Returns the most common CSS unit in the radius values.
+ *
+ * @param {Object|string} values Radius values.
+ * @return {string} Most common CSS unit in values.
+ */
+export function getAllUnit( values = {} ) {
+ if ( typeof values === 'string' ) {
+ const [ , unit ] = parseUnit( values );
+ return unit || 'px';
+ }
+
+ const allUnits = Object.values( values ).map( ( value ) => {
+ const [ , unit ] = parseUnit( value );
+ return unit;
+ } );
+
+ return mode( allUnits );
+}
+
+/**
+ * Gets the 'all' input value and unit from values data.
+ *
+ * @param {Object|string} values Radius values.
+ * @return {string} A value + unit for the 'all' input.
+ */
+export function getAllValue( values = {} ) {
+ /**
+ * Border radius support was originally a single pixel value.
+ *
+ * To maintain backwards compatibility treat this case as the all value.
+ */
+ if ( typeof values === 'string' ) {
+ return values;
+ }
+
+ const parsedValues = Object.values( values ).map( ( value ) =>
+ parseUnit( value )
+ );
+
+ const allValues = parsedValues.map( ( value ) => value[ 0 ] );
+ const allUnits = parsedValues.map( ( value ) => value[ 1 ] );
+
+ const value = allValues.every( ( v ) => v === allValues[ 0 ] )
+ ? allValues[ 0 ]
+ : '';
+ const unit = mode( allUnits );
+
+ /**
+ * The isNumber check is important. On reset actions, the incoming value
+ * may be null or an empty string.
+ *
+ * Also, the value may also be zero (0), which is considered a valid unit value.
+ *
+ * isNumber() is more specific for these cases, rather than relying on a
+ * simple truthy check.
+ */
+ const allValue = isNumber( value ) ? `${ value }${ unit }` : null;
+
+ return allValue;
+}
+
+/**
+ * Checks to determine if values are mixed.
+ *
+ * @param {Object} values Radius values.
+ * @return {boolean} Whether values are mixed.
+ */
+export function isValuesMixed( values = {} ) {
+ const allValue = getAllValue( values );
+ const isMixed = isNaN( parseFloat( allValue ) );
+
+ return isMixed;
+}
+
+/**
+ * Checks to determine if values are defined.
+ *
+ * @param {Object} values Radius values.
+ * @return {boolean} Whether values are mixed.
+ */
+export function isValuesDefined( values ) {
+ return (
+ values !== undefined &&
+ ( typeof values === 'string' ||
+ ! isEmpty( Object.values( values ).filter( Boolean ) ) )
+ );
+}
diff --git a/packages/block-editor/src/components/index.js b/packages/block-editor/src/components/index.js
index ef4095a17a1d3..a781b86dc2524 100644
--- a/packages/block-editor/src/components/index.js
+++ b/packages/block-editor/src/components/index.js
@@ -34,6 +34,7 @@ export {
BlockVerticalAlignmentToolbar,
BlockVerticalAlignmentControl,
} from './block-vertical-alignment-control';
+export { default as __experimentalBorderRadiusControl } from './border-radius-control';
export { default as __experimentalBorderStyleControl } from './border-style-control';
export {
// This is a typo, but kept here for back-compat.
diff --git a/packages/block-editor/src/hooks/border-radius.js b/packages/block-editor/src/hooks/border-radius.js
index 6684004871dfb..fe734f05144f8 100644
--- a/packages/block-editor/src/hooks/border-radius.js
+++ b/packages/block-editor/src/hooks/border-radius.js
@@ -1,21 +1,8 @@
-/**
- * WordPress dependencies
- */
-import {
- __experimentalUnitControl as UnitControl,
- __experimentalUseCustomUnits as useCustomUnits,
- __experimentalParseUnit as parseUnit,
-} from '@wordpress/components';
-import { useState } from '@wordpress/element';
-import { __ } from '@wordpress/i18n';
-
/**
* Internal dependencies
*/
+import BorderRadiusControl from '../components/border-radius-control';
import { cleanEmptyObject } from './utils';
-import useSetting from '../components/use-setting';
-
-const MIN_BORDER_RADIUS_VALUE = 0;
/**
* Inspector control panel containing the border radius related configuration.
@@ -30,16 +17,6 @@ export function BorderRadiusEdit( props ) {
setAttributes,
} = props;
- // Step value is maintained in state so step is appropriate for current unit
- // even when current radius value is undefined.
- const initialStep =
- parseUnit( style?.border?.radius )[ 1 ] === 'px' ? 1 : 0.25;
- const [ step, setStep ] = useState( initialStep );
-
- const onUnitChange = ( newUnit ) => {
- setStep( newUnit === 'px' ? 1 : 0.25 );
- };
-
const onChange = ( newRadius ) => {
let newStyle = {
...style,
@@ -56,19 +33,10 @@ export function BorderRadiusEdit( props ) {
setAttributes( { style: newStyle } );
};
- const units = useCustomUnits( {
- availableUnits: useSetting( 'layout.units' ) || [ 'px', 'em', 'rem' ],
- } );
-
return (
-
);
}
diff --git a/packages/block-editor/src/style.scss b/packages/block-editor/src/style.scss
index a3c9a9f055b34..1ec4d59b7bda0 100644
--- a/packages/block-editor/src/style.scss
+++ b/packages/block-editor/src/style.scss
@@ -30,6 +30,7 @@
@import "./components/block-variation-picker/style.scss";
@import "./components/block-pattern-setup/style.scss";
@import "./components/block-variation-transforms/style.scss";
+@import "./components/border-radius-control/style.scss";
@import "./components/border-style-control/style.scss";
@import "./components/button-block-appender/style.scss";
@import "./components/colors-gradients/style.scss";
From e0b2e9e4ceb00f77bb704bcfddef0ccfb8a9b2dc Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Mon, 10 May 2021 19:46:04 +1000
Subject: [PATCH 09/22] Update global styles sidebar border controls UI
---
.../block-editor/src/hooks/border-width.js | 4 +-
.../src/components/sidebar/border-panel.js | 124 ++++++++++--------
.../src/components/sidebar/style.scss | 14 ++
3 files changed, 87 insertions(+), 55 deletions(-)
diff --git a/packages/block-editor/src/hooks/border-width.js b/packages/block-editor/src/hooks/border-width.js
index 4dffeb18bf3f0..37ea6c2795903 100644
--- a/packages/block-editor/src/hooks/border-width.js
+++ b/packages/block-editor/src/hooks/border-width.js
@@ -32,8 +32,8 @@ export const BorderWidthEdit = ( props ) => {
// Step value is maintained in state so step is appropriate for current unit
// even when current radius value is undefined.
- const initialStep =
- parseUnit( style?.border?.width )[ 1 ] === 'px' ? 1 : 0.25;
+ const hasPixelUnit = parseUnit( style?.border?.width )[ 1 ] === 'px';
+ const initialStep = hasPixelUnit ? 1 : 0.25;
const [ step, setStep ] = useState( initialStep );
const onUnitChange = ( newUnit ) => {
diff --git a/packages/edit-site/src/components/sidebar/border-panel.js b/packages/edit-site/src/components/sidebar/border-panel.js
index 0f11de6aff7d8..0d67105f1fc1e 100644
--- a/packages/edit-site/src/components/sidebar/border-panel.js
+++ b/packages/edit-site/src/components/sidebar/border-panel.js
@@ -2,10 +2,15 @@
* WordPress dependencies
*/
import {
+ __experimentalBorderRadiusControl as BorderRadiusControl,
__experimentalBorderStyleControl as BorderStyleControl,
__experimentalColorGradientControl as ColorGradientControl,
} from '@wordpress/block-editor';
-import { PanelBody, RangeControl } from '@wordpress/components';
+import {
+ PanelBody,
+ __experimentalUnitControl as UnitControl,
+} from '@wordpress/components';
+import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
@@ -13,14 +18,16 @@ import { __ } from '@wordpress/i18n';
*/
import { useSetting } from '../editor/utils';
-const MIN_BORDER_RADIUS_VALUE = 0;
-const MAX_BORDER_RADIUS_VALUE = 50;
const MIN_BORDER_WIDTH = 0;
-const MAX_BORDER_WIDTH = 50;
// Defining empty array here instead of inline avoids unnecessary re-renders of
// color control.
const EMPTY_ARRAY = [];
+const CSS_UNITS = [
+ { value: 'px', label: 'px', default: '' },
+ { value: 'em', label: 'em', default: '' },
+ { value: 'rem', label: 'rem', default: '' },
+];
export function useHasBorderPanel( { supports, name } ) {
const controls = [
@@ -61,28 +68,32 @@ function useHasBorderWidthControl( { supports, name } ) {
);
}
+function parseUnit( cssValue ) {
+ const value = String( cssValue ).trim();
+ const unitMatch = value.match( /[\d.\-\+]*\s*(.*)/ )[ 1 ];
+ const unit = unitMatch !== undefined ? unitMatch.toLowerCase() : '';
+ const currentUnit = CSS_UNITS.find( ( item ) => item.value === unit );
+
+ return currentUnit?.value || 'px';
+}
+
export default function BorderPanel( {
context: { supports, name },
getStyle,
setStyle,
} ) {
- // Border style.
- const hasBorderStyle = useHasBorderStyleControl( { supports, name } );
- const borderStyle = getStyle( name, 'borderStyle' );
-
// Border width.
const hasBorderWidth = useHasBorderWidthControl( { supports, name } );
- const borderWidthValue = parseInt(
- getStyle( name, 'borderWidth' ) || 0,
- 10
- );
+ const borderWidthValue = getStyle( name, 'borderWidth' );
- // Border radius.
- const hasBorderRadius = useHasBorderRadiusControl( { supports, name } );
- const borderRadiusValue = parseInt(
- getStyle( name, 'borderRadius' ) || 0,
- 10
- );
+ // Step value is maintained in state so step is appropriate for current unit
+ // even when current width value is undefined.
+ const initialStep = parseUnit( borderWidthValue ) === 'px' ? 1 : 0.25;
+ const [ step, setStep ] = useState( initialStep );
+
+ // Border style.
+ const hasBorderStyle = useHasBorderStyleControl( { supports, name } );
+ const borderStyle = getStyle( name, 'borderStyle' );
// Border color.
const colors = useSetting( 'color.palette' ) || EMPTY_ARRAY;
@@ -91,43 +102,42 @@ export default function BorderPanel( {
const hasBorderColor = useHasBorderColorControl( { supports, name } );
const borderColor = getStyle( name, 'borderColor' );
+ // Border radius.
+ const hasBorderRadius = useHasBorderRadiusControl( { supports, name } );
+ const borderRadiusValues = getStyle( name, 'borderRadius' );
+
return (
- { hasBorderStyle && (
-
- setStyle( name, 'borderStyle', value )
- }
- />
- ) }
- { hasBorderWidth && (
- {
- const widthStyle = value ? `${ value }px` : undefined;
- setStyle( name, 'borderWidth', widthStyle );
- } }
- />
- ) }
- { hasBorderRadius && (
- {
- const radiusStyle = value ? `${ value }px` : undefined;
- setStyle( name, 'borderRadius', radiusStyle );
- } }
- />
+ { ( hasBorderWidth || hasBorderStyle ) && (
+
+ { hasBorderWidth && (
+ {
+ setStyle(
+ name,
+ 'borderWidth',
+ value || undefined
+ );
+ } }
+ onUnitChange={ ( newUnit ) => {
+ setStep( newUnit === 'px' ? 1 : 0.25 );
+ } }
+ step={ step }
+ units={ CSS_UNITS }
+ />
+ ) }
+ { hasBorderStyle && (
+
+ setStyle( name, 'borderStyle', value )
+ }
+ />
+ ) }
+
) }
{ hasBorderColor && (
) }
+ { hasBorderRadius && (
+
+ setStyle( name, 'borderRadius', value )
+ }
+ />
+ ) }
);
}
diff --git a/packages/edit-site/src/components/sidebar/style.scss b/packages/edit-site/src/components/sidebar/style.scss
index bc75cfb88ec40..712db7bed14a0 100644
--- a/packages/edit-site/src/components/sidebar/style.scss
+++ b/packages/edit-site/src/components/sidebar/style.scss
@@ -24,3 +24,17 @@
.edit-site-global-styles-sidebar__reset-button.components-button {
margin-left: auto;
}
+
+.edit-site-global-styles-sidebar__border-controls-row {
+ display: flex;
+ justify-content: space-between;
+ margin-bottom: $grid-unit-15;
+
+ > * {
+ width: calc(50% - #{ $grid-unit-10 });
+ }
+
+ .components-border-style-control__buttons {
+ margin-bottom: 0;
+ }
+}
From 0eb04804422b39b7e1b1a935548f66a8c3aa9a0b Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Thu, 20 May 2021 14:57:54 +1000
Subject: [PATCH 10/22] Move border style icons to icons package
---
.../components/border-style-control/icons.js | 30 -------------------
.../components/border-style-control/index.js | 12 +++-----
packages/icons/src/index.js | 3 ++
packages/icons/src/library/line-dashed.js | 16 ++++++++++
packages/icons/src/library/line-dotted.js | 16 ++++++++++
packages/icons/src/library/line-solid.js | 12 ++++++++
6 files changed, 51 insertions(+), 38 deletions(-)
delete mode 100644 packages/block-editor/src/components/border-style-control/icons.js
create mode 100644 packages/icons/src/library/line-dashed.js
create mode 100644 packages/icons/src/library/line-dotted.js
create mode 100644 packages/icons/src/library/line-solid.js
diff --git a/packages/block-editor/src/components/border-style-control/icons.js b/packages/block-editor/src/components/border-style-control/icons.js
deleted file mode 100644
index 8af50413999a0..0000000000000
--- a/packages/block-editor/src/components/border-style-control/icons.js
+++ /dev/null
@@ -1,30 +0,0 @@
-/**
- * WordPress dependencies
- */
-import { Path, SVG } from '@wordpress/components';
-
-export const solidIcon = (
-
-);
-
-export const dashedIcon = (
-
-);
-
-export const dottedIcon = (
-
-);
diff --git a/packages/block-editor/src/components/border-style-control/index.js b/packages/block-editor/src/components/border-style-control/index.js
index ee3a936bc3291..6c44b7bc2fe8f 100644
--- a/packages/block-editor/src/components/border-style-control/index.js
+++ b/packages/block-editor/src/components/border-style-control/index.js
@@ -3,16 +3,12 @@
*/
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
-
-/**
- * Internal dependencies
- */
-import { dashedIcon, dottedIcon, solidIcon } from './icons';
+import { lineDashed, lineDotted, lineSolid } from '@wordpress/icons';
const BORDER_STYLES = [
- { label: __( 'Solid' ), icon: solidIcon, value: 'solid' },
- { label: __( 'Dashed' ), icon: dashedIcon, value: 'dashed' },
- { label: __( 'Dotted' ), icon: dottedIcon, value: 'dotted' },
+ { label: __( 'Solid' ), icon: lineSolid, value: 'solid' },
+ { label: __( 'Dashed' ), icon: lineDashed, value: 'dashed' },
+ { label: __( 'Dotted' ), icon: lineDotted, value: 'dotted' },
];
/**
diff --git a/packages/icons/src/index.js b/packages/icons/src/index.js
index d98147331d084..e325bddc3fa0c 100644
--- a/packages/icons/src/index.js
+++ b/packages/icons/src/index.js
@@ -102,6 +102,9 @@ export { default as keyboardClose } from './library/keyboard-close';
export { default as keyboardReturn } from './library/keyboard-return';
export { default as layout } from './library/layout';
export { default as lifesaver } from './library/lifesaver';
+export { default as lineDashed } from './library/line-dashed';
+export { default as lineDotted } from './library/line-dotted';
+export { default as lineSolid } from './library/line-solid';
export { default as link } from './library/link';
export { default as linkOff } from './library/link-off';
export { default as list } from './library/list';
diff --git a/packages/icons/src/library/line-dashed.js b/packages/icons/src/library/line-dashed.js
new file mode 100644
index 0000000000000..2d8ca1f79aef2
--- /dev/null
+++ b/packages/icons/src/library/line-dashed.js
@@ -0,0 +1,16 @@
+/**
+ * WordPress dependencies
+ */
+import { Path, SVG } from '@wordpress/primitives';
+
+const lineDashed = (
+
+);
+
+export default lineDashed;
diff --git a/packages/icons/src/library/line-dotted.js b/packages/icons/src/library/line-dotted.js
new file mode 100644
index 0000000000000..01d7e9ab671cc
--- /dev/null
+++ b/packages/icons/src/library/line-dotted.js
@@ -0,0 +1,16 @@
+/**
+ * WordPress dependencies
+ */
+import { Path, SVG } from '@wordpress/primitives';
+
+const lineDotted = (
+
+);
+
+export default lineDotted;
diff --git a/packages/icons/src/library/line-solid.js b/packages/icons/src/library/line-solid.js
new file mode 100644
index 0000000000000..c8dc39c453669
--- /dev/null
+++ b/packages/icons/src/library/line-solid.js
@@ -0,0 +1,12 @@
+/**
+ * WordPress dependencies
+ */
+import { Path, SVG } from '@wordpress/primitives';
+
+const lineSolid = (
+
+);
+
+export default lineSolid;
From dbba840c21987b3e5cb6896d3deee6b543472064 Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Thu, 17 Jun 2021 18:38:36 +1000
Subject: [PATCH 11/22] Prevent empty string values for longhand border radii
---
.../src/components/border-radius-control/input-controls.js | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/packages/block-editor/src/components/border-radius-control/input-controls.js b/packages/block-editor/src/components/border-radius-control/input-controls.js
index 74fb62db36192..45c5117a2a044 100644
--- a/packages/block-editor/src/components/border-radius-control/input-controls.js
+++ b/packages/block-editor/src/components/border-radius-control/input-controls.js
@@ -14,7 +14,10 @@ export default function BoxInputControls( {
...props
} ) {
const createHandleOnChange = ( corner ) => ( next ) => {
- onChange( { ...values, [ corner ]: next } );
+ onChange( {
+ ...values,
+ [ corner ]: next ? next : undefined,
+ } );
};
// For backwards compatibility, handle possible flat string value.
From 5021f2d7e4d18f739ac126809ac4b2754b2fa610 Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Fri, 25 Jun 2021 10:23:15 +1000
Subject: [PATCH 12/22] Switch to useCustomUnits and unit control parseUnit
---
.../components/border-radius-control/index.js | 2 +-
.../components/border-radius-control/utils.js | 21 +---------------
.../src/components/sidebar/border-panel.js | 24 +++++++------------
3 files changed, 10 insertions(+), 37 deletions(-)
diff --git a/packages/block-editor/src/components/border-radius-control/index.js b/packages/block-editor/src/components/border-radius-control/index.js
index a7e202ff8ab94..3d8898446f96b 100644
--- a/packages/block-editor/src/components/border-radius-control/index.js
+++ b/packages/block-editor/src/components/border-radius-control/index.js
@@ -3,6 +3,7 @@
*/
import {
RangeControl,
+ __experimentalParseUnit as parseUnit,
__experimentalUseCustomUnits as useCustomUnits,
} from '@wordpress/components';
import { useState } from '@wordpress/element';
@@ -20,7 +21,6 @@ import {
getAllUnit,
isValuesDefined,
isValuesMixed,
- parseUnit,
} from './utils';
export const MIN_BORDER_RADIUS_VALUE = 0;
diff --git a/packages/block-editor/src/components/border-radius-control/utils.js b/packages/block-editor/src/components/border-radius-control/utils.js
index 18fed86442e26..137255aef39fd 100644
--- a/packages/block-editor/src/components/border-radius-control/utils.js
+++ b/packages/block-editor/src/components/border-radius-control/utils.js
@@ -6,6 +6,7 @@ import { isEmpty, isNumber } from 'lodash';
/**
* WordPress dependencies
*/
+import { __experimentalParseUnit as parseUnit } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
export const LABELS = {
@@ -24,26 +25,6 @@ export const DEFAULT_VALUES = {
bottomRight: null,
};
-const CSS_UNITS = [ 'px', 'em', 'rem' ];
-
-/**
- * Parses a number and unit from a value.
- *
- * @param {string} initialValue Value to parse
- * @return {Array} The extracted number and unit.
- */
-export function parseUnit( initialValue ) {
- const value = String( initialValue ).trim();
-
- let num = parseFloat( value, 10 );
- num = isNaN( num ) ? '' : num;
-
- const unitMatch = value.match( /[\d.\-\+]*\s*(.*)/ )[ 1 ]?.toLowerCase();
- const unit = CSS_UNITS.includes( unitMatch ) ? unitMatch : undefined;
-
- return [ num, unit ];
-}
-
/**
* Gets an items with the most occurrence within an array
* https://stackoverflow.com/a/20762713
diff --git a/packages/edit-site/src/components/sidebar/border-panel.js b/packages/edit-site/src/components/sidebar/border-panel.js
index 0d67105f1fc1e..6f513d00d5003 100644
--- a/packages/edit-site/src/components/sidebar/border-panel.js
+++ b/packages/edit-site/src/components/sidebar/border-panel.js
@@ -8,7 +8,9 @@ import {
} from '@wordpress/block-editor';
import {
PanelBody,
+ __experimentalParseUnit as parseUnit,
__experimentalUnitControl as UnitControl,
+ __experimentalUseCustomUnits as useCustomUnits,
} from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
@@ -23,11 +25,6 @@ const MIN_BORDER_WIDTH = 0;
// Defining empty array here instead of inline avoids unnecessary re-renders of
// color control.
const EMPTY_ARRAY = [];
-const CSS_UNITS = [
- { value: 'px', label: 'px', default: '' },
- { value: 'em', label: 'em', default: '' },
- { value: 'rem', label: 'rem', default: '' },
-];
export function useHasBorderPanel( { supports, name } ) {
const controls = [
@@ -68,27 +65,22 @@ function useHasBorderWidthControl( { supports, name } ) {
);
}
-function parseUnit( cssValue ) {
- const value = String( cssValue ).trim();
- const unitMatch = value.match( /[\d.\-\+]*\s*(.*)/ )[ 1 ];
- const unit = unitMatch !== undefined ? unitMatch.toLowerCase() : '';
- const currentUnit = CSS_UNITS.find( ( item ) => item.value === unit );
-
- return currentUnit?.value || 'px';
-}
-
export default function BorderPanel( {
context: { supports, name },
getStyle,
setStyle,
} ) {
+ const units = useCustomUnits( {
+ availableUnits: [ 'px', 'em', 'rem' ],
+ } );
+
// Border width.
const hasBorderWidth = useHasBorderWidthControl( { supports, name } );
const borderWidthValue = getStyle( name, 'borderWidth' );
// Step value is maintained in state so step is appropriate for current unit
// even when current width value is undefined.
- const initialStep = parseUnit( borderWidthValue ) === 'px' ? 1 : 0.25;
+ const [ , initialStep ] = parseUnit( borderWidthValue ) === 'px' ? 1 : 0.25;
const [ step, setStep ] = useState( initialStep );
// Border style.
@@ -126,7 +118,7 @@ export default function BorderPanel( {
setStep( newUnit === 'px' ? 1 : 0.25 );
} }
step={ step }
- units={ CSS_UNITS }
+ units={ units }
/>
) }
{ hasBorderStyle && (
From 6ab923d014619fcec5a437ee422fc6842846f2ea Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Fri, 25 Jun 2021 10:32:18 +1000
Subject: [PATCH 13/22] Fix border width step determination in global styles
panel
---
packages/edit-site/src/components/sidebar/border-panel.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/packages/edit-site/src/components/sidebar/border-panel.js b/packages/edit-site/src/components/sidebar/border-panel.js
index 6f513d00d5003..ed3733571995c 100644
--- a/packages/edit-site/src/components/sidebar/border-panel.js
+++ b/packages/edit-site/src/components/sidebar/border-panel.js
@@ -80,7 +80,7 @@ export default function BorderPanel( {
// Step value is maintained in state so step is appropriate for current unit
// even when current width value is undefined.
- const [ , initialStep ] = parseUnit( borderWidthValue ) === 'px' ? 1 : 0.25;
+ const initialStep = parseUnit( borderWidthValue )[ 1 ] === 'px' ? 1 : 0.25;
const [ step, setStep ] = useState( initialStep );
// Border style.
From 81874aad39fe0c7c87c9a31f9caeca38118187d2 Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Fri, 25 Jun 2021 12:13:03 +1000
Subject: [PATCH 14/22] Add aria labels and general polish to border radius
control
---
.../all-input-control.js | 1 +
.../components/border-radius-control/index.js | 16 ++++---
.../border-radius-control/input-controls.js | 45 ++++++++-----------
.../border-radius-control/linked-button.js | 22 +++++----
.../border-radius-control/style.scss | 10 +++++
.../components/border-radius-control/utils.js | 17 -------
.../block-editor/src/hooks/border-width.js | 4 +-
7 files changed, 51 insertions(+), 64 deletions(-)
diff --git a/packages/block-editor/src/components/border-radius-control/all-input-control.js b/packages/block-editor/src/components/border-radius-control/all-input-control.js
index 7fe5bc57d3442..86298f78b70a0 100644
--- a/packages/block-editor/src/components/border-radius-control/all-input-control.js
+++ b/packages/block-editor/src/components/border-radius-control/all-input-control.js
@@ -27,6 +27,7 @@ export default function AllInputControl( {
return (
setIsLinked( ! isLinked );
diff --git a/packages/block-editor/src/components/border-radius-control/input-controls.js b/packages/block-editor/src/components/border-radius-control/input-controls.js
index 45c5117a2a044..1788ddbeb4e1c 100644
--- a/packages/block-editor/src/components/border-radius-control/input-controls.js
+++ b/packages/block-editor/src/components/border-radius-control/input-controls.js
@@ -7,6 +7,14 @@ import { noop } from 'lodash';
* WordPress dependencies
*/
import { __experimentalUnitControl as UnitControl } from '@wordpress/components';
+import { __ } from '@wordpress/i18n';
+
+const CORNERS = {
+ topLeft: __( 'Top left' ),
+ topRight: __( 'Top right' ),
+ bottomLeft: __( 'Bottom left' ),
+ bottomRight: __( 'Bottom right' ),
+};
export default function BoxInputControls( {
onChange = noop,
@@ -20,7 +28,7 @@ export default function BoxInputControls( {
} );
};
- // For backwards compatibility, handle possible flat string value.
+ // For shorthand style & backwards compatibility, handle flat string value.
const values =
typeof valuesProp !== 'string'
? valuesProp
@@ -31,33 +39,18 @@ export default function BoxInputControls( {
bottomRight: valuesProp,
};
+ // Controls are wrapped in tooltips as visible labels aren't desired here.
return (
-
-
-
-
+ { Object.entries( CORNERS ).map( ( [ key, label ] ) => (
+
+ ) ) }
);
}
diff --git a/packages/block-editor/src/components/border-radius-control/linked-button.js b/packages/block-editor/src/components/border-radius-control/linked-button.js
index c4c7527bb9d89..58fec1012b065 100644
--- a/packages/block-editor/src/components/border-radius-control/linked-button.js
+++ b/packages/block-editor/src/components/border-radius-control/linked-button.js
@@ -10,18 +10,16 @@ export default function LinkedButton( { isLinked, ...props } ) {
return (
-
-
-
+
);
}
diff --git a/packages/block-editor/src/components/border-radius-control/style.scss b/packages/block-editor/src/components/border-radius-control/style.scss
index a45f89a8c0ac3..4cfeaaeb6fc73 100644
--- a/packages/block-editor/src/components/border-radius-control/style.scss
+++ b/packages/block-editor/src/components/border-radius-control/style.scss
@@ -21,6 +21,7 @@
.components-base-control__field {
margin-bottom: 0;
+ height: 30px;
}
.components-range-control__wrapper {
@@ -44,4 +45,13 @@
margin-right: $grid-unit-10;
}
}
+
+ .component-border-radius-control__linked-button.has-icon {
+ display: flex;
+ justify-content: center;
+
+ svg {
+ margin-right: 0;
+ }
+ }
}
diff --git a/packages/block-editor/src/components/border-radius-control/utils.js b/packages/block-editor/src/components/border-radius-control/utils.js
index 137255aef39fd..8fe51a0b9e452 100644
--- a/packages/block-editor/src/components/border-radius-control/utils.js
+++ b/packages/block-editor/src/components/border-radius-control/utils.js
@@ -7,23 +7,6 @@ import { isEmpty, isNumber } from 'lodash';
* WordPress dependencies
*/
import { __experimentalParseUnit as parseUnit } from '@wordpress/components';
-import { __ } from '@wordpress/i18n';
-
-export const LABELS = {
- all: __( 'All' ),
- topLeft: __( 'Top left' ),
- topRight: __( 'Top right' ),
- bottomLeft: __( 'Bottom left' ),
- bottomRight: __( 'Bottom right' ),
- mixed: __( 'Mixed' ),
-};
-
-export const DEFAULT_VALUES = {
- topLeft: null,
- topRight: null,
- bottomLeft: null,
- bottomRight: null,
-};
/**
* Gets an items with the most occurrence within an array
diff --git a/packages/block-editor/src/hooks/border-width.js b/packages/block-editor/src/hooks/border-width.js
index 37ea6c2795903..4dffeb18bf3f0 100644
--- a/packages/block-editor/src/hooks/border-width.js
+++ b/packages/block-editor/src/hooks/border-width.js
@@ -32,8 +32,8 @@ export const BorderWidthEdit = ( props ) => {
// Step value is maintained in state so step is appropriate for current unit
// even when current radius value is undefined.
- const hasPixelUnit = parseUnit( style?.border?.width )[ 1 ] === 'px';
- const initialStep = hasPixelUnit ? 1 : 0.25;
+ const initialStep =
+ parseUnit( style?.border?.width )[ 1 ] === 'px' ? 1 : 0.25;
const [ step, setStep ] = useState( initialStep );
const onUnitChange = ( newUnit ) => {
From af4dfd5d543634757ea841cb0a278b8e83d78479 Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Mon, 5 Jul 2021 10:47:41 +1000
Subject: [PATCH 15/22] Remove unnecessary lodash import
---
.../border-radius-control/all-input-control.js | 11 +----------
1 file changed, 1 insertion(+), 10 deletions(-)
diff --git a/packages/block-editor/src/components/border-radius-control/all-input-control.js b/packages/block-editor/src/components/border-radius-control/all-input-control.js
index 86298f78b70a0..471c0a1e04e16 100644
--- a/packages/block-editor/src/components/border-radius-control/all-input-control.js
+++ b/packages/block-editor/src/components/border-radius-control/all-input-control.js
@@ -1,8 +1,3 @@
-/**
- * External dependencies
- */
-import { noop } from 'lodash';
-
/**
* WordPress dependencies
*/
@@ -14,11 +9,7 @@ import { __ } from '@wordpress/i18n';
*/
import { getAllValue, isValuesMixed, isValuesDefined } from './utils';
-export default function AllInputControl( {
- onChange = noop,
- values,
- ...props
-} ) {
+export default function AllInputControl( { onChange, values, ...props } ) {
const allValue = getAllValue( values );
const hasValues = isValuesDefined( values );
const isMixed = hasValues && isValuesMixed( values );
From d08a3aec6ba8723259c0e77c189b20f22f868f72 Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Mon, 5 Jul 2021 11:05:04 +1000
Subject: [PATCH 16/22] Remove import of noop
---
.../border-radius-control/input-controls.js | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/packages/block-editor/src/components/border-radius-control/input-controls.js b/packages/block-editor/src/components/border-radius-control/input-controls.js
index 1788ddbeb4e1c..db294c7f8bd1b 100644
--- a/packages/block-editor/src/components/border-radius-control/input-controls.js
+++ b/packages/block-editor/src/components/border-radius-control/input-controls.js
@@ -1,8 +1,3 @@
-/**
- * External dependencies
- */
-import { noop } from 'lodash';
-
/**
* WordPress dependencies
*/
@@ -17,11 +12,15 @@ const CORNERS = {
};
export default function BoxInputControls( {
- onChange = noop,
+ onChange,
values: valuesProp,
...props
} ) {
const createHandleOnChange = ( corner ) => ( next ) => {
+ if ( ! onChange ) {
+ return;
+ }
+
onChange( {
...values,
[ corner ]: next ? next : undefined,
From c3355b15e398191e93cc16b8917ccaef33668fdf Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Mon, 5 Jul 2021 11:15:10 +1000
Subject: [PATCH 17/22] Remove lodash use from utils
---
.../components/border-radius-control/utils.js | 37 +++++++++----------
1 file changed, 17 insertions(+), 20 deletions(-)
diff --git a/packages/block-editor/src/components/border-radius-control/utils.js b/packages/block-editor/src/components/border-radius-control/utils.js
index 8fe51a0b9e452..a6b8ecd0de1cb 100644
--- a/packages/block-editor/src/components/border-radius-control/utils.js
+++ b/packages/block-editor/src/components/border-radius-control/utils.js
@@ -1,8 +1,3 @@
-/**
- * External dependencies
- */
-import { isEmpty, isNumber } from 'lodash';
-
/**
* WordPress dependencies
*/
@@ -73,16 +68,7 @@ export function getAllValue( values = {} ) {
: '';
const unit = mode( allUnits );
- /**
- * The isNumber check is important. On reset actions, the incoming value
- * may be null or an empty string.
- *
- * Also, the value may also be zero (0), which is considered a valid unit value.
- *
- * isNumber() is more specific for these cases, rather than relying on a
- * simple truthy check.
- */
- const allValue = isNumber( value ) ? `${ value }${ unit }` : null;
+ const allValue = value === 0 || value ? `${ value }${ unit }` : null;
return allValue;
}
@@ -107,9 +93,20 @@ export function isValuesMixed( values = {} ) {
* @return {boolean} Whether values are mixed.
*/
export function isValuesDefined( values ) {
- return (
- values !== undefined &&
- ( typeof values === 'string' ||
- ! isEmpty( Object.values( values ).filter( Boolean ) ) )
- );
+ if ( ! values ) {
+ return false;
+ }
+
+ // A string value represents a shorthand value.
+ if ( typeof values === 'string' ) {
+ return true;
+ }
+
+ // An object represents longhand border radius values, if any are set
+ // flag values as being defined.
+ const filteredValues = Object.values( values ).filter( ( value ) => {
+ return !! value || value === 0;
+ } );
+
+ return !! filteredValues.length;
}
From 9bb70c1ae531d0881b9b2ec99051a31c5e13503d Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Mon, 5 Jul 2021 11:24:13 +1000
Subject: [PATCH 18/22] Improve util function naming
---
.../components/border-radius-control/all-input-control.js | 6 +++---
.../src/components/border-radius-control/index.js | 6 +++---
.../src/components/border-radius-control/utils.js | 4 ++--
3 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/packages/block-editor/src/components/border-radius-control/all-input-control.js b/packages/block-editor/src/components/border-radius-control/all-input-control.js
index 471c0a1e04e16..00073ba297c66 100644
--- a/packages/block-editor/src/components/border-radius-control/all-input-control.js
+++ b/packages/block-editor/src/components/border-radius-control/all-input-control.js
@@ -7,12 +7,12 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
-import { getAllValue, isValuesMixed, isValuesDefined } from './utils';
+import { getAllValue, hasMixedValues, hasDefinedValues } from './utils';
export default function AllInputControl( { onChange, values, ...props } ) {
const allValue = getAllValue( values );
- const hasValues = isValuesDefined( values );
- const isMixed = hasValues && isValuesMixed( values );
+ const hasValues = hasDefinedValues( values );
+ const isMixed = hasValues && hasMixedValues( values );
const allPlaceholder = isMixed ? __( 'Mixed' ) : null;
return (
diff --git a/packages/block-editor/src/components/border-radius-control/index.js b/packages/block-editor/src/components/border-radius-control/index.js
index d27ff0da77a47..ce0361a6cb460 100644
--- a/packages/block-editor/src/components/border-radius-control/index.js
+++ b/packages/block-editor/src/components/border-radius-control/index.js
@@ -18,8 +18,8 @@ import LinkedButton from './linked-button';
import {
getAllValue,
getAllUnit,
- isValuesDefined,
- isValuesMixed,
+ hasDefinedValues,
+ hasMixedValues,
} from './utils';
const DEFAULT_VALUES = {
@@ -41,7 +41,7 @@ const MIN_BORDER_RADIUS_VALUE = 0;
*/
export default function BorderRadiusControl( { onChange, values } ) {
const [ isLinked, setIsLinked ] = useState(
- ! isValuesDefined( values ) || ! isValuesMixed( values )
+ ! hasDefinedValues( values ) || ! hasMixedValues( values )
);
const units = useCustomUnits( { availableUnits: [ 'px', 'em', 'rem' ] } );
diff --git a/packages/block-editor/src/components/border-radius-control/utils.js b/packages/block-editor/src/components/border-radius-control/utils.js
index a6b8ecd0de1cb..c5f2d2163ba5f 100644
--- a/packages/block-editor/src/components/border-radius-control/utils.js
+++ b/packages/block-editor/src/components/border-radius-control/utils.js
@@ -79,7 +79,7 @@ export function getAllValue( values = {} ) {
* @param {Object} values Radius values.
* @return {boolean} Whether values are mixed.
*/
-export function isValuesMixed( values = {} ) {
+export function hasMixedValues( values = {} ) {
const allValue = getAllValue( values );
const isMixed = isNaN( parseFloat( allValue ) );
@@ -92,7 +92,7 @@ export function isValuesMixed( values = {} ) {
* @param {Object} values Radius values.
* @return {boolean} Whether values are mixed.
*/
-export function isValuesDefined( values ) {
+export function hasDefinedValues( values ) {
if ( ! values ) {
return false;
}
From 67e6dc87deb1fab8836fe3095f9496324f3934bf Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Mon, 5 Jul 2021 12:03:50 +1000
Subject: [PATCH 19/22] Use default units step values from UnitControl
---
.../src/components/border-radius-control/index.js | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/packages/block-editor/src/components/border-radius-control/index.js b/packages/block-editor/src/components/border-radius-control/index.js
index ce0361a6cb460..7d5233d5cabe6 100644
--- a/packages/block-editor/src/components/border-radius-control/index.js
+++ b/packages/block-editor/src/components/border-radius-control/index.js
@@ -29,6 +29,11 @@ const DEFAULT_VALUES = {
bottomRight: null,
};
const MIN_BORDER_RADIUS_VALUE = 0;
+const MAX_BORDER_RADIUS_VALUES = {
+ px: 100,
+ em: 20,
+ rem: 20,
+};
/**
* Control to display border radius options.
@@ -46,7 +51,9 @@ export default function BorderRadiusControl( { onChange, values } ) {
const units = useCustomUnits( { availableUnits: [ 'px', 'em', 'rem' ] } );
const unit = getAllUnit( values );
- const step = unit === 'px' ? 1 : 0.25;
+ const unitConfig = units.find( ( item ) => item.value === unit );
+ const step = unitConfig?.step || 1;
+
const [ allValue ] = parseUnit( getAllValue( values ) );
const toggleLinked = () => setIsLinked( ! isLinked );
@@ -68,12 +75,12 @@ export default function BorderRadiusControl( { onChange, values } ) {
onChange={ onChange }
unit={ unit }
units={ units }
- step={ step }
/>
Date: Wed, 7 Jul 2021 11:15:01 +1000
Subject: [PATCH 20/22] Use default step values for border width
---
packages/block-editor/src/hooks/border-width.js | 14 --------------
1 file changed, 14 deletions(-)
diff --git a/packages/block-editor/src/hooks/border-width.js b/packages/block-editor/src/hooks/border-width.js
index 4dffeb18bf3f0..702877cc80099 100644
--- a/packages/block-editor/src/hooks/border-width.js
+++ b/packages/block-editor/src/hooks/border-width.js
@@ -4,9 +4,7 @@
import {
__experimentalUnitControl as UnitControl,
__experimentalUseCustomUnits as useCustomUnits,
- __experimentalParseUnit as parseUnit,
} from '@wordpress/components';
-import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
@@ -30,16 +28,6 @@ export const BorderWidthEdit = ( props ) => {
setAttributes,
} = props;
- // Step value is maintained in state so step is appropriate for current unit
- // even when current radius value is undefined.
- const initialStep =
- parseUnit( style?.border?.width )[ 1 ] === 'px' ? 1 : 0.25;
- const [ step, setStep ] = useState( initialStep );
-
- const onUnitChange = ( newUnit ) => {
- setStep( newUnit === 'px' ? 1 : 0.25 );
- };
-
const onChange = ( newWidth ) => {
let newStyle = {
...style,
@@ -66,8 +54,6 @@ export const BorderWidthEdit = ( props ) => {
label={ __( 'Width' ) }
min={ MIN_BORDER_WIDTH }
onChange={ onChange }
- onUnitChange={ onUnitChange }
- step={ step }
units={ units }
/>
);
From bc0be1420e36ade013a9116a3baa4d74d6266b25 Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Wed, 7 Jul 2021 11:22:24 +1000
Subject: [PATCH 21/22] Use default steps for border width in global styles
---
.../edit-site/src/components/sidebar/border-panel.js | 11 -----------
1 file changed, 11 deletions(-)
diff --git a/packages/edit-site/src/components/sidebar/border-panel.js b/packages/edit-site/src/components/sidebar/border-panel.js
index ed3733571995c..85f36bc10253e 100644
--- a/packages/edit-site/src/components/sidebar/border-panel.js
+++ b/packages/edit-site/src/components/sidebar/border-panel.js
@@ -8,11 +8,9 @@ import {
} from '@wordpress/block-editor';
import {
PanelBody,
- __experimentalParseUnit as parseUnit,
__experimentalUnitControl as UnitControl,
__experimentalUseCustomUnits as useCustomUnits,
} from '@wordpress/components';
-import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
@@ -78,11 +76,6 @@ export default function BorderPanel( {
const hasBorderWidth = useHasBorderWidthControl( { supports, name } );
const borderWidthValue = getStyle( name, 'borderWidth' );
- // Step value is maintained in state so step is appropriate for current unit
- // even when current width value is undefined.
- const initialStep = parseUnit( borderWidthValue )[ 1 ] === 'px' ? 1 : 0.25;
- const [ step, setStep ] = useState( initialStep );
-
// Border style.
const hasBorderStyle = useHasBorderStyleControl( { supports, name } );
const borderStyle = getStyle( name, 'borderStyle' );
@@ -114,10 +107,6 @@ export default function BorderPanel( {
value || undefined
);
} }
- onUnitChange={ ( newUnit ) => {
- setStep( newUnit === 'px' ? 1 : 0.25 );
- } }
- step={ step }
units={ units }
/>
) }
From be6c12abebfe21af7dc70f01141d21127bbf119e Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Wed, 7 Jul 2021 11:29:56 +1000
Subject: [PATCH 22/22] Fix typos and indentation in comments
---
.../src/components/border-radius-control/utils.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/packages/block-editor/src/components/border-radius-control/utils.js b/packages/block-editor/src/components/border-radius-control/utils.js
index c5f2d2163ba5f..a5bf6e176c92d 100644
--- a/packages/block-editor/src/components/border-radius-control/utils.js
+++ b/packages/block-editor/src/components/border-radius-control/utils.js
@@ -4,11 +4,11 @@
import { __experimentalParseUnit as parseUnit } from '@wordpress/components';
/**
- * Gets an items with the most occurrence within an array
+ * Gets the item with the highest occurrence within an array
* https://stackoverflow.com/a/20762713
*
* @param {Array} arr Array of items to check.
- * @return {any} The item with the most occurrences.
+ * @return {any} The item with the most occurrences.
*/
function mode( arr ) {
return arr