Skip to content

Commit fc193a2

Browse files
committed
guidelines: livewire: improve
1 parent 6d0c0c0 commit fc193a2

File tree

5 files changed

+78
-11
lines changed

5 files changed

+78
-11
lines changed

.ai/livewire/2/.gitkeep

Whitespace-only changes.

.ai/livewire/2/core.blade.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
- `wire:model` is live by default.
2+
- **Namespace**: Components typically exist in `App\Http\Livewire`.
3+
- **Events**: Use `emit()`, `emitTo()`, `emitSelf()` and `dispatchBrowserEvent()` for events.
4+
- Alpine is included separately to Livewire.
5+
- You can listen for `livewire:load` to hook into Livewire initialization, and `Livewire.onPageExpired` for when the page expires:
6+
@verbatim
7+
<code-snippet name="livewire:load example" lang="js">
8+
document.addEventListener('livewire:load', function () {
9+
Livewire.onPageExpired(() => {
10+
alert('Your session expired');
11+
});
12+
13+
Livewire.onError(status => console.error(status));
14+
});
15+
</code-snippet>
16+
@endverbatim

.ai/livewire/3/core.blade.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
1-
#### Key Changes from Livewire 2
2-
- These changed in Livewire 2, but may not have in this project. Verify this project's setup to ensure you conform with conventions.
3-
1+
## Key Changes from Livewire 2
2+
- These changed in Livewire 2, but may not have been updated in this project. Verify this project's setup to ensure you conform with project conventions.
3+
- **Wire:model**: Use `wire:model.live` for real-time updates, `wire:model` is now deferred by default.
44
- **Namespace**: Components now use `App\Livewire` (not `App\Http\Livewire`).
55
- **Events**: Use `$this->dispatch()` (not `emit` or `dispatchBrowserEvent`).
66
- **Layout path**: `components.layouts.app` (not `layouts.app`).
7-
- **Deferred by default**: Use `wire:model.live` for real-time updates.
8-
- **Alpine included**: Don't manually include Alpine.js.
7+
8+
## New directives
9+
- `wire:show`, `wire:transition`, `wire:cloak, `wire:offline`, `wire:target` are available for use. Use the docs to find usages.
10+
11+
## Alpine
12+
- Alpine is now included with Livewire, don't manually include Alpine.js.
13+
- Plugins built in to Alpine: persist, intersect, collapse, and focus.
14+
15+
## Lifecycle hooks
16+
- You can listen for `livewire:init` to hook into Livewire initialization, and `fail.status === 419` for the page expiring:
17+
@verbatim
18+
<code-snippet name="livewire:load example" lang="js">
19+
document.addEventListener('livewire:init', function () {
20+
Livewire.hook('request', ({ fail }) => {
21+
if (fail && fail.status === 419) {
22+
alert('Your session expired');
23+
}
24+
});
25+
26+
Livewire.hook('message.failed', (message, component) => {
27+
console.error(message);
28+
});
29+
});
30+
</code-snippet>
31+
@endverbatim

.ai/livewire/core.blade.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
## Livewire Core
2-
- Use the `search-docs` tool to find exact version specific documentation for how to write Livewire.
3-
2+
- Use the `search-docs` tool to find exact version specific documentation for how to write Livewire & Livewire tests.
3+
- Use the `php artisan make:livewire [Posts\\CreatePost]` artisan command to create new components
4+
- State should live on the server, with the UI reflecting it.
5+
- All Livewire requests hit the Laravel backend, they're like regular HTTP requests. Always validate form data, and run authorization checks in Livewire actions.
46

57
## Livewire Best Practices
6-
- **Single root element** in Blade components
7-
- **Loading states**: Use `wire:loading` and `wire:dirty`.
8-
- **Add wire:key** in loops:
8+
- Livewire components require a single root element.
9+
- Use `wire:loading` and `wire:dirty` for delightful loading states.
10+
- Add `wire:key` in loops:
911
@verbatim
1012
```blade
1113
@foreach ($items as $item)
@@ -15,3 +17,28 @@
1517
@endforeach
1618
```
1719
@endverbatim
20+
- Prefer lifecycle hooks like `mount()`, `updatedFoo()`) for initialization and reactive side effects:
21+
@verbatim
22+
<code-snippet name="Lifecycle hook examples" lang="php">
23+
public function mount(User $user) { $this->user = $user; }
24+
public function updatedSearch() { $this->resetPage(); }
25+
</code-snippet>
26+
@endverbatim
27+
28+
## Testing Livewire
29+
@verbatim
30+
<code-snippet name="Example Livewire component test" lang="php">
31+
Livewire::test(Counter::class)
32+
->assertSet('count', 0)
33+
->call('increment')
34+
->assertSet('count', 1)
35+
->assertSee(1)
36+
->assertStatus(200);
37+
</code-snippet>
38+
@endverbatim
39+
@verbatim
40+
<code-snippet name="Testing a Livewire component exists within a page" lang="php">
41+
$this->get('/posts/create')
42+
->assertSeeLivewire(CreatePost::class);
43+
</code-snippet>
44+
@endverbatim

.ai/volt/core.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
- This project uses Livewire Volt for interactivity within its pages. New pages requiring interactivity must also use Livewire Volt. There is documentation available for it.
2-
- Volt is an elegantly crafted **functional** API for Livewire that supports single-file components, allowing a component's PHP logic and Blade templates to coexist in the same file
2+
- Make new Volt components using `php artisan make:volt [name] [--test] [--pest]`
3+
- Volt is a **functional** API for Livewire that supports single-file components, allowing a component's PHP logic and Blade templates to coexist in the same file
34
- **Single-File Components**: Livewire Volt allows PHP logic and Blade templates in one file. Components use the `@volt` directive.
45
- You must check existing Volt components to find out if they're functional or class based. If you can't detect that, ask the user which they prefer before writing a Volt component.
56

0 commit comments

Comments
 (0)