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

Reduce Specificity of Input Styles #18957

Open
mikeselander opened this issue Dec 5, 2019 · 1 comment
Open

Reduce Specificity of Input Styles #18957

mikeselander opened this issue Dec 5, 2019 · 1 comment
Labels
CSS Styling Related to editor and front end styles, CSS-specific issues. [Feature] UI Components Impacts or related to the UI component system [Type] Enhancement A suggestion for improvement.

Comments

@mikeselander
Copy link
Contributor

Describe the bug
This is in-kind with #10067; I am opening this ticket as a distinct example of over-specificity needing to be fixed.

The specificity of the block input styles causes custom styles which are correctly using simple classnames to get trampled over.

For example, let's say that I have a custom stylesheet which I have loaded in via add_editor_style. This stylesheet defines how inputs should look within a custom block, but uses a company-wide design library, and therefore has styles which cannot be modified just for the G'berg editor.

This stylesheet has well-written selectors that only use classes such as .client-input {...}. This then gets converted to .editor-styles-wrapper .client-input {...} after being passed through add_editor_style. Core has an (ahem) comprehensive set of selectors for every single (or just about) type of input which are compiled to .block-editor-block-list__block input[type="number"].

These appear to be defined here:

@mixin reset {
box-sizing: border-box;
*,
*::before,
*::after {
box-sizing: inherit;
}
.input-control, // Upstream name is `.regular-text`.
input[type="text"],
input[type="search"],
input[type="radio"],
input[type="tel"],
input[type="time"],
input[type="url"],
input[type="week"],
input[type="password"],
input[type="checkbox"],
input[type="color"],
input[type="date"],
input[type="datetime"],
input[type="datetime-local"],
input[type="email"],
input[type="month"],
input[type="number"],
select,
textarea {
font-family: $default-font;
padding: 6px 8px;
@include input-style__neutral();
/* Fonts smaller than 16px causes mobile safari to zoom. */
font-size: $mobile-text-min-font-size;
/* Override core line-height. To be reviewed. */
line-height: normal;
@include break-small {
font-size: $default-font-size;
/* Override core line-height. To be reviewed. */
line-height: normal;
}
&:focus {
@include input-style__focus();
}
}
input[type="number"] {
padding-left: 4px;
padding-right: 4px;
}
select {
padding: 2px;
font-size: $default-font-size;
color: $dark-gray-500;
&:focus {
border-color: $blue-medium-600;
// Windows High Contrast mode will show this outline
outline: 2px solid transparent;
outline-offset: 0;
}
}
input[type="checkbox"],
input[type="radio"] {
border: $border-width + 1 solid $dark-gray-300;
margin-right: 12px;
transition: none;
&:focus {
border-color: $dark-gray-300;
box-shadow: 0 0 0 1px $dark-gray-300;
}
&:checked {
background: theme(toggle);
border-color: theme(toggle);
}
&:checked:focus {
box-shadow: 0 0 0 2px $dark-gray-500;
}
}
input[type="checkbox"] {
border-radius: $radius-round-rectangle / 2;
&:checked::before,
&[aria-checked="mixed"]::before {
margin: -3px -5px;
color: $white;
@include break-medium() {
margin: -4px 0 0 -5px;
}
}
&[aria-checked="mixed"] {
background: theme(toggle);
border-color: theme(toggle);
&::before {
// Inherited from `forms.css`.
// See: https://github.com/WordPress/wordpress-develop/tree/5.1.1/src/wp-admin/css/forms.css#L122-L132
content: "\f460";
float: left;
display: inline-block;
vertical-align: middle;
width: 16px;
/* stylelint-disable */
font: normal 30px/1 dashicons;
/* stylelint-enable */
speak: none;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
@include break-medium() {
float: none;
font-size: 21px;
}
}
&:focus {
box-shadow: 0 0 0 2px $dark-gray-500;
}
}
}
// We provide explicit pixel dimensions to ensure a crisp appearance.
// This radio button style should be ported upstream.
input[type="radio"] {
border-radius: $radius-round;
&:checked::before {
width: 6px;
height: 6px;
margin: 6px 0 0 6px;
background-color: $white;
@include break-medium() {
margin: 3px 0 0 3px;
}
}
}
// Placeholder colors
input,
textarea {
// Use opacity to work in various editor styles.
&::-webkit-input-placeholder {
color: $dark-opacity-300;
}
&::-moz-placeholder {
opacity: 1; // Necessary because Firefox reduces this from 1.
color: $dark-opacity-300;
}
&:-ms-input-placeholder {
color: $dark-opacity-300;
}
.is-dark-theme & {
&::-webkit-input-placeholder {
color: $light-opacity-300;
}
&::-moz-placeholder {
opacity: 1; // Necessary because Firefox reduces this from 1.
color: $light-opacity-300;
}
&:-ms-input-placeholder {
color: $light-opacity-300;
}
}
}
}

This leaves our custom styles with a specificity of 0,0,2,0, whereas core has a specificity of 0,0,2,1 which just barely overrides a well-written selector. This means that the inputs on my custom block are no longer in sync with the frontend and I have confused editors. My only options are to start a specificity war with G'berg's styles or use !important. Both are terrible options and should be unnecessary.

Expected behavior
I would expect the inputs to be styled only with a tag selector and a parent class. For example, .block-editor-block-list__block input or potentially apply the styles only to specific core blocks or to core input components.

@talldan
Copy link
Contributor

talldan commented Jan 24, 2020

@mikeselander This is a fair point and I think it also affects the reusability of the components in @wordpress/components that various styles are mixins instead of part of the components themselves.

@talldan talldan added [Feature] UI Components Impacts or related to the UI component system [Type] Enhancement A suggestion for improvement. labels Jan 24, 2020
@tellthemachines tellthemachines added the CSS Styling Related to editor and front end styles, CSS-specific issues. label Apr 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CSS Styling Related to editor and front end styles, CSS-specific issues. [Feature] UI Components Impacts or related to the UI component system [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

3 participants