Skip to content

Commit ee815a3

Browse files
authored
Merge pull request #134 from hotwired-laravel/turbo-drive-components
Turbo Drive components
2 parents cbc273d + 21579ca commit ee815a3

30 files changed

+183
-289
lines changed

docs/broadcasting.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ You may add this tag to any Blade view passing the channel you want to listen to
2626
/>
2727
```
2828

29-
For convenience, you may prefer using the `<x-turbo-stream-from>` Blade component that ships with Turbo Laravel (it requires that you have a custom element named `<turbo-echo-stream-source>` available, since that's the tag this component will render). You may pass the model as the `source` prop to it, it will figure out the channel name for that specific model using [Laravel's conventions](https://laravel.com/docs/broadcasting#model-broadcasting-conventions):
29+
For convenience, you may prefer using the `<x-turbo::stream-from>` Blade component that ships with Turbo Laravel (it requires that you have a custom element named `<turbo-echo-stream-source>` available, since that's the tag this component will render). You may pass the model as the `source` prop to it, it will figure out the channel name for that specific model using [Laravel's conventions](https://laravel.com/docs/broadcasting#model-broadcasting-conventions):
3030

3131
```blade
32-
<x-turbo-stream-from :source="$post" />
32+
<x-turbo::stream-from :source="$post" />
3333
```
3434

3535
By default, it expects a private channel, so it must be used in a page where users are already authenticated. You may control the channel type in the tag with a `type` attribute.
3636

3737
```blade
38-
<x-turbo-stream-from :source="$post" type="public" />
38+
<x-turbo::stream-from :source="$post" type="public" />
3939
```
4040

4141
Make sure you have the Broadcast Auth Route for your models registered in your `routes/channels.php` file:

docs/helpers.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,39 @@ Which will generate a `comments_post_123` DOM ID, assuming your Post model has a
2828

2929
## Blade Components
3030

31-
### The `<x-turbo-frame>` Blade Component
31+
### The `<x-turbo::frame>` Blade Component
3232

33-
You may also prefer using the `<x-turbo-frame>` Blade component that ships with the package. This way, you don't need to worry about using the `@domid()` helper for your Turbo Frame:
33+
You may also prefer using the `<x-turbo::frame>` Blade component that ships with the package. This way, you don't need to worry about using the `@domid()` helper for your Turbo Frame:
3434

3535
```blade
36-
<x-turbo-frame :id="$post">
36+
<x-turbo::frame :id="$post">
3737
<!-- Content -->
38-
</x-turbo-frame>
38+
</x-turbo::frame>
3939
```
4040

4141
To the `:id` prop, you may pass a string, which will be used as-is as the DOM ID, an Eloquent model instance, which will be passed to the `dom_id()` function that ships with the package (the same one as the `@domid()` Blade directive uses behind the scenes), or an array tuple where the first item is an instance of an Eloquent model and the second is the prefix of the DOM ID, something like this:
4242

4343
```blade
44-
<x-turbo-frame :id="[$post, 'comments']">
44+
<x-turbo::frame :id="[$post, 'comments']">
4545
<!-- Comments -->
46-
</x-turbo-frame>
46+
</x-turbo::frame>
4747
```
4848

49-
Additionally, you may also pass along any prop that is supported by the Turbo Frame custom Element to the `<x-turbo-frame>` Blade component, like `target`, `src`, or `loading`. These are the listed attributes, but any other attribute will also be forwarded to the `<turbo-frame>` tag that will be rendered by the `<x-turbo-frame>` component. For a full list of what's possible to do with Turbo Frames, see the [documentation](https://turbo.hotwired.dev/handbook/frames).
49+
Additionally, you may also pass along any prop that is supported by the Turbo Frame custom Element to the `<x-turbo::frame>` Blade component, like `target`, `src`, or `loading`. These are the listed attributes, but any other attribute will also be forwarded to the `<turbo-frame>` tag that will be rendered by the `<x-turbo::frame>` component. For a full list of what's possible to do with Turbo Frames, see the [documentation](https://turbo.hotwired.dev/handbook/frames).
5050

51-
### The `<x-turbo-stream>` Blade Component
51+
### The `<x-turbo::stream>` Blade Component
5252

53-
If you're rendering a Turbo Stream inside a your Blade files, you may use the `<x-turbo-stream>` helper:
53+
If you're rendering a Turbo Stream inside a your Blade files, you may use the `<x-turbo::stream>` helper:
5454

5555
```blade
56-
<x-turbo-stream :target="$post" action="update">
56+
<x-turbo::stream :target="$post" action="update">
5757
@include('posts._post', ['post' => $post])
58-
<x-turbo-stream>
58+
<x-turbo::stream>
5959
```
6060

6161
Just like in the Turbo Frames' `:id` prop, the `:target` prop of the Turbo Stream component accepts a string, a model instance, or an array to resolve the DOM ID using the `dom_id()` function.
6262

63-
### The `<x-turbo-refreshes-with>` Blade Component
63+
### The `<x-turbo::refreshes-with>` Blade Component
6464

6565
We can configure which update method Turbo should so to update the document:
6666

@@ -76,10 +76,10 @@ You can also configure the scroll behavior on Turbo:
7676
| `reset` | Resets the scroll position to the top, mimicking for the browser handles new page visits |
7777
| `preserve` | Preserves the current scroll position (usually results in a better UX when used with the `morph` method) |
7878

79-
You may use the `<x-turbo-refreshes-with />` component in your main layout's `<head>` tag or on specific pages to configure how Turbo should update the page. Here's an example:
79+
You may use the `<x-turbo::refreshes-with />` component in your main layout's `<head>` tag or on specific pages to configure how Turbo should update the page. Here's an example:
8080

8181
```blade
82-
<x-turbo-refreshes-with method="morph" scroll="preserve" />
82+
<x-turbo::refreshes-with method="morph" scroll="preserve" />
8383
```
8484

8585
This will render two HTML `<meta>` tags:

docs/turbo-frames.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ The Turbo Stream tag that ships with Turbo can be used on your Blade views just
1515
In this case, the `@domid()` directive is being used to create a dom ID that looks like this `create_comment_post_123`. There's also a Blade Component that ships with Turbo Laravel and can be used like this:
1616

1717
```blade
18-
<x-turbo-frame :id="[$post, 'create_comment']">
18+
<x-turbo::frame :id="[$post, 'create_comment']">
1919
<p>Loading...</p>
20-
</x-turbo-frame>
20+
</x-turbo::frame>
2121
```
2222

2323
When using the Blade Component, you don't have to worry about using the `@domid()` directive or the `dom_id()` function, as this gets handled automatically by the package. You may also pass a string if you want to enforce your own DOM ID.
2424

2525
Any other attribute passed to the Blade Component will get forwarded to the underlying `<turbo-frame>` element, so if you want to turn a Turbo Frame into a lazy-loading Turbo Frame using the Blade Component, you can do it like so:
2626

2727
```blade
28-
<x-turbo-frame :id="[$post, 'create_comment']" loading="lazy" :src="route('post.comments.create', $post)">
28+
<x-turbo::frame :id="[$post, 'create_comment']" loading="lazy" :src="route('post.comments.create', $post)">
2929
<p>Loading...</p>
30-
</x-turbo-frame>
30+
</x-turbo::frame>
3131
```
3232

3333
This will work for any other attribute you want to forward to the underlying component.

docs/turbo-streams.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,24 +291,24 @@ Remember, these are Blade views, so you have the full power of Blade at your han
291291
@endif
292292
```
293293

294-
Similar to the `<x-turbo-frame>` Blade component, there's also a `<x-turbo-stream>` Blade component that can simplify things a bit. It has the same convention of figuring out the DOM ID when you're passing a model instance or an array as `target` attribute of the `<x-turbo-frame>` component. When using the component version, there's no need to specify the template wrapper for the Turbo Stream tag, as that will be added by the component itself. So, the same example would look something like this:
294+
Similar to the `<x-turbo::frame>` Blade component, there's also a `<x-turbo::stream>` Blade component that can simplify things a bit. It has the same convention of figuring out the DOM ID when you're passing a model instance or an array as `target` attribute of the `<x-turbo::frame>` component. When using the component version, there's no need to specify the template wrapper for the Turbo Stream tag, as that will be added by the component itself. So, the same example would look something like this:
295295

296296
```blade
297297
@include('layouts.turbo.flash_stream')
298298
299-
<x-turbo-stream :target="[$comment->post, 'comments']" action="append">
299+
<x-turbo::stream :target="[$comment->post, 'comments']" action="append">
300300
@include('comments._comment', ['comment' => $comment])
301-
</x-turbo-stream>
301+
</x-turbo::stream>
302302
```
303303

304304
I hope you can see how powerful this can be to reusing views.
305305

306306
## Custom Actions
307307

308-
You may also use the `<x-turbo-stream>` Blade component for your custom actions as well:
308+
You may also use the `<x-turbo::stream>` Blade component for your custom actions as well:
309309

310310
```blade
311-
<x-turbo-stream action="console_log" value="Hello World" />
311+
<x-turbo::stream action="console_log" value="Hello World" />
312312
```
313313

314314
Custom actions are only supported from Blade views. You cannot return those from controllers using the Pending Streams Builder.

docs/upgrade.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ First, update the namespaces from the previous package. You can either do it fro
1010
find app config resources tests -type f -exec sed -i 's/Tonysm\\TurboLaravel/HotwiredLaravel\\TurboLaravel/g' {} +
1111
```
1212

13-
Next, require the new package and remove the previous one:
13+
Next, update your views referencing the old components as `<x-turbo-*` to the new format which is `<x-turbo::*`. This command should be enough:
14+
15+
```bash
16+
find app resources tests -type f -exec sed -i 's/x-turbo-/x-turbo::/g' {} +
17+
```
18+
19+
Then, require the new package and remove the previous one:
1420

1521
```bash
1622
composer require hotwired-laravel/turbo-laravel:2.0.0-beta1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<meta name="turbo-cache-control" content="no-cache">
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<meta name="turbo-cache-control" content="no-preview">

resources/views/components/frame.blade.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
@props(['id', 'loading' => null, 'src' => null, 'target' => null])
2+
3+
@php
4+
$domId = (function ($id) {
5+
if (is_string($id)) {
6+
return $id;
7+
}
8+
9+
if ($id instanceof Illuminate\Database\Eloquent\Model) {
10+
return dom_id($id);
11+
}
12+
13+
return dom_id(...$id);
14+
})($id);
15+
@endphp
16+
117
<turbo-frame
218
id="{{ $domId }}"
319
@if ($loading) loading="{{ $loading }}" @endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<meta name="turbo-visit-control" content="reload">
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1+
@props(['method' => 'replace', 'scroll' => 'reset'])
2+
3+
@php
4+
throw_unless(in_array($method, ['replace', 'morph']), HotwiredLaravel\TurboLaravel\Exceptions\PageRefreshStrategyException::invalidRefreshMethod($method));
5+
throw_unless(in_array($scroll, ['reset', 'preserve']), HotwiredLaravel\TurboLaravel\Exceptions\PageRefreshStrategyException::invalidRefreshScroll($scroll));
6+
@endphp
7+
18
<meta name="turbo-refresh-method" content="{{ $method }}">
29
<meta name="turbo-refresh-scroll" content="{{ $scroll }}">

0 commit comments

Comments
 (0)