+
My content
- // class="alert bg-blue text-md flex items-center justify-center"
+ // class="alert bg-red text-md flex items-center justify-center"
CVA and Tailwind CSS
~~~~~~~~~~~~~~~~~~~~
-CVA work perfectly with tailwindcss. The only drawback is you can have class conflicts,
-to have a better control you can use this following bundle (
-https://github.com/tales-from-a-dev/twig-tailwind-extra
-) in addition to the cva function:
+CVA work perfectly with Tailwind CSS. The only drawback is that you can have class conflicts.
+To "merge" conflicting classes together and keep only the ones you need, use the
+``tailwind_merge()` method from `tales-from-a-dev/twig-tailwind-extra`_
+with the ``cva()`` function:
.. code-block:: terminal
@@ -1131,29 +1135,17 @@ https://github.com/tales-from-a-dev/twig-tailwind-extra
{% props color = 'blue', size = 'md' %}
{% set alert = cva({
- base: 'alert ',
- variants: {
- color: {
- blue: 'bg-blue',
- red: 'bg-red',
- green: 'bg-green',
- },
- size: {
- sm: 'text-sm',
- md: 'text-md',
- lg: 'text-lg',
- }
- }
+ // ...
}) %}
{% block content %}{% endblock %}
-Compounds variants
-~~~~~~~~~~~~~~~~~~
+Compound Variants
+~~~~~~~~~~~~~~~~~
-You can define compound variants. A compound variant is a variants that apply
+You can define compound variants. A compound variant is a variant that applies
when multiple other variant conditions are met.
.. code-block:: html+twig
@@ -1175,7 +1167,8 @@ when multiple other variant conditions are met.
lg: 'text-lg',
}
},
- compound: {
+ compoundVariants: {
+ // if colors=red AND size = (md or lg), add the `font-bold` class
colors: ['red'],
size: ['md', 'lg'],
class: 'font-bold'
@@ -1203,11 +1196,10 @@ when multiple other variant conditions are met.
// class="alert bg-green text-lg font-bold"
-Default variants
+Default Variants
~~~~~~~~~~~~~~~~
-You can define defaults variants, so if no variants are matching you
-can still defined a default set of class to apply.
+If no variants match, you can define a default set of classes to apply:
.. code-block:: html+twig
@@ -1615,3 +1607,6 @@ https://symfony.com/doc/current/contributing/code/bc.html
.. _`Live Nested Components`: https://symfony.com/bundles/ux-live-component/current/index.html#nested-components
.. _`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
+.. _`shadcn/ui`: https://ui.shadcn.com
+.. _`tales-from-a-dev/twig-tailwind-extra`: https://github.com/tales-from-a-dev/twig-tailwind-extra
diff --git a/src/TwigComponent/tests/Unit/CVATest.php b/src/TwigComponent/tests/Unit/CVATest.php
index 49d105ccee7..5a5bf42aa41 100644
--- a/src/TwigComponent/tests/Unit/CVATest.php
+++ b/src/TwigComponent/tests/Unit/CVATest.php
@@ -29,6 +29,29 @@ public function testRecipes(array $recipe, array $recipes, string $expected): vo
$this->assertEquals($expected, $recipeClass->resolve($recipes));
}
+ public function testApply(): void
+ {
+ $recipe = new CVA('font-semibold border rounded', [
+ 'colors' => [
+ 'primary' => 'text-primary',
+ 'secondary' => 'text-secondary',
+ ],
+ 'sizes' => [
+ 'sm' => 'text-sm',
+ 'md' => 'text-md',
+ 'lg' => 'text-lg',
+ ],
+ ], [
+ [
+ 'colors' => ['primary'],
+ 'sizes' => ['sm'],
+ 'class' => 'text-red-500',
+ ],
+ ]);
+
+ $this->assertEquals('font-semibold border rounded text-primary text-sm text-red-500', $recipe->apply(['colors' => 'primary', 'sizes' => 'sm']));
+ }
+
public static function recipeProvider(): iterable
{
yield 'base null' => [