Skip to content

Global styles – per-block custom css #44412

Closed

Description

@jameskoster

When editing the styles for a specific block, it might be nice to offer an input for custom css. This way folks don't have to worry about finding / using the best selector.

Mockup: a "Custom CSS" button at the bottom of the Advanced section of a Group block, which slides the inspector to the right just as the content-only editing system, and Global Styles, does:

Group custom CSS

This could align well with how it could look for Global Styles:

Global styles custom CSS


Issue updated Oct 25. Related to #30142

Activity

  1. added
    Global StylesAnything related to the broader Global Styles efforts, including Styles Engine and theme.json
    on Sep 23, 2022
  2. mtias commented on Oct 7, 2022

    @mtias
    Member

    Some prior art: #25413

  3. jasmussen commented on Oct 25, 2022

    @jasmussen
  4. jameskoster commented on Oct 25, 2022

    @jameskoster
    ContributorAuthor

    I like the implementation – a dedicated panel makes sense and provides adequate space. The connection to Global Styles is nice too.

    But that Advanced panel is starting to look a little chaotic. Obviously it's separate, but I wonder if we should give it the Design Tools treatment?

  5. jasmussen commented on Oct 25, 2022

    @jasmussen
    Contributor

    But that Advanced panel is starting to look a little chaotic. Obviously it's separate, but I wonder if we should give it the Design Tools treatment?

    Sounds good to me, including keeping that separate! 🚀

    Alright, I'll update issues.

  6. added
    Needs DevReady for, and needs developer efforts
    and removed on Oct 25, 2022
  7. jasmussen commented on Oct 25, 2022

    @jasmussen
    Contributor

    Added "Needs Dev" and updated the main issue. Should be good to work on!

  8. moved this to 🔖 Needs development in Gutenberg Phase 2: Customizationon Nov 2, 2022
  9. aristath commented on Nov 9, 2022

    @aristath
    Member

    We'd like to start working on this issue, but there's a decision that needs to be made before we start.
    The main problem we saw when discussing this, is how to handle the selectors in our custom-CSS field.

    Assuming we want the final CSS to be like this:

    .wp-block-search {
      background: #222;
      border: 0;
    }

    we have a few ways to accomplish that:

    Allow users to arbitrarily add custom CSS

    The problem with this approach is that each snippet of custom-CSS in this case is not actually "per-block", but global. I can easily add some CSS in the search block, and target an image block - which is something we should definitely avoid.

    Use a hard-coded selector per-block

    We could define in block.json that the selector for the search block is .wp-block-search. In this case, the per-block "custom-CSS" field would accept something like this:

    background: #222;
    border: 0;
    

    Internally we can then build the CSS from that, by doing $selector { $customCSS }.
    That's simple and can work. However, if we go with something like that, it's impossible to target pseudo-elements and states like :hover, :focus, :after and so on. So while the example CSS above can be written by omitting the selector, we wouldn't be able to write something like this:

    .wp-block-search {
      opacity: 0.8;
    }
    .wp-block-search:hover,
    .wp-block-search:focus {
      opacity: 1;
    }

    So we need to come up with a different solution.
    One option would be to use a placeholder like el (short for element). In this case, we can just replace .wp-block-search with el when writing the CSS, and internally we make the conversion.
    Another option would be to omit the selector completely, so the user would write something like this:

    {  opacity: 0.8; }
    :hover,:focus { opacity: 1; }

    Or, something like this:

    opacity: 0.8;
    &:hover: opacity: 1;
    &:focus: opacity: 1;

    I personally like that last one as it would provide a lot of flexibility, but in this case users wouldn't be able to just copy-paste a snippet they found online and expect it to work, not to mention the complaints we'll get like "After reinventing HTML, now you want to reinvent CSS"

    We need to decide how we want the CSS to be formatted - because it definitely should not be 100% freeform, it would defeat the "per-block" purpose.

  10. 17 remaining items

  11. noisysocks commented on Nov 21, 2022

    @noisysocks
    Member

    @Mamaduka: I see you're working on #30142. Do you think it also makes sense to take this one? They seem closely related 🙂

  12. wingmatt commented on Nov 23, 2022

    @wingmatt

    It's exciting to see this option being explored, and I see a lot of great ideas have been shared already!

    I love the idea of having a variable like :self (or :block) to reference the dynamically-generated block ID for easy block-scoping specificity. This is what Elementor does with its Custom CSS section, and it's perfect for fulfilling styling edge cases that no-code tools can't cover:

    Elementor Custom CSS widget. Includes helper text explaining to use Selector to target the widget being edited

    With that said, I'd like to gently challenge the assumption that this custom CSS should always be wrapped in a :self selector.

    Consider blocks that render child elements, such as WooCommerce's Hand-Picked Products block. In these cases, locking the custom CSS so it can only affect the block's container element via :self would remove most of the utility this section would otherwise provide.

    I recognize that this section would be a tool for folks of all CSS skill levels to use, and how that requirement introduces some tricky UX challenges. If the custom CSS could be locked to :self by default, but with some sort of option to unlock it without cluttering the interface, that would be ideal in my opinion!

  13. oandregal commented on Dec 1, 2022

    @oandregal
    Member

    Looking at the options, if we actually want this CSS to be scoped to the block, I'd favor this approach (not using :self or :block but making sure everything is nested within the own block selector derived from block.json).

    The rationale is that if we use :self or :block, how do we make sure this does not happen?

    :block { /* some block styles go here */ }
    
    .wp-element-link { /* CSS unrelated to the actual block */ }
  14. aristath commented on Dec 1, 2022

    @aristath
    Member

    Looking at the options, if we actually want this CSS to be scoped to the block, I'd favor #44412 (comment) (not using :self or :block but making sure everything is nested within the own block selector derived from block.json).

    I tend to agree with that assessment... The danger we run if CSS is unscoped, is that we're basically introducing 70+ custom-css fields that can be applied globally 😅

  15. oandregal commented on Dec 1, 2022

    @oandregal
    Member

    Shared #30142 (comment) a potential first step for both this issue and the other one.

  16. mtias commented on Dec 1, 2022

    @mtias
    Member

    I also lean towards enforcing the scope selector initially and remove possible footguns. Tracing orphaned selectors across a large collection of blocks is not going to be fun. With native CSS nesting hopefully coming soon we should be able to design for this anyways:

    :block {
      /* some block styles go here */
      .wp-element-link {
        /* CSS unrelated to the actual block */ }
      }
    }

    Which would handle most needs in more complex blocks. Alternatively we could allow :block .wp-element-link {}.

  17. linked a pull request that will close this issueGlobal Styles: Try per block custom CSS #46571on Dec 15, 2022
  18. moved this from 💻 Needs development to 👀 Needs review in Gutenberg Phase 2: Customizationon Dec 16, 2022
  19. moved this to 🔎 Needs Review in WordPress 6.2 Editor Taskson Jan 19, 2023
  20. moved this from 👀 Needs review to ✅ Done in Gutenberg Phase 2: Customizationon Jan 23, 2023
  21. moved this from 🔎 Needs Review to ✅ Done in WordPress 6.2 Editor Taskson Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Global StylesAnything related to the broader Global Styles efforts, including Styles Engine and theme.jsonNeeds DevReady for, and needs developer efforts[Type] EnhancementA suggestion for improvement.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions