@@ -1061,14 +1061,16 @@ Exclude specific attributes:
1061
1061
Component with Complex Variants (CVA)
1062
1062
-------------------------------------
1063
1063
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 `_.
1066
1070
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:
1072
1074
1073
1075
.. code-block :: html+twig
1074
1076
@@ -1095,9 +1097,11 @@ In the variants key you define the different variants of your component.
1095
1097
{% block content %}{% endblock %}
1096
1098
</div>
1097
1099
1100
+ Then use the ``color `` and ``size `` variants to select the classes needed:
1098
1101
1099
- {# index. html. twig #}
1102
+ .. code-block :: html+ twig
1100
1103
1104
+ {# index.html.twig #}
1101
1105
<twig:Alert color="red" size="lg">
1102
1106
<div>My content</div>
1103
1107
</twig:Alert>
@@ -1108,18 +1112,18 @@ In the variants key you define the different variants of your component.
1108
1112
</twig:Alert>
1109
1113
// class="alert bg-green text-sm"
1110
1114
1111
- <twig:Alert class="flex items-center justify-center">
1115
+ <twig:Alert color="red" class="flex items-center justify-center">
1112
1116
<div>My content</div>
1113
1117
</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"
1115
1119
1116
1120
CVA and Tailwind CSS
1117
1121
~~~~~~~~~~~~~~~~~~~~
1118
1122
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:
1123
1127
1124
1128
.. code-block :: terminal
1125
1129
@@ -1131,29 +1135,17 @@ https://github.com/tales-from-a-dev/twig-tailwind-extra
1131
1135
{% props color = 'blue', size = 'md' %}
1132
1136
1133
1137
{% 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
+ // ...
1147
1139
}) %}
1148
1140
1149
1141
<div class="{{ alert.apply({color, size}, attributes.render('class')) | tailwind_merge }}">
1150
1142
{% block content %}{% endblock %}
1151
1143
</div>
1152
1144
1153
- Compounds variants
1154
- ~~~~~~~~~~~~~~~~~~
1145
+ Compound Variants
1146
+ ~~~~~~~~~~~~~~~~~
1155
1147
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
1157
1149
when multiple other variant conditions are met.
1158
1150
1159
1151
.. code-block :: html+twig
@@ -1175,7 +1167,8 @@ when multiple other variant conditions are met.
1175
1167
lg: 'text-lg',
1176
1168
}
1177
1169
},
1178
- compound: {
1170
+ compoundVariants: {
1171
+ // if colors=red AND size = (md or lg), add the `font-bold ` class
1179
1172
colors: ['red'],
1180
1173
size: ['md', 'lg'],
1181
1174
class: 'font-bold'
@@ -1203,11 +1196,10 @@ when multiple other variant conditions are met.
1203
1196
</twig:Alert>
1204
1197
// class="alert bg-green text-lg font-bold"
1205
1198
1206
- Default variants
1199
+ Default Variants
1207
1200
~~~~~~~~~~~~~~~~
1208
1201
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:
1211
1203
1212
1204
.. code-block :: html+twig
1213
1205
@@ -1615,3 +1607,6 @@ https://symfony.com/doc/current/contributing/code/bc.html
1615
1607
.. _`Live Nested Components` : https://symfony.com/bundles/ux-live-component/current/index.html#nested-components
1616
1608
.. _`Passing Blocks to Live Components` : https://symfony.com/bundles/ux-live-component/current/index.html#passing-blocks
1617
1609
.. _`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
0 commit comments