Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TwigComponent] Deprecate cva twig function #2144

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Map/src/Twig/MapExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\UX\Map\Twig;

use Symfony\UX\Map\Renderer\Renderers;
use Twig\DeprecatedCallableInfo;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

Expand All @@ -27,9 +28,9 @@ public function getFunctions(): array
return [
new TwigFunction('render_map', [Renderers::class, 'renderMap'], [
'is_safe' => ['html'],
'deprecated' => '2.20',
'deprecating_package' => 'symfony/ux-map',
'alternative' => 'ux_map',
...(class_exists(DeprecatedCallableInfo::class)
? ['deprecation_info' => new DeprecatedCallableInfo('symfony/ux-map', '2.20', 'ux_map')]
: ['deprecated' => '2.20', 'deprecating_package' => 'symfony/ux-map', 'alternative' => 'ux_map']),
]),
new TwigFunction('ux_map', [Renderers::class, 'renderMap'], ['is_safe' => ['html']]),
];
Expand Down
1 change: 1 addition & 0 deletions src/TwigComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.20.0

- Add Anonymous Component support for 3rd-party bundles #2019
- Deprecate `cva` Twig function in favor of [`html_cva` from `twig/html-extra`](https://twig.symfony.com/html_cva) #2144

## 2.17.0

Expand Down
8 changes: 7 additions & 1 deletion src/TwigComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,11 @@ Component with Complex Variants (CVA)

The ``cva`` function was added in TwigComponents 2.16.

.. deprecated:: 2.20

The ``cva`` function was deprecated in TwigComponents 2.20, and will be removed in 3.0.
The function is now provided by the ``twig/html-extra:^3.12`` package under the name `html_cva`_.

`CVA (Class Variant Authority)`_ is a concept from the JavaScript world and used
by the well-known `shadcn/ui`_.
CVA allows you to display a component with different variants (color, size, etc.),
Expand Down Expand Up @@ -1763,6 +1768,7 @@ https://symfony.com/doc/current/contributing/code/bc.html
.. _`Passing Blocks to Live Components`: https://symfony.com/bundles/ux-live-component/current/index.html#passing-blocks
.. _`Stimulus controller`: https://symfony.com/bundles/StimulusBundle/current/index.html
.. _`CVA (Class Variant Authority)`: https://cva.style/docs/getting-started/variants
.. _`html_cva`: https://twig.symfony.com/doc/3.x/functions/html_cva.html
.. _`shadcn/ui`: https://ui.shadcn.com
.. _`tales-from-a-dev/twig-tailwind-extra`: https://github.com/tales-from-a-dev/twig-tailwind-extra
.. _`ignore not defined options`: https://symfony.com/doc/current/components/options_resolver.html#ignore-not-defined-options
.. _`ignore not defined options`: https://symfony.com/doc/current/components/options_resolver.html#ignore-not-defined-options
2 changes: 2 additions & 0 deletions src/TwigComponent/src/CVA.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* @author Mathéo Daninos <[email protected]>
*
* @experimental
*
* @deprecated since TwigComponent 2.20, use CVA from the "twig/html-extra:^3.12.0" package instead.
*/
final class CVA
{
Expand Down
9 changes: 8 additions & 1 deletion src/TwigComponent/src/Twig/ComponentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\UX\TwigComponent\ComponentRenderer;
use Symfony\UX\TwigComponent\CVA;
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
use Twig\DeprecatedCallableInfo;
use Twig\Error\RuntimeError;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
Expand All @@ -42,7 +43,11 @@ public function getFunctions(): array
{
return [
new TwigFunction('component', [$this, 'render'], ['is_safe' => ['all']]),
new TwigFunction('cva', [$this, 'cva']),
new TwigFunction('cva', [$this, 'cva'], [
...(class_exists(DeprecatedCallableInfo::class)
? ['deprecation_info' => new DeprecatedCallableInfo('symfony/ux-twig-component', '2.20', 'html_cva', 'twig/html-extra')]
: ['deprecated' => '2.20', 'deprecating_package' => 'symfony/ux-twig-component', 'alternative' => 'html_cva']),
]),
];
}

Expand Down Expand Up @@ -105,6 +110,8 @@ public function finishEmbeddedComponentRender(): void
*/
public function cva(array $cva): CVA
{
trigger_deprecation('symfony/ux-twig-component', '2.20', 'Twig Function "cva" is deprecated; use "html_cva" from the "twig/html-extra" package (available since version 3.12) instead.');

return new CVA(
$cva['base'] ?? '',
$cva['variants'] ?? [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
<div class="{{ alert.apply({color, size}, attributes.render('class'), 'flex p-4') }}">
{% block content %}
{% endblock %}
</div>
</div>
13 changes: 13 additions & 0 deletions src/TwigComponent/tests/Integration/ComponentExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\UX\TwigComponent\Tests\Integration;

use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\TwigComponent\Tests\Fixtures\User;
use Twig\Environment;
Expand All @@ -21,6 +22,8 @@
*/
final class ComponentExtensionTest extends KernelTestCase
{
use ExpectDeprecationTrait;

public function testCanRenderComponent(): void
{
$output = $this->renderComponent('component_a', [
Expand Down Expand Up @@ -264,8 +267,13 @@ class="block"
];
}

/**
* @group legacy
*/
public function testComponentWithClassMerge(): void
{
$this->expectDeprecation('Since symfony/ux-twig-component 2.20: Twig Function "cva" is deprecated; use "html_cva" from the "twig/html-extra" package (available since version 3.12) instead.');

$output = self::getContainer()->get(Environment::class)->render('class_merge.html.twig');

$this->assertStringContainsString('class="alert alert-red alert-lg font-semibold rounded-md dark:bg-gray-600 flex p-4"', $output);
Expand Down Expand Up @@ -373,8 +381,13 @@ public function testComponentWithEmptyProps(): void
$this->assertStringContainsString('I have an empty props tag', $output);
}

/**
* @group legacy
*/
public function testAnonymousComponentWithPropsOverwriteParentsProps(): void
{
$this->expectDeprecation('Since symfony/ux-twig-component 2.20: Twig Function "cva" is deprecated; use "html_cva" from the "twig/html-extra" package (available since version 3.12) instead.');

$output = self::getContainer()->get(Environment::class)->render('anonymous_component_with_props_overwrite_parents_props.html.twig');

$this->assertStringContainsString('I am an icon', $output);
Expand Down
4 changes: 4 additions & 0 deletions src/TwigComponent/tests/Unit/CVATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
use Symfony\UX\TwigComponent\CVA;

/**
* To remove in TwigComponent 3.0.
*
* @author Mathéo Daninos <[email protected]>
*
* @group legacy
*/
class CVATest extends TestCase
{
Expand Down