I would like to see an action hook added so that we can enqueue scripts/stylesheets for specific elements. This would allow us to enqueue these files if and only if the element is being used. For example, I like to add a custom javascript file that enhances the functionality of the checkbox filters. Right now, I have to add a conditional to target the specific page(s) or else it gets added across the entire site.
It looks like this would be a very easy thing to do. Something like this…
Not every element has a script or stylesheet I could use that for. The filters do, but other elements don’t enqueue anything. I used filters as an example.
Also, it’s not just scripts I want to enqueue. Using the method you mentioned for stylesheets will cause a FOUC.
Maybe introduce a new action like do_action( 'bricks/assets/on_demand_enqueue_asset, $bricks_settings_string ); in assets.php, placed before Font Awesome is enqueued. This would run early enough for developers to access $bricks_settings_string and conditionally enqueue their own assets in the <head> or footer.
The Element API’s enqueue_scripts() method currently only loads assets in the footer.
This is a nice solution but my problem with this is, that if ever they change a property like $scripts that you override, you would have to manually adjust it in your code I guess. It’s cleaner if they would provide a hook for this.
That’s why I’m using filters such as bricks/element/set_root_attributes to enqueue styles /scripts and bricks/elements/{element_name}/scripts to extend the scripts instead replacing the $scripts property.
But there are a few pitfalls, like e.g. Elements that are being rendered with a VUE-JS Template or that you need to enqueue your Assets inside the Canvas/Iframe no matter what, to apply those styles whenever a new Element is added via the builder etc.