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
Blockbase: Add support for custom palettes customization #4098
Merged
scruffian
merged 23 commits into
trunk
from
try/blockbase-customizer-alternative-palettes
Jun 28, 2021
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
933aa2c
first pass at color palette controls without functionality
MaggieCabrera 284b68e
made palettes look like color annotations plugin
MaggieCabrera 2f66ce8
make color selection functional
scruffian 47c697c
get palettes from theme.json
scruffian 50ac20e
rename file
scruffian dd15473
rename colors preview file
scruffian d55abbd
rename files
scruffian 753e1ef
more file renaming for clarity
scruffian ec413a7
simplify settings
scruffian 049c68f
rebuilt mayland and seedlet with alternative palettes
MaggieCabrera 8bf752c
renamed default template, dynamic theme name
MaggieCabrera e73bf28
remove space
scruffian ebf38dc
Added default Varia/Seedlet palettes to blockbase
MaggieCabrera a61c6ac
check if the custom palette array is empty
MaggieCabrera e0e9522
Only render color palette picker controls if there are alternatives t…
pbking 5808c73
Only render color palette picker controls if there are alternatives t…
pbking 555c9b1
Removed commented code
pbking ddc7b0b
change palettes to an array
scruffian df3dcb0
chagne props comment
scruffian 842dc5d
removed unnecesary credit
MaggieCabrera 28f033b
undo last commit, brought back the credits
MaggieCabrera 2a80ac9
readd the settings file
scruffian 5d328b8
rebuild children
scruffian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,6 +116,41 @@ | |
"secondary": "var(--wp--preset--color--secondary)", | ||
"selection": "var(--wp--preset--color--selection)" | ||
}, | ||
"colorPalettes": [ | ||
{ | ||
"label": "Featured", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is a mechanism now for labels for presets to get translated, but is there a way yet to mark that a custom variable needs to be translated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we do it like this. Since we haven't done this for any of the strings that we have (such as the template parts) I think this merits its own separate PR |
||
"slug": "palette-1", | ||
"colors": { | ||
"primary": "#C8133E", | ||
"secondary": "#4E2F4B", | ||
"foreground": "#1D1E1E", | ||
"background": "#FFFFFF", | ||
"selection": "#F9F9F9" | ||
} | ||
}, | ||
{ | ||
"label": "Featured", | ||
"slug": "palette-2", | ||
"colors": { | ||
"primary": "#35845D", | ||
"secondary": "#233252", | ||
"foreground": "#242527", | ||
"background": "#EEF4F7", | ||
"selection": "#F9F9F9" | ||
} | ||
}, | ||
{ | ||
"label": "Featured", | ||
"slug": "palette-3", | ||
"colors": { | ||
"primary": "#9FD3E8", | ||
"secondary": "#FBE6AA", | ||
"foreground": "#FFFFFF", | ||
"background": "#1F2527", | ||
"selection": "#364043" | ||
} | ||
} | ||
], | ||
"form": { | ||
"padding": "calc( 0.5 * var(--wp--custom--margin--horizontal) )", | ||
"border": { | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm getting an error here w/ Quadrat due to there being no palette's currently defined in theme.json
Likely the Quadrat palette options weren't included in the change (yet).
But seems reasonable that a BB child might not ship with any customizations so probably a relevant use-case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, that fixed disappeared on a conflict, let me get it back
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be fine now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed this small change to only render these controls if there are alternatives to default to show. Also addressed that error that seemed to stick around despite the recent change.