@@ -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 ();
0 commit comments