Skip to content

Commit 2a66e5e

Browse files
authored
Merge pull request #158 from hotwired-laravel/tm/boost-guidelines
Move the Boost guidelines to the expected folder
2 parents 23b7d7f + 96fb041 commit 2a66e5e

File tree

3 files changed

+36
-95
lines changed

3 files changed

+36
-95
lines changed

.ai/hotwire.blade.php renamed to resources/boost/guidelines/core.blade.php

Lines changed: 36 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
11
## Hotwire/Turbo Core Principles
22
- For standard application development, use Hotwire (Turbo + Stimulus)
3-
- Send HTML over the wire instead of JSON. Keep complexity on the server side.
4-
- Use Turbo Drive for smooth page transitions without full page reloads.
5-
- Decompose pages with Turbo Frames for independent sections that update separately.
6-
- Use Turbo Streams for real-time updates and dynamic content changes.
3+
- For most interactions, use regular links and form submits (Turbo Drive will make them fast and dynamic)
4+
- Decompose pages with Turbo Frames for independent sections that update separately
5+
- Use Turbo Streams for real-time updates and dynamic content changes
76
- Leverage Stimulus for progressive JavaScript enhancement when Turbo isn't sufficient (if Stimulus is available)
8-
- Prefer server-side template rendering and state management over client-side frameworks.
9-
- Enable "morphing" for seamless page updates that preserve scroll position and focus.
10-
- Use data attributes for JavaScript hooks
11-
- For more complex JavaScript dependencies, use Importmap Laravel
7+
- Prefer server-side template rendering and state management over client-side frameworks and state
8+
- Use data attributes for JavaScript hooks and CSS styling for as much as possible
129

13-
## Turbo Setup & Base Helpers
10+
## Base Helpers
1411
@verbatim
1512
- Turbo automatically handles page navigation, form submissions, and CSRF protection
16-
- Enable morphing in your layout (preserves DOM state during page updates): `<x-turbo::refresh-method method="morph" />`
17-
- Configure scroll behavior in your layout: `<x-turbo::refresh-scroll scroll="preserve" />`
18-
- Enable both morphing and scroll preservation with a single component: `<x-turbo::refreshes-with method="morph" scroll="preserve" />`
19-
- Generate unique DOM IDs from models: use function `dom_id($model, 'optional_prefix')` or Blade directive `@domid($model, 'optional_prefix')`
20-
- Generate CSS classes from models: use function `dom_class($model, 'optional_prefix')` or Blade directive `@domclass($model, 'optional_prefix')`
13+
- You may configure morphing and scroll preservation for a page (or layout) with: `<x-turbo::refreshes-with method="morph" scroll="preserve" />`
14+
- Generate unique DOM IDs from models: use the `dom_id($model, 'optional_prefix')` global function or Blade directive `@domid($model, 'optional_prefix')`
15+
- Generate CSS classes from models: use the `dom_class($model, 'optional_prefix')` global function or Blade directive `@domclass($model, 'optional_prefix')`
2116
@endverbatim
2217

2318
## Turbo Frames Best Practices
24-
- Use frames to decompose pages into independent sections that can update without full page reloads:
19+
- Use frames to decompose pages into independent sections that can update without full page reloads
20+
- Forms and links inside frames automatically target their containing frame (no configuration needed)
21+
- You may override the default frame target of a link or form with `[data-turbo-frame]` attribute:
22+
- Use a frame's DOM ID to target a specific frame
23+
- Use the value `_top` to break out of frames and navigate the full page
24+
- The `[:id]` prop accepts models and automatically generates DOM IDs for them
25+
- The `[:src]` prop accepts a URL to lazy-load from content. Optionally, you may pair it with a `[loading=lazy]` so it only loads when the element is visible in the viewport
26+
27+
Example:
2528
@verbatim
2629
```blade
2730
<x-turbo::frame :id="$post">
2831
<h3>{{ $post->title }}</h3>
2932
<p>{{ $post->content }}</p>
30-
<a href="{{ route('posts.edit', $post) }}">Edit</a>
31-
</x-turbo::frame>
32-
```
33-
@endverbatim
34-
- Forms and links inside frames automatically target their containing frame (no configuration needed):
35-
@verbatim
36-
```blade
37-
<x-turbo::frame :id="$post">
33+
<a href="{{ route('posts.edit', $post) }}" data-turbo-frame="_top">Edit</a>
3834
<form action="{{ route('posts.store') }}" method="POST">
3935
@csrf
4036
<input type="text" name="title" required>
@@ -43,17 +39,10 @@
4339
</x-turbo::frame>
4440
```
4541
@endverbatim
46-
- Override default frame targeting with `data-turbo-frame` attribute:
47-
- Use a frame's DOM ID to target a specific frame
48-
- Use `_top` to break out of frames and navigate the full page:
49-
@verbatim
50-
```blade
51-
<a href="{{ route('posts.show', $post) }}" data-turbo-frame="_top">View Full Post</a>
52-
```
53-
@endverbatim
5442

5543
## Turbo Streams for Dynamic Updates
56-
- Return Turbo Stream responses from controllers to update specific page elements without full page reload:
44+
45+
- You may return Turbo Streams from controllers after form submissions to update specific page elements (always check if the request accepts Turbo Streams for resilience)
5746
@verbatim
5847
<code-snippet name="Controller returning Turbo Streams" lang="php">
5948
public function store(Request $request)
@@ -64,53 +53,30 @@ public function store(Request $request)
6453
return turbo_stream([
6554
turbo_stream()->append('posts', view('posts.partials.post', ['post' => $post])),
6655
turbo_stream()->update('create_post', view('posts.partials.form', ['post' => new Post()])),
56+
// turbo_stream()->prepend('some_dom_id', view('posts.partials.post', ['post' => $post])),
57+
// turbo_stream()->before('some_dom_id', view('...'))
58+
// turbo_stream()->after('some_dom_id', view('...'))
59+
// turbo_stream()->replace('some_dom_id', view('...'))
60+
// turbo_stream()->remove('some_dom_id')
6761
]);
6862
}
6963

7064
return back();
7165
}
7266
</code-snippet>
7367
@endverbatim
74-
- Available Turbo Stream actions for manipulating DOM elements:
68+
- Turbo Streams can also be broadcasted using Laravel Echo for real-time updates to all users connected to a channel:
7569
@verbatim
76-
<code-snippet name="Turbo Stream actions" lang="php">
77-
// Append content
78-
turbo_stream()->append($comment, view('comments.partials.comment', [
79-
'comment' => $comment,
80-
]));
81-
82-
// Prepend content
83-
turbo_stream()->prepend($comment, view('comments.partials.comment', [
84-
'comment' => $comment,
85-
]));
86-
87-
// Insert before
88-
turbo_stream()->before($comment, view('comments.partials.comment', [
89-
'comment' => $comment,
90-
]));
91-
92-
// Insert after
93-
turbo_stream()->after($comment, view('comments.partials.comment', [
94-
'comment' => $comment,
95-
]));
96-
97-
// Replace content (swaps the target element)
98-
turbo_stream()->replace($comment, view('comments.partials.comment', [
99-
'comment' => $comment,
100-
]));
101-
102-
// Update content (keeps the target element and only updates its contents)
103-
turbo_stream()->update($comment, view('comments.partials.comment', [
104-
'comment' => $comment,
105-
]));
106-
107-
// Removes content
108-
turbo_stream()->remove($comment);
70+
<code-snippet name="Listening to Broadcasts" lang="blade">
71+
<x-turbo::stream-from :source="$post" />
10972
</code-snippet>
110-
@endverbatim
111-
- Broadcast Turbo Streams over WebSockets to push real-time updates to all connected users:
112-
@verbatim
73+
11374
<code-snippet name="Broadcasting Turbo Streams" lang="php">
75+
// Ensure the channel is defined in `routes/channels.php`:
76+
Broadcast::channel(Post::class, function (User $user, Post $post) {
77+
return $user->belongsToProject($post->project);
78+
});
79+
11480
// Add the trait to the model:
11581
use HotwiredLaravel\TurboLaravel\Models\Broadcasts;
11682

@@ -120,9 +86,9 @@ class Post extends Model
12086
}
12187

12288
// When you want to trigger the broadcasting from anywhere (including model events)...
123-
$post->broadcastAppend()->to('posts');
12489
$post->broadcastUpdate();
12590
$post->broadcastRemove();
91+
$post->broadcastAppend()->to('posts');
12692
</code-snippet>
12793
@endverbatim
12894

src/Commands/PublishBoostGuidelineCommand.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/TurboServiceProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use HotwiredLaravel\TurboLaravel\Broadcasters\Broadcaster;
66
use HotwiredLaravel\TurboLaravel\Broadcasters\LaravelBroadcaster;
77
use HotwiredLaravel\TurboLaravel\Broadcasting\Limiter;
8-
use HotwiredLaravel\TurboLaravel\Commands\PublishBoostGuidelineCommand;
98
use HotwiredLaravel\TurboLaravel\Commands\TurboInstallCommand;
109
use HotwiredLaravel\TurboLaravel\Facades\Turbo as TurboFacade;
1110
use HotwiredLaravel\TurboLaravel\Http\Middleware\TurboMiddleware;
@@ -77,7 +76,6 @@ private function configurePublications(): void
7776

7877
$this->commands([
7978
TurboInstallCommand::class,
80-
PublishBoostGuidelineCommand::class,
8179
]);
8280
}
8381

0 commit comments

Comments
 (0)