-
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
Can't access className from a block's save function #7308
Comments
@joemcgill Can you update the description to include a representative snippet of code?
What happens in this scenario? |
@danielbachhuber I've updated the description with a reduced case and explain how the few core blocks that include a In my code example above, I don't know if there is a workaround that wouldn't require explicitly passing the |
Thanks @joemcgill |
Not sure this directly solves it, but if you add (This can lead to another issue where blocks that don't explicitly support |
Thanks for the suggestion @chrisvanpatten. I guess specifically duplicating the className as an attribute is an option, but seems duplicative. A nice workaround for the moment though. |
@joemcgill looking at the Cover Image block, it looks like the className is being passed in as |
All correct @chrisvanpatten. This is how you can reference |
@gziolo Can you elaborate on this? Should this work or not? When using |
@ocean90 I think the way it works now is
So |
(So potentially, this should get a Documentation flag.) |
@chrisvanpatten Unfortunately there's also no |
I think that using filter: So you can't really access it. The idea is that the generated class name is always applied to the root element. |
@gziolo Thanks for your answer! So the linked references should probably be updated? |
The generated className isn't auto-added in the I think there are a few potential paths:
I think either way we should audit the core blocks to be sure they aren't pulling in undefined Sounds like option 2, plus an audit of the blocks that are |
The documentation has since been updated in #11605. Should this then be closed? Otherwise, what are the specific action items of said audit? (What measure can be made for closure of the issue?) |
Oops. This can be closed! Just saw it was merged. |
I know this was closed because
|
@mrmadhat You can just specify |
Or use |
@ocean90 TIL… that's super handy! |
Thanks @ocean90 that's perfect |
I've found another good use for ocean90's approach, but this time for the If you're working with block styles and you attempt something like this:
The output will look something like this (once you've chosen a style):
The |
@rodrigodagostino Nice addition to the solution of @ocean90, I was facing this exact same issue today. Thanks! |
@jlambe really glad I could help, my friend :) |
Describe the bug
At times, it's useful to reference the
className
property of a block in the save function. This is currently not possible, as theclassName
value is not passed to the save function for a block, even though several core blocks expect to receive aclassName
argument (e.g., cover image, subhead, verse).For example, if you want to use the default
className
value in your save function to dynamically make your block use a BEM like class syntax, you might write something like this:However,
className
is undefined, so this doesn't work, even though several core blocks (as noted) show this exact argument signature. In those cases, the undefinedclassName
value is hidden by the fact that it is passed, along with other class names to theclassnames()
function or as theclassName
attribute of aRichText.Content
component, both of which hide the fact that this value is undefined.To Reproduce
Steps to reproduce the behavior:
Expected behavior
The
className
value should be passed as an argument to the save function.The text was updated successfully, but these errors were encountered: