|
12 | 12 | namespace Symfony\UX\TwigComponent;
|
13 | 13 |
|
14 | 14 | /**
|
15 |
| - * @author Mathéo Daninos <[email protected]> |
| 15 | + * Class Variant Authority (CVA) resolver. |
| 16 | + * |
| 17 | + * The CVA concept is used to render multiple variations of components, applying |
| 18 | + * a set of conditions and recipes to dynamically compose CSS class strings. |
16 | 19 | *
|
17 |
| - * CVA (class variant authority), is a concept from the js world. |
18 |
| - * https://cva.style/docs |
19 |
| - * The UI library shadcn is build on top of this principle |
20 |
| - * https://ui.shadcn.com |
21 |
| - * The concept behind CVA is to let you build component with a lot of different variations called recipes. |
| 20 | + * @see https://cva.style/docs |
| 21 | + * |
| 22 | + * @doc https://symfony.com/bundles/ux-twig-component/current/index.html |
| 23 | + * |
| 24 | + * @author Mathéo Daninos <[email protected]> |
22 | 25 | *
|
23 | 26 | * @experimental
|
24 | 27 | */
|
25 | 28 | final class CVA
|
26 | 29 | {
|
27 | 30 | /**
|
28 |
| - * @var string|list<string|null>|null |
29 |
| - * @var array<string, array<string, string|list<string|null>>|null the array should have the following format [variantCategory => [variantName => classes]] |
30 |
| - * ex: ['colors' => ['primary' => 'bleu-8000', 'danger' => 'red-800 text-bold'], 'size' => [...]] |
31 |
| - * @var array<array<string, string|array<string>>> the array should have the following format ['variantsCategory' => ['variantName', 'variantName'], 'class' => 'text-red-500'] |
32 |
| - * @var array<string, string>|null |
| 31 | + * @var list<string> |
| 32 | + */ |
| 33 | + private readonly array $base; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param string|list<string> $base The base classes to apply to the component |
33 | 37 | */
|
34 | 38 | public function __construct(
|
35 |
| - private string|array|null $base = null, |
36 |
| - private ?array $variants = null, |
37 |
| - private ?array $compoundVariants = null, |
38 |
| - private ?array $defaultVariants = null, |
| 39 | + string|array $base = [], |
| 40 | + /** |
| 41 | + * The variants to apply based on recipes. |
| 42 | + * |
| 43 | + * Format: [variantCategory => [variantName => classes]] |
| 44 | + * |
| 45 | + * Example: |
| 46 | + * 'colors' => [ |
| 47 | + * 'primary' => 'bleu-8000', |
| 48 | + * 'danger' => 'red-800 text-bold', |
| 49 | + * ], |
| 50 | + * 'size' => [...], |
| 51 | + * |
| 52 | + * @var array<string, array<string, string|list<string>>> |
| 53 | + */ |
| 54 | + private readonly array $variants = [], |
| 55 | + |
| 56 | + /** |
| 57 | + * The compound variants to apply based on recipes. |
| 58 | + * |
| 59 | + * Format: [variantsCategory => ['variantName', 'variantName'], class: classes] |
| 60 | + * |
| 61 | + * Example: |
| 62 | + * [ |
| 63 | + * 'colors' => ['primary'], |
| 64 | + * 'size' => ['small'], |
| 65 | + * 'class' => 'text-red-500', |
| 66 | + * ], |
| 67 | + * [ |
| 68 | + * 'size' => ['large'], |
| 69 | + * 'class' => 'font-weight-500', |
| 70 | + * ] |
| 71 | + * |
| 72 | + * @var array<array<string, string|array<string>>> |
| 73 | + */ |
| 74 | + private readonly array $compoundVariants = [], |
| 75 | + |
| 76 | + /** |
| 77 | + * The default variants to apply if specific recipes aren't provided. |
| 78 | + * |
| 79 | + * Format: [variantCategory => variantName] |
| 80 | + * |
| 81 | + * Example: |
| 82 | + * 'colors' => 'primary', |
| 83 | + * |
| 84 | + * @var array<string, string> |
| 85 | + */ |
| 86 | + private readonly array $defaultVariants = [], |
39 | 87 | ) {
|
| 88 | + if (\is_string($base)) { |
| 89 | + $base = preg_split('#\s+#', $base) ?: []; |
| 90 | + } |
| 91 | + $this->base = $base; |
40 | 92 | }
|
41 | 93 |
|
42 |
| - public function apply(array $recipes, ?string ...$classes): string |
| 94 | + public function resolve(array $recipes): string |
43 | 95 | {
|
44 |
| - return trim($this->resolve($recipes).' '.implode(' ', array_filter($classes))); |
| 96 | + trigger_deprecation('symfony/ux-twig-component', '2.17', 'The method "%s()" is deprecated and will be remove in 3.0. Use "apply()" instead.', __METHOD__); |
| 97 | + |
| 98 | + return $this->apply($recipes); |
45 | 99 | }
|
46 | 100 |
|
47 |
| - public function resolve(array $recipes): string |
| 101 | + public function apply(array $recipes, string ...$additionalClasses): string |
48 | 102 | {
|
49 |
| - if (\is_array($this->base)) { |
50 |
| - $classes = implode(' ', $this->base); |
51 |
| - } else { |
52 |
| - $classes = $this->base ?? ''; |
53 |
| - } |
| 103 | + $classes = [...$this->base, ...$additionalClasses]; |
54 | 104 |
|
55 | 105 | foreach ($recipes as $recipeName => $recipeValue) {
|
56 |
| - if (!isset($this->variants[$recipeName][$recipeValue])) { |
57 |
| - continue; |
| 106 | + if (isset($this->variants[$recipeName][$recipeValue])) { |
| 107 | + $classes = [...$classes, ...(array) $this->variants[$recipeName][$recipeValue]]; |
58 | 108 | }
|
59 |
| - |
60 |
| - if (\is_string($this->variants[$recipeName][$recipeValue])) { |
61 |
| - $classes .= ' '.$this->variants[$recipeName][$recipeValue]; |
62 |
| - } else { |
63 |
| - $classes .= ' '.implode(' ', $this->variants[$recipeName][$recipeValue]); |
| 109 | + } |
| 110 | + foreach ($this->compoundVariants as $compound) { |
| 111 | + if ($compoundClasses = $this->resolveCompoundVariant($compound, $recipes)) { |
| 112 | + $classes = [...$classes, ...$compoundClasses]; |
64 | 113 | }
|
65 | 114 | }
|
66 |
| - |
67 |
| - if (null !== $this->compoundVariants) { |
68 |
| - foreach ($this->compoundVariants as $compound) { |
69 |
| - $isCompound = true; |
70 |
| - foreach ($compound as $compoundName => $compoundValues) { |
71 |
| - if ('class' === $compoundName) { |
72 |
| - continue; |
73 |
| - } |
74 |
| - |
75 |
| - if (!isset($recipes[$compoundName])) { |
76 |
| - $isCompound = false; |
77 |
| - break; |
78 |
| - } |
79 |
| - |
80 |
| - if (!\is_array($compoundValues)) { |
81 |
| - $compoundValues = [$compoundValues]; |
82 |
| - } |
83 |
| - |
84 |
| - if (!\in_array($recipes[$compoundName], $compoundValues)) { |
85 |
| - $isCompound = false; |
86 |
| - break; |
87 |
| - } |
88 |
| - } |
89 |
| - |
90 |
| - if ($isCompound) { |
91 |
| - if (!isset($compound['class'])) { |
92 |
| - throw new \LogicException('A compound recipe matched but no classes are registered for this match'); |
93 |
| - } |
94 |
| - |
95 |
| - if (!\is_string($compound['class']) && !\is_array($compound['class'])) { |
96 |
| - throw new \LogicException('The class of a compound recipe should be a string or an array of string'); |
97 |
| - } |
98 |
| - |
99 |
| - if (\is_string($compound['class'])) { |
100 |
| - $classes .= ' '.$compound['class']; |
101 |
| - } else { |
102 |
| - $classes .= ' '.implode(' ', $compound['class']); |
103 |
| - } |
104 |
| - } |
| 115 | + foreach ($this->defaultVariants as $variantName => $variantValue) { |
| 116 | + if (!isset($recipes[$variantName]) && isset($this->variants[$variantName][$variantValue])) { |
| 117 | + $classes = [...$classes, ...(array) $this->variants[$variantName][$variantValue]]; |
105 | 118 | }
|
106 | 119 | }
|
107 | 120 |
|
108 |
| - if (null !== $this->defaultVariants) { |
109 |
| - foreach ($this->defaultVariants as $defaultVariantName => $defaultVariantValue) { |
110 |
| - if (!isset($recipes[$defaultVariantName])) { |
111 |
| - $classes .= ' '.$this->variants[$defaultVariantName][$defaultVariantValue]; |
112 |
| - } |
| 121 | + return implode(' ', array_unique(array_map('trim', $classes))); |
| 122 | + } |
| 123 | + |
| 124 | + private function resolveCompoundVariant(array $compound, array $recipes): ?array |
| 125 | + { |
| 126 | + foreach ($compound as $compoundName => $compoundValues) { |
| 127 | + if ('class' === $compoundName) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + if (!isset($recipes[$compoundName]) || !\in_array($recipes[$compoundName], (array) $compoundValues)) { |
| 131 | + return null; // Early exit if the recipe does not match the compound variant |
113 | 132 | }
|
114 | 133 | }
|
115 | 134 |
|
116 |
| - return trim($classes); |
| 135 | + // Return compound class as array, ensuring it's not null and is always treated as an array |
| 136 | + return (array) ($compound['class'] ?? []); |
117 | 137 | }
|
118 | 138 | }
|
0 commit comments