Skip to content

Commit ae3813a

Browse files
committed
feat: support mimic/clamping by using WP generated presets
1 parent ea5101e commit ae3813a

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/hooks/use-preset-controls.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const PRESET_MAPPING = {
99
},
1010
spacingSizes: {
1111
settings: [ 'spacing', 'spacingSizes' ],
12-
prefix: 'spacing-size',
12+
prefix: 'spacing',
1313
},
1414
blockHeights: {
1515
settings: [ 'blockHeights' ],

src/plugins/global-settings/preset-controls/editor-loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const PRESET_MAPPING = {
1414
prefix: 'font-size',
1515
},
1616
spacingSizes: {
17-
prefix: 'spacing-size',
17+
prefix: 'spacing',
1818
},
1919
blockHeights: {
2020
prefix: 'block-height',

src/plugins/global-settings/preset-controls/index.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Stackable_Size_And_Spacing_Preset_Controls {
2121
),
2222
'spacingSizes' => array(
2323
'settings' => array( 'spacing', 'spacingSizes' ),
24-
'prefix' => 'spacing-size',
24+
'prefix' => 'spacing',
2525
),
2626
'blockHeights' => array(
2727
'settings' => array( 'blockHeights' ),
@@ -71,27 +71,41 @@ private function load_presets( $json_path ) {
7171
* @param array $property
7272
* @param array $presets
7373
* @param array $prefix
74+
* @param bool $isTheme
7475
* @return mixed
7576
*/
76-
public function generate_css_variables( $property, $presets, $prefix ) {
77+
public function generate_css_variables( $property, $presets, $prefix, $isTheme = false ) {
78+
$filter_name = current_filter();
7779
$custom_presets = $this->custom_presets[ $property ] ?? [];
7880

7981
$css = "";
8082

81-
// Convert presets into an associative array with key 'slug'
83+
8284
$presets_by_slug = [];
85+
// Convert presets into an associative array with key 'slug'
8386
foreach ( $presets as $preset ) {
8487
$presets_by_slug[ $preset[ 'slug' ] ] = $preset;
8588
}
86-
// Override values in base presets if it exist in custom presets
87-
foreach ( $custom_presets as $custom ) {
88-
$presets_by_slug[ $custom[ 'slug' ] ] = $custom;
89+
90+
if ( $filter_name !== 'stackable_inline_editor_styles' ) {
91+
// Override values in base presets if it exist in custom presets
92+
foreach ( $custom_presets as $custom ) {
93+
$custom[ '__is_custom' ] = true;
94+
$presets_by_slug[ $custom[ 'slug' ] ] = $custom;
95+
}
8996
}
90-
91-
foreach ( $presets_by_slug as $preset ) {
92-
$slug = $preset[ 'slug' ];
93-
$size = $preset[ 'size' ];
94-
$css .= "--stk--preset--$prefix--{$preset['slug']}: {$preset['size']};\n";
97+
98+
// If custom presets or using stackable presets, use the given size.
99+
// If using theme presets, use WP generated --wp-preset to support theme.json specific
100+
// configuration (fluid, clamping, etc.)
101+
foreach ( $presets_by_slug as $slug => $preset ) {
102+
$is_custom = $preset[ '__is_custom' ] ?? false;
103+
104+
$value = $is_custom || ! $isTheme
105+
? $preset['size']
106+
: "var(--wp--preset--$prefix--$slug)";
107+
108+
$css .= "--stk--preset--$prefix--$slug: $value;\n";
95109
}
96110

97111
return $css;
@@ -124,12 +138,14 @@ public function add_preset_controls_styles( $current_css ) {
124138
$key,
125139
$this->deepGet( $this->theme_presets, $value[ 'settings' ] )[ 'theme' ],
126140
$value[ 'prefix' ],
141+
true
127142
);
128143
} elseif ( ! empty( $this->deepGet( $this->default_presets, $value[ 'settings' ] )[ 'default' ] ) ) {
129144
$generated_css .= $this->generate_css_variables(
130145
$key,
131146
$this->deepGet( $this->default_presets, $value[ 'settings' ] )[ 'default' ],
132147
$value[ 'prefix' ],
148+
true
133149
);
134150
} else {
135151
$generated_css .= $this->generate_css_variables(

0 commit comments

Comments
 (0)