Skip to content

Commit 4fe373d

Browse files
Add <twig:turbo-stream-*> components
1 parent d72aa7e commit 4fe373d

13 files changed

+285
-0
lines changed

Diff for: src/Turbo/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.20.0
4+
5+
- Add `<twig:turbo-stream-*>` components
6+
37
## 2.19.0
48

59
- Fix Doctrine proxies are not Broadcasted #3139

Diff for: src/Turbo/config/twig_components.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
13+
14+
use Symfony\UX\Turbo\Twig\Component\TurboStreamAfterComponent;
15+
use Symfony\UX\Turbo\Twig\Component\TurboStreamAppendComponent;
16+
use Symfony\UX\Turbo\Twig\Component\TurboStreamBeforeComponent;
17+
use Symfony\UX\Turbo\Twig\Component\TurboStreamComponent;
18+
use Symfony\UX\Turbo\Twig\Component\TurboStreamPrependComponent;
19+
use Symfony\UX\Turbo\Twig\Component\TurboStreamRefreshComponent;
20+
use Symfony\UX\Turbo\Twig\Component\TurboStreamRemoveComponent;
21+
use Symfony\UX\Turbo\Twig\Component\TurboStreamReplaceComponent;
22+
use Symfony\UX\Turbo\Twig\Component\TurboStreamUpdateComponent;
23+
24+
/*
25+
* @author Kévin Dunglas <kevin@dunglas.fr>
26+
*/
27+
return static function (ContainerConfigurator $container): void {
28+
$container->services()
29+
30+
->set('ux.twig_component.turbo_stream', TurboStreamComponent::class)
31+
->tag('twig.component', [
32+
'key' => 'turbo-stream',
33+
'template' => '@Turbo/components/turbo_stream.html.twig',
34+
])
35+
36+
->set('ux.twig_component.turbo_stream_after', TurboStreamAfterComponent::class)
37+
->tag('twig.component', [
38+
'key' => 'turbo-stream-after',
39+
'template' => '@Turbo/components/turbo_stream.html.twig',
40+
])
41+
42+
->set('ux.twig_component.turbo_stream_append', TurboStreamAppendComponent::class)
43+
->tag('twig.component', [
44+
'key' => 'turbo-stream-append',
45+
'template' => '@Turbo/components/turbo_stream.html.twig',
46+
])
47+
48+
->set('ux.twig_component.turbo_stream_before', TurboStreamBeforeComponent::class)
49+
->tag('twig.component', [
50+
'key' => 'turbo-stream-before',
51+
'template' => '@Turbo/components/turbo_stream.html.twig',
52+
])
53+
54+
->set('ux.twig_component.turbo_stream_prepend', TurboStreamPrependComponent::class)
55+
->tag('twig.component', [
56+
'key' => 'turbo-stream-prepend',
57+
'template' => '@Turbo/components/turbo_stream.html.twig',
58+
])
59+
60+
->set('ux.twig_component.turbo_stream_refresh', TurboStreamRefreshComponent::class)
61+
->tag('twig.component', [
62+
'key' => 'turbo-stream-refresh',
63+
'template' => '@Turbo/components/turbo_stream.html.twig',
64+
])
65+
66+
->set('ux.twig_component.turbo_stream_remove', TurboStreamRemoveComponent::class)
67+
->tag('twig.component', [
68+
'key' => 'turbo-stream-remove',
69+
'template' => '@Turbo/components/turbo_stream.html.twig',
70+
])
71+
72+
->set('ux.twig_component.turbo_stream_replace', TurboStreamReplaceComponent::class)
73+
->tag('twig.component', [
74+
'key' => 'turbo-stream-replace',
75+
'template' => '@Turbo/components/turbo_stream.html.twig',
76+
])
77+
78+
->set('ux.twig_component.turbo_stream_update', TurboStreamUpdateComponent::class)
79+
->tag('twig.component', [
80+
'key' => 'turbo-stream-update',
81+
'template' => '@Turbo/components/turbo_stream.html.twig',
82+
])
83+
;
84+
};

Diff for: src/Turbo/src/DependencyInjection/TurboExtension.php

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2525
use Symfony\UX\Turbo\Broadcaster\BroadcasterInterface;
2626
use Symfony\UX\Turbo\Twig\TurboStreamListenRendererInterface;
27+
use Symfony\UX\TwigComponent\TwigComponentBundle;
2728

2829
/**
2930
* @author Kévin Dunglas <kevin@dunglas.fr>
@@ -41,6 +42,10 @@ public function load(array $configs, ContainerBuilder $container): void
4142

4243
$this->registerTwig($config, $container);
4344
$this->registerBroadcast($config, $container, $loader);
45+
46+
if (class_exists(TwigComponentBundle::class)) {
47+
$loader->load('twig_components.php');
48+
}
4449
}
4550

4651
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Turbo\Twig\Component;
13+
14+
/**
15+
* @see https://turbo.hotwired.dev/reference/streams#after
16+
*/
17+
final class TurboStreamAfterComponent extends TurboStreamComponent
18+
{
19+
public string $action = 'after';
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Turbo\Twig\Component;
13+
14+
/**
15+
* @see https://turbo.hotwired.dev/reference/streams#append
16+
*/
17+
final class TurboStreamAppendComponent extends TurboStreamComponent
18+
{
19+
public string $action = 'append';
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Turbo\Twig\Component;
13+
14+
/**
15+
* @see https://turbo.hotwired.dev/reference/streams#before
16+
*/
17+
final class TurboStreamBeforeComponent extends TurboStreamComponent
18+
{
19+
public string $action = 'before';
20+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Turbo\Twig\Component;
13+
14+
/**
15+
* @see https://turbo.hotwired.dev/reference/streams
16+
*/
17+
class TurboStreamComponent
18+
{
19+
public string $action;
20+
public ?string $target; // A CSS selector - not for "refresh" action
21+
public bool $morph = false; // Only for "replace" and "update" actions
22+
public ?string $requestId = null; // Only for "refresh" action
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Turbo\Twig\Component;
13+
14+
/**
15+
* @see https://turbo.hotwired.dev/reference/streams#prepend
16+
*/
17+
final class TurboStreamPrependComponent extends TurboStreamComponent
18+
{
19+
public string $action = 'prepend';
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Turbo\Twig\Component;
13+
14+
/**
15+
* @see https://turbo.hotwired.dev/reference/streams#refresh
16+
*/
17+
final class TurboStreamRefreshComponent extends TurboStreamComponent
18+
{
19+
public string $action = 'refresh';
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Turbo\Twig\Component;
13+
14+
/**
15+
* @see https://turbo.hotwired.dev/reference/streams#remove
16+
*/
17+
final class TurboStreamRemoveComponent extends TurboStreamComponent
18+
{
19+
public string $action = 'remove';
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Turbo\Twig\Component;
13+
14+
/**
15+
* @see https://turbo.hotwired.dev/reference/streams#replace
16+
*/
17+
final class TurboStreamReplaceComponent extends TurboStreamComponent
18+
{
19+
public string $action = 'append';
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Turbo\Twig\Component;
13+
14+
/**
15+
* @see https://turbo.hotwired.dev/reference/streams#update
16+
*/
17+
final class TurboStreamUpdateComponent extends TurboStreamComponent
18+
{
19+
public string $action = 'update';
20+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% if 'refresh' == this.action %}
2+
<turbo-stream action="{{ this.action }}"{% if this.requestId is not null %} request-id="{{ this.requestId }}"{% endif %} {{ attributes }}></turbo-stream>
3+
{% else %}
4+
<turbo-stream action="{{ this.action }}" targets="{{ this.target }}"{% if this.morph %} method="morph"{% endif %} {{ attributes }}>
5+
{% if 'remove' != this.action -%}
6+
<template>{% block content %}{% endblock %}</template>
7+
{%- endif %}
8+
</turbo-stream>
9+
{% endif %}

0 commit comments

Comments
 (0)