guglielmoseminara / x-livewire
A package that allows you to use livewire components the same way you would use blade components. Ie, giving it slots, atttributes etc
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 1
pkg:composer/guglielmoseminara/x-livewire
Requires
- php: ^8.1
- illuminate/contracts: ^9.0 || ^10.0 || ^11.0 || ^12.0
- livewire/livewire: ^3.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- jeroen-g/laravel-packager: ^2.8
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpunit/phpunit: ^9.5
This package is not auto-updated.
Last update: 2025-10-15 12:04:06 UTC
README
⚠️ Still in beta phase. Do not use in production environments (without testing) yet Contributions are highly welcomed
X-livewire
This package allows you to render livewire components like a blade component, giving it attributes, slots etc.
Assuming you wanted to create a livewire component, alert, usually you would render the component by:
<livewire:alert/>. However, there a few problems.
- You can't allow slots within the component. That is, this is invalid:
<livewire:alert>Alert</livewire:alert>. - You can't access the
$attributesbag. Thus, can't access the$attributesvariable directly. That is, if you do this:<livewire:alert title="Alert!"/>, you can't access the$titleattribute by$attributes->get('title'). Instead, you'd have to make$titlea public property in the component. Not to mention, other methods on$attributesare not accessible. Such as->merge([]),->whereStartsWith(), etc.
The creator of livewire, Caleb Porzio has made it clear that adding slots, attributes etc are not currently on the roadmap.
That's why I created X-livewire.
With X-Livewire, you can do:
<x-livewire _="alert">
My alert message
</x-livewire>
And, just like with Blade, you can:
- Access the
$attributesvariable:{{ $attributes->get('title') }}, - Access the
$slotvariable:{{ $slot }}
Installation
You can install the package via composer:
composer require titonova/x-livewire
Usage
- After creating your livewire component, make it extend
XLivewireBaseComponentrather thanComponent. ie:class Alert extends XLivewireBaseComponent{ - If you want to access the
$attributesbag in your x-livewire component's backend, add$this->setProps()as the first line in your component'smount()method. - In the view file of the component, e.g
alert.blade.phpadd@setUpXLivewireto the top of the file. - When you want to render the component:
Blade <x-livewire _="alert" title="Warning"> My alert message </x-livewire>
You can access the $slot and $attributes variables just like you would in a Blade component:
{{ $slot }} {{ $attributes->get('title') }}
You can also access the array of attributes that were passed to the x-livewire's component's tag but were not explicitedly declared in the class as
$tagAttributes property.
{{ $tagAttributes->get('href') }}
For example, attributes like primary, lg etc that don't need corresponding properties declarations in the class.
E.g
```HTML
Google
....
<span>
<a href="{{ $tagAttributes->get('href') }}>{{ $slot }}</a>
</span>
```
You can add and access named slots as such:
My alert message
<x-slot name="footer">My custom footer </x-slot>
</x-livewire>
....
<div class="alert ...">
{{ $slot }}
<div class="alert-footer">
{{ $footer ?? 'Default footer content' }}
</div>
</div>
If you want to access the slots directly as their Illuminate\View\ComponentSlot class, you can use the following:
$this->laravelSlots()['footer'].
Which would return an instance of Illuminate\View\ComponentSlot.
E.g:
+attributes: Illuminate\View\ComponentAttributeBag {#1379 ▼
#attributes: []
}
#contents: "<b><i>hello!!! </i> </b>"
With available methods such as
public withAttributes(array $attributes): $this Set the extra attributes that the slot should make available.
public toHtml(): string Get the slot's HTML string.
public isEmpty(): bool Determine if the slot is empty.
public isNotEmpty(): bool Determine if the slot is not empty.
public __toString(): string Get the slot's HTML string.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
TODO/Roadmap
[ ] Add Tests
[ ] Shorten tag declartion to <x-livewire:alert>
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.