Skip to content

Custom editor styles on element selectors effect block control styles.  #10178

Closed

Description

@m-e-h

Describe the bug
If in my theme-editor-styles.css I have the following:

button {
	box-shadow: 10px 5px 5px red;
	min-height: 2.5rem;
}

then havoc is wreaked on my editor.

gutbutton

Expected behavior
The styles above are exaggerated but I don't think styles on button or input elements are totally edge cases. #10067 (comment)

Possible solution
One fix that would need more testing is to unset styles on the Gutenberg specific class before styles are added.
So, for example, adding the following to the top of components/style.css fixed the button styles for me:

.components-button {
	all: unset;
}

This could be done for each class that Gutenberg applies to an html element other than div. Mostly button and input I would guess.

Desktop (please complete the following information):

  • OS: Ubuntu
  • Browser: chome, firefox
  • Version: latest

Additional context

  • Gutenberg v3.9

Activity

  1. m-e-h commented on Sep 25, 2018

    @m-e-h
    MemberAuthor

    I'm pretty sure autoprefixer doesn't cover all or unset. So an ie11 friendly version would look more like this: 😬

    .components-button {
      background-color: initial;
      background-image: initial;
      background-position-x: initial;
      background-position-y: initial;
      background-repeat: initial;
      background-attachment: initial;
      background-clip: initial;
      background-origin: initial;
      background-size: initial;
      border-top-color: initial;
      border-top-style: initial;
      border-top-width: initial;
      border-right-color: initial;
      border-right-style: initial;
      border-right-width: initial;
      border-bottom-color: initial;
      border-bottom-style: initial;
      border-bottom-width: initial;
      border-left-color: initial;
      border-left-style: initial;
      border-left-width: initial;
      border-top-left-radius: initial;
      border-top-right-radius: initial;
      border-bottom-right-radius: initial;
      border-bottom-left-radius: initial;
      border-image-source: initial;
      border-image-outset: initial;
      border-image-repeat: initial;
      border-image-width: initial;
      border-image-slice: initial;
      display: initial;
      position: initial;
      overflow-x: initial;
      overflow-y: initial;
      -moz-appearance: initial;
      color: initial;
      font-size: initial;
      text-indent: initial;
      cursor: initial;
      margin-top: initial;
      margin-right: initial;
      margin-bottom: initial;
      margin-left: initial;
      padding-top: initial;
      padding-right: initial;
      padding-bottom: initial;
      padding-left: initial;
      align-items: initial;
      box-sizing: initial;
      text-decoration-line: initial;
      text-decoration-style: initial;
      text-decoration-color: initial;
    ...
    }
    
  2. m-e-h commented on Oct 31, 2018

    @m-e-h
    MemberAuthor

    Reckon I should go ahead and do a PR on this?

    Maybe we cut it down to a reset of only the most commonly used properties of input, select, textarea and button.

    By adding the following to the top of packages/block-library/src/style.scss, the editor becomes much more resistant to user styles causing deformed or broken controls.

    .components-button,
    .editor-post-text-editor,
    .edit-post-sidebar__panel-tab,
    .editor-block-types-list__item,
    .document-outline__button,
    .components-checkbox-control__input,
    .editor-inserter__search,
    .components-select-control__input,
    .components-textarea-control__input,
    .components-text-control__input {
    	background: unset;
    	border: unset;
    	display: unset;
    	position: unset;
    	transition: unset;
    	color: unset;
    	font: unset;
    	line-height: unset;
    	vertical-align: unset;
    	cursor: unset;
    	padding: unset;
    	margin: unset;
    	width: unset;
    	min-width: unset;
    	max-width: unset;
    	box-sizing: unset;
    	box-shadow: unset;
    	height: unset;
    	min-height: unset;
    	max-height: unset;
    	opacity: unset;
    }
    

    I've see zero side effects in my tests.

  3. youknowriad commented on Nov 1, 2018

    @youknowriad
    Contributor

    This is indeed a good idea, but I don't like the fact that we need to polyfill it for IE11. Can we just start with the other browsers for now? I'm worried about the extra bytes in CSS this will create.

    That said, I'm wondering if you considered using the editor styles mechanism to provide your editor styles, it shouldn't interfere that much using this technique. https://wordpress.org/gutenberg/handbook/extensibility/theme-support/#editor-styles

    (The editor styles work better in 4.2 RC1)

  4. m-e-h commented on Nov 1, 2018

    @m-e-h
    MemberAuthor

    Supporting all browsers including IE11 wouldn't be a problem. unset just chooses whether inherit or initial is used.
    We'd just need to be more selective and thoughtful in the properties chosen and the reset used.
    For example,

    color: inherit;
    padding: initial;
    

    That being said, I forgot about the editor-styles mechanism.
    It actually works against this solution. 😯

    .editor-block-list__block input, 
    .editor-block-list__block select, 
    .editor-block-list__block textarea, 
    .editor-block-list__block button
    

    This targets the block controls even more specifically. To the point where unsetting .components-button wouldn't be specific enough to protect the insertion-point-button from user's button styles.
    We can't increase the specificity of the unset or it'll override core styles added to single class selectors.

    I'm going to think about it some more but right now I don't know how this initial proposal could work.

    To be clear, my whole point here is that I want my editor buttons and inputs to look as they do on the front-end.
    With these things largely coming from plugins and shortcodes, the [class], if there is one, can't be known at the time the theme is built. Styling the elements is the only way.

  5. m-e-h commented on Nov 5, 2018

    @m-e-h
    MemberAuthor
  6. m-e-h commented on Nov 6, 2018

    @m-e-h
    MemberAuthor

    One possible solution here would be to modify the way the style wrapper #10956 works for non-"root" selectors. In addition to wrapping selectors, we add a :not() pseudo-class to them, negating a class or attribute assigned to editor components and controls using elements.

    An example of a modified user's editor-style would look something like this:

    .editor-styles-wrapper button:not([class^="components-"]) {
    

    The [class^="components-"] in the above example seems to cover it for the buttons but it may be better to add a new class or attribute, for targeting, to all non-div editor elements.

  7. chrisvanpatten commented on Nov 9, 2018

    @chrisvanpatten
    Contributor

    @m-e-h I'm curious — how much of this could be solved by applying a generic reset in CSS to our components and making their selectors more specific, so our component styles are less likely to be impacted? I think I'd personally feel more comfortable with that, vs. a CSS rewriting hack which seems more fragile.

  8. m-e-h commented on Nov 9, 2018

    @m-e-h
    MemberAuthor

    Yes @chrisvanpatten a CSS reset like I originally proposed in this issue would work perfectly for those that are enqueuing their styles without using the editor-styles theme_support method.

    However when using the current CSS rewriting hack, the themes styles become more specific than our reset: .editor-styles-wrapper button > .components-button so in this case the theme's button styles will override the block button controls.

    Perhaps it's worth throwing a reset at the top of the components anyway to cover those not using the editor-styles?
    For those that are using editor-styles, I'm not particularly a fan of the current CSS rewriting either but I don't know that modifying it would make it any more fragile than it already is.

  9. m-e-h commented on Nov 9, 2018

    @m-e-h
    MemberAuthor

    An example

    button {
    	min-height: 3rem;
    }
    

    Since the controls on the blocks don't specify min-height all of them will now be 3rem tall and appear as ovals instead of circles.

    The following reset, placed before core defines any styles will ensure any properties not specified(by at least a single class selector) later in the stylesheet will retain their initial or inherited values.
    (actual would be more verbose for browser compat)

    .components-button {
    	all: unset;
    }
    

    Now we add the wrapper to our theme's button style:

    .editor-styles-wrapper button {
    	min-height: 3rem;
    }
    

    It overrides our reset and our button control.

    We can increase our reset specificity:

    .components-button.components-button {
    	all: unset;
    }
    

    But now our reset is overriding our own control styles.
    Quite the can of worms.

  10. laurelfulford commented on Nov 12, 2018

    @laurelfulford
    Contributor

    #11505 is a duplicate report I made of the above issue: button styles in Twenty Seventeen's editor CSS, pulled into the editor with editor-styles, can change the font sizes in some Gutenberg buttons. Affected buttons include media upload buttons, and the edit buttons in reusable blocks:

    image

    The culprit is the .editor-styles-wrapper button selector @m-e-h mentions above, which lets the editor styles override Gutenberg's own styles, in part because they're embedded.

    It makes more sense to move that discussion here, so closing #11505 in favour of this issue!

  11. chrisvanpatten commented on Nov 13, 2018

    @chrisvanpatten
    Contributor

    Hey folks -

    I'm going to remove the documentation label on this, as I'm not entirely sure documentation is appropriate here. I was prepared to write this off as an edge case but if it's happening in the twenty(.*) themes, it probably warrants a closer look into how we can truly fix the problem (at least for buttons and possibly for other elements as well).

  12. 22 remaining items

  13. m-e-h commented on Apr 30, 2019

    @m-e-h
    MemberAuthor

    @jasmussen Thanks, as always for the thorough answer! That all makes sense

    I'm not necessarily opposed to a theming system that goes outside the editing canvas (though I'm struggling to understand the use cases?)

    I'm not talking about doing anything that we're not already doing. Many of the UI components, that this is meant to protect, aren't limited to the editing canvas.

    I suppose for the purpose of this experiment we can start out assuming that everyone is using the editor-styles rewrite feature. I'll limit the reset to UI components within .editor-styles-wrapper.

    Testing with just this:

    :root .editor-styles-wrapper .dashicon:not(div),
    :root .editor-styles-wrapper .components-button:not(div),
    :root .editor-styles-wrapper .components-dropdown-menu__indicator:not(div),
    :root .editor-styles-wrapper .block-editor-block-icon:not(div) {
    	all: unset;
    	box-sizing: inherit;
    }
    

    and I've bumped up the specificity evenly across 'block-editor', 'editor', 'components', and 'edit-post' stylesheets using postcss during the build process.

    Changing the default themes to add_editor_style( 'style.css' ) and everything is looking pretty close to perfect.

    twentyseventeen style.css without the reset
    2017old

    twentyseventeen style.css with reset
    2017new

    I'll put my rough first pass together in a PR for some feedback and testing. I'd never even looked at Gutenberg's build process till last night so I'm definitely going to need some direction on any conventions used there.

  14. jasmussen commented on May 1, 2019

    @jasmussen
    Contributor

    That's very cool!

    I'd never even looked at Gutenberg's build process till last night so I'm definitely going to need some direction on any conventions used there.

    Please if there's ever any blockage here, you know where to find me. You can ask openly for help in the core slack, #core-editor, or you can ping me directly (@joen in core slack), or just here. Happy to help, so we can get your expert input!

  15. m-e-h commented on May 1, 2019

    @m-e-h
    MemberAuthor

    Thanks @jasmussen !

    PR created here #15377
    Any testing anyone can do would be awesome!

    It's still an early draft. I know I need to move some of the files around but it seems to work well.

  16. felixarntz commented on Oct 1, 2019

    @felixarntz
    Member

    I've encountered similar issues with the components buttons as well as the permalink. As noticed before, it's problematic that Gutenberg's styles for editor controls are not wrapped in .editor-styles-wrapper, while the theme's editor styles are.

    I like some of the suggestions made here, including the PR by @m-e-h, which seems hard to get right though. I know it's not so great from a pure CSS standpoint, but could we not wrap Gutenberg's styles in the .editor-styles-wrapper class as well (with a build step tool)? If the built stylesheet contained rules like .components-button, .editor-styles-wrapper .components-button { ... }, I think that would resolve most of the issues where generic theme editor styles (e.g. button, input, textarea, select { ... } override Gutenberg core component styles.

  17. jasmussen commented on Oct 1, 2019

    @jasmussen
    Contributor

    My plate is all kinds of full, but this is still on my radar. I hope to resurrect the ongoing efforts here that were close, if I recall correctly.

  18. m-e-h commented on Oct 1, 2019

    @m-e-h
    MemberAuthor

    I'd really like to explore some options here and hope I can find some time in the near future to do so. My extra time unfortunately disappeared around the time I started experimenting with solutions for this.

    An editor-styles-wrapper would help some by increasing the specificity of the already declared style properties.
    But I think the more vulnerable area here is when a theme or plugin uses style properties that haven't been declared on button, input, textarea, select. It's also a harder bug to track down since it has nothing to do with specificity like most would think.

    For example, I don't think a wrapper would help at all with the case used at the start of this issue thread. #10178 (comment)

    button {
    	box-shadow: 10px 5px 5px red;
    	min-height: 2.5rem;
    }
    
  19. jasmussen commented on Oct 8, 2019

    @jasmussen
    Contributor

    Taking a look at this with slightly fresher eyes, it seems clear that while theme developers can work within the limitations currently present due to the CSS bleed, it's still onerous and they shouldn't have to. So while it may not be urgent to address this issue, addressing this issue should make virtually all developers lives easier.

    In my experience, it's always useful to look at the utopian solution first, and then plot a course towards that. In my estimation, Shadow DOM was created for this exact purpose; to encapsulate an element from inheriting styles. It seems like if we were able to encapsulate the most in-editor used components, such as the Block Toolbar, the Placeholder, and the Mover control, we'd be mostly there.

    I know Shadow DOM has been explored and abandoned, but I can't recall what was holding us back — was it the lack of support in IE11/Classic Edge? Given the new Chromium based Edge supports Shadow DOM, could we consider editor styles to be a progressive enhancement for modern browsers?

    Similar to how Shadow DOM protects the styles of an element, the most promising approach from this thread seemed to involve strengthening the UI styles to insulate them from being overridden by editor styles. The main point of feedback against this approach, cc @youknowriad, was that ideally we should be be content agnostic: since we can't know what additional components a plugin developer might add to the UI, it seems confusing to just protect a few elements.

    But would this not also be the case with Shadow DOM? And is there a way to simplify the API surface so that controls you do not want to inherit theme styles can be wrapped? I believe this is what @m-e-h is referring to here.

    So it seems like defining what content agnosticism we want is key to unsticking this issue. Let's explore that a bit:

    1. There are UI elements that should never inherit CSS. Things like the <Button /> component, and many of the other — sliders, radio buttons, toggles checkboxes. Common to most of those is that there's a finite amount, and when plugin developers use them their experiences are consistent and arguably follow best practices.
    2. There are also UI elements where you DO want for styles to be inherited. For example, selecting the Image block pops out a caption field. This caption field should inherit the font specified by the editor style.

    Based on the above two take-aways, it seems like a pragmatic appraoch would be to:

    • Create an allowlist of Block Editor UI components to protect
    • Create instructions for developers on how to protect their own custom elements

    Potentially this would solve 80% of our problems.

    And it might even plot us a path towards Shadow DOM. Long term, Shadow DOM would protect elements, but near term, higher element specificity would do so. Specifically this idea seems like it could be compatible with an allowlist of elements to protect:

    Both of these options would be made much simpler and more scalable if we can add a uniform .class or data-att** on each element we need to protect.

    To throw it out there, this could be an .is-protected class you add to components, or even data-protected-styles. For now we'd use those classes or attributes to strengthen their styles as @m-e-h suggested, but long term could we not also use those to target what to Shadow DOM encapsulate?

    In the end this would provide developers with a set of components they know won't inherit CSS from theme styles, and instructions for how to protect their own elements in a similar way. Would love your thoughts, and would love to unstick this.

  20. paaljoachim commented on Aug 3, 2021

    @paaljoachim
    Contributor

    Can we get an update from someone on this very old issue?
    Thank you!

  21. fabiankaegy commented on May 30, 2022

    @fabiankaegy
    Member

    The current PR that seems to work in this issue is #33494

  22. mtias commented on Jul 2, 2024

    @mtias
    Member

    Unless I'm missing something, the introduction of the iframe for the editor content should be preventing the most common of these issues.

  23. tcmulder commented on Jan 20, 2025

    @tcmulder

    @mtias things like the block appender button (button.block-editor-button-block-appender) are super common and are rendered within the iframe, so any button {...} styles still bleed into them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions