Style Engine

Consistent API for rendering CSS styles for blocks across client-side and server-side applications.

Since: 6.1.0
Source: wp-includes/style-engine.php, wp-includes/style-engine/

Components

ComponentDescription
functions.mdPublic API functions
class-wp-style-engine.mdCore parsing and compilation engine
class-wp-style-engine-css-rule.mdCSS rule representation
class-wp-style-engine-css-declarations.mdCSS declaration collection
class-wp-style-engine-processor.mdStylesheet compilation

Architecture

Block Style Object (theme.json / block attributes)
    │
    ├── wp_style_engine_get_styles()
    │       └── WP_Style_Engine::parse_block_styles()
    │               ├── get_classnames()
    │               └── get_css_declarations()
    │
    └── wp_style_engine_get_stylesheet_from_css_rules()
            └── WP_Style_Engine_Processor
                    ├── add_rules()
                    ├── combine_rules_selectors() [optional]
                    └── get_css()

Supported Style Properties

CategoryProperties
backgroundbackgroundImage, backgroundPosition, backgroundRepeat, backgroundSize, backgroundAttachment
colortext, background, gradient
bordercolor, radius, style, width, top, right, bottom, left
shadowshadow (box-shadow)
dimensionsaspectRatio, minHeight
spacingpadding, margin
typographyfontSize, fontFamily, fontStyle, fontWeight, lineHeight, textColumns, textDecoration, textTransform, letterSpacing, writingMode

Preset Variables

The engine converts preset references to CSS custom properties:

php
// Input
'var:preset|color|vivid-red'

// Output
'var(--wp--preset--color--vivid-red)'

CSS Storage

Styles can be stored in named contexts for later retrieval:

php
// Store during parsing
wp_style_engine_get_styles( $styles, array(
    'context'  => 'block-supports',
    'selector' => '.wp-block-example',
) );

// Retrieve compiled stylesheet
$css = wp_style_engine_get_stylesheet_from_context( 'block-supports' );