Skip to content

Commit

Permalink
Use the BorderPanel component in block inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Mar 1, 2023
1 parent 91a145e commit 9c8a744
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 302 deletions.
67 changes: 60 additions & 7 deletions packages/block-editor/src/components/global-styles/border-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { useCallback } from '@wordpress/element';
import { useCallback, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
Expand Down Expand Up @@ -98,8 +98,9 @@ function BorderToolsPanel( {
}

const DEFAULT_CONTROLS = {
borderStyle: true,
borderRadius: true,
radius: true,
color: true,
width: true,
};

export default function BorderPanel( {
Expand All @@ -111,12 +112,52 @@ export default function BorderPanel( {
panelId,
defaultControls = DEFAULT_CONTROLS,
} ) {
const colors = useColorsPerOrigin( settings );
const decodeValue = ( rawValue ) =>
getValueFromVariable( { settings }, '', rawValue );
const border = inheritedValue?.border;
const encodeColorValue = ( colorValue ) => {
const allColors = colors.flatMap(
( { colors: originColors } ) => originColors
);
const colorObject = allColors.find(
( { color } ) => color === colorValue
);
return colorObject
? 'var:preset|color|' + colorObject.slug
: colorValue;
};
const decodeColorValue = useCallback(
( colorValue ) => {
const allColors = colors.flatMap(
( { colors: originColors } ) => originColors
);
const colorObject = allColors.find(
( { slug } ) => colorValue === 'var:preset|color|' + slug
);
return colorObject ? colorObject.color : colorValue;
},
[ colors ]
);
const border = useMemo( () => {
if ( hasSplitBorders( inheritedValue?.border ) ) {
const borderValue = { ...inheritedValue?.border };
[ 'top', 'right', 'bottom', 'left' ].forEach( ( side ) => {
borderValue[ side ] = {
...borderValue[ side ],
color: decodeColorValue( borderValue[ side ]?.color ),
};
} );
return borderValue;
}
return {
...inheritedValue?.border,
color: inheritedValue?.border?.color
? decodeColorValue( inheritedValue?.border?.color )
: undefined,
};
}, [ inheritedValue?.border, decodeColorValue ] );
const setBorder = ( newBorder ) =>
onChange( { ...value, border: newBorder } );
const colors = useColorsPerOrigin( settings );
const showBorderColor = useHasBorderColorControl( settings );
const showBorderStyle = useHasBorderStyleControl( settings );
const showBorderWidth = useHasBorderWidthControl( settings );
Expand Down Expand Up @@ -172,6 +213,13 @@ export default function BorderPanel( {
...newBorderWithStyle,
};

[ 'top', 'right', 'bottom', 'left' ].forEach( ( side ) => {
updatedBorder[ side ] = {
...updatedBorder[ side ],
color: encodeColorValue( updatedBorder[ side ]?.color ),
};
} );

// As radius is maintained separately to color, style, and width
// maintain its value. Undefined values here will be cleaned when
// global styles are saved.
Expand All @@ -185,6 +233,9 @@ export default function BorderPanel( {
};
}, [] );

const showBorderByDefault =
defaultControls?.color || defaultControls?.width;

return (
<Wrapper
resetAllFilter={ resetAllFilter }
Expand All @@ -197,7 +248,8 @@ export default function BorderPanel( {
hasValue={ () => isDefinedBorder( value?.border ) }
label={ __( 'Border' ) }
onDeselect={ () => resetBorder() }
isShownByDefault={ defaultControls.borderStyle }
isShownByDefault={ showBorderByDefault }
panelId={ panelId }
>
<BorderBoxControl
colors={ colors }
Expand All @@ -217,7 +269,8 @@ export default function BorderPanel( {
hasValue={ hasBorderRadius }
label={ __( 'Radius' ) }
onDeselect={ () => setBorderRadius( undefined ) }
isShownByDefault={ defaultControls.borderRadius }
isShownByDefault={ defaultControls.radius }
panelId={ panelId }
>
<BorderRadiusControl
values={ borderRadiusValues }
Expand Down
70 changes: 0 additions & 70 deletions packages/block-editor/src/hooks/border-radius.js

This file was deleted.

Loading

0 comments on commit 9c8a744

Please sign in to comment.