Skip to content

Conversation

browner12
Copy link
Contributor

currently it is required that a name be passed to a slot for a component via 1 of 3 methods:

<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 $__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, which is available in the component as $slot.

<x-slot></x-slot>

compiles to:

<?php $__env->slot('slot', null, []); ?> slot <?php $__env->endSlot(); ?>

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.

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.
@browner12
Copy link
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.

@taylorotwell taylorotwell merged commit 42076ce into laravel:12.x Sep 3, 2025
63 checks passed
@browner12 browner12 deleted the AB-default-slot branch September 3, 2025 18:09
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>`
@AhmedAlaa4611
Copy link
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 <x-slot> instead of using it directly?

@AhmedAlaa4611
Copy link
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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants