-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Global styles – per-block custom css #44412
Comments
Some prior art: #25413 |
This comment was marked as resolved.
This comment was marked as resolved.
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? |
Sounds good to me, including keeping that separate! 🚀 Alright, I'll update issues. |
Added "Needs Dev" and updated the main issue. Should be good to work on! |
We'd like to start working on this issue, but there's a decision that needs to be made before we start. 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 CSSThe 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-blockWe could define in
Internally we can then build the CSS from that, by doing .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. { 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. |
Nice, Ari! The example CSS in the mockup was perhaps a bit misleading, I copied it from a template. But I like @youknowriad's solution from #25413, which was to use
|
To further expand on the very insightful note from @aristath, how should the custom css that is input in the settings of a block only apply to that particular instance of the block or all blocks of that type that are loaded on the page? |
Ah I had forgotten about that one! Yes, I'll agree with that proposal. Using So... Should we start implementing this with the assumption that we'll use a placeholder like |
Just to play devils advocate, do we definitely want to enable pseudo-element targeting here? If so where do we draw the line, should we also support things like Personally I would err towards being a little more restrictive here. We don't want people writing the css for their entire site in one single block 🙈 For the sake of argument, we might do something like: And point folks to the custom css feature within global styles itself for more comprehensive styling. Just an idea. |
I like to think that hover/focus styles and their handling through theme.json can be evolved a bit further with a UI before we start to optimize for their CSS input. |
Ah yes, if |
+1 @jameskoster I had a discussion with @carolinan earlier today about that exact same thing (using a dropdown to allow users to enter styles for specific pseudo-elements)... The problem we found is that this would be too restrictive. :self:not(.some-random-class) {
color: #fff;
}
:self > caption {
background: #000;
opacity: 0.8;
}
:self:hover > caption {
opacity: 1;
} |
We could also embrace the fact that CSS nesting and allow something like: color: red;
&:not(.some-random-class) {
color: #fff;
}
& > caption {
background: #000;
opacity: 0.8;
}
&:hover > caption {
opacity: 1;
}
some child {
} I'm using SASS syntax here but nesting is coming to CSS too, so we can align with CSS syntax too. |
We could do that, yes... And it would be relatively easy to handle the |
I cannot find where I mentioned this before, but we could also consider We definitely want to make this very flexible and open ended, the only enforcing is the block class name being dynamically generated and the fact we only output this CSS if the block is used on a given request. |
It is difficult to estimate how the custom CSS fields will be used and what the different users types beginner, developer and designer expects without user testing. Yes it will be used by people who understand SASS, nesting, :self (and know enough that |
If |
Yes there needs to be built in scoping, my concern is how it is presented to the user. |
I was thinking this design for the locked CSS code, and by patterns I'm not expecting any separate work at all, simply that if you create a pattern that includes a group with CSS, the pattern will have CSS. |
The field in the customizer has the following text: Add your own CSS code here to customize the appearance and layout of your site. Learn more about CSS When using a keyboard to navigate: |
I will try again. I don't think that displaying :self, block or similar in the input filed will be helpful, unless you would consider having an advanced mode. This is what I think will happen:
Having a code snippet already placed would be more confusing than helpful. An advanced mode could have the code, the scoping, visible, as well as have the syntax highlighter by default. |
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: 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! |
Looking at the options, if we actually want this CSS to be scoped to the block, I'd favor this approach (not using The rationale is that if we use :block { /* some block styles go here */ }
.wp-element-link { /* CSS unrelated to the actual block */ } |
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 😅 |
Shared #30142 (comment) a potential first step for both this issue and the other one. |
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 |
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:
This could align well with how it could look for Global Styles:
Issue updated Oct 25. Related to #30142
The text was updated successfully, but these errors were encountered: