When Block Styles Are Not Enough: Extending Blocks in Themes #130
Replies: 6 comments 3 replies
-
Sharing some other custom alternatives to block styles: Text shadowsThis one is probably the simplest, so it'd make for a good introduction to the concept in the post: List markers/bulletsThis one is almost as simple as the text-shadow, but it has an extra hurdle to jump when dealing with Color variations (color groups) for style entire sectionsThis is probably too complex for an introductory tutorial because it would need to discuss color design systems in detail: |
Beta Was this translation helpful? Give feedback.
-
You've got my vote! I'm dealing with all these and more on a site I'm building — well, actually, I'm doing without. |
Beta Was this translation helpful? Give feedback.
-
I was trying to think of a use case that would show off what you could do while also being a bit fun. Since I had no electricity for the day, a full laptop battery charge, and nothing but my imagination to keep me company, I present to you an emoji-fied Separator block. Right now, this just uses the regular block styles system, but you can see how easily that gets out of hand. This should be also be a simple enough example to keep this tutorial within a reasonable length. The big things would be:
separator-icon-styles.mp4Backup of SCSS for safekeeping: // Icon styles.
hr.wp-block-separator[class*=is-style-icon] {
display: flex;
justify-content: center;
border: none;
text-align: center;
// Some characters get cut off. This ensures they are visible.
overflow: visible;
// Use `!important` to overrule core's use of the `background` shorthand
// instead of the targeted property itself.
background-color: transparent !important;
background-repeat: repeat-x !important;
background-position: 0% 50% !important;
background-size: 100% 2px !important;
// Don't use `!important` for the background image, which allows
// background gradients to work.
background-image: var( --hr-bg, linear-gradient( 90deg, currentColor, currentColor ) );
// If there's a background color, use our custom gradient which uses the
// current color.
&:where( [class*=-background-color] ) {
background-image: linear-gradient( 90deg, currentColor, currentColor );
}
&::before {
content: var( --hr-icon, "\2766" ); // floral heart default.
padding: 0 0.25em;
font-size: var( --wp--preset--font-size--xl );
line-height: 1;
background-color: var( --color-base, var( --wp--preset--color--base ) );
}
}
.is-style-icon-♫ { --hr-icon: "♫"; } // beamed eighth notes
.is-style-icon-★ { --hr-icon: "★"; } // black star
.is-style-icon-❦ { --hr-icon: "❦"; } // floral heart
.is-style-icon-☘ { --hr-icon: "☘"; } // shamrock
.is-style-icon-✻ { --hr-icon: "✻"; } // teardrop spoked asterisk
// We can set these backgrounds dynamically if moving to a custom control and
// outside of the block styles system.
.is-style-icon-☀️ { --hr-icon: "☀️"; --hr-bg: var( --wp--preset--gradient--true-sunset ); }
.is-style-icon-🪶 { --hr-icon: "🪶"; --hr-bg: var( --wp--preset--gradient--shy-rainbow ); }
.is-style-icon-🍃 { --hr-icon: "🍃"; --hr-bg: var( --wp--preset--gradient--emerald ); }
.is-style-icon-✏️ { --hr-icon: "✏️"; --hr-bg: var( --wp--preset--gradient--mohave ); }
.is-style-icon-🌻 { --hr-icon: "🌻"; --hr-bg: var( --wp--preset--gradient--true-sunset ); }
.is-style-icon-⛱️ { --hr-icon: "⛱️"; --hr-bg: var( --wp--preset--gradient--powerpuff ); } |
Beta Was this translation helpful? Give feedback.
-
A huge +1 for this! I've written a somewhat similar post on my blog which I'm happy to share as inspiration/reference. I'm most excited because of this:
I believe you're right this doesn't exist and I haven't been able to find any information about this when trying to figure it out for myself. I've got a current setup, but I'll be very interested to see how yours compares. Either way, this is super important information and should probably morph into a Handbook page eventually. |
Beta Was this translation helpful? Give feedback.
-
Love this! I'd like to see this documentation as it's a use case we would likely make use of in our managed network. We have plugins with custom blocks in them, but inevitably will have custom themes that are developed that need to modify those blocks which would be preferable to building a similar block from scratch for that client. Figuring out a maintainable and efficient way to extend blocks in a theme in some ways is really important. I'd be especially interested in tips for ensuring those changes made in the theme can be as safe as possible during WP upgrades to avoid breaking, etc. |
Beta Was this translation helpful? Give feedback.
-
The topic has been approved. The discussion is now locked, |
Beta Was this translation helpful? Give feedback.
-
Tutorial proposal:
One of the oft-repeated issues with WordPress' block styles system is that users can only add one style to a block at a time. More on this Gutenberg ticket: WordPress/gutenberg#14598
There are pros and cons to this system:
There has to be a middle ground...
How to combine block styles?
That is the question that has frustrated many developers. For example, suppose you had a "Large Text" style and a separate "Gradient Outline" style for the block. Do you create a third style named "Large Text with Gradient Outline"? That is what some devs have resorted to, but it is a slippery slope with each new style you add. And, you would never do this in a solid design system.
At the end of the day, a block style is nothing more than a simple class that uses a standard
.is-style-{$slug}
naming scheme. The reason this is great for themes is that it doesn't negatively impact user content if/when users switch themes.And this is (relatively) simple to recreate.
On the most low-effort DX end of the scale, you just have users input custom classes into the "Additional CSS Class(es)" box and call it a day. But, that makes for a poor UX.
On the other end of the spectrum, you build custom controls and let the user interact with them through the interface. Unlike custom blocks or plugins that add custom attributes, this system would use CSS classes under the hood. That means it also plays nicely with the WordPress theme review guidelines.
I've created several solutions in the past couple of weeks for testing this system. It works really, really well.
For example, here's a "Gradient Outline" picker that I put together:
Teaching theme authors block editor JavaScript
There are two things at play:
@wordpress/scripts
package and integrate it into a theme. AFAIK, there is no documentation aimed directly at theme dev, but it definitely works.This is potentially a two-part series because both of the topics could be relatively beefy on their own.
Beta Was this translation helpful? Give feedback.
All reactions