Skip to content

Commit

Permalink
Add default controls support
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewserong committed Dec 18, 2023
1 parent 36a18ef commit 8d59e4e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
33 changes: 27 additions & 6 deletions packages/block-editor/src/hooks/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ function InspectorImagePreview( { label, filename, url: imgUrl } ) {
);
}

function BackgroundImagePanelItem( { clientId, setAttributes } ) {
function BackgroundImagePanelItem( {
clientId,
isShownByDefault,
setAttributes,
} ) {
const style = useSelect(
( select ) =>
select( blockEditorStore ).getBlockAttributes( clientId )?.style,
Expand Down Expand Up @@ -286,7 +290,7 @@ function BackgroundImagePanelItem( { clientId, setAttributes } ) {
hasValue={ () => hasValue }
label={ __( 'Background image' ) }
onDeselect={ () => resetBackgroundImage( style, setAttributes ) }
isShownByDefault={ true }
isShownByDefault={ isShownByDefault }
resetAllFilter={ resetAllFilter }
panelId={ clientId }
>
Expand Down Expand Up @@ -346,7 +350,11 @@ function backgroundSizeHelpText( value ) {
return __( 'Repeat the image, and set a fixed size.' );
}

function BackgroundSizePanelItem( { clientId, setAttributes } ) {
function BackgroundSizePanelItem( {
clientId,
isShownByDefault,
setAttributes,
} ) {
const style = useSelect(
( select ) =>
select( blockEditorStore ).getBlockAttributes( clientId )?.style,
Expand Down Expand Up @@ -398,7 +406,7 @@ function BackgroundSizePanelItem( { clientId, setAttributes } ) {
hasValue={ () => hasValue }
label={ __( 'Background size' ) }
onDeselect={ () => resetBackgroundSize( style, setAttributes ) }
isShownByDefault={ true }
isShownByDefault={ isShownByDefault }
resetAllFilter={ resetAllFilter }
panelId={ clientId }
>
Expand Down Expand Up @@ -457,10 +465,23 @@ function BackgroundImagePanelPure( props ) {
backgroundSize && hasBackgroundSupport( props.name, 'backgroundSize' )
);

const defaultControls = getBlockSupport( props.name, [
BACKGROUND_SUPPORT_KEY,
'__experimentalDefaultControls',
] );

return (
<InspectorControls group="background">
<BackgroundImagePanelItem { ...props } />
{ showBackgroundSize && <BackgroundSizePanelItem { ...props } /> }
<BackgroundImagePanelItem
isShownByDefault={ defaultControls?.backgroundImage }
{ ...props }
/>
{ showBackgroundSize && (
<BackgroundSizePanelItem
isShownByDefault={ defaultControls?.backgroundSize }
{ ...props }
/>
) }
</InspectorControls>
);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/group/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
"html": false,
"background": {
"backgroundImage": true,
"backgroundSize": true
"backgroundSize": true,
"__experimentalDefaultControls": {
"backgroundImage": true
}
},
"color": {
"gradients": true,
Expand Down

0 comments on commit 8d59e4e

Please sign in to comment.