Skip to content

Conversation

@WebMamba
Copy link
Contributor

@WebMamba WebMamba commented Mar 9, 2024

Hey! This PR introduces CVA to Twig. All of this has already been merged into SymfonyUX (symfony/ux#1416), but @kbond suggested that this repo can be a better place for this feature.

Here is a description from the PR merged in to SymfonyUX:


This PR introduces a new concept CVA (Class Variance Authority), by adding a1 twig function, to help you manage your class in your component.

Let's take an example an Alert component. In your app, an alert can have a lot of different styles one for success, one for alert, one for warning, and different sizes, with icons or not... You need something that lets you completely change the style of your component without creating a new component, and without creating too much complexity in your template.

Here is the reason came CVA.

Your Alert component can now look like this:

{% props color = 'blue', size = 'md' %}

{% set alert =  html_cva(
      'alert rounded-lg',
     {
        color: {
            blue: 'text-blue-800 bg-blue-50 dark:bg-gray-800 dark:text-blue-400',
            red: 'text-red-800 bg-red-50 dark:bg-gray-800 dark:text-red-400',
            green: 'text-green-800 bg-green-50 dark:bg-gray-800 dark:text-green-400',
            yellow: 'text-yellow-800 bg-yellow-50 dark:bg-gray-800 dark:text-yellow-400',
        },
        size: {
            sm: 'px-4 py-3 text-sm',
            md: 'px-6 py-4 text-base',
            lg: 'px-8 py-5 text-lg',
        }
    },
    [{
           color: ['red'],
           size: ['lg'],
          class: 'font-semibold'
    }],
   {
           rounded: 'md'
    }
}) %}

<div class="{{ cva.apply({color, size}, attribute.render('class'), 'flex p-4') }}">
    ...
</div>

So here you have a cva function that lets you define different variants of your component.

You can now use your component like this:

<twig:Alert color="red" size="md"/>

<twig:Alert color="green" size="sm"/>

<twig:Alert color="yellow" size="lg"/>

<twig:Alert color="red" size="md" class="dark:bg-gray-800"/>

And then you get the following result:

Capture d’écran 2024-01-24 à 00 52 33

If you want to know more about the concept I implement here you can look at:

Tell me what you think about it! Thanks for your time! Cheers 🧡


@WebMamba WebMamba force-pushed the introduce_cva_to_html_extra branch 2 times, most recently from 470fb79 to 7ae03bd Compare March 9, 2024 16:41
Copy link
Contributor

@fabpot fabpot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add some docs for the cva function under doc/.

@WebMamba WebMamba force-pushed the introduce_cva_to_html_extra branch 6 times, most recently from 9850a2d to e07a5a4 Compare August 13, 2024 10:17
@WebMamba
Copy link
Contributor Author

WebMamba commented Aug 13, 2024

Thanks @fabpot for your review! I applied your comments and synced this PR with the code we have now on the UX side. And I added some documentation as well 😁

@WebMamba WebMamba force-pushed the introduce_cva_to_html_extra branch 2 times, most recently from abadfcb to 23ed8e4 Compare August 13, 2024 13:43
@WebMamba
Copy link
Contributor Author

Thanks a lot for your review @stof! I choose to leverage the supports of named argument, so now the cva function take 4 arguments instead of one big array 👍

@stof
Copy link
Member

stof commented Aug 13, 2024

@WebMamba please update the PR description so that the code snippet reflects that update

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

This is powered by a ``cva()`` Twig
function where you define ``base`` classes that should always be present and then different
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they must be present, first argument of the function / CVA should not be nullable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the classes inside the base that should always be present not the base argument

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could avoid using "base" for two distinct things then ?

@smnandre
Copy link
Contributor

I've open an issue in symfony/ux regarding the "transition".. any idea/feedback/advice appreciated :)

@WebMamba
Copy link
Contributor Author

Finally I renamed the function to html_cva, to ease BC break in the TwigComponent package

@WebMamba WebMamba force-pushed the introduce_cva_to_html_extra branch from e05479b to 826a5d4 Compare August 17, 2024 09:47
@WebMamba WebMamba force-pushed the introduce_cva_to_html_extra branch from 30c7667 to 5c84e80 Compare August 17, 2024 21:22
@fabpot fabpot force-pushed the introduce_cva_to_html_extra branch from 41560d5 to e3ac14e Compare August 18, 2024 07:36
@fabpot
Copy link
Contributor

fabpot commented Aug 18, 2024

Thank you @WebMamba.

@fabpot fabpot merged commit 0bfef5b into twigphp:3.x Aug 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants