-
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
added 'assets_callback' to 'register_block_type' function #5740
Conversation
What of these would not be possible with existing |
Hi @aduth, thanks for reviewing my PR! Currently, Obviously you can just ignore these properties and use the existing hooks, On
My PR with the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For us to better understand how this is meant to be used, can you provide a real world example and its existing alternative?
The compelling bit for me is attribute-dependent additions. I could imagine this being useful, though I'd agree some real world use-cases may be valuable to have. |
Ok, guys, to better explain how the On this block, you select a set of images from your media library, and then you can customize the pieces that compose the slideshow. One of these pieces is the navigational arrows: For the navigational arrows, I added options for:
Each one of these options is an attribute. All of them are the kind of attributes that you can't set using css classes. You have to either generate the css to apply these attributes, or inline everything. So here is the implementation of these attributes with the current state of Gutenberg, and with the Current StateThe only way to add styles like color, background color and opacity is through inline styles. So you have to generate the html markup with the styles applied. For the arrow background, if you want to add a semi-transparent background, you need to create an html element only for that (if #5835 is accepted and merged, then this extra element would not be necessary anymore). On your block's JavaScript file, the <div class="wp-block-slideshow">
<div class="slideshow-container">
<div class="slideshow-wrapper">
<div class="slideshow-slide">
<img src="(...)" alt="">
</div>
<div class="slideshow-slide">
<img src="(...)" alt="">
</div>
</div>
<div class="slideshow-arrows">
<div class="slideshow-button-prev">
<span class="slideshow-arrow" style="width: 95px; height: 95px; color: #ffffff;">
<!-- extra element because this is the only way to do a semi-transparent background right now -->
<span class="arrow-bg" style="background-color: #340087; opacity: 0.7; border-radius: 47.5px;"></span>
<svg width="500" height="500" viewBox="0 0 500 500" class="arrow-icon-prev">(...)</svg>
</span>
</div>
<div class="slideshow-button-next">
<span class="slideshow-arrow" style="width: 95px; height: 95px; color: #ffffff;">
<!-- extra element because this is the only way to do a semi-transparent background right now -->
<span class="arrow-bg" style="background-color: #340087; opacity: 0.7; border-radius: 47.5px;"></span>
<svg width="500" height="500" viewBox="0 0 500 500" class="arrow-icon-next">(...)</svg>
</span>
</div>
</div>
</div>
</div> So all css styles are inline, and the With
|
Hi, @diegoliv, thanks for your contribution. A couple of points:
|
@mcsf, thanks for your comment! Here are some considerations:
The example provided is just one example of what you can do when you have access to blocks attributes inside the right action hook. My point of not using inline styles is not about being convenient. It's more about best practices and flexibility. The decrease of the amount of styles inlined is just a plus. My point is that:
The scoped attribute is currently obsolete and seems like will eventually be deprecated. But dude, I really like CSS variables! They're really cool, and your jsfiddle is a good example on how they can be handy. But one of the downsides is that they're not supported on IE. Of course, the current support is pretty good. But still, like you said, depending on your user base, it might be a good fit or not. If I have a the option to style my block and at the same time support all browsers, even some old versions, I think that this can be a better fit.
Yes, you can use the With all of that said, I took a look on #5445 and I really like the idea of dynamic bundles. One way of doing it is not only bundling CSS assets for all blocks on a page, but we could also add to the bundle some attribute based styles generated for a block. This is a approach similar to what Beaver Builder does. On BB, for a custom module, there's a php file where you can use a module's attributes to generate CSS. This CSS is then bundled with all of the generated CSS styles from the modules used on the page, and then this bundle is served on a single CSS file enqueued for that specific page. If we could do something like that, it would be awesome. |
Hi @diegoliv, Thanks for all of your work on this pull request. For now, I'd suggest we close it, but keep the issue open, and revisit the conversation in a couple of months once we've seen Gutenberg in the wild a little bit longer. I think time will help guide a better decision on this. |
any reason why 'assets_callback' not working in 'register_block_type' ? |
@prajaktagadhave: This pull request was closed without merging, meaning its changes were not effected. See #5445 for a broader issue on the topic. |
Description
Like suggested in #5705, this PR adds
assets_callback
to theregister_block_type
function. With this, developers can access block attributes onenqueue_block_assets
andenqueue_block_editor_assets
action hooks and use them to do things like conditionally enqueue scripts / styles, add inline styles or scripts, etc.An example of how to use this callback:
##Tested with: