From cc2f9182d07d4b8a310b83337d27c22f92e4479b Mon Sep 17 00:00:00 2001 From: Bastien Sulyan Date: Thu, 26 Nov 2026 11:22:26 +0100 Subject: [PATCH] Fix html_cva() ignoring default variants in compound variants --- CHANGELOG | 2 +- extra/html-extra/Cva.php | 14 +-- extra/html-extra/Tests/CvaTest.php | 172 +++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 868a802124d..a00fd4699c5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ # 3.31.2 (2026-XX-XX) - * n/a + * Fix `html_cva()` ignoring default variants when resolving compound variants # 3.31.1 (2026-11-24) diff --git a/extra/html-extra/Cva.php b/extra/html-extra/Cva.php index 4c0f0763f25..7113f7591ee 100644 --- a/extra/html-extra/Cva.php +++ b/extra/html-extra/Cva.php @@ -98,12 +98,6 @@ public function apply(array $recipes, ?string ...$additionalClasses): string $classes = [...$classes, ...self::toList($recipeClasses)]; } - // Resolve compound variants - foreach ($this->compoundVariants as $compound) { - $compoundClasses = $this->resolveCompoundVariant($compound, $recipes) ?? []; - $classes = [...$classes, ...$compoundClasses]; - } - // Apply default variants if specific recipes aren't provided foreach ($this->defaultVariants as $defaultVariantName => $defaultVariantValue) { if (!isset($recipes[$defaultVariantName])) { @@ -111,6 +105,11 @@ public function apply(array $recipes, ?string ...$additionalClasses): string $classes = [...$classes, ...self::toList($variantClasses)]; } } + + // Resolve compound variants + foreach ($this->compoundVariants as $compound) { + $classes = [...$classes, ...$this->resolveCompoundVariant($compound, $recipes)]; + } $classes = [...$classes, ...array_values($additionalClasses)]; $classes = implode(' ', array_filter($classes, 'is_string')); @@ -147,7 +146,8 @@ private function resolveCompoundVariant(array $compound, array $recipes): array if ('class' === $compoundName) { continue; } - if (!isset($recipes[$compoundName]) || !\in_array($recipes[$compoundName], self::toList($compoundValues), true)) { + $recipeValue = $recipes[$compoundName] ?? $this->defaultVariants[$compoundName] ?? null; + if (null === $recipeValue || !\in_array($recipeValue, self::toList($compoundValues), true)) { return []; } } diff --git a/extra/html-extra/Tests/CvaTest.php b/extra/html-extra/Tests/CvaTest.php index 33e54e487e9..b12c8619fc6 100644 --- a/extra/html-extra/Tests/CvaTest.php +++ b/extra/html-extra/Tests/CvaTest.php @@ -602,6 +602,178 @@ public static function recipeProvider(): iterable 'button disabled', ]; + yield 'compound variants with default variants' => [ + [ + 'base' => 'font-semibold border rounded', + 'variants' => [ + 'colors' => [ + 'primary' => 'text-primary', + 'secondary' => 'text-secondary', + ], + 'sizes' => [ + 'sm' => 'text-sm', + 'md' => 'text-md', + ], + ], + 'compounds' => [ + [ + 'colors' => ['primary'], + 'sizes' => ['sm'], + 'class' => 'text-red-500', + ], + ], + 'defaultVariants' => [ + 'colors' => 'primary', + 'sizes' => 'sm', + ], + ], + [], + 'font-semibold border rounded text-primary text-sm text-red-500', + ]; + + yield 'compound variants with partial default variants' => [ + [ + 'base' => 'font-semibold border rounded', + 'variants' => [ + 'colors' => [ + 'primary' => 'text-primary', + 'secondary' => 'text-secondary', + ], + 'sizes' => [ + 'sm' => 'text-sm', + 'md' => 'text-md', + ], + ], + 'compounds' => [ + [ + 'colors' => ['primary'], + 'sizes' => ['sm'], + 'class' => 'text-red-500', + ], + ], + 'defaultVariants' => [ + 'colors' => 'primary', + 'sizes' => 'sm', + ], + ], + ['colors' => 'primary'], + 'font-semibold border rounded text-primary text-sm text-red-500', + ]; + + yield 'compound variants with null recipe falling back to default variant' => [ + [ + 'base' => 'font-semibold border rounded', + 'variants' => [ + 'colors' => [ + 'primary' => 'text-primary', + 'secondary' => 'text-secondary', + ], + 'sizes' => [ + 'sm' => 'text-sm', + 'md' => 'text-md', + ], + ], + 'compounds' => [ + [ + 'colors' => ['primary'], + 'sizes' => ['sm'], + 'class' => 'text-red-500', + ], + ], + 'defaultVariants' => [ + 'colors' => 'primary', + 'sizes' => 'sm', + ], + ], + ['colors' => null, 'sizes' => 'sm'], + 'font-semibold border rounded text-sm text-primary text-red-500', + ]; + + yield 'compound variants overridden by recipe does not match default' => [ + [ + 'base' => 'font-semibold border rounded', + 'variants' => [ + 'colors' => [ + 'primary' => 'text-primary', + 'secondary' => 'text-secondary', + ], + 'sizes' => [ + 'sm' => 'text-sm', + 'md' => 'text-md', + ], + ], + 'compounds' => [ + [ + 'colors' => ['primary'], + 'sizes' => ['sm'], + 'class' => 'text-red-500', + ], + ], + 'defaultVariants' => [ + 'colors' => 'primary', + 'sizes' => 'sm', + ], + ], + ['colors' => 'secondary', 'sizes' => 'sm'], + 'font-semibold border rounded text-secondary text-sm', + ]; + + yield 'compound variants with boolean default variant' => [ + [ + 'base' => 'button', + 'variants' => [ + 'colors' => [ + 'blue' => 'btn-blue', + ], + 'disabled' => [ + 'true' => 'disabled', + 'false' => 'opacity-100', + ], + ], + 'compounds' => [ + [ + 'colors' => ['blue'], + 'disabled' => [false], + 'class' => 'cursor-pointer', + ], + ], + 'defaultVariants' => [ + 'colors' => 'blue', + 'disabled' => false, + ], + ], + [], + 'button btn-blue opacity-100 cursor-pointer', + ]; + + yield 'compound variants with boolean recipe overriding boolean default' => [ + [ + 'base' => 'button', + 'variants' => [ + 'colors' => [ + 'blue' => 'btn-blue', + ], + 'disabled' => [ + 'true' => 'disabled', + 'false' => 'opacity-100', + ], + ], + 'compounds' => [ + [ + 'colors' => ['blue'], + 'disabled' => [false], + 'class' => 'cursor-pointer', + ], + ], + 'defaultVariants' => [ + 'colors' => 'blue', + 'disabled' => false, + ], + ], + ['disabled' => true], + 'button disabled btn-blue', + ]; + yield 'boolean string variants true / true' => [ [ 'variants' => [