Skip to content

Commit

Permalink
Fix passing attrs down
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 5, 2023
1 parent 5c57bab commit 135b6b0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 35 deletions.
26 changes: 11 additions & 15 deletions packages/block-editor/src/hooks/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ export const IMAGE_BACKGROUND_TYPE = 'image';
* Checks if there is a current value in the background image block support
* attributes.
*
* @param {Object} props Block props.
* @param {Object} style Style attribute.
* @return {boolean} Whether or not the block has a background image value set.
*/
export function hasBackgroundImageValue( props ) {
export function hasBackgroundImageValue( style ) {
const hasValue =
!! props.attributes.style?.background?.backgroundImage?.id ||
!! props.attributes.style?.background?.backgroundImage?.url;
!! style?.background?.backgroundImage?.id ||
!! style?.background?.backgroundImage?.url;

return hasValue;
}
Expand Down Expand Up @@ -83,13 +83,10 @@ export function hasBackgroundSupport( blockName, feature = 'any' ) {
* Resets the background image block support attributes. This can be used when disabling
* the background image controls for a block via a `ToolsPanel`.
*
* @param {Object} props Block props.
* @param {Object} props.attributes Block's attributes.
* @param {Object} props.setAttributes Function to set block's attributes.
* @param {Object} style Style attribute.
* @param {Function} setAttributes Function to set block's attributes.
*/
export function resetBackgroundImage( { attributes = {}, setAttributes } ) {
const { style = {} } = attributes;

export function resetBackgroundImage( style = {}, setAttributes ) {
setAttributes( {
style: cleanEmptyObject( {
...style,
Expand Down Expand Up @@ -146,8 +143,7 @@ function InspectorImagePreview( { label, filename, url: imgUrl } ) {
);
}

function BackgroundImagePanelItem( props ) {
const { clientId, setAttributes } = props;
function BackgroundImagePanelItem( { clientId, setAttributes } ) {
const style = useSelect(
( select ) =>
select( blockEditorStore ).getBlockAttributes( clientId )?.style,
Expand Down Expand Up @@ -248,14 +244,14 @@ function BackgroundImagePanelItem( props ) {
};
}, [] );

const hasValue = hasBackgroundImageValue( props );
const hasValue = hasBackgroundImageValue( style );

return (
<ToolsPanelItem
className="single-column"
hasValue={ () => hasValue }
label={ __( 'Background image' ) }
onDeselect={ () => resetBackgroundImage( props ) }
onDeselect={ () => resetBackgroundImage( style, setAttributes ) }
isShownByDefault={ true }
resetAllFilter={ resetAllFilter }
panelId={ clientId }
Expand Down Expand Up @@ -290,7 +286,7 @@ function BackgroundImagePanelItem( props ) {
// closed and focus is redirected to the dropdown toggle button.
toggleButton?.focus();
toggleButton?.click();
resetBackgroundImage( props );
resetBackgroundImage( style, setAttributes );
} }
>
{ __( 'Reset ' ) }
Expand Down
10 changes: 3 additions & 7 deletions packages/block-editor/src/hooks/border.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ function BordersInspectorControl( { children, resetAllFilter } ) {
);
}

function BorderPanelPure( props ) {
const { clientId, name, setAttributes } = props;
function BorderPanelPure( { clientId, name, setAttributes } ) {
const settings = useBlockSettings( name );
const isEnabled = useHasBorderPanel( settings );
function selector( select ) {
Expand All @@ -148,10 +147,7 @@ function BorderPanelPure( props ) {
}
const { style, borderColor } = useSelect( selector, [ clientId ] );
const value = useMemo( () => {
return attributesToStyle( {
style,
borderColor,
} );
return attributesToStyle( { style, borderColor } );
}, [ style, borderColor ] );

const onChange = ( newStyle ) => {
Expand All @@ -162,7 +158,7 @@ function BorderPanelPure( props ) {
return null;
}

const defaultControls = getBlockSupport( props.name, [
const defaultControls = getBlockSupport( name, [
BORDER_SUPPORT_KEY,
'__experimentalDefaultControls',
] );
Expand Down
9 changes: 4 additions & 5 deletions packages/block-editor/src/hooks/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ function ColorInspectorControl( { children, resetAllFilter } ) {
);
}

function ColorEditPure( props ) {
const { clientId, name, setAttributes } = props;
function ColorEditPure( { clientId, name, setAttributes } ) {
const settings = useBlockSettings( name );
const isEnabled = useHasColorPanel( settings );
function selector( select ) {
Expand Down Expand Up @@ -321,7 +320,7 @@ function ColorEditPure( props ) {
return null;
}

const defaultControls = getBlockSupport( props.name, [
const defaultControls = getBlockSupport( name, [
COLOR_SUPPORT_KEY,
'__experimentalDefaultControls',
] );
Expand All @@ -334,7 +333,7 @@ function ColorEditPure( props ) {
// Deactivating it requires `enableContrastChecker` to have
// an explicit value of `false`.
false !==
getBlockSupport( props.name, [
getBlockSupport( name, [
COLOR_SUPPORT_KEY,
'enableContrastChecker',
] );
Expand All @@ -349,7 +348,7 @@ function ColorEditPure( props ) {
defaultControls={ defaultControls }
enableContrastChecker={
false !==
getBlockSupport( props.name, [
getBlockSupport( name, [
COLOR_SUPPORT_KEY,
'enableContrastChecker',
] )
Expand Down
18 changes: 12 additions & 6 deletions packages/block-editor/src/hooks/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ function DimensionsInspectorControl( { children, resetAllFilter } ) {
);
}

function DimensionsPanelPure( props ) {
const { clientId, name, setAttributes, __unstableParentLayout } = props;
function DimensionsPanelPure( {
clientId,
name,
setAttributes,
__unstableParentLayout,
} ) {
const settings = useBlockSettings( name, __unstableParentLayout );
const isEnabled = useHasDimensionsPanel( settings );
const value = useSelect(
Expand All @@ -86,11 +90,11 @@ function DimensionsPanelPure( props ) {
return null;
}

const defaultDimensionsControls = getBlockSupport( props.name, [
const defaultDimensionsControls = getBlockSupport( name, [
DIMENSIONS_SUPPORT_KEY,
'__experimentalDefaultControls',
] );
const defaultSpacingControls = getBlockSupport( props.name, [
const defaultSpacingControls = getBlockSupport( name, [
SPACING_SUPPORT_KEY,
'__experimentalDefaultControls',
] );
Expand All @@ -113,13 +117,15 @@ function DimensionsPanelPure( props ) {
{ !! settings?.spacing?.padding && (
<PaddingVisualizer
forceShow={ visualizedProperty === 'padding' }
{ ...props }
clientId={ clientId }
value={ value }
/>
) }
{ !! settings?.spacing?.margin && (
<MarginVisualizer
forceShow={ visualizedProperty === 'margin' }
{ ...props }
clientId={ clientId }
value={ value }
/>
) }
</>
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/hooks/padding.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function getComputedCSS( element, property ) {
.getPropertyValue( property );
}

export function PaddingVisualizer( { clientId, attributes, forceShow } ) {
export function PaddingVisualizer( { clientId, value, forceShow } ) {
const blockElement = useBlockElement( clientId );
const [ style, setStyle ] = useState();

const padding = attributes?.style?.spacing?.padding;
const padding = value?.spacing?.padding;

useEffect( () => {
if (
Expand Down

0 comments on commit 135b6b0

Please sign in to comment.