Skip to content

Block API: explore aligning block attributes, block supports, and theme.json shape #22887

@oandregal

Description

@oandregal

A block declaration is made of a few things, including attributes, usesContext, and supports.

Take the following as a potential example:

{
    "name": "my/custom-block",
    "attributes": {
        "align": {
            "type": "string"
        },
        "dropCap": {
            "type": "boolean",
            "default": false
        },
        "backgroundColor": {
            "type": "string"
        },
        "foregroundColor": {
            "type": "string"
        },
        "style": {
            "type": 'object',
            "default": {
                "color": {
                    "background": "value",
                    "gradient": "value",
                    "text": "default value"
                }
            }
        }
    },
    "usesContext": [
        "backgroundColor",
        "foregroundColor"
    ],
    "supports": {
        "align": [ "wide", "full" ],
        "anchor": true,
        "html": false,
        "inserter": true,
        "color": {
                "__experimentalSkipSerialization": true,
                "gradients": true,
                "linkColor": true,
        },
        "fontSize": true,
        "__experimentalFontStyle": true,
        "__experimentalFontWeight": true,
        "__experimentalTextTransform": true,
        "__experimentalFontFamily": true,
        "__experimentalTextDecoration": true
        "__experimentalSelector": {
	    "my/custom-block/1": {
	        "selector": "h1",
	        "title": "h1",
	        "attributes": {
	            "level": 1
	        }
	    },
	    "my/custom-block/2": {
	        "selector": "h2",
	        "title": "h2",
	        "attributes": {
	            "level": 2
	        }
	    },
    }
}

See that the block:

  • Some features need to be registered in different places: align need to be set on both attributes and supports.
  • Defaults can't be provided via the block supports. Even for the attributes that are implicit (created by declaring support keys for them such as color, font size, etc), the block author needs to declare the "style" attribute to set defaults.
  • It's worth looking at the current shape of theme.json (docs) to see if some properties can be consolidated. For example, grouping all font settings under a "typography" key, like in theme.json
  • Some attributes will take their value from the context, which is something the block itself will have to manage (backgroundColor and foregroundColor). See example Social Links Block: Add icon and background color options #25372

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] Block APIAPI that allows to express the block paradigm.[Type] New APINew API to be used by plugin developers or package users.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions