Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Styles: Remove the ! important tag from user predefined style selections #40121

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
6 changes: 3 additions & 3 deletions lib/compat/wordpress-5.9/class-wp-theme-json-5-9.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ protected static function get_blocks_metadata() {
foreach ( static::ELEMENTS as $el_name => $el_selector ) {
$element_selector = array();
foreach ( $block_selectors as $selector ) {
$element_selector[] = $selector . ' ' . $el_selector;
$element_selector[] = ':where(' . $selector . ' ' . $el_selector . ')';
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
}
static::$blocks_metadata[ $block_name ]['elements'][ $el_name ] = implode( ',', $element_selector );
}
Expand Down Expand Up @@ -958,7 +958,7 @@ protected static function compute_preset_classes( $settings, $selector, $origins
array(
array(
'name' => $property,
'value' => 'var(' . $css_var . ') !important',
'value' => 'var(' . $css_var . ')',
),
)
);
Expand Down Expand Up @@ -1409,7 +1409,7 @@ protected static function get_style_nodes( $theme_json, $selectors = array() ) {
foreach ( $theme_json['styles']['elements'] as $element => $node ) {
$nodes[] = array(
'path' => array( 'styles', 'elements', $element ),
'selector' => static::ELEMENTS[ $element ],
'selector' => ':where(' . static::ELEMENTS[ $element ] . ')',
);
}
}
Expand Down
10 changes: 7 additions & 3 deletions packages/block-library/src/button/style.scss
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// This variable is repeated across Button, Buttons, and Buttons editor styles.
$blocks-block__margin: 0.5em;

:where(.wp-block-button__link) {
color: $white;
background-color: #32373c;
font-size: 1.125em;
}

// Prefer the link selector instead of the regular button classname
// to support the previous markup in addition to the new one.
.wp-block-button__link {
color: $white;
background-color: #32373c;

border-radius: 9999px; // 100% causes an oval, but any explicit but really high value retains the pill shape.
box-shadow: none;
cursor: pointer;
display: inline-block;
font-size: 1.125em;
padding: calc(0.667em + 2px) calc(1.333em + 2px); // The extra 2px are added to size solids the same as the outline versions.
text-align: center;
text-decoration: none;
Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/code/theme.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
:where(.wp-block-code) {
font-family: Menlo, Consolas, monaco, monospace;
}
.wp-block-code {
border: 1px solid #ccc;
border-radius: 4px;
font-family: Menlo, Consolas, monaco, monospace;
padding: 0.8em 1em;
}
5 changes: 4 additions & 1 deletion packages/block-library/src/pullquote/theme.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
.wp-block-pullquote {
:where(.wp-block-pullquote) {
border-top: 4px solid currentColor;
border-bottom: 4px solid currentColor;
margin-bottom: 1.75em;
color: currentColor;
}
.wp-block-pullquote {
margin-bottom: 1.75em;

cite,
footer,
Expand Down
6 changes: 4 additions & 2 deletions packages/block-library/src/search/style.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.wp-block-search__button {
:where(.wp-block-search__button) {
background: #f7f7f7;
border: 1px solid #ccc;
padding: 0.375em 0.625em;
color: #32373c;
}
.wp-block-search__button {
padding: 0.375em 0.625em;
margin-left: 0.625em;
word-break: normal;
font-size: inherit;
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/separator/style.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.wp-block-separator {
:where(.wp-block-separator) {
border-top: 1px solid currentColor;
border-bottom: 1px solid currentColor;
}
.wp-block-separator {
// Default, thin style, is stored in theme.scss so it can be opted out of

// Dots style
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/table/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
"width": true
}
},
"__experimentalSelector": ".wp-block-table > table"
"__experimentalSelector": ":where(.wp-block-table > table)"
},
"styles": [
{
Expand Down
46 changes: 4 additions & 42 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ function test_get_stylesheet() {

$variables = 'body{--wp--preset--color--grey: grey;--wp--preset--font-family--small: 14px;--wp--preset--font-family--big: 41px;}.wp-block-group{--wp--custom--base-font: 16;--wp--custom--line-height--small: 1.2;--wp--custom--line-height--medium: 1.4;--wp--custom--line-height--large: 1.8;}';
$styles = 'body { margin: 0; }body{color: var(--wp--preset--color--grey);}.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-site-blocks > * { margin-block-start: 0; margin-block-end: 0; }.wp-site-blocks > * + * { margin-block-start: var( --wp--style--block-gap ); }a{background-color: #333;color: #111;}.wp-block-group{border-radius: 10px;padding: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.wp-block-image{border-top-left-radius: 10px;border-bottom-right-radius: 1em;margin-bottom: 30px;}';
glendaviesnz marked this conversation as resolved.
Show resolved Hide resolved
$presets = '.has-grey-color{color: var(--wp--preset--color--grey) !important;}.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}.has-grey-border-color{border-color: var(--wp--preset--color--grey) !important;}.has-small-font-family{font-family: var(--wp--preset--font-family--small) !important;}.has-big-font-family{font-family: var(--wp--preset--font-family--big) !important;}';
$presets = '.has-grey-color{color: var(--wp--preset--color--grey);}.has-grey-background-color{background-color: var(--wp--preset--color--grey);}.has-grey-border-color{border-color: var(--wp--preset--color--grey);}.has-small-font-family{font-family: var(--wp--preset--font-family--small);}.has-big-font-family{font-family: var(--wp--preset--font-family--big);}';
$all = $variables . $styles . $presets;
$this->assertEquals( $all, $theme_json->get_stylesheet() );
$this->assertEquals( $styles, $theme_json->get_stylesheet( array( 'styles' ) ) );
Expand Down Expand Up @@ -559,7 +559,7 @@ function test_get_stylesheet_preset_classes_work_with_compounded_selectors() {
);

$this->assertEquals(
'h1.has-white-color,h2.has-white-color,h3.has-white-color,h4.has-white-color,h5.has-white-color,h6.has-white-color{color: var(--wp--preset--color--white) !important;}h1.has-white-background-color,h2.has-white-background-color,h3.has-white-background-color,h4.has-white-background-color,h5.has-white-background-color,h6.has-white-background-color{background-color: var(--wp--preset--color--white) !important;}h1.has-white-border-color,h2.has-white-border-color,h3.has-white-border-color,h4.has-white-border-color,h5.has-white-border-color,h6.has-white-border-color{border-color: var(--wp--preset--color--white) !important;}',
'h1.has-white-color,h2.has-white-color,h3.has-white-color,h4.has-white-color,h5.has-white-color,h6.has-white-color{color: var(--wp--preset--color--white);}h1.has-white-background-color,h2.has-white-background-color,h3.has-white-background-color,h4.has-white-background-color,h5.has-white-background-color,h6.has-white-background-color{background-color: var(--wp--preset--color--white);}h1.has-white-border-color,h2.has-white-border-color,h3.has-white-border-color,h4.has-white-border-color,h5.has-white-border-color,h6.has-white-border-color{border-color: var(--wp--preset--color--white);}',
$theme_json->get_stylesheet( array( 'presets' ) )
);
}
Expand Down Expand Up @@ -595,7 +595,7 @@ function test_get_stylesheet_preset_rules_come_after_block_rules() {
);

$styles = 'body { margin: 0; }.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-block-group{color: red;}';
$presets = '.wp-block-group.has-grey-color{color: var(--wp--preset--color--grey) !important;}.wp-block-group.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}.wp-block-group.has-grey-border-color{border-color: var(--wp--preset--color--grey) !important;}';
$presets = '.wp-block-group.has-grey-color{color: var(--wp--preset--color--grey);}.wp-block-group.has-grey-background-color{background-color: var(--wp--preset--color--grey);}.wp-block-group.has-grey-border-color{border-color: var(--wp--preset--color--grey);}';
$variables = '.wp-block-group{--wp--preset--color--grey: grey;}';
$all = $variables . $styles . $presets;
$this->assertEquals( $all, $theme_json->get_stylesheet() );
Expand Down Expand Up @@ -637,7 +637,7 @@ function test_get_stylesheet_generates_proper_classes_and_css_vars_from_slugs()
);

$this->assertEquals(
'.has-grey-color{color: var(--wp--preset--color--grey) !important;}.has-dark-grey-color{color: var(--wp--preset--color--dark-grey) !important;}.has-light-grey-color{color: var(--wp--preset--color--light-grey) !important;}.has-white-2-black-color{color: var(--wp--preset--color--white-2-black) !important;}.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}.has-dark-grey-background-color{background-color: var(--wp--preset--color--dark-grey) !important;}.has-light-grey-background-color{background-color: var(--wp--preset--color--light-grey) !important;}.has-white-2-black-background-color{background-color: var(--wp--preset--color--white-2-black) !important;}.has-grey-border-color{border-color: var(--wp--preset--color--grey) !important;}.has-dark-grey-border-color{border-color: var(--wp--preset--color--dark-grey) !important;}.has-light-grey-border-color{border-color: var(--wp--preset--color--light-grey) !important;}.has-white-2-black-border-color{border-color: var(--wp--preset--color--white-2-black) !important;}',
'.has-grey-color{color: var(--wp--preset--color--grey);}.has-dark-grey-color{color: var(--wp--preset--color--dark-grey);}.has-light-grey-color{color: var(--wp--preset--color--light-grey);}.has-white-2-black-color{color: var(--wp--preset--color--white-2-black);}.has-grey-background-color{background-color: var(--wp--preset--color--grey);}.has-dark-grey-background-color{background-color: var(--wp--preset--color--dark-grey);}.has-light-grey-background-color{background-color: var(--wp--preset--color--light-grey);}.has-white-2-black-background-color{background-color: var(--wp--preset--color--white-2-black);}.has-grey-border-color{border-color: var(--wp--preset--color--grey);}.has-dark-grey-border-color{border-color: var(--wp--preset--color--dark-grey);}.has-light-grey-border-color{border-color: var(--wp--preset--color--light-grey);}.has-white-2-black-border-color{border-color: var(--wp--preset--color--white-2-black);}',
$theme_json->get_stylesheet( array( 'presets' ) )
);
$this->assertEquals(
Expand All @@ -647,44 +647,6 @@ function test_get_stylesheet_generates_proper_classes_and_css_vars_from_slugs()

}

public function test_get_stylesheet_preset_values_are_marked_as_important() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'palette' => array(
array(
'slug' => 'grey',
'color' => 'grey',
),
),
),
),
'styles' => array(
'blocks' => array(
'core/paragraph' => array(
'color' => array(
'text' => 'red',
'background' => 'blue',
),
'typography' => array(
'fontSize' => '12px',
'lineHeight' => '1.3',
),
),
),
),
),
'default'
);

$this->assertEquals(
'body{--wp--preset--color--grey: grey;}body { margin: 0; }.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }p{background-color: blue;color: red;font-size: 12px;line-height: 1.3;}.has-grey-color{color: var(--wp--preset--color--grey) !important;}.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}.has-grey-border-color{border-color: var(--wp--preset--color--grey) !important;}',
$theme_json->get_stylesheet()
);
}

public function test_merge_incoming_data() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
Expand Down