Skip to content

Commit 2ddc47b

Browse files
committed
minor #1564 Update CVA docs and fixes (WebMamba, kbond, weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- Update CVA docs and fixes | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Issues | | License | MIT `@weaverryan` asked for a few changes here: #1416. So here are the fixes. 😁 Commits ------- 8567c68 [Twig] Fixing docs syntax in one more spot 75fa44f [Twig] Fixing rst docs syntax 08a94f1 [Twig] Minor docs tweaks 6b3a8ea Update src/TwigComponent/doc/index.rst 6f49dc1 Compound variant 057d68b Update CVA docs and fixes
2 parents de4d29f + 8567c68 commit 2ddc47b

File tree

3 files changed

+53
-36
lines changed

3 files changed

+53
-36
lines changed

src/TwigComponent/CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
- Introduce CVA to style TwigComponent #1416
66
- Drop Twig 2 support #1436
77
- Fix full context is stored in profiler #1552
8+
- Add CVA (Class variant authority) integration #1416
89

910
## 2.15.0
1011

1112
- Add the ability to render specific attributes from the `attributes` variable #1442
1213
- Restrict Twig 3.9 for now #1486
1314
- Build reproducible TemplateMap to fix possible post-deploy breakage #1497
14-
- Add CVA (Class variant authority) integration #1416
1515

1616
## 2.14.0
1717

1818
- Make `ComponentAttributes` traversable/countable
1919
- Fixed lexing some `{# twig comments #}` with HTML Twig syntax
2020
- Fix various usages of deprecated Twig code
21-
- Add attribute rendering system
2221

2322
## 2.13.0
2423

src/TwigComponent/doc/index.rst

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,14 +1061,16 @@ Exclude specific attributes:
10611061
Component with Complex Variants (CVA)
10621062
-------------------------------------
10631063

1064-
CVA (Class Variant Authority) is a concept from the JS world (https://cva.style/docs/getting-started/variants).
1065-
It's a concept used by the famous shadcn/ui library (https://ui.shadcn.com).
1064+
.. versionadded:: 2.16
1065+
1066+
The ``cva`` function was added in TwigComponents 2.16.
1067+
1068+
`CVA (Class Variant Authority)`_ is a concept from the JavaScript world and used
1069+
by the well-known `shadcn/ui`_.
10661070
CVA allows you to display a component with different variants (color, size, etc.),
1067-
to create highly reusable and customizable components.
1068-
You can use the cva function to define variants for your component.
1069-
The cva function take as argument an array key-value pairs.
1070-
The base key allow you define a set of classes commune to all variants.
1071-
In the variants key you define the different variants of your component.
1071+
to create highly reusable and customizable components. This is powered by a ``cva()`` Twig
1072+
function where you define ``base`` classes that should always be present and then different
1073+
``variants`` and the corresponding classes:
10721074

10731075
.. code-block:: html+twig
10741076

@@ -1095,9 +1097,11 @@ In the variants key you define the different variants of your component.
10951097
{% block content %}{% endblock %}
10961098
</div>
10971099

1100+
Then use the ``color`` and ``size`` variants to select the classes needed:
10981101

1099-
{# index.html.twig #}
1102+
.. code-block:: html+twig
11001103

1104+
{# index.html.twig #}
11011105
<twig:Alert color="red" size="lg">
11021106
<div>My content</div>
11031107
</twig:Alert>
@@ -1108,18 +1112,18 @@ In the variants key you define the different variants of your component.
11081112
</twig:Alert>
11091113
// class="alert bg-green text-sm"
11101114

1111-
<twig:Alert class="flex items-center justify-center">
1115+
<twig:Alert color="red" class="flex items-center justify-center">
11121116
<div>My content</div>
11131117
</twig:Alert>
1114-
// class="alert bg-blue text-md flex items-center justify-center"
1118+
// class="alert bg-red text-md flex items-center justify-center"
11151119

11161120
CVA and Tailwind CSS
11171121
~~~~~~~~~~~~~~~~~~~~
11181122

1119-
CVA work perfectly with tailwindcss. The only drawback is you can have class conflicts,
1120-
to have a better control you can use this following bundle (
1121-
https://github.com/tales-from-a-dev/twig-tailwind-extra
1122-
) in addition to the cva function:
1123+
CVA work perfectly with Tailwind CSS. The only drawback is that you can have class conflicts.
1124+
To "merge" conflicting classes together and keep only the ones you need, use the
1125+
``tailwind_merge()` method from `tales-from-a-dev/twig-tailwind-extra`_
1126+
with the ``cva()`` function:
11231127

11241128
.. code-block:: terminal
11251129
@@ -1131,29 +1135,17 @@ https://github.com/tales-from-a-dev/twig-tailwind-extra
11311135
{% props color = 'blue', size = 'md' %}
11321136

11331137
{% set alert = cva({
1134-
base: 'alert ',
1135-
variants: {
1136-
color: {
1137-
blue: 'bg-blue',
1138-
red: 'bg-red',
1139-
green: 'bg-green',
1140-
},
1141-
size: {
1142-
sm: 'text-sm',
1143-
md: 'text-md',
1144-
lg: 'text-lg',
1145-
}
1146-
}
1138+
// ...
11471139
}) %}
11481140

11491141
<div class="{{ alert.apply({color, size}, attributes.render('class')) | tailwind_merge }}">
11501142
{% block content %}{% endblock %}
11511143
</div>
11521144

1153-
Compounds variants
1154-
~~~~~~~~~~~~~~~~~~
1145+
Compound Variants
1146+
~~~~~~~~~~~~~~~~~
11551147

1156-
You can define compound variants. A compound variant is a variants that apply
1148+
You can define compound variants. A compound variant is a variant that applies
11571149
when multiple other variant conditions are met.
11581150

11591151
.. code-block:: html+twig
@@ -1175,7 +1167,8 @@ when multiple other variant conditions are met.
11751167
lg: 'text-lg',
11761168
}
11771169
},
1178-
compound: {
1170+
compoundVariants: {
1171+
// if colors=red AND size = (md or lg), add the `font-bold` class
11791172
colors: ['red'],
11801173
size: ['md', 'lg'],
11811174
class: 'font-bold'
@@ -1203,11 +1196,10 @@ when multiple other variant conditions are met.
12031196
</twig:Alert>
12041197
// class="alert bg-green text-lg font-bold"
12051198

1206-
Default variants
1199+
Default Variants
12071200
~~~~~~~~~~~~~~~~
12081201

1209-
You can define defaults variants, so if no variants are matching you
1210-
can still defined a default set of class to apply.
1202+
If no variants match, you can define a default set of classes to apply:
12111203

12121204
.. code-block:: html+twig
12131205

@@ -1615,3 +1607,6 @@ https://symfony.com/doc/current/contributing/code/bc.html
16151607
.. _`Live Nested Components`: https://symfony.com/bundles/ux-live-component/current/index.html#nested-components
16161608
.. _`Passing Blocks to Live Components`: https://symfony.com/bundles/ux-live-component/current/index.html#passing-blocks
16171609
.. _`Stimulus controller`: https://symfony.com/bundles/StimulusBundle/current/index.html
1610+
.. _`CVA (Class Variant Authority)`: https://cva.style/docs/getting-started/variants
1611+
.. _`shadcn/ui`: https://ui.shadcn.com
1612+
.. _`tales-from-a-dev/twig-tailwind-extra`: https://github.com/tales-from-a-dev/twig-tailwind-extra

src/TwigComponent/tests/Unit/CVATest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ public function testRecipes(array $recipe, array $recipes, string $expected): vo
2929
$this->assertEquals($expected, $recipeClass->resolve($recipes));
3030
}
3131

32+
public function testApply(): void
33+
{
34+
$recipe = new CVA('font-semibold border rounded', [
35+
'colors' => [
36+
'primary' => 'text-primary',
37+
'secondary' => 'text-secondary',
38+
],
39+
'sizes' => [
40+
'sm' => 'text-sm',
41+
'md' => 'text-md',
42+
'lg' => 'text-lg',
43+
],
44+
], [
45+
[
46+
'colors' => ['primary'],
47+
'sizes' => ['sm'],
48+
'class' => 'text-red-500',
49+
],
50+
]);
51+
52+
$this->assertEquals('font-semibold border rounded text-primary text-sm text-red-500', $recipe->apply(['colors' => 'primary', 'sizes' => 'sm']));
53+
}
54+
3255
public static function recipeProvider(): iterable
3356
{
3457
yield 'base null' => [

0 commit comments

Comments
 (0)