Skip to content

CVA: Allow array for base, variants and compound #1600

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

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -88,6 +88,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 @@ -126,6 +144,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 @@ -171,6 +208,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