Skip to content

Commit f4fd991

Browse files
committed
feature #1600 CVA: Allow array for base, variants and compound (WebMamba)
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
2 parents 8dd4ebd + 600685c commit f4fd991

File tree

3 files changed

+83
-8
lines changed

3 files changed

+83
-8
lines changed

src/TwigComponent/src/CVA.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ final class CVA
2626
{
2727
/**
2828
* @var string|list<string|null>|null
29-
* @var array<string, array<string, string>>|null the array should have the following format [variantCategory => [variantName => classes]]
29+
* @var array<string, array<string, string|list<string|null>>|null the array should have the following format [variantCategory => [variantName => classes]]
3030
* ex: ['colors' => ['primary' => 'bleu-8000', 'danger' => 'red-800 text-bold'], 'size' => [...]]
31-
* @var array<array<string, string[]>>|null the array should have the following format ['variantsCategory' => ['variantName', 'variantName'], 'class' => 'text-red-500']
31+
* @var array<array<string, string|array<string>>> the array should have the following format ['variantsCategory' => ['variantName', 'variantName'], 'class' => 'text-red-500']
3232
* @var array<string, string>|null
3333
*/
3434
public function __construct(
@@ -57,7 +57,11 @@ public function resolve(array $recipes): string
5757
continue;
5858
}
5959

60-
$classes .= ' '.$this->variants[$recipeName][$recipeValue];
60+
if (\is_string($this->variants[$recipeName][$recipeValue])) {
61+
$classes .= ' '.$this->variants[$recipeName][$recipeValue];
62+
} else {
63+
$classes .= ' '.implode(' ', $this->variants[$recipeName][$recipeValue]);
64+
}
6165
}
6266

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

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

87-
$classes .= ' '.$compound['class'];
91+
if (!\is_string($compound['class']) && !\is_array($compound['class'])) {
92+
throw new \LogicException('The class of a compound recipe should be a string or an array of string');
93+
}
94+
95+
if (\is_string($compound['class'])) {
96+
$classes .= ' '.$compound['class'];
97+
} else {
98+
$classes .= ' '.implode(' ', $compound['class']);
99+
}
88100
}
89101
}
90102
}

src/TwigComponent/src/Twig/ComponentExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public function finishEmbeddedComponentRender(): void
8888

8989
/**
9090
* @param array{
91-
* base: string|string[]|null,
92-
* variants: array<string, array<string, string>>,
93-
* compoundVariants: array<array<string, string>>,
91+
* base: string|list<string|null>|null,
92+
* variants: array<string, array<string, string|array<string>>>,
93+
* compoundVariants: array<array<string, string|array<string>>>,
9494
* defaultVariants: array<string, string>
9595
* } $cva
9696
*

src/TwigComponent/tests/Unit/CVATest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ public static function recipeProvider(): iterable
111111
'text-primary text-sm',
112112
];
113113

114+
yield 'base array' => [
115+
[
116+
'base' => ['font-semibold', 'border', 'rounded'],
117+
'variants' => [
118+
'colors' => [
119+
'primary' => 'text-primary',
120+
'secondary' => 'text-secondary',
121+
],
122+
'sizes' => [
123+
'sm' => 'text-sm',
124+
'md' => 'text-md',
125+
'lg' => 'text-lg',
126+
],
127+
]],
128+
['colors' => 'primary', 'sizes' => 'sm'],
129+
'font-semibold border rounded text-primary text-sm',
130+
];
131+
114132
yield 'no recipes match' => [
115133
[
116134
'base' => 'font-semibold border rounded',
@@ -149,6 +167,25 @@ public static function recipeProvider(): iterable
149167
'font-semibold border rounded text-primary text-sm',
150168
];
151169

170+
yield 'simple variants as array' => [
171+
[
172+
'base' => 'font-semibold border rounded',
173+
'variants' => [
174+
'colors' => [
175+
'primary' => ['text-primary', 'uppercase'],
176+
'secondary' => ['text-secondary', 'uppercase'],
177+
],
178+
'sizes' => [
179+
'sm' => 'text-sm',
180+
'md' => 'text-md',
181+
'lg' => 'text-lg',
182+
],
183+
],
184+
],
185+
['colors' => 'primary', 'sizes' => 'sm'],
186+
'font-semibold border rounded text-primary uppercase text-sm',
187+
];
188+
152189
yield 'simple variants with custom' => [
153190
[
154191
'base' => 'font-semibold border rounded',
@@ -194,6 +231,32 @@ public static function recipeProvider(): iterable
194231
'font-semibold border rounded text-primary text-sm text-red-500',
195232
];
196233

234+
yield 'compound variants as array' => [
235+
[
236+
'base' => 'font-semibold border rounded',
237+
'variants' => [
238+
'colors' => [
239+
'primary' => 'text-primary',
240+
'secondary' => 'text-secondary',
241+
],
242+
'sizes' => [
243+
'sm' => 'text-sm',
244+
'md' => 'text-md',
245+
'lg' => 'text-lg',
246+
],
247+
],
248+
'compounds' => [
249+
[
250+
'colors' => ['primary'],
251+
'sizes' => ['sm'],
252+
'class' => ['text-red-500', 'bold'],
253+
],
254+
],
255+
],
256+
['colors' => 'primary', 'sizes' => 'sm'],
257+
'font-semibold border rounded text-primary text-sm text-red-500 bold',
258+
];
259+
197260
yield 'multiple compound variants' => [
198261
[
199262
'base' => 'font-semibold border rounded',

0 commit comments

Comments
 (0)