-
-
Notifications
You must be signed in to change notification settings - Fork 364
Introduce CVA to style TwigComponent #1416
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1058,6 +1058,198 @@ Exclude specific attributes: | |||||||||
My Component! | ||||||||||
</div> | ||||||||||
|
||||||||||
Component with Complex Variants (CVA) | ||||||||||
------------------------------------- | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need a |
||||||||||
|
||||||||||
CVA (Class Variant Authority) is a concept from the JS world (https://cva.style/docs/getting-started/variants). | ||||||||||
WebMamba marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
It's a concept used by the famous shadcn/ui library (https://ui.shadcn.com). | ||||||||||
CVA allows you to display a component with different variants (color, size, etc.), | ||||||||||
to create highly reusable and customizable components. | ||||||||||
You can use the cva function to define variants for your component. | ||||||||||
WebMamba marked this conversation as resolved.
Show resolved
Hide resolved
WebMamba marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
The cva function take as argument an array key-value pairs. | ||||||||||
The base key allow you define a set of classes commune to all variants. | ||||||||||
In the variants key you define the different variants of your component. | ||||||||||
|
||||||||||
.. code-block:: html+twig | ||||||||||
|
||||||||||
{# templates/components/Alert.html.twig #} | ||||||||||
{% props color = 'blue', size = 'md' %} | ||||||||||
|
||||||||||
{% set alert = 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}, attributes.render('class')) }}"> | ||||||||||
{% block content %}{% endblock %} | ||||||||||
</div> | ||||||||||
|
||||||||||
WebMamba marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
{# index.html.twig #} | ||||||||||
|
||||||||||
<twig:Alert color="red" size="lg"> | ||||||||||
<div>My content</div> | ||||||||||
</twig:Alert> | ||||||||||
// class="alert bg-red text-lg" | ||||||||||
|
||||||||||
<twig:Alert color="green" size="sm"> | ||||||||||
<div>My content</div> | ||||||||||
</twig:Alert> | ||||||||||
// class="alert bg-green text-sm" | ||||||||||
|
||||||||||
<twig:Alert class="flex items-center justify-center"> | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Show a |
||||||||||
<div>My content</div> | ||||||||||
</twig:Alert> | ||||||||||
// class="alert bg-blue text-md flex items-center justify-center" | ||||||||||
|
||||||||||
CVA and Tailwind CSS | ||||||||||
~~~~~~~~~~~~~~~~~~~~ | ||||||||||
|
||||||||||
CVA work perfectly with tailwindcss. The only drawback is you can have class conflicts, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tailwind CSS There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still need to fixing the casing of Tailwind in this sentence :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
to have a better control you can use this following bundle ( | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
https://github.com/tales-from-a-dev/twig-tailwind-extra | ||||||||||
) in addition to the cva function: | ||||||||||
|
||||||||||
.. code-block:: terminal | ||||||||||
|
||||||||||
$ composer require tales-from-a-dev/twig-tailwind-extra | ||||||||||
|
||||||||||
.. code-block:: html+twig | ||||||||||
|
||||||||||
{# templates/components/Alert.html.twig #} | ||||||||||
{% props color = 'blue', size = 'md' %} | ||||||||||
|
||||||||||
{% set alert = cva({ | ||||||||||
base: 'alert ', | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can just put |
||||||||||
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}, attributes.render('class')) | tailwind_merge }}"> | ||||||||||
{% block content %}{% endblock %} | ||||||||||
</div> | ||||||||||
|
||||||||||
Compounds variants | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. * |
||||||||||
~~~~~~~~~~~~~~~~~~ | ||||||||||
|
||||||||||
You can define compound variants. A compound variant is a variants that apply | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
when multiple other variant conditions are met. | ||||||||||
|
||||||||||
.. code-block:: html+twig | ||||||||||
|
||||||||||
{# templates/components/Alert.html.twig #} | ||||||||||
{% props color = 'blue', size = 'md' %} | ||||||||||
|
||||||||||
{% set alert = cva({ | ||||||||||
base: 'alert ', | ||||||||||
variants: { | ||||||||||
color: { | ||||||||||
blue: 'bg-blue', | ||||||||||
red: 'bg-red', | ||||||||||
green: 'bg-green', | ||||||||||
}, | ||||||||||
size: { | ||||||||||
sm: 'text-sm', | ||||||||||
md: 'text-md', | ||||||||||
lg: 'text-lg', | ||||||||||
} | ||||||||||
}, | ||||||||||
compound: { | ||||||||||
colors: ['red'], | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a comment above this would help describe it - e.g.
|
||||||||||
size: ['md', 'lg'], | ||||||||||
class: 'font-bold' | ||||||||||
} | ||||||||||
}) %} | ||||||||||
|
||||||||||
<div class="{{ alert.apply({color, size}) }}"> | ||||||||||
{% block content %}{% endblock %} | ||||||||||
</div> | ||||||||||
|
||||||||||
{# index.html.twig #} | ||||||||||
|
||||||||||
<twig:Alert color="red" size="lg"> | ||||||||||
<div>My content</div> | ||||||||||
</twig:Alert> | ||||||||||
// class="alert bg-red text-lg font-bold" | ||||||||||
|
||||||||||
<twig:Alert color="green" size="sm"> | ||||||||||
<div>My content</div> | ||||||||||
</twig:Alert> | ||||||||||
// class="alert bg-green text-sm" | ||||||||||
|
||||||||||
<twig:Alert color="red" size="md"> | ||||||||||
<div>My content</div> | ||||||||||
</twig:Alert> | ||||||||||
// class="alert bg-green text-lg font-bold" | ||||||||||
|
||||||||||
Default variants | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
~~~~~~~~~~~~~~~~ | ||||||||||
|
||||||||||
You can define defaults variants, so if no variants are matching you | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
can still defined a default set of class to apply. | ||||||||||
|
||||||||||
.. code-block:: html+twig | ||||||||||
|
||||||||||
{# templates/components/Alert.html.twig #} | ||||||||||
{% props color = 'blue', size = 'md' %} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need And if so, then does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CVA can be use outside of a component this is why we may need it. What do you think ? |
||||||||||
|
||||||||||
{% set alert = 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', | ||||||||||
} | ||||||||||
}, | ||||||||||
defaultsVariants: { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
rounded: 'rounded-md', | ||||||||||
} | ||||||||||
}) %} | ||||||||||
|
||||||||||
<div class="{{ alert.apply({color, size}) }}"> | ||||||||||
{% block content %}{% endblock %} | ||||||||||
</div> | ||||||||||
|
||||||||||
{# index.html.twig #} | ||||||||||
|
||||||||||
<twig:Alert color="red" size="lg"> | ||||||||||
<div>My content</div> | ||||||||||
</twig:Alert> | ||||||||||
// class="alert bg-red text-lg font-bold rounded-md" | ||||||||||
|
||||||||||
|
||||||||||
Test Helpers | ||||||||||
------------ | ||||||||||
|
||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent; | ||
|
||
/** | ||
WebMamba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @author Mathéo Daninos <matheo.daninos@gmail.com> | ||
* | ||
* CVA (class variant authority), is a concept from the js world. | ||
* https://cva.style/docs | ||
* The UI library shadcn is build on top of this principle | ||
* https://ui.shadcn.com | ||
* The concept behind CVA is to let you build component with a lot of different variations called recipes. | ||
* | ||
* @experimental | ||
*/ | ||
final class CVA | ||
{ | ||
/** | ||
* @var string|list<string|null>|null | ||
* @var array<string, array<string, string>>|null the array should have the following format [variantCategory => [variantName => classes]] | ||
* ex: ['colors' => ['primary' => 'bleu-8000', 'danger' => 'red-800 text-bold'], 'size' => [...]] | ||
* @var array<array<string, string[]>>|null the array should have the following format ['variantsCategory' => ['variantName', 'variantName'], 'class' => 'text-red-500'] | ||
* @var array<string, string>|null | ||
*/ | ||
public function __construct( | ||
private string|array|null $base = null, | ||
private ?array $variants = null, | ||
private ?array $compoundVariants = null, | ||
private ?array $defaultVariants = null, | ||
) { | ||
} | ||
|
||
public function apply(array $recipes, string ...$classes): string | ||
{ | ||
return trim($this->resolve($recipes).' '.implode(' ', $classes)); | ||
} | ||
|
||
public function resolve(array $recipes): string | ||
{ | ||
if (\is_array($this->base)) { | ||
$classes = implode(' ', $this->base); | ||
} else { | ||
$classes = $this->base ?? ''; | ||
} | ||
|
||
foreach ($recipes as $recipeName => $recipeValue) { | ||
if (!isset($this->variants[$recipeName][$recipeValue])) { | ||
continue; | ||
} | ||
|
||
$classes .= ' '.$this->variants[$recipeName][$recipeValue]; | ||
} | ||
|
||
if (null !== $this->compoundVariants) { | ||
foreach ($this->compoundVariants as $compound) { | ||
$isCompound = true; | ||
foreach ($compound as $compoundName => $compoundValues) { | ||
if ('class' === $compoundName) { | ||
continue; | ||
} | ||
|
||
if (!isset($recipes[$compoundName])) { | ||
$isCompound = false; | ||
break; | ||
} | ||
|
||
if (!\in_array($recipes[$compoundName], $compoundValues)) { | ||
$isCompound = false; | ||
break; | ||
} | ||
} | ||
|
||
if ($isCompound) { | ||
if (!isset($compound['class']) || !\is_string($compound['class'])) { | ||
throw new \LogicException('A compound recipe matched but no classes are registered for this match'); | ||
} | ||
|
||
$classes .= ' '.$compound['class']; | ||
WebMamba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
if (null !== $this->defaultVariants) { | ||
foreach ($this->defaultVariants as $defaultVariantName => $defaultVariantValue) { | ||
if (!isset($recipes[$defaultVariantName])) { | ||
$classes .= ' '.$this->variants[$defaultVariantName][$defaultVariantValue]; | ||
} | ||
} | ||
} | ||
|
||
return trim($classes); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<twig:Alert color='red' size='lg' class='dark:bg-gray-600'/> |
Uh oh!
There was an error while loading. Please reload this page.