-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Introduce CVA to html-extra #4006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
470fb79 to
7ae03bd
Compare
fabpot
left a comment
There was a problem hiding this 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/.
9850a2d to
e07a5a4
Compare
|
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 😁 |
abadfcb to
23ed8e4
Compare
|
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 👍 |
|
@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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ?
|
I've open an issue in symfony/ux regarding the "transition".. any idea/feedback/advice appreciated :) |
|
Finally I renamed the function to html_cva, to ease BC break in the TwigComponent package |
e05479b to
826a5d4
Compare
30c7667 to
5c84e80
Compare
41560d5 to
e3ac14e
Compare
|
Thank you @WebMamba. |
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
cvafunction that lets you define different variants of your component.You can now use your component like this:
And then you get the following result:
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 🧡