Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/wp-includes/block-supports/states.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,21 @@ function wp_normalize_state_preset_vars( $value ) {
return $value;
}

$unwrapped_name = str_replace( '|', '--', substr( $value, strlen( 'var:' ) ) );
return "var(--wp--$unwrapped_name)";
$parts = explode( '|', substr( $value, strlen( 'var:' ) ) );

/*
* The slug is kebab-cased when the preset's custom property is generated,
* so a reference has to be converted the same way or it points at a
* property that does not exist (`--wp--preset--font-size--3xl` for a preset
* generated as `--wp--preset--font-size--3-xl`). Mirrors
* `WP_Theme_JSON_Gutenberg::convert_custom_properties()` and the JS style
* engine's `getCSSValueFromRawStyle()`.
*/
if ( 3 === count( $parts ) && 'preset' === $parts[0] ) {
$parts[2] = _wp_to_kebab_case( $parts[2] );
}

return 'var(--wp--' . implode( '--', $parts ) . ')';
}

/**
Expand Down
12 changes: 10 additions & 2 deletions tests/phpunit/tests/block-supports/states.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,25 @@ public function test_build_state_selector_splits_selector_lists_without_splittin
public function test_converts_state_preset_vars_to_css_vars() {
$actual = wp_normalize_state_preset_vars(
array(
'border' => array(
'border' => array(
'color' => 'var:preset|color|accent-1',
),
'typography' => array(
'fontSize' => 'var:preset|font-size|3xl',
'fontFamily' => 'var:preset|font-family|heavenlyBlue',
),
)
);

$this->assertSame(
array(
'border' => array(
'border' => array(
'color' => 'var(--wp--preset--color--accent-1)',
),
'typography' => array(
'fontSize' => 'var(--wp--preset--font-size--3-xl)',
'fontFamily' => 'var(--wp--preset--font-family--heavenly-blue)',
),
),
$actual
);
Expand Down
Loading