[10.x] Add @slots directive for optional component slots #47574
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR will add a
@slots
blade directive mainly for use in anonymous components.It adds a mechanism to declare optional slots at the top of your components which will automatically be transformed into ComponentSlots when they are a string or do not exist.
The issue this solves is, say you're building a dialog component with text and a button which you can optionally change the styling and contents of. You might have your template look like this.
However this will never work as button is
null
and$button->attributes
will throw an error let alone merging classes.The options you have are adding php at the top or coalescing the
$button->attributes->class
and copy-pasting your classes.The solution this offers is the following template
Which will define all optional slots used in the code clearly at the top, and allow us to set default data to be overwritten if the named slot is used.
This is not breaking as it is a new feature which affects nothing if it is not used.