WP_Theme_JSON::get_block_style_variation_feature_selector( array $style_variation, string $feature_selector ): string

In this article

Applies a block style variation class to a feature selector.

Description

Feature selectors can target a different element than the block’s root selector. For example, the Button block’s root selector targets the inner link, while its dimensions width selector targets the outer wrapper. Apply the variation class directly to the selector that will receive the declarations instead of deriving it by subtracting the root selector from the feature selector.

Parameters

$style_variationarrayrequired
Style variation metadata.
$feature_selectorstringrequired
CSS selector for the feature.

Return

string Feature selector with block style variation selector added.

Source

protected static function get_block_style_variation_feature_selector( $style_variation, $feature_selector ) {
	$variation_path = $style_variation['path'] ?? array();
	$variation_name = $style_variation['name'] ?? ( is_array( $variation_path ) ? end( $variation_path ) : null );

	if ( ! $variation_name ) {
		return $style_variation['selector'] ?? $feature_selector;
	}

	$variation_class = ".is-style-$variation_name";
	$selector_parts  = static::split_selector_list( $feature_selector );
	$selector_parts  = array_map(
		static function ( $selector ) use ( $variation_class ) {
			$prefix = $variation_class . ' ';

			if ( str_starts_with( $selector, $prefix ) ) {
				return substr( $selector, strlen( $prefix ) );
			}

			return $selector;
		},
		$selector_parts
	);

	return static::get_block_style_variation_selector(
		$variation_name,
		implode( ', ', $selector_parts )
	);
}

Changelog

VersionDescription
7.0.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.