Skip to content

Commit

Permalink
feature #1600 CVA: Allow array for base, variants and compound (WebMa…
Browse files Browse the repository at this point in the history
…mba)

This PR was merged into the 2.x branch.

Discussion
----------

CVA: Allow array for base, variants and compound

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| Issues        | Fix #1586
| License       | MIT

Commits
-------

600685c CVA: Allow array for base, variants and compound
  • Loading branch information
kbond committed Mar 11, 2024
2 parents 8dd4ebd + 600685c commit f4fd991
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
22 changes: 17 additions & 5 deletions src/TwigComponent/src/CVA.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ 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]]
* @var array<string, array<string, string|list<string|null>>|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<array<string, string|array<string>>> the array should have the following format ['variantsCategory' => ['variantName', 'variantName'], 'class' => 'text-red-500']
* @var array<string, string>|null
*/
public function __construct(
Expand Down Expand Up @@ -57,7 +57,11 @@ public function resolve(array $recipes): string
continue;
}

$classes .= ' '.$this->variants[$recipeName][$recipeValue];
if (\is_string($this->variants[$recipeName][$recipeValue])) {
$classes .= ' '.$this->variants[$recipeName][$recipeValue];
} else {
$classes .= ' '.implode(' ', $this->variants[$recipeName][$recipeValue]);
}
}

if (null !== $this->compoundVariants) {
Expand All @@ -80,11 +84,19 @@ public function resolve(array $recipes): string
}

if ($isCompound) {
if (!isset($compound['class']) || !\is_string($compound['class'])) {
if (!isset($compound['class'])) {
throw new \LogicException('A compound recipe matched but no classes are registered for this match');
}

$classes .= ' '.$compound['class'];
if (!\is_string($compound['class']) && !\is_array($compound['class'])) {
throw new \LogicException('The class of a compound recipe should be a string or an array of string');
}

if (\is_string($compound['class'])) {
$classes .= ' '.$compound['class'];
} else {
$classes .= ' '.implode(' ', $compound['class']);
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/TwigComponent/src/Twig/ComponentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public function finishEmbeddedComponentRender(): void

/**
* @param array{
* base: string|string[]|null,
* variants: array<string, array<string, string>>,
* compoundVariants: array<array<string, string>>,
* base: string|list<string|null>|null,
* variants: array<string, array<string, string|array<string>>>,
* compoundVariants: array<array<string, string|array<string>>>,
* defaultVariants: array<string, string>
* } $cva
*
Expand Down
63 changes: 63 additions & 0 deletions src/TwigComponent/tests/Unit/CVATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ public static function recipeProvider(): iterable
'text-primary text-sm',
];

yield 'base array' => [
[
'base' => ['font-semibold', 'border', 'rounded'],
'variants' => [
'colors' => [
'primary' => 'text-primary',
'secondary' => 'text-secondary',
],
'sizes' => [
'sm' => 'text-sm',
'md' => 'text-md',
'lg' => 'text-lg',
],
]],
['colors' => 'primary', 'sizes' => 'sm'],
'font-semibold border rounded text-primary text-sm',
];

yield 'no recipes match' => [
[
'base' => 'font-semibold border rounded',
Expand Down Expand Up @@ -149,6 +167,25 @@ public static function recipeProvider(): iterable
'font-semibold border rounded text-primary text-sm',
];

yield 'simple variants as array' => [
[
'base' => 'font-semibold border rounded',
'variants' => [
'colors' => [
'primary' => ['text-primary', 'uppercase'],
'secondary' => ['text-secondary', 'uppercase'],
],
'sizes' => [
'sm' => 'text-sm',
'md' => 'text-md',
'lg' => 'text-lg',
],
],
],
['colors' => 'primary', 'sizes' => 'sm'],
'font-semibold border rounded text-primary uppercase text-sm',
];

yield 'simple variants with custom' => [
[
'base' => 'font-semibold border rounded',
Expand Down Expand Up @@ -194,6 +231,32 @@ public static function recipeProvider(): iterable
'font-semibold border rounded text-primary text-sm text-red-500',
];

yield 'compound variants as array' => [
[
'base' => 'font-semibold border rounded',
'variants' => [
'colors' => [
'primary' => 'text-primary',
'secondary' => 'text-secondary',
],
'sizes' => [
'sm' => 'text-sm',
'md' => 'text-md',
'lg' => 'text-lg',
],
],
'compounds' => [
[
'colors' => ['primary'],
'sizes' => ['sm'],
'class' => ['text-red-500', 'bold'],
],
],
],
['colors' => 'primary', 'sizes' => 'sm'],
'font-semibold border rounded text-primary text-sm text-red-500 bold',
];

yield 'multiple compound variants' => [
[
'base' => 'font-semibold border rounded',
Expand Down

0 comments on commit f4fd991

Please sign in to comment.