|
1 | 1 |
|
2 | 2 | [<img src="https://github-ads.s3.eu-central-1.amazonaws.com/support-ukraine.svg?t=1" />](https://supportukrainenow.org) |
3 | 3 |
|
4 | | -# A package that allows you to use livewire components the same way you would use blade components. Ie, giving it slots, atttributes etc |
| 4 | +# X-livewire |
5 | 5 |
|
6 | 6 | [](https://packagist.org/packages/titonova/x-livewire) |
7 | 7 | [](https://github.com/titonova/x-livewire/actions?query=workflow%3Arun-tests+branch%3Amain) |
8 | 8 | [](https://github.com/titonova/x-livewire/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) |
9 | 9 | [](https://packagist.org/packages/titonova/x-livewire) |
10 | 10 |
|
11 | | -This is where your description should go. Limit it to a paragraph or two. Consider adding a small example. |
| 11 | +This package allows you to render livewire components like a blade component, giving it attributes, slots etc. |
12 | 12 |
|
13 | | -## Support us |
| 13 | +Assuming you wanted to create a livewire component, `alert`, usually you would render the component by: |
| 14 | +`<livewire:alert/>`. However, there a few problems. |
| 15 | +1. You can't allow slots within the component. That is, this is invalid: `<livewire:alert>Alert</livewire:alert>`. |
| 16 | +2. You can't access the `$attributes` bag. Thus, can't access the `$attributes` variable directly. |
| 17 | + That is, if you do this: `<livewire:alert title="Alert!"/>`, you can't access the `$title` attribute by `$attributes->get('title')`. Instead, you'd have to make `$title` a public property in the component. Not to mention, other methods on `$attributes` are not accessible. Such as `->merge([])`, `->whereStartsWith()`, etc. |
14 | 18 |
|
15 | | -[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/x-livewire.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/x-livewire) |
| 19 | +The creator of livewire, Caleb Porzio has made it clear that [adding slots, attributes etc are not currently on the roadmap](https://github.com/livewire/livewire/issues/68#issuecomment-599012420). |
16 | 20 |
|
17 | | -We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). |
| 21 | +That's why I created X-livewire. |
18 | 22 |
|
19 | | -We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). |
| 23 | +With X-Livewire, you can do: |
20 | 24 |
|
| 25 | + <x-livewire _="alert"> |
| 26 | + My alert message |
| 27 | + </x-livewire> |
| 28 | +And, just like with Blade, you can: |
| 29 | +1. Access the `$attributes` variable: |
| 30 | + `{{ $attributes->get('title') }}`, |
| 31 | +2. Access the `$slot` variable: |
| 32 | + `{{ $slot }}` |
21 | 33 | ## Installation |
22 | 34 |
|
23 | 35 | You can install the package via composer: |
24 | 36 |
|
25 | | -```bash |
26 | | -composer require titonova/x-livewire |
27 | | -``` |
28 | | - |
29 | | -You can publish and run the migrations with: |
30 | | - |
31 | | -```bash |
32 | | -php artisan vendor:publish --tag="x-livewire-migrations" |
33 | | -php artisan migrate |
34 | | -``` |
35 | | - |
36 | | -You can publish the config file with: |
37 | | - |
38 | | -```bash |
39 | | -php artisan vendor:publish --tag="x-livewire-config" |
40 | | -``` |
41 | | - |
42 | | -This is the contents of the published config file: |
43 | | - |
44 | | -```php |
45 | | -return [ |
46 | | -]; |
47 | | -``` |
48 | | - |
49 | | -Optionally, you can publish the views using |
50 | | - |
51 | | -```bash |
52 | | -php artisan vendor:publish --tag="x-livewire-views" |
53 | | -``` |
| 37 | + composer require titonova/x-livewire |
54 | 38 |
|
55 | 39 | ## Usage |
56 | 40 |
|
57 | | -```php |
58 | | -$xLivewire = new Titonova\XLivewire(); |
59 | | -echo $xLivewire->echoPhrase('Hello, Titonova!'); |
60 | | -``` |
61 | | - |
| 41 | +1. After creating your livewire component, make it extend `XLivewireBaseComponent` rather than `Component`. |
| 42 | +ie: `class Alert extends XLivewireBaseComponent{` |
| 43 | +2. In the view file of the component, e.g `alert.blade.php` add `@setUpXLivewire` to the top of the file. |
| 44 | +3. When you want to render the component: |
| 45 | + ```Blade |
| 46 | + <x-livewire _="alert" title="Warning"> |
| 47 | + My alert message |
| 48 | + </x-livewire> |
| 49 | + ``` |
| 50 | +4. You can access the `$slot` and `$attributes` variables just like you would in a Blade component: |
| 51 | + ``` |
| 52 | + {{ $slot }} |
| 53 | + {{ $attributes->get('title') }} |
| 54 | + ``` |
62 | 55 | ## Testing |
63 | 56 |
|
64 | 57 | ```bash |
|
0 commit comments