Skip to content
This repository has been archived by the owner on Jan 15, 2019. It is now read-only.

Sync customizer color settings to Gutenberg and frontend #373

Merged
merged 49 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
f1cce8f
Merge pull request #5 from WordPress/master
fabiankaegy Oct 18, 2018
418e655
Merge pull request #6 from WordPress/master
fabiankaegy Oct 18, 2018
f254535
Merge pull request #7 from WordPress/master
fabiankaegy Oct 19, 2018
924f4df
Merge pull request #8 from WordPress/master
fabiankaegy Oct 19, 2018
3fb0305
Merge pull request #9 from WordPress/master
fabiankaegy Oct 22, 2018
30a323f
Merge pull request #10 from WordPress/master
fabiankaegy Oct 23, 2018
ed41d5e
Added ability to change primary color of theme
fabiankaegy Oct 18, 2018
f5834d2
very crude working version
fabiankaegy Oct 24, 2018
2fd0c30
Fix minor variable typo
kjellr Oct 25, 2018
8f2f569
Merge branch 'master' into try/sync-customizer-colors-to-gutenberg
allancole Oct 27, 2018
a800aef
Fixing merge problems
allancole Oct 27, 2018
ee3040b
recompiling styles and fixing old hentry selectors
allancole Oct 27, 2018
a1a06f4
rename primary-color-hover strings
allancole Oct 27, 2018
816ea9e
Site title and site footer should not inherit color options
allancole Oct 27, 2018
629438c
syncing color styles between backend and frontend
allancole Oct 27, 2018
2962e9f
Fixes cover image editor colors
allancole Oct 27, 2018
4e18c94
Add custom color support for media&text block
allancole Oct 27, 2018
69d6ee0
SYncing button styles between editor and frontend
allancole Oct 27, 2018
7b1cefd
Clean up pull-quote styles in editor/frontend
allancole Oct 27, 2018
99ad81e
Cleaning up floating for pullquotes in editor
allancole Oct 27, 2018
aff372b
removing unused function
allancole Oct 27, 2018
410eb6f
Fix pullquote color overrides
allancole Oct 27, 2018
effdcbc
Cleaning up styles
allancole Oct 27, 2018
fd84047
recompiling styles
allancole Oct 27, 2018
9e45cb1
Adds HSL support for better contrast consitency
allancole Oct 28, 2018
8fe4e67
More improvements and code clean up
allancole Oct 28, 2018
d470e64
Various styling improvements for editor and frontend
allancole Oct 28, 2018
09cc925
Re-syncing with master, add HSL color support, add dynamic styles for…
allancole Oct 28, 2018
3c5a128
Code tidying
allancole Oct 28, 2018
2112c57
Avoid overwriting changes from #375
kjellr Oct 29, 2018
22636e6
Restore body selector to pullquote editor styles.
kjellr Oct 29, 2018
86a0abf
Compile editor styles.
kjellr Oct 29, 2018
fbca675
Set pullquote background color to custom color by default.
kjellr Oct 29, 2018
9d5e057
Fix issue where site title would appear black on single post pages wi…
kjellr Oct 29, 2018
cd896fc
Fix footer hover color, border for blockquotes inserted via classic e…
kjellr Oct 29, 2018
ce784c1
Add hover colors for Gutenberg text.
kjellr Oct 29, 2018
cd4891e
Fix solid color pullquote link color.
kjellr Oct 29, 2018
78a870a
Make sure we don't overwrite editor color styles for links that are o…
kjellr Oct 29, 2018
3935cff
Fixing display of text on blocks that use the custom primary color.
kjellr Oct 29, 2018
d36b6aa
Fix hover colors in post footers.
kjellr Oct 29, 2018
a6ea555
Fix classic editor blockquote color in Gutenberg.
kjellr Oct 29, 2018
8ccd08f
code tidying
allancole Oct 29, 2018
f3b83d3
Merge branch 'master' into try/sync-customizer-colors-to-gutenberg
allancole Oct 29, 2018
338751e
Add a default color option
allancole Oct 29, 2018
c134e19
Fixing travis.ci errors
allancole Oct 29, 2018
6a1ca76
recompiling styles
allancole Oct 29, 2018
4368a52
More fixes for travis.ci error
allancole Oct 29, 2018
04f8036
More travius.ci errors fixed
allancole Oct 29, 2018
2b84034
Fixeing more travis errors
allancole Oct 29, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,6 @@ function twentynineteen_setup() {
)
);

// Set up the WordPress core custom background feature.
add_theme_support(
'custom-background',
apply_filters(
'twentynineteen_custom_background_args',
array(
'default-color' => 'ffffff',
'default-image' => '',
)
)
);

/**
* Add support for core custom logo.
*
Expand Down Expand Up @@ -107,6 +95,17 @@ function twentynineteen_setup() {
// Enqueue editor styles
add_editor_style( 'style-editor.css' );

// Add custom color to the editor color palette
add_theme_support(
'editor-color-palette', array(
array(
'name' => esc_html__( 'Primary Color', 'twentynineteen' ),
'slug' => 'primary',
'color' => twentynineteen_hsl_hex( absint(get_theme_mod( 'primary-color', '199' )), 100, 33 ), // "#ff3333", // esc_attr( 'hsl('. absint( get_theme_mod( 'primary-color', '199' ) ) .', 100, 33)' ),
)
)
);

}
endif;
add_action( 'after_setup_theme', 'twentynineteen_setup' );
Expand Down Expand Up @@ -152,11 +151,43 @@ function twentynineteen_scripts() {
* Enqueue supplemental block editor styles
*/
function twentynineteen_editor_frame_styles() {

// Include color patterns
require_once( get_parent_theme_file_path( '/inc/color-patterns.php' ) );

wp_enqueue_style( 'twentynineteen-editor-frame-styles', get_theme_file_uri( '/style-editor-frame.css' ), false, '1.0', 'all' );
wp_add_inline_style( 'twentynineteen-editor-frame-styles', twentynineteen_custom_colors_css() );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the existing styles in this file move over to our usual editor stylesheet (#262), this file will be empty by default. That seems weird, right? This approach is functional for now, but I think we'll want to come up with a better way to handle this, probably in concert with the Gutenberg team.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, good catch! Agreed that there’s probably a more efficient way to get Gutenberg to catch these theme color overrides.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best way I've found for passing dynamic CSS to the new editor is to add it to the styles array in https://github.com/WordPress/gutenberg/blob/master/lib/client-assets.php#L1691

Registered editor stylesheets get processed here: https://github.com/WordPress/gutenberg/blob/master/lib/client-assets.php#L1567-L1581

The same thing can be done by passing the generated CSS string directly instead of a file. This will ensure that the CSS gets wrapped/scoped when #262 is merged.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure I’m following here @spencerfinnell

The best way I've found for passing dynamic CSS to the new editor is to add it to the styles array in https://github.com/WordPress/gutenberg/blob/master/lib/client-assets.php#L1691

The same thing can be done by passing the generated CSS string directly instead of a file.

When I pass an array with the css string to add_editor_style(), I get an “expects parameter 1 to be string” error. Can you explain in a little more detail or offer a snippet on how you did these two things?

}

add_action( 'enqueue_block_editor_assets', 'twentynineteen_editor_frame_styles' );

/**
* Display custom color CSS.
*/
function twentynineteen_colors_css_wrap() {

// Set defaults and grab theme mod
$default_primary_color = 199;
$primary_color = absint( get_theme_mod( 'primary-color', $default_primary_color ) );

if ( $primary_color === $default_primary_color ) {
return;
}

// Include color patterns
require_once( get_parent_theme_file_path( '/inc/color-patterns.php' ) );

// Enable live color changes in customizer
$customize_preview_data_primary_color = '';
if ( is_customize_preview() ) {
$customize_preview_data_primary_color = 'data-primary-color="' . $primary_color . '"';
} ?>
<style type="text/css" id="custom-theme-colors" <?php echo $customize_preview_data_primary_color; ?>>
<?php echo twentynineteen_custom_colors_css(); ?>
</style>
<?php
}
add_action( 'wp_head', 'twentynineteen_colors_css_wrap' );

/**
* SVG Icons class.
*/
Expand Down
193 changes: 193 additions & 0 deletions inc/color-patterns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php
/**
* Twenty Nineteen: Color Patterns
*
* @package WordPress
* @subpackage TwentyNineteen
* @since 1.0
*/

/**
* Generate the CSS for the current custom color scheme.
*/
function twentynineteen_custom_colors_css() {

$default_primary_color = 199;
$primary_color = absint( get_theme_mod( 'primary-color', $default_primary_color ) );

/**
* Filter Twenty Nineteen default saturation level.
*
* @since Twenty Nineteen 1.0
*
* @param int $saturation Color saturation level.
*/

$saturation = absint( apply_filters( 'twentynineteen_custom_colors_saturation', 100 ) );
$reduced_saturation = ( .8 * $saturation ) . '%';
$saturation = $saturation . '%';

$theme_css = '
/*
* Set background for:
* - featured image :before
* - featured image :before
* - post thumbmail :before
* - post thumbmail :before
* - Submenu
* - Sticky Post
* - buttons
* - WP Block Button
* - Blocks
*/
.image-filters-enabled .site-header.featured-image .site-featured-image:before,
.image-filters-enabled .site-header.featured-image .site-featured-image:after,
.image-filters-enabled .entry .post-thumbnail:before,
.image-filters-enabled .entry .post-thumbnail:after,
.main-navigation .sub-menu,
.sticky-post,
.entry-content .wp-block-button .wp-block-button__link,
.button, button, input[type="button"], input[type="reset"], input[type="submit"],
.entry-content > *[class^="wp-block-"].has-primary-background-color,
.entry-content > *[class^="wp-block-"] .has-primary-background-color,
.entry-content > *[class^="wp-block-"].is-style-solid-color .has-primary-background-color,
.entry-content .wp-block-file .wp-block-file__button {
background-color: hsl( ' . $primary_color . ', ' . $saturation . ', 33% ); /* base: #0073a8; */
}

/*
* Set Color for:
* - all links
* - main navigation links
* - Post navigation links
* - Post entry meta hover
* - Post entry header more-link hover
* - main navigation svg
* - comment navigation
* - Comment edit link hover
* - Site Footer Link hover
*/
a,
a:visited,
.main-navigation ul.main-menu > li > a,
.post-navigation .post-title,
.entry .entry-meta a:hover,
.entry .entry-footer a:hover,
.entry .entry-content .more-link:hover,
.main-navigation .main-menu > li > a + svg,
.comment-navigation .nav-previous a:hover,
.comment-navigation .nav-next a:hover,
.comment .comment-metadata .comment-edit-link:hover,
.site-footer a:hover,
.entry-content .wp-block-button.is-style-outline .wp-block-button__link,
.entry-content .wp-block-button.is-style-outline .wp-block-button__link,
.entry-content .wp-block-button.is-style-outline .wp-block-button__link,
.entry-content > *[class^="wp-block-"] .has-primary-color,
.entry-content > *[class^="wp-block-"].is-style-solid-color .has-primary-color {
color: hsl( ' . $primary_color . ', ' . $saturation . ', 33% ); /* base: #0073a8; */
}

/*
* Set left border color for:
* wp block quote
*/
.entry-content .wp-block-quote:not(.is-large), .entry-content .wp-block-quote:not(.is-style-large) {
border-left-color: hsl( ' . $primary_color . ', ' . $saturation . ', 33% ); /* base: #0073a8; */
}

/*
* Set border color for:
* :focus
*/
input[type="text"]:focus,
input[type="email"]:focus,
input[type="url"]:focus,
input[type="password"]:focus,
input[type="search"]:focus,
input[type="number"]:focus,
input[type="tel"]:focus,
input[type="range"]:focus,
input[type="date"]:focus,
input[type="month"]:focus,
input[type="week"]:focus,
input[type="time"]:focus,
input[type="datetime"]:focus,
input[type="datetime-local"]:focus,
input[type="color"]:focus,
textarea:focus {
border-color: hsl( ' . $primary_color . ', ' . $saturation . ', 33% ); /* base: #0073a8; */
}

.gallery-item > div > a:focus {
box-shadow: 0 0 0 2px hsl( ' . $primary_color . ', ' . $saturation . ', 33% ); /* base: #0073a8; */
}

/* Hover colors */
a:hover, a:active,
.main-navigation .main-menu > li > a:hover,
.main-navigation .main-menu > li > a:hover + svg,
.post-navigation .nav-links a:hover,
.comment .comment-author .fn a:hover,
.comment-reply-link:hover,
#cancel-comment-reply-link:hover {
color: hsl( ' . $primary_color . ', ' . $saturation . ', 23% ); /* base: #005177; */
}

.main-navigation .sub-menu > li > a:hover,
.main-navigation .sub-menu > li > a:focus,
.main-navigation .sub-menu > li > a:hover:after,
.main-navigation .sub-menu > li > a:focus:after {
background: hsl( ' . $primary_color . ', ' . $saturation . ', 23% ); /* base: #005177; */
}';

$editor_css = '
/*
* Set colors for:
* - links
* - blockquote
* - pullquote (solid color)
* - buttons
*/
.editor-block-list__layout .editor-block-list__block a,
.editor-block-list__layout .editor-block-list__block .wp-block-button.is-style-outline .wp-block-button__link:not(.has-text-color),
.editor-block-list__layout .editor-block-list__block .wp-block-button.is-style-outline:hover .wp-block-button__link:not(.has-text-color),
.editor-block-list__layout .editor-block-list__block .wp-block-button.is-style-outline:focus .wp-block-button__link:not(.has-text-color),
.editor-block-list__layout .editor-block-list__block .wp-block-button.is-style-outline:active .wp-block-button__link:not(.has-text-color) {
color: hsl( ' . $primary_color . ', ' . $saturation . ', 33% ); /* base: #0073a8; */
}

.editor-block-list__layout .editor-block-list__block .wp-block-quote:not(.is-large):not(.is-style-large),
.editor-block-list__layout .wp-block-freeform blockquote {
border-left: 2px solid hsl( ' . $primary_color . ', ' . $saturation . ', 33% ); /* base: #0073a8; */
}

.editor-block-list__layout .editor-block-list__block .wp-block-pullquote.is-style-solid-color:not(.has-background-color) {
background-color: hsl( ' . $primary_color . ', ' . $saturation . ', 33% ); /* base: #0073a8; */
}

.editor-block-list__layout .editor-block-list__block .wp-block-file .wp-block-file__button,
.editor-block-list__layout .editor-block-list__block .wp-block-button:not(.is-style-outline) .wp-block-button__link,
.editor-block-list__layout .editor-block-list__block .wp-block-button:not(.is-style-outline) .wp-block-button__link:active,
.editor-block-list__layout .editor-block-list__block .wp-block-button:not(.is-style-outline) .wp-block-button__link:focus,
.editor-block-list__layout .editor-block-list__block .wp-block-button:not(.is-style-outline) .wp-block-button__link:hover {
background-color: hsl( ' . $primary_color . ', ' . $saturation . ', 33% ); /* base: #0073a8; */
}';

$css = '';
if ( function_exists( 'register_block_type' ) && is_admin() ) {
$css .= $editor_css;
} else if ( ! is_admin() ) {
$css = $theme_css;
}

/**
* Filters Twenty Nineteen custom colors CSS.
*
* @since Twenty Nineteen 1.0
*
* @param string $css Base theme colors CSS.
* @param int $primary_color The user's selected color hue.
* @param string $saturation Filtered theme color saturation level.
*/
return apply_filters( 'twentynineteen_custom_colors_css', $css, $primary_color, $saturation );
}
20 changes: 20 additions & 0 deletions inc/customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ function twentynineteen_customize_register( $wp_customize ) {
);
}

// Add page primary color setting and control.
$wp_customize->add_setting( 'primary-color',
array(
'default' => 199,
'transport' => 'postMessage',
'sanitize_callback' => 'absint',
)
);

$wp_customize->add_control(
new WP_Customize_Color_Control( $wp_customize, 'primary-color',
array(
'label' => __( 'Primary Color' ),
'description' => __( 'Changes the Color of the Featured Image overlay, Buttons, Links etc.' ),
'section' => 'colors',
'mode' => 'hue',
)
)
);

$wp_customize->add_setting(
'image_filter',
array(
Expand Down
82 changes: 82 additions & 0 deletions inc/template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,85 @@ function twentynineteen_add_dropdown_icons( $output, $item, $depth, $args ) {
return $output;
}
add_filter( 'walker_nav_menu_start_el', 'twentynineteen_add_dropdown_icons', 10, 4 );

/**
* Convert HSL to HEX colors
*/
function twentynineteen_hsl_hex($h, $s, $l, $to_hex = true) {

$h /= 360;
$s /= 100;
$l /= 100;

$r = $l;
$g = $l;
$b = $l;
$v = ($l <= 0.5) ? ($l * (1.0 + $s)) : ($l + $s - $l * $s);
if ($v > 0) {
$m;
$sv;
$sextant;
$fract;
$vsf;
$mid1;
$mid2;

$m = $l + $l - $v;
$sv = ($v - $m ) / $v;
$h *= 6.0;
$sextant = floor($h);
$fract = $h - $sextant;
$vsf = $v * $sv * $fract;
$mid1 = $m + $vsf;
$mid2 = $v - $vsf;

switch ($sextant) {
case 0:
$r = $v;
$g = $mid1;
$b = $m;
break;
case 1:
$r = $mid2;
$g = $v;
$b = $m;
break;
case 2:
$r = $m;
$g = $v;
$b = $mid1;
break;
case 3:
$r = $m;
$g = $mid2;
$b = $v;
break;
case 4:
$r = $mid1;
$g = $m;
$b = $v;
break;
case 5:
$r = $v;
$g = $m;
$b = $mid2;
break;
}
}
$r = round($r * 255, 0);
$g = round($g * 255, 0);
$b = round($b * 255, 0);

if ($to_hex) {

$r = ($r < 15)? '0' . dechex($r) : dechex($r);
$g = ($g < 15)? '0' . dechex($g) : dechex($g);
$b = ($b < 15)? '0' . dechex($b) : dechex($b);

return "#$r$g$b";

} else {

return "rgb($r, $g, $b)";
}
}
7 changes: 0 additions & 7 deletions inc/ui-icons.php

This file was deleted.

Loading