Is your feature request related to a problem? Please describe.
The block styles variations API introduced in #7362 uses an is-style-{styleName}
className to the block for style variants. This works well for core blocks and new blocks, but may be overly opinionated for converting existing content that has an established approach to styling variants (see https://github.com/WordPress/gutenberg/pull/7362#issuecomment-398342742). For example, a pattern library that uses BEM conventions might use .element
for a default element, and .element.element--variant
for a variant of that element. I'd advocate for a solution that does not require changing an existing naming convention if one exists.
Describe the solution you'd like
A className
attribute could be added to each style variation which, if supplied, would be used instead of the default generated className. Here's some pseudocode:
styles: [
{
name: 'default',
label: __( 'Rounded' ),
isDefault: true
},
{
name: 'outline',
label: __( 'Outline' ),
className: 'button--outline'
},
{
name: 'squared',
label: __( 'Squared' )
className: 'button--squared'
},
],
Describe alternatives you've considered
The only alternative at present is to use the opinionated is-style-{styleName}
naming convention.
Completely agree. The point about converting old code seems especially relevant.
It's possible to get active style?
If I want to change the paragraph position depending on style, how I can do this?
@vicolaspetru The CSS class is applied to the block container both in the admin/editor and front-end, so registering gutenberg editor styles should be all you need to do.
Ist der still no way to remove that 'is-style' prefix? i just need that to use bootstrap classes.
Please add filter to remove/disable prefixes like: has- , is-style-
Admittedly, requiring to use the is-style prefix is frustrating because we're having to change our naming structure (BEM based) for our growing pattern library/components to match Gutenberg's decisions. :/
As a workaround you can prepend a whitespace to your class. Then you will have a useless "is-style-" class and the class you want.
@ecdani Tried your solution. Turns out when you change between variations, the classnames are always added, not changed.
in my example i want to create a large and small paragraph. when i change default paragraph to large, .is-style
and .text-large
get applied. when i change agains to small, text-large
remains and text-small
gets added. changing to default again keeps those previous classnames and adds is-style-default
.
Still no solution for this? I'd like to use my existing classes so I don't have to manage Block Editor CSS and my Global CSS ...
You are right @jurajk .
In my case i ended up using the provided classes.
But in other zone of my code i'm using the blocks.getSaveContent.extraProps filter. It allows me to intercept the classes and edit it before save it.
I see it here: https://wordpress.stackexchange.com/questions/308021/add-custom-class-to-core-blocks-in-gutenberg
Maybe it will be useful to someone with this problem ...
@ecdani thanks for the example. i was hoping to do this outside javascript, but it does the job nicely.
馃檶
Most helpful comment
Admittedly, requiring to use the is-style prefix is frustrating because we're having to change our naming structure (BEM based) for our growing pattern library/components to match Gutenberg's decisions. :/