[12.x] provide a default slot name when compiling#56883
Merged
Conversation
currently it is required that a name be passed to a slot for a component via 1 of 3 methods: ```php <x-slot:name></x-slot> <x-slot name="name"></x-slot> <x-slot :name="$name"></x-slot> ``` If you omit all of these options you currently get a syntax error due to an incorrectly compiled slot. ```php <?php $__env->slot(, null, []); ?> slot <?php $__env->endSlot(); ?> ``` This commit adds a default slot name of "slot" if no others are given. This allows the user to omit designating a name if they wish to use the slot as the default, wich is available in the component as `$slot`. This simple string of "slot" joins the "inline name" and "attribute name" in needing to be wrapped in single quotes, unlike the "bound name". In order to accomplish this I reversed the logic of the conditional change so instead of looking for either not empty "inline name" or not empty "attribute name", it looks for empty "bound name". the inversion of logic should behave the same, and gives a very small performance improvement.
these is a case when a user passes both the "bound name" and 1 of the other types, which screws up this inversion. we'll move the short ternary outside of the `stripQuotes()` method, so we can explictly set our wrapped `'slot'` value.
Contributor
Author
|
had to revert the logic inversion and handle the default name slightly different due to a case where multiple names are passed to the slot. |
browner12
added a commit
to browner12/docs
that referenced
this pull request
Sep 3, 2025
while we always have the option for `$slot` content to live anywhere in the component, we can also be explicit by using the unnamed `<x-slot>` tag. this is necessary when you want to pass attributes to your default slot. this is kind of documentation for laravel/framework#56883 but the ability to have a default slot actually already existed, you just needed to name it "slot". `<x-slot:slot>`
Contributor
|
Before this PR: <x-link href="#">
default slot
</x-link>After: <x-link href="#">
<x-slot>default slot</x-slot>
</x-link>Why introduce extra complexity by wrapping |
Contributor
|
I think it may help with readability in larger examples: <x-card class="shadow-sm">
<x-slot:heading class="font-bold">
Heading
</x-slot>
<x-slot class="font-bold">
Content
</x-slot>
<x-slot:footer class="text-sm">
Footer
</x-slot>
</x-card> |
Contributor
Author
|
it's always been possible to wrap it, this just adds the default name which allows you to do <x-slot>instead of <x-slot:slot>While someone could use this for readability, the main reason you'd wrap it is if you have dedicated attributes for the slot. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
currently it is required that a name be passed to a slot for a component via 1 of 3 methods:
If you omit all of these options you currently get a syntax error due to an incorrectly compiled slot.
This commit adds a default slot name of "slot" if no others are given. This allows the user to omit designating a name if they wish to use the slot as the default, which is available in the component as
$slot.compiles to:
This simple string of "slot" joins the "inline name" and "attribute name" in needing to be wrapped in single quotes, unlike the "bound name". In order to accomplish this I reversed the logic of the conditional change so instead of looking for either not empty "inline name" or not empty "attribute name", it looks for empty "bound name". the inversion of logic should behave the same, and gives a very small performance improvement.
I could see someone making the argument this should be sent to master. Even though it was never documented, I'm considering it more as a bug fix, as using it this way in the past would have resulted in an error. Happy to resubmit though.