|
12 | 12 | namespace Symfony\UX\TwigComponent;
|
13 | 13 |
|
14 | 14 | /**
|
15 |
| - * @author Mathéo Daninos <matheo.daninos@gmail.com> |
| 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. |
| 19 | + * |
| 20 | + * @see https://cva.style/docs |
16 | 21 | *
|
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. |
| 22 | + * @doc https://symfony.com/bundles/ux-twig-component/current/index.html |
| 23 | + * |
| 24 | + * @author Mathéo Daninos <matheo.daninos@gmail.com> |
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|null> |
| 32 | + */ |
| 33 | + private readonly array $base; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param string|list<string|null> $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 | + $this->base = (array) $base; |
40 | 89 | }
|
41 | 90 |
|
42 |
| - public function apply(array $recipes, ?string ...$classes): string |
| 91 | + public function apply(array $recipes, ?string ...$additionalClasses): string |
43 | 92 | {
|
44 |
| - return trim($this->resolve($recipes).' '.implode(' ', array_filter($classes))); |
45 |
| - } |
| 93 | + $classes = [...$this->base]; |
46 | 94 |
|
47 |
| - public function resolve(array $recipes): string |
48 |
| - { |
49 |
| - if (\is_array($this->base)) { |
50 |
| - $classes = implode(' ', $this->base); |
51 |
| - } else { |
52 |
| - $classes = $this->base ?? ''; |
| 95 | + // Resolve recipes against variants |
| 96 | + foreach ($recipes as $recipeName => $recipeValue) { |
| 97 | + $recipeClasses = $this->variants[$recipeName][$recipeValue] ?? []; |
| 98 | + $classes = [...$classes, ...(array) $recipeClasses]; |
53 | 99 | }
|
54 | 100 |
|
55 |
| - foreach ($recipes as $recipeName => $recipeValue) { |
56 |
| - if (!isset($this->variants[$recipeName][$recipeValue])) { |
57 |
| - continue; |
58 |
| - } |
| 101 | + // Resolve compound variants |
| 102 | + foreach ($this->compoundVariants as $compound) { |
| 103 | + $compoundClasses = $this->resolveCompoundVariant($compound, $recipes) ?? []; |
| 104 | + $classes = [...$classes, ...$compoundClasses]; |
| 105 | + } |
59 | 106 |
|
60 |
| - if (\is_string($this->variants[$recipeName][$recipeValue])) { |
61 |
| - $classes .= ' '.$this->variants[$recipeName][$recipeValue]; |
62 |
| - } else { |
63 |
| - $classes .= ' '.implode(' ', $this->variants[$recipeName][$recipeValue]); |
| 107 | + // Apply default variants if specific recipes aren't provided |
| 108 | + foreach ($this->defaultVariants as $defaultVariantName => $defaultVariantValue) { |
| 109 | + if (!isset($recipes[$defaultVariantName])) { |
| 110 | + $variantClasses = $this->variants[$defaultVariantName][$defaultVariantValue] ?? []; |
| 111 | + $classes = [...$classes, ...(array) $variantClasses]; |
64 | 112 | }
|
65 | 113 | }
|
| 114 | + $classes = [...$classes, ...array_values($additionalClasses)]; |
66 | 115 |
|
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 |
| - } |
| 116 | + $classes = implode(' ', array_filter($classes, is_string(...))); |
| 117 | + $classes = preg_split('#\s+#', $classes, -1, \PREG_SPLIT_NO_EMPTY) ?: []; |
89 | 118 |
|
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 |
| - } |
| 119 | + return implode(' ', array_unique($classes)); |
| 120 | + } |
98 | 121 |
|
99 |
| - if (\is_string($compound['class'])) { |
100 |
| - $classes .= ' '.$compound['class']; |
101 |
| - } else { |
102 |
| - $classes .= ' '.implode(' ', $compound['class']); |
103 |
| - } |
104 |
| - } |
| 122 | + private function resolveCompoundVariant(array $compound, array $recipes): array |
| 123 | + { |
| 124 | + foreach ($compound as $compoundName => $compoundValues) { |
| 125 | + if ('class' === $compoundName) { |
| 126 | + continue; |
105 | 127 | }
|
106 |
| - } |
107 |
| - |
108 |
| - if (null !== $this->defaultVariants) { |
109 |
| - foreach ($this->defaultVariants as $defaultVariantName => $defaultVariantValue) { |
110 |
| - if (!isset($recipes[$defaultVariantName])) { |
111 |
| - $classes .= ' '.$this->variants[$defaultVariantName][$defaultVariantValue]; |
112 |
| - } |
| 128 | + if (!isset($recipes[$compoundName]) || !\in_array($recipes[$compoundName], (array) $compoundValues)) { |
| 129 | + return []; |
113 | 130 | }
|
114 | 131 | }
|
115 | 132 |
|
116 |
| - return trim($classes); |
| 133 | + return (array) ($compound['class'] ?? []); |
117 | 134 | }
|
118 | 135 | }
|
0 commit comments