Performance: Components and slots
This affects page load speed and rendering performance by controlling how reusable UI parts are rendered and how much DOM is generated.
<!-- Blade component usage -->
<x-card>
<x-slot name="title">{{ $title }}</x-slot>
{{ $slot }}
</x-card><!-- Blade template without components -->
<div>
<h2>{{ $title }}</h2>
<p>{{ $slot }}</p>
</div>
<!-- Repeated inline in many places -->| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline repeated markup | High (many duplicated nodes) | Multiple reflows per repeated block | High paint cost due to large DOM | [X] Bad |
| Blade components with slots | Low (reusable nodes) | Single reflow for component | Lower paint cost with smaller DOM | [OK] Good |
header to a Laravel Blade component called card?<x-slot name="slotName">...</x-slot> inside the component tags.<x-card><x-slot name="header">Title</x-slot></x-card> correctly uses <x-slot name="header">. Others misuse slot attribute or tag.<x-alert> <x-slot name="title">Warning</x-slot> This is an alert message. </x-alert>
<div class="alert">
<h4>{{ $title }}</h4>
<p>{{ $slot }}</p>
</div>$title variable receives content from the named slot title. The default $slot holds the inner content outside named slots.title slot is "Warning", and the default slot is "This is an alert message." so they fill <h4> and <p> respectively.<x-modal> <x-slot name="footer">Close</x-slot> <x-slot>Content here</x-slot> </x-modal>
<x-slot> without a name.<x-slot> without a name is invalid. Default slot content is just plain content inside the component.profile-card component that accepts a header slot for the user's name, a default slot for the bio, and a footer slot for action buttons. Which Blade component template correctly supports these slots?header and footer correspond to $header and $footer. The default slot content is in $slot.<div class="profile-card">
<header>{{ $header }}</header>
<section>{{ $slot }}</section>
<footer>{{ $footer }}</footer>
</div> places $header in <header>, $slot in <section>, and $footer in <footer>, matching the requirement.Pass the test and this pattern moves unknown