Skip to content

Commit 42076ce

Browse files
authored
[12.x] provide a default slot name when compiling (#56883)
* provide a default slot name when compiling 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. * revert quote wrapping logic 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. * add test
1 parent 2733fc7 commit 42076ce

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/Illuminate/View/Compilers/ComponentTagCompiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ public function compileSlots(string $value)
560560
/x";
561561

562562
$value = preg_replace_callback($pattern, function ($matches) {
563-
$name = $this->stripQuotes($matches['inlineName'] ?: $matches['name'] ?: $matches['boundName']);
563+
$name = $this->stripQuotes($matches['inlineName'] ?: $matches['name'] ?: $matches['boundName']) ?: "'slot'";
564564

565565
if (Str::contains($name, '-') && ! empty($matches['inlineName'])) {
566566
$name = Str::camel($name);

tests/Integration/View/BladeTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ public function test_bound_name_attribute_can_be_used_if_using_short_slot_names_
207207
</div>', trim($content));
208208
}
209209

210+
public function test_no_name_passed_to_slot_uses_default_name()
211+
{
212+
$content = Blade::render('<x-link href="#"><x-slot>default slot</x-slot></x-link>');
213+
214+
$this->assertSame('<a href="#">default slot</a>', trim($content));
215+
}
216+
210217
public function testViewCacheCommandHandlesConfiguredBladeExtensions()
211218
{
212219
View::addExtension('sh', 'blade');

0 commit comments

Comments
 (0)