1111use Tempest \View \Element ;
1212use Tempest \View \Parser \TempestViewCompiler ;
1313use Tempest \View \Parser \TempestViewParser ;
14+ use Tempest \View \Parser \Token ;
1415use Tempest \View \Slot ;
1516use Tempest \View \ViewComponent ;
17+ use Tempest \View \WithToken ;
1618
1719use function Tempest \Support \arr ;
1820use function Tempest \Support \str ;
1921
20- final class ViewComponentElement implements Element
22+ final class ViewComponentElement implements Element, WithToken
2123{
2224 use IsElement;
2325
2426 private array $ dataAttributes ;
2527
2628 public function __construct (
29+ public readonly Token $ token ,
2730 private readonly Environment $ environment ,
2831 private readonly TempestViewCompiler $ compiler ,
2932 private readonly ViewComponent $ viewComponent ,
@@ -47,19 +50,19 @@ public function getSlots(): ImmutableArray
4750 {
4851 $ slots = arr ();
4952
50- $ default = [];
53+ $ defaultTokens = [];
5154
52- foreach ($ this ->getChildren () as $ child ) {
53- if ($ child instanceof SlotElement ) {
54- $ slot = Slot::fromElement ($ child );
55+ foreach ($ this ->token -> children as $ child ) {
56+ if ($ child-> tag === ' x-slot ' ) {
57+ $ slot = Slot::named ($ child );
5558
5659 $ slots [$ slot ->name ] = $ slot ;
5760 } else {
58- $ default [] = $ child ;
61+ $ defaultTokens [] = $ child ;
5962 }
6063 }
6164
62- $ slots [Slot::DEFAULT ] = Slot::fromElement ( new CollectionElement ( $ default ) );
65+ $ slots [Slot::DEFAULT ] = Slot::default (... $ defaultTokens );
6366
6467 return $ slots ;
6568 }
@@ -154,7 +157,9 @@ public function compile(): string
154157 return $ this ->environment ->isProduction () ? '' : ('<!-- ' . $ matches [0 ] . '--> ' );
155158 }
156159
157- $ compiled = $ slot ->content ;
160+ $ slotElement = $ this ->getSlotElement ($ slot ->name );
161+
162+ $ compiled = $ slotElement ?->compile() ?? '' ;
158163
159164 // There's no default slot content, but there's a default value in the view component
160165 if (trim ($ compiled ) === '' ) {
@@ -167,4 +172,25 @@ public function compile(): string
167172
168173 return $ this ->compiler ->compile ($ compiled ->toString ());
169174 }
175+
176+ private function getSlotElement (string $ name ): SlotElement |CollectionElement |null
177+ {
178+ $ defaultElements = [];
179+
180+ foreach ($ this ->getChildren () as $ childElement ) {
181+ if ($ childElement instanceof SlotElement && $ childElement ->name === $ name ) {
182+ return $ childElement ;
183+ }
184+
185+ if (! ($ childElement instanceof SlotElement)) {
186+ $ defaultElements [] = $ childElement ;
187+ }
188+ }
189+
190+ if ($ name === Slot::DEFAULT ) {
191+ return new CollectionElement ($ defaultElements );
192+ }
193+
194+ return null ;
195+ }
170196}
0 commit comments