Skip to content

Commit d840726

Browse files
committed
feat: generate global css for presets
1 parent 868503d commit d840726

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed

plugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ function is_frontend() {
230230
require_once( plugin_dir_path( __FILE__ ) . 'src/dynamic-breakpoints.php' );
231231
require_once( plugin_dir_path( __FILE__ ) . 'src/design-library/init.php' );
232232
require_once( plugin_dir_path( __FILE__ ) . 'src/global-settings.php' );
233+
require_once( plugin_dir_path( __FILE__ ) . 'src/plugins/global-settings/preset-controls/index.php' );
233234
require_once( plugin_dir_path( __FILE__ ) . 'src/custom-block-styles.php' );
234235
require_once( plugin_dir_path( __FILE__ ) . 'src/css-optimize.php' );
235236
require_once( plugin_dir_path( __FILE__ ) . 'src/compatibility/index.php' );
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Size and Spacing Preset Controls
4+
*/
5+
6+
// Exit if accessed directly.
7+
if ( ! defined( 'ABSPATH' ) ) {
8+
exit;
9+
}
10+
11+
if ( ! class_exists( 'Stackable_Size_And_Spacing_Preset_Controls' ) ) {
12+
13+
/**
14+
* Size and Spacing Preset Controls
15+
*/
16+
class Stackable_Size_And_Spacing_Preset_Controls {
17+
18+
public $stackable_presets;
19+
public $theme_presets;
20+
21+
/**
22+
* Initialize
23+
*/
24+
function __construct() {
25+
$this->stackable_presets = $this->load_presets( __DIR__ . '/presets.json');
26+
$this->theme_presets = $this->load_presets( get_template_directory() . '/theme.json' );
27+
// Register our settings.
28+
// add_action( 'register_stackable_global_settings', array( $this, 'register_preset_controls' ) );
29+
30+
add_filter( 'stackable_inline_styles_nodep', array( $this, 'add_preset_controls_styles' ) );
31+
}
32+
33+
/**
34+
* Register the settings we need for preset controls
35+
*
36+
* @return void
37+
*/
38+
// public function register_preset_controls() {
39+
40+
// }
41+
42+
public function sanitize_array_setting( $input ) {
43+
return ! is_array( $input ) ? array( array() ) : $input;
44+
}
45+
46+
private function load_presets( $json_path ) {
47+
if ( file_exists( $json_path ) ) {
48+
$json_data = file_get_contents( $json_path );
49+
$decoded_data = json_decode( $json_data, true );
50+
return $decoded_data[ 'settings' ] ?? [];
51+
}
52+
return [];
53+
}
54+
55+
// Generate CSS variables based on preset type (e.g., fontSizes, spacing)
56+
public function generate_css_variables( $presets, $prefix ) {
57+
$css = "";
58+
foreach ( $presets as $preset ) {
59+
$slug = $preset[ 'slug' ];
60+
$size = $preset[ 'size' ];
61+
$css .= "--stk--preset--$prefix--$slug: $size;\n";
62+
}
63+
64+
return $css;
65+
}
66+
67+
/**
68+
* Add our global preset control styles.
69+
*
70+
* @param String $current_css
71+
* @return String
72+
*/
73+
public function add_preset_controls_styles( $current_css ) {
74+
$presets = $this->stackable_presets;
75+
76+
if ( isset ( $this->theme_presets ) ) {
77+
$presets = $this->theme_presets;
78+
}
79+
80+
$generated_css = ":root {\n";
81+
$generated_css .= $this->generate_css_variables( $presets[ 'spacing' ][ 'spacingSizes' ], 'spacing' );
82+
$generated_css .= $this->generate_css_variables( $presets[ 'typography' ][ 'fontSizes' ], 'font-size' );
83+
$generated_css .= "}\n";
84+
85+
if ( ! $generated_css ) {
86+
return $current_css;
87+
}
88+
89+
$current_css .= $generated_css;
90+
return apply_filters( 'stackable_frontend_css' , $current_css );
91+
}
92+
}
93+
94+
new Stackable_Size_And_Spacing_Preset_Controls();
95+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
{
2+
"settings": {
3+
"spacing": {
4+
"spacingSizes": [
5+
{
6+
"label": "XS",
7+
"size": "0.25rem",
8+
"slug": "x-small"
9+
},
10+
{
11+
"label": "S",
12+
"size": "0.5rem",
13+
"slug": "small"
14+
},
15+
{
16+
"label": "M",
17+
"size": "1rem",
18+
"slug": "medium"
19+
},
20+
{
21+
"label": "L",
22+
"size": "1.5rem",
23+
"slug": "large"
24+
},
25+
{
26+
"label": "XL",
27+
"size": "2rem",
28+
"slug": "x-large"
29+
},
30+
{
31+
"label": "2XL",
32+
"size": "3rem",
33+
"slug": "xx-large"
34+
},
35+
{
36+
"label": "3XL",
37+
"size": "4rem",
38+
"slug": "xxx-large"
39+
},
40+
{
41+
"label": "4XL",
42+
"size": "6rem",
43+
"slug": "xxxx-large"
44+
}
45+
]
46+
},
47+
"typography": {
48+
"fontSizes": [
49+
{
50+
"label": "XS",
51+
"size": "0.75rem",
52+
"slug": "x-small"
53+
},
54+
{
55+
"label": "S",
56+
"size": "0.875rem",
57+
"slug": "small"
58+
},
59+
{
60+
"label": "M",
61+
"size": "1rem",
62+
"slug": "medium"
63+
},
64+
{
65+
"label": "L",
66+
"size": "1.125rem",
67+
"slug": "large"
68+
},
69+
{
70+
"label": "XL",
71+
"size": "1.5rem",
72+
"slug": "x-large"
73+
},
74+
{
75+
"label": "2XL",
76+
"size": "2rem",
77+
"slug": "xx-large"
78+
},
79+
{
80+
"label": "3XL",
81+
"size": "2.5rem",
82+
"slug": "xxx-large"
83+
},
84+
{
85+
"label": "4XL",
86+
"size": "3rem",
87+
"slug": "xxxx-large"
88+
},
89+
{
90+
"label": "5XL",
91+
"size": "4rem",
92+
"slug": "xxxxx-large"
93+
}
94+
]
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)