This repository has been archived by the owner on Feb 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Blockbase: Add support for custom palettes customization (#4098)
* first pass at color palette controls without functionality * made palettes look like color annotations plugin * make color selection functional * get palettes from theme.json * rename file * rename colors preview file * rename files * more file renaming for clarity * simplify settings * rebuilt mayland and seedlet with alternative palettes * renamed default template, dynamic theme name * remove space * Added default Varia/Seedlet palettes to blockbase * check if the custom palette array is empty * Only render color palette picker controls if there are alternatives to default. * Only render color palette picker controls if there are alternatives to default. (fix) * Removed commented code * change palettes to an array * chagne props comment * removed unnecesary credit * undo last commit, brought back the credits * readd the settings file * rebuild children Co-authored-by: Ben Dwyer <[email protected]> Co-authored-by: Jason Crist <[email protected]>
- Loading branch information
1 parent
c562229
commit 9e49d27
Showing
15 changed files
with
420 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
.customize-control-color-palette .color-palette-group { | ||
display: grid; | ||
flex-wrap: wrap; | ||
grid-template-columns: 1fr 1fr 1fr; | ||
column-gap: 15px; | ||
row-gap: 15px; | ||
} | ||
|
||
.customize-control-color-palette .color-palette-group input { | ||
display: none; | ||
} | ||
|
||
.customize-control-color-palette .color-palette-group .color-option { | ||
padding: 0; | ||
} | ||
|
||
.customize-control-color-palette input:checked + .color-option .custom-color-palette { | ||
border-color: #2271b1; | ||
} | ||
|
||
.custom-color-palette { | ||
position: relative; | ||
display: flex; | ||
border-radius: 2px; | ||
border: 1px solid rgba(0,0,0,0.1); | ||
height: 50px; | ||
width: 72px; | ||
overflow: hidden; | ||
} | ||
|
||
.color-palette-label { | ||
background: #000; | ||
color: #fff; | ||
font-size: 10px; | ||
opacity: 0.7; | ||
position: absolute; | ||
bottom: 5px; | ||
right: 0; | ||
left: 0; | ||
text-indent: 5px; | ||
line-height: 1.5; | ||
padding-bottom: 1px; | ||
} | ||
|
||
.color-stripe { | ||
flex: 1 1 auto; | ||
border: 1px solid rgba(0,0,0,0.1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
wp.customize( 'color_palette', ( value ) => { | ||
value.bind( ( newValue ) => { | ||
const newPalette = colorPalettes[ newValue ].colors; | ||
Object.keys( newPalette ).forEach( function ( slug ) { | ||
const color = newPalette[ slug ]; | ||
wp.customize | ||
.control( 'customize-global-styles' + slug ) | ||
.setting.set( color ); | ||
} ); | ||
} ); | ||
} ); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
require_once 'wp-customize-palette-control.php'; | ||
|
||
class GlobalStylesColorPalettes { | ||
private $palettes = array(); | ||
|
||
function __construct() { | ||
add_action( 'customize_register', array( $this, 'color_palette_control' ) ); | ||
add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_preview_js' ) ); | ||
} | ||
|
||
function customize_preview_js() { | ||
wp_enqueue_script( 'customizer-color-palettes', get_template_directory_uri() . '/inc/color-palettes-controls.js', array( 'customize-controls' ) ); | ||
wp_localize_script( 'customizer-color-palettes', 'colorPalettes', $this->palettes ); | ||
} | ||
|
||
function build_palettes( $theme_json ) { | ||
|
||
$default_palette = $theme_json['settings']['color']['palette']['theme']; | ||
|
||
$default_palette_setting = array(); | ||
foreach ( $default_palette as $default_color ) { | ||
$default_palette_setting[ $default_color['slug'] ] = $default_color['color']; | ||
} | ||
|
||
$this->palettes['default-palette'] = array( | ||
'label' => 'Default', | ||
'colors' => $default_palette_setting, | ||
); | ||
|
||
$custom_palettes = $theme_json['settings']['custom']['colorPalettes']; | ||
if ( ! empty( $custom_palettes ) ) { | ||
foreach ( $custom_palettes as $custom_palette ) { | ||
$custom_palette_setting = array(); | ||
foreach ( $custom_palette['colors'] as $color_slug => $color ) { | ||
$custom_palette_setting[ $color_slug ] = $color; | ||
} | ||
|
||
$this->palettes[ $custom_palette['slug'] ] = array( | ||
'label' => $custom_palette['label'], | ||
'colors' => $custom_palette_setting, | ||
); | ||
} | ||
} | ||
} | ||
|
||
function color_palette_control( $wp_customize ) { | ||
|
||
$theme_json = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_raw_data(); | ||
|
||
if ( ! isset( $theme_json['settings']['custom']['colorPalettes'] ) ) { | ||
return; | ||
} | ||
|
||
$this->build_palettes( $theme_json ); | ||
|
||
$wp_customize->add_setting( | ||
'color_palette', | ||
array( | ||
'default' => 'default-palette', | ||
'capability' => 'edit_theme_options', | ||
'transport' => 'postMessage', // We need this to stop the page refreshing. | ||
) | ||
); | ||
|
||
$wp_customize->add_control( | ||
new Color_Palette_Control( | ||
$wp_customize, | ||
'color_palette', | ||
array( | ||
'label' => __( 'Color Scheme', 'blockbase' ), | ||
'description' => __( 'Choose a color scheme for your website.', 'blockbase' ), | ||
'section' => 'customize-global-styles', | ||
'choices' => $this->palettes, | ||
'settings' => 'color_palette', | ||
) | ||
) | ||
); | ||
|
||
} | ||
} | ||
|
||
new GlobalStylesColorPalettes; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* Color Palette Customizer Control | ||
* | ||
* Based on https://github.com/HardeepAsrani/o2 | ||
*/ | ||
|
||
if ( class_exists( 'WP_Customize_Control' ) && ! class_exists( 'Color_Palette_Control' ) ) { | ||
class Color_Palette_Control extends WP_Customize_Control { | ||
|
||
public $type = 'color-palette'; | ||
|
||
public function enqueue() { | ||
wp_enqueue_style( 'color-customization', get_template_directory_uri() . '/inc/color-customization.css' ); | ||
} | ||
|
||
public function render_content() { | ||
?> | ||
<label> | ||
<?php if ( ! empty( $this->label ) ) : ?> | ||
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span> | ||
<?php endif; | ||
if ( ! empty( $this->description ) ) : ?> | ||
<span class="description customize-control-description"><?php echo esc_html( $this->description ); ?></span> | ||
<?php endif; ?> | ||
<div class="color-palette-group"> | ||
<?php foreach ( $this->choices as $value => $label ) : ?> | ||
<input name="color_palette_<?php echo esc_attr( $this->id ); ?>" id="color_palette_<?php echo esc_attr( $this->id ); ?>_<?php echo esc_attr( $value ); ?>" type="radio" value="<?php echo esc_attr( $value ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> > | ||
<label for="color_palette_<?php echo esc_attr( $this->id ); ?>_<?php echo esc_attr( $value ); ?>" class="color-option"> | ||
<div class="custom-color-palette"> | ||
<span class="color-palette-label"><?php echo esc_attr( $label['label'] ); ?></span> | ||
<?php foreach ( $label['colors'] as $slug => $color ) : ?> | ||
<div class="color-stripe" style="background-color: <?php echo esc_attr( $color ); ?>"> </div> | ||
<?php endforeach; ?> | ||
</tr> | ||
</div> | ||
</label> | ||
</input> | ||
<?php endforeach; ?> | ||
</div> | ||
</label> | ||
<?php } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.