Skip to content

Blocks and Theme Styling Overview #5360

Closed

Description

@mtias

This is an overview of theme related considerations for Gutenberg as gathered after several months of testing and feedback.

WordPress Block Styles vs Theme Styles

During the evolution of Gutenberg we have introduced several blocks with somewhat opinionated styles (quote, pullquote, separator, etc). We have seen how some themes really don't want these — they might conflict with their base styles — while other themes really want to build on top of them (particularly new themes). We have opted to disable the opinionated styles for some of the blocks that map to HTML primitives, like blockquote, because almost every theme defines those for themselves and would be tough to accommodate all conflicts. This has meant we have prevented new themes from reusing the styles that appear on the editor for all these elements. We are seeing some vanilla themes that do want these styles and would rather just customize on top of them.

This means we are looking at a split between themes that don't want any visual style (not considering structural ones here, like columns, galleries, etc) coming from core blocks and themes that do want these styles as their base. It seems we should figure out a way to accommodate both needs.

Anatomy of a Block's Style

Currently we define block styles in two stylesheets: "style" and "edit". The main problem is that if we want to supply visual styles to some themes, we cannot split them from structural styles that should come for all themes easily. So I am proposing we look at establishing three separations for block styles:

  • style.scss are the shared structural styles.
  • edit.scss are the editor specific styles.
  • theme.scss are opinionated visual styles.

With these separations we can always include the base styles for all themes, but make theme styles optional. A theme would have to do something like add_theme_support( 'wp_block_styles ' ) to load these if they want to. (Bonus points if through the structure of theme.scss we could find easier ways for a theme to supply their own styles to the editor. Imagine having a blocks folder under your theme where you can add a css/scss file per block which is used to concatenate into the editor, but I digress.) During build, we should include the visual styles in a separate stylesheet.

Previous: #2905

Classes vs Inline Styles

A similar split exists between basic and advanced customization options. We have a couple blocks that have a fair degree of styling possibilities around colors and font sizes. We allow themes to disable custom colors and to register their own already. We are also looking at providing an array of font sizes that would map to classes is-small-text, is-regular-text, is-large-text, etc, so themes can dynamically set these values. The ability to set custom values is still welcomed for various contexts, so it's looking like the best separation is:

  • Basic: Classes for the predefined sets (colors and fonts).
  • Advanced: Inline for custom values.

For basic we avoid all saved inline styles, and advanced is something that could be disabled by a theme or plugin if necessary.

Colors

Would be registered assigning a name to each color value. The name would be used to construct a .has-color-{name} class as well as show a tooltip for accessibility.

$colors = array(
	'Blue' => '#00B1D4',
	'Violet' => '#934ED4'
);

Applying one of these colors won't generate any inline style. See #5273

Fonts

In the case of fonts, there'll be a set of predefined sizes that would map to classes. The set would be provided as an array of values which would be used to generate the value in the editor, while the only thing saved would be the corresponding class (i.e is-small-text, is-regular-text, is-large-text, is-larger-text).

The UI would be close to this proposal:

image

See #5269

This issue will be updated with further details as they come by.

Activity

  1. added
    Customization
    on Mar 2, 2018
  2. StaggerLeee commented on Mar 3, 2018

    @StaggerLeee

    Please don't forget what value it is in GUI, for font-size: em, px, pt, percent.

  3. bordoni commented on Mar 4, 2018

    @bordoni
    Member

    It would be incredibly useful for plugins too, having the information if a given theme chose to style the blocks, in one method from WP core.

  4. karmatosed commented on Mar 5, 2018

    @karmatosed
    Member

    I really like the three separations (style, edit, theme) for style files. This feels like a robust pattern going forward.

    On the font UI, lets iterate a little on styling with the reset being the same, but that's a minor issue.

  5. added
    [Type] Task
    on Mar 7, 2018
  6. added this to the milestone on Mar 7, 2018
  7. aristath commented on Mar 7, 2018

    @aristath
    Member

    About the font-sizes controls: I've seen a number of tickets here that all use a numeric field (slider etc) for font-size. However that assumes that the user wants to use pixels... and that's just wrong. I haven't used pixels for typography sizes in years, I prefer using em or rem depending on the use-case. Others prefer using % values, WCAG uses pt for its calculations so in some sites it's more convenient to use that unit and so on.
    There are many valid scenarios for each CSS unit, px is only good for a subset of those scenarios. We should not restrict our users to using px.

  8. mtias commented on Mar 7, 2018

    @mtias
    MemberAuthor

    The value doesn't currently have a unit on purpose.

  9. samikeijonen commented on Mar 7, 2018

    @samikeijonen
    Contributor

    Seems like a great approach! Will ask questions later on.

  10. delucks1 commented on Mar 20, 2018

    @delucks1

    Hi, I just found your discussion on editor / block styles (typo and color) and wonder, if and when you plan to roll out the function in the plugin. Else is there a possibility for me as a beginner in theme development to try that already?

  11. kanishkdudeja commented on Mar 22, 2018

    @kanishkdudeja

    I've been thinking about this and had a few questions:

    • theme.scss are opinionated visual styles.

      With these separations we can always include the base styles for all themes, but make theme styles optional. A theme would have to do something like add_theme_support( 'wp_block_styles ' ) to load these if they want to.

      These opinionated visual styles will only apply to the front-end or the editor as well?

    • I had a general question about theme-supplied styles for core blocks (supplied by WordPress by default). Regardless of the way we choose for themes to supply styles for core blocks (either a extendBlockType function or a blocks/ subdirectory in the theme folder which contains css files for each block or a filter on the block-specific enqueue), will those theme-supplied styles only augment block styles supplied by WordPress core?

      Or will those theme-supplied styles be the only source of truth (meaning block styles supplied by WordPress will be discarded) ?

  12. mtias commented on Mar 27, 2018

    @mtias
    MemberAuthor

    These opinionated visual styles will only apply to the front-end or the editor as well?

    Both. An example is the quote style, that has a left border and some color choices. This currently don't apply on the front-end equally.

    will those theme-supplied styles only augment block styles supplied by WordPress core?

    Probably should be generic enough to apply to any block.

    Or will those theme-supplied styles be the only source of truth (meaning block styles supplied by WordPress will be discarded) ?

    If the theme doesn't opt in into having the default block styles, these won't be loaded. Shouldn't matter what we do with theme-supplied styles in that case.

  13. mapk commented on Apr 3, 2018

    @mapk
    Contributor

    I like the separation of stylesheets. My only suggestion would be changing the name of edit.scss to editor.scss. The first sounds like a verb while the second is the noun.

    A theme would have to do something like add_theme_support( 'wp_block_styles ' ) to load these if they want to.

    This makes sense, but my concern is that when G merges into Core, there will be a lot of themes that don't initially inject their styles into the Editor. This will be a big disconnect for users, I think. Is it too difficult to inject the theme styles automatically? I'm sure there's already discussion around this, I'll look for it.

  14. davidakennedy commented on Apr 10, 2018

    @davidakennedy

    I wanted to drop some thoughts here since I've been thinking about this lately, and testing some themes with Gutenberg in mind...

    The separated approach laid out by @mtias seems like a nice balanced way ahead. Theme authors will have the choice as to whether to opt in, or go their own way. That's very WordPress-friendly, in terms of what everyone is used to having. It also lets both the type of themes exist: custom ones, and ones that build on existing Gutenberg structure, styles, etc.

    I'd also +1 the font and color approach mentioned. It strikes and good balance and gives authors a lot of flexibility.

  15. 27 remaining items

  16. karmatosed commented on May 29, 2018

    @karmatosed
    Member

    @chrisvanpatten thanks for that visual, this really plays to my bigger concern of that being in the sidebar. If you think of scale that easily breaks. What other options are there? Are there any applications doing something like this right now we can learn from?

  17. chrisvanpatten commented on May 29, 2018

    @chrisvanpatten
    Contributor

    @karmatosed I'm not sure where else to put it, but I'm certainly open to ideas.

  18. chrisvanpatten commented on May 29, 2018

    @chrisvanpatten
    Contributor

    (To elaborate, I think there will be an issue of scale anywhere it goes — and that's applicable to any point in the UI where plugins can add additional options. I'm not sure if putting it behind a toolbar button is better or worse but I'm certainly open to experimenting with that!)

  19. chrisvanpatten commented on May 29, 2018

    @chrisvanpatten
    Contributor

    Here's an interesting example from Squarespace, on how you can select alternate layouts for their Instagram block.

    ig_block

    For them, they place the options behind a separate Design tab.

    Another interesting take, in Excel of all places… alternate table designs in a dropdown:

    cond_format5_2010_001

  20. melchoyce commented on May 29, 2018

    @melchoyce
    Contributor

    Thanks @chrisvanpatten, that visualization helps.

    Some more examples from other sources:

    Slideshow block in Weebly

    screen shot 2018-05-29 at 12 19 00 pm

    Layout blocks in Weebly

    ezgif com-video-to-gif 1

    Section Designs in Wix ADI

    screen shot 2018-05-29 at 12 24 33 pm

    screen shot 2018-05-29 at 12 25 24 pm

    More design settings in Squarespace

    screen shot 2018-05-29 at 12 26 01 pm

  21. chrisvanpatten commented on May 29, 2018

    @chrisvanpatten
    Contributor

    Here's another take, from Pages:

    untitled_pages

  22. mrwweb commented on May 30, 2018

    @mrwweb

    As promised, here's a couple concepts. I think I like this first one best, placing the drop down with the block definition in the sidebar. It's kind of subtle and prominent at the same time:

    No Style Selected
    A dropdown below the "Paragraph" block definition in the sidebar. No selection made. Says "Select a style..."

    Select an option
    Drop down from previous image is active with two options.

    Class added
    Green Drop Shadow Box selected in new styles select menu. Selected paragraph now had green background, padding, border, and drop-shadow.

    I also think this is a great example of how powerful something like this can be! It applies 4 styles (background, padding, border, box-shadow) that would be hard to apply any other way but also way overkill for creating unique blocks for. Any interface that allowed setting padding and creating box shadows wouldn't be easy to replicate like this and would be both tedious to use and likely to be used inconsistently from page to page.

    Another option would be to put the variant selection menu in the inline toolbar:

    Drop down after the block select. Says "No Style..."

  23. mtias commented on May 31, 2018

    @mtias
    MemberAuthor

    I think the ideas being presented here (specifically offering/ selecting block variants) are really nice and will be very helpful however I would be interested in looking back at the original ticket and finding out if that is something that's going to happen?

    @BinaryMoon yes, the original intention of the issue is unrelated to variants and is being worked on — initial implementation should land for 3.0. @mrwweb let's reopen #783 for this conversation, as it's getting a bit away from the purpose of the current issue, and make out a plan before deciding if we can make it into 5.0 or not.

    but the concept of a variant as I envision it would just be a more user-friendly way of the current "Additional CSS Class" field (not as a replacement of course, just as user-friendly alternative that essentially does something similar)

    To be clear, I totally agree with this, and I think it's something we should pursue. :) I'm not entirely sure how we would present it — perhaps starting by having the dropdown within the "Advanced" panel is a safe start. One of the problems is that a design principle has been to allow as much as possible to happen in the block directly, with the sidebar being more about sophisticated attributes.

    This poses a problem for how we deal with variants. Where should they fall? If they are considered useful user-friendly additions (and I'd tend to agree with this) then we should see how they could be incorporated to the block body/toolbar instead of the sidebar. But a dropdown list with the names in the block toolbar doesn't scale too well and can complicate the UI. (Issues with mobile, not immediately visual, etc.)

    Also, even if technically this is trivial to do, we should look at how we could facilitate the usage of it longer term, because I think it's a good feature to have.

    @chrisvanpatten I totally agree on the semantic distinction being relevant here. I'd actually use that to make the case that "subheading" should be a separate block, because it's not a purely visual change, it has semantic value to the user.

  24. samikeijonen commented on Jun 29, 2018

    @samikeijonen
    Contributor

    Quick note about theme.css. It would make sense that themers could dequeue or opt out theme.css also from the editor.

    I tried it with wp_dequeue_style( 'wp-core-blocks-theme' ); with hooks enqueue_block_editor_assets and enqueue_block_assets but no luck. Could be something to do with depencies.

  25. mtias commented on Jun 29, 2018

    @mtias
    MemberAuthor

    @brandonpayton when you have a moment could you look at the above? Thanks!

  26. BinaryMoon commented on Jun 29, 2018

    @BinaryMoon

    I agree with @samikeijonen - I want to include my styles in the editor but I'm having a hard time overriding the default ones.

  27. brandonpayton commented on Jul 6, 2018

    @brandonpayton
    Member

    I tried it with wp_dequeue_style( 'wp-core-blocks-theme' ); with hooks enqueue_block_editor_assets and enqueue_block_assets but no luck. Could be something to do with depencies.

    Hi @samikeijonen, you're right that it has to do with dependencies. The 'wp-edit-blocks' styles depend on the 'wp-core-blocks-theme' styles. Because of this, we cannot simply dequeue the theme styles, but we can replace them for 'wp-core-blocks-theme' by using wp_deregister_style and wp_register_style. enqueue_block_editor_assets looks like the right hook to use for this. Does that help?

    @mtias do you want us to do anything more to support overriding theme CSS in the editor?

  28. samikeijonen commented on Jul 8, 2018

    @samikeijonen
    Contributor

    Thanks @brandonpayton! I opened new issue in #7776 just in case. I feel like replacing the original theme.css with empty one is not the best practise.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Customization[Type] Task

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions