Skip to content

Commit

Permalink
Adding background panel for blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Apr 11, 2024
1 parent 137c6c4 commit dfc432a
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 74 deletions.
13 changes: 13 additions & 0 deletions docs/reference-guides/theme-json-reference/theme-json-living.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ Generate custom CSS custom properties of the form `--wp--custom--{key}--{nested-
## Styles


### background

Background styles.

| Property | Type | Props |
| --- | --- |--- |
| backgroundImage | string, object | |
| backgroundPosition | string, object | |
| backgroundRepeat | string, object | |
| backgroundSize | string, object | |

---

### border

Border styles.
Expand Down
8 changes: 4 additions & 4 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,10 @@ class WP_Theme_JSON_Gutenberg {
*/
const VALID_STYLES = array(
'background' => array(
'backgroundImage' => 'top',
'backgroundPosition' => 'top',
'backgroundRepeat' => 'top',
'backgroundSize' => 'top',
'backgroundImage' => null,
'backgroundPosition' => null,
'backgroundRepeat' => null,
'backgroundSize' => null,
),
'border' => array(
'color' => null,
Expand Down
9 changes: 9 additions & 0 deletions packages/block-editor/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,15 @@ export function useSettingsForBlockElement(
}
} );

[ 'backgroundImage', 'backgroundSize' ].forEach( ( key ) => {
if ( ! supportedStyles.includes( key ) ) {
updatedSettings.background = {
...updatedSettings.background,
[ key ]: false,
};
}
} );

updatedSettings.shadow = supportedStyles.includes( 'shadow' )
? updatedSettings.shadow
: false;
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ export { getSpacingClassesAndStyles } from './use-spacing-props';
export { getTypographyClassesAndStyles } from './use-typography-props';
export { getGapCSSValue } from './gap';
export { useCachedTruthy } from './use-cached-truthy';
export { setBackgroundStyleDefaults } from './background';
export { useZoomOut } from './use-zoom-out';
8 changes: 6 additions & 2 deletions packages/block-editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import { cleanEmptyObject, useStyleOverride } from './hooks/utils';
import BlockQuickNavigation from './components/block-quick-navigation';
import { LayoutStyle } from './components/block-list/layout';
import { BlockRemovalWarningModal } from './components/block-removal-warning-modal';
import { useLayoutClasses, useLayoutStyles } from './hooks';
import {
setBackgroundStyleDefaults,
useLayoutClasses,
useLayoutStyles,
} from './hooks';
import DimensionsTool from './components/dimensions-tool';
import ResolutionTool from './components/resolution-tool';
import {
Expand Down Expand Up @@ -73,5 +77,5 @@ lock( privateApis, {
selectBlockPatternsKey,
requiresWrapperOnCopy,
PrivateRichText,
reusableBlocksSelectKey,
setBackgroundStyleDefaults,
} );
33 changes: 33 additions & 0 deletions packages/edit-site/src/components/global-styles/screen-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import {
VariationsPanel,
} from './variations/variations-panel';

// Default controls for the background panel for blocks.
const BACKGROUND_BLOCK_DEFAULT_VALUES = {
backgroundImage: true,
backgroundSize: false,
};

function applyFallbackStyle( border ) {
if ( ! border ) {
return border;
Expand Down Expand Up @@ -70,13 +76,16 @@ const {
useHasFiltersPanel,
useHasImageSettingsPanel,
useGlobalStyle,
useHasBackgroundPanel,
BackgroundPanel: StylesBackgroundPanel,
BorderPanel: StylesBorderPanel,
ColorPanel: StylesColorPanel,
TypographyPanel: StylesTypographyPanel,
DimensionsPanel: StylesDimensionsPanel,
FiltersPanel: StylesFiltersPanel,
ImageSettingsPanel,
AdvancedPanel: StylesAdvancedPanel,
setBackgroundStyleDefaults,
} = unlock( blockEditorPrivateApis );

function ScreenBlock( { name, variation } ) {
Expand Down Expand Up @@ -121,6 +130,7 @@ function ScreenBlock( { name, variation } ) {
}

const blockVariations = useBlockVariations( name );
const hasBackgroundPanel = useHasBackgroundPanel( settings );
const hasTypographyPanel = useHasTypographyPanel( settings );
const hasColorPanel = useHasColorPanel( settings );
const hasBorderPanel = useHasBorderPanel( settings );
Expand Down Expand Up @@ -232,6 +242,19 @@ function ScreenBlock( { name, variation } ) {
setStyle( { ...newStyle, border: { ...updatedBorder, radius } } );
};

const onChangeBackground = ( newStyle ) => {
const backgroundStyles = setBackgroundStyleDefaults(
newStyle?.background
);
setStyle( {
...newStyle,
background: {
...newStyle?.background,
...backgroundStyles,
},
} );
};

return (
<>
<ScreenHeader
Expand Down Expand Up @@ -296,6 +319,16 @@ function ScreenBlock( { name, variation } ) {
/>
) }

{ hasBackgroundPanel && (
<StylesBackgroundPanel
inheritedValue={ inheritedStyle }
value={ style }
onChange={ onChangeBackground }
settings={ settings }
defaultValues={ BACKGROUND_BLOCK_DEFAULT_VALUES }
/>
) }

{ canEditCSS && (
<PanelBody title={ __( 'Advanced' ) } initialOpen={ false }>
<p>
Expand Down
139 changes: 71 additions & 68 deletions schemas/json/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,74 @@
"stylesProperties": {
"type": "object",
"properties": {
"background": {
"description": "Background styles.",
"type": "object",
"properties": {
"backgroundImage": {
"description": "Sets the `background-image` CSS property.",
"oneOf": [
{
"description": "A valid CSS value for the background-image property.",
"type": "string"
},
{
"type": "object",
"properties": {
"source": {
"description": "The origin of the image. 'file' denotes that the 'url' is a path to an image or other file.",
"type": "string",
"enum": [ "file" ],
"default": "file"
},
"url": {
"description": "A URL to an image file.",
"type": "string"
}
},
"additionalProperties": false
},
{
"$ref": "#/definitions/refComplete"
}
]
},
"backgroundPosition": {
"description": "Sets the `background-position` CSS property.",
"oneOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/refComplete"
}
]
},
"backgroundRepeat": {
"description": "Sets the `background-repeat` CSS property.",
"oneOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/refComplete"
}
]
},
"backgroundSize": {
"description": "Sets the `background-size` CSS property.",
"oneOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/refComplete"
}
]
}
},
"additionalProperties": false
},
"border": {
"description": "Border styles.",
"type": "object",
Expand Down Expand Up @@ -1763,6 +1831,7 @@
},
{
"properties": {
"background": {},
"border": {},
"color": {},
"dimensions": {},
Expand All @@ -1789,6 +1858,7 @@
{
"properties": {
"border": {},
"background": {},
"color": {},
"filter": {},
"shadow": {},
Expand Down Expand Up @@ -2197,6 +2267,7 @@
},
{
"properties": {
"background": {},
"border": {},
"color": {},
"dimensions": {},
Expand Down Expand Up @@ -2302,74 +2373,6 @@
"blocks": {
"description": "Styles defined on a per-block basis using the block's selector.",
"$ref": "#/definitions/stylesBlocksPropertiesComplete"
},
"background": {
"description": "Background styles.",
"type": "object",
"properties": {
"backgroundImage": {
"description": "Sets the `background-image` CSS property.",
"oneOf": [
{
"description": "A valid CSS value for the background-image property.",
"type": "string"
},
{
"type": "object",
"properties": {
"source": {
"description": "The origin of the image. 'file' denotes that the 'url' is a path to an image or other file.",
"type": "string",
"enum": [ "file" ],
"default": "file"
},
"url": {
"description": "A URL to an image file.",
"type": "string"
}
},
"additionalProperties": false
},
{
"$ref": "#/definitions/refComplete"
}
]
},
"backgroundPosition": {
"description": "Sets the `background-position` CSS property.",
"oneOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/refComplete"
}
]
},
"backgroundRepeat": {
"description": "Sets the `background-repeat` CSS property.",
"oneOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/refComplete"
}
]
},
"backgroundSize": {
"description": "Sets the `background-size` CSS property.",
"oneOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/refComplete"
}
]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
Expand Down

0 comments on commit dfc432a

Please sign in to comment.