Skip to content

Commit 11b37da

Browse files
committed
wip
1 parent 0306cbd commit 11b37da

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

packages/view/src/Elements/ViewComponentElement.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public function __construct(
2727
private readonly TempestViewCompiler $compiler,
2828
private readonly ViewComponent $viewComponent,
2929
array $attributes,
30-
) {
30+
)
31+
{
3132
$this->attributes = $attributes;
3233
$this->dataAttributes = arr($attributes)
3334
->filter(fn ($_, $key) => ! str_starts_with($key, ':'))
@@ -80,6 +81,10 @@ public function getSlot(string $name = 'slot'): ?Element
8081
$elements[] = $child;
8182
}
8283

84+
if ($elements === []) {
85+
return null;
86+
}
87+
8388
return new CollectionElement($elements);
8489
}
8590

@@ -156,18 +161,25 @@ public function compile(): string
156161
'<?php $attributes = $_previousAttributes ?? null; ?>',
157162
'<?php unset($_previousAttributes); ?>',
158163
)
164+
159165
// Compile slots
160166
->replaceRegex(
161-
regex: '/<x-slot\s*(name="(?<name>[\w-]+)")?((\s*\/>)|><\/x-slot>)/',
167+
regex: '/<x-slot\s*(name="(?<name>[\w-]+)")?/',
162168
replace: function ($matches) {
163-
$name = $matches['name'] ?: 'slot';
169+
$name = $matches['name'] ?? 'slot';
164170

165171
$slot = $this->getSlot($name);
166172

167-
if ($slot === null) {
173+
if ($slot === null && $this->environment->isProduction()) {
168174
// A slot doesn't have any content, so we'll comment it out.
169175
// This is to prevent DOM parsing errors (slots in <head> tags is one example, see #937)
170-
return $this->environment->isProduction() ? '' : ('<!--' . $matches[0] . '-->');
176+
return ('<!--' . $matches[0] . '-->');
177+
} elseif($slot === null) {
178+
$default = $slotElement->match('/<x-slot.*?>((.|\n)*?)<\/x-slot>/') ?? '';
179+
180+
if ($default) {
181+
$slot = new RawElement(tag: null, content: $default);
182+
}
171183
}
172184

173185
return $slot->compile();

tests/Integration/View/ViewComponentTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,4 +897,38 @@ public function test_escape_expression_attribute_in_view_components(): void
897897

898898
$this->assertSnippetsMatch('<div class="foo" :escaped="foo"></div>', $html);
899899
}
900+
901+
public function test_default_slot_value(): void
902+
{
903+
$this->registerViewComponent('x-test', <<<'HTML'
904+
<x-slot>Default</x-slot>
905+
<x-slot name="a">Default A</x-slot>
906+
<x-slot name="b">Default B</x-slot>
907+
HTML);
908+
909+
$this->assertSnippetsMatch(<<<'HTML'
910+
Overwritten
911+
Overwritten A
912+
Overwritten B
913+
HTML, $this->render('<x-test>
914+
Overwritten
915+
<x-slot name="a">Overwritten A</x-slot>
916+
<x-slot name="b">Overwritten B</x-slot>
917+
</x-test>'));
918+
919+
// $this->assertSnippetsMatch(<<<'HTML'
920+
// Overwritten
921+
// Default A
922+
// Overwritten B
923+
// HTML, $this->render('<x-test>
924+
// Overwritten
925+
// <x-slot name="b">Overwritten B</x-slot>
926+
// </x-test>'));
927+
//
928+
// $this->assertSnippetsMatch(<<<'HTML'
929+
// Default
930+
// Default A
931+
// Default B
932+
// HTML, $this->render('<x-test></x-test>'));
933+
}
900934
}

0 commit comments

Comments
 (0)