Skip to content

Commit

Permalink
Updating theme.json to include background settings and styles
Browse files Browse the repository at this point in the history
Ensuring the blocks in global styles only show background UI when it's allowed
  • Loading branch information
ramonjd committed Mar 26, 2024
1 parent b4b9126 commit fad270f
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 68 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 @@ -205,6 +205,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
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const BACKGROUND_BLOCK_DEFAULT_CONTROL_VALUES = {
backgroundSize: 'cover',
};

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

function applyFallbackStyle( border ) {
if ( ! border ) {
return border;
Expand Down Expand Up @@ -323,6 +329,7 @@ function ScreenBlock( { name, variation } ) {
value={ style }
onChange={ onChangeBackground }
settings={ settings }
defaultControls={ BACKGROUND_BLOCK_DEFAULT_CONTROLS }
defaultControlValues={
BACKGROUND_BLOCK_DEFAULT_CONTROL_VALUES
}
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 fad270f

Please sign in to comment.