File: html_cva.rst

package info (click to toggle)
php-twig 3.20.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,940 kB
  • sloc: php: 23,320; makefile: 110; sh: 43
file content (190 lines) | stat: -rw-r--r-- 5,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
``html_cva``
============

.. versionadded:: 3.12

    The ``html_cva`` function was added in Twig 3.12.


`CVA (Class Variant Authority)`_ is a concept from the JavaScript world and used
by the well-known `shadcn/ui`_ library.
The CVA concept is used to render multiple variations of components, applying
a set of conditions and recipes to dynamically compose CSS class strings (color, size, etc.),
to create highly reusable and customizable templates.

The concept of CVA is powered by a ``html_cva()`` Twig
function where you define ``base`` classes that should always be present and then different
``variants`` and the corresponding classes:

.. code-block:: html+twig

    {# templates/alert.html.twig #}
    {% set alert = html_cva(
        base: 'alert',
        variants: {
            color: {
                blue: 'bg-blue',
                red: 'bg-red',
                green: 'bg-green',
            },
            size: {
                sm: 'text-sm',
                md: 'text-md',
                lg: 'text-lg',
            }
        }
    ) %}

    <div class="{{ alert.apply({color, size}, class) }}">
        ...
    </div>

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

.. code-block:: twig

    {# index.html.twig #}
    {{ include('alert.html.twig', {'color': 'blue', 'size': 'md'}) }}
    // class="alert bg-blue text-md"

    {{ include('alert.html.twig', {'color': 'green', 'size': 'sm'}) }}
    // class="alert bg-green text-sm"

    {{ include('alert.html.twig', {'color': 'red', 'class': 'flex items-center justify-center'}) }}
    // class="alert bg-red flex items-center justify-center"

CVA and Tailwind CSS
--------------------

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()`` filter from `tales-from-a-dev/twig-tailwind-extra`_
with the ``html_cva()`` function:

.. code-block:: terminal

    $ composer require tales-from-a-dev/twig-tailwind-extra

.. code-block:: html+twig

    {% set alert = html_cva(
       // ...
    ) %}

    <div class="{{ alert.apply({color, size}, class)|tailwind_merge }}">
         ...
    </div>

Compound Variants
-----------------

You can define compound variants. A compound variant is a variant that applies
when multiple other variant conditions are met:

.. code-block:: html+twig

    {% set alert = html_cva(
        base: 'alert',
        variants: {
            color: {
                blue: 'bg-blue',
                red: 'bg-red',
                green: 'bg-green',
            },
            size: {
                sm: 'text-sm',
                md: 'text-md',
                lg: 'text-lg',
            }
        },
        compoundVariants: [{
            // if color = red AND size = (md or lg), add the `font-bold` class
            color: ['red'],
            size: ['md', 'lg'],
            class: 'font-bold'
        }]
    ) %}

    <div class="{{ alert.apply({color, size}) }}">
         ...
    </div>

    {# index.html.twig #}

    {{ include('alert.html.twig', {color: 'red', size: 'lg'}) }}
    // class="alert bg-red text-lg font-bold"

    {{ include('alert.html.twig', {color: 'green', size: 'sm'}) }}
    // class="alert bg-green text-sm"

    {{ include('alert.html.twig', {color: 'red', size: 'md'}) }}
    // class="alert bg-green text-md font-bold"

Default Variants
----------------

If no variants match, you can define a default set of classes to apply:

.. code-block:: html+twig

    {% set alert = html_cva(
        base: 'alert',
        variants: {
            color: {
                blue: 'bg-blue',
                red: 'bg-red',
                green: 'bg-green',
            },
            size: {
                sm: 'text-sm',
                md: 'text-md',
                lg: 'text-lg',
            },
            rounded: {
                sm: 'rounded-sm',
                md: 'rounded-md',
                lg: 'rounded-lg',
            }
        },
        defaultVariant: {
            rounded: 'md',
        }
    ) %}

    <div class="{{ alert.apply({color, size}) }}">
         ...
    </div>

    {# index.html.twig #}

    {{ include('alert.html.twig', {color: 'red', size: 'lg'}) }}
    // class="alert bg-red text-lg rounded-md"

.. note::

    The ``html_cva`` function is part of the ``HtmlExtension`` which is not
    installed by default. Install it first:

    .. code-block:: bash

        $ composer require twig/html-extra

    Then, on Symfony projects, install the ``twig/extra-bundle``:

    .. code-block:: bash

            $ composer require twig/extra-bundle

    Otherwise, add the extension explicitly on the Twig environment::

            use Twig\Extra\Html\HtmlExtension;

            $twig = new \Twig\Environment(...);
            $twig->addExtension(new HtmlExtension());

This function works best when used with `TwigComponent`_.

.. _`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
.. _`TwigComponent`: https://symfony.com/bundles/ux-twig-component/current/index.html