Repository navigation
Open
Description
Hi 👋🏻
This follows #108, which completes the hash of a Twig function argument from the array shape of its PHP parameter. symfony/ux-css now has a cva() Twig function (symfony/ux#4243) that needs a bit more: like html_cva() of twig/html-extra, it takes four named arguments.
Here is its PHP method:
/**
* @param CssStyles $base
* @param array<string, array<array-key, CssStyles>> $variants
* @param list<array{css?: CssStyles}> $compoundVariants
* @param array<string, string|int|float|bool> $defaultVariants
*/
public function cva(array $base = [], array $variants = [], array $compoundVariants = [], array $defaultVariants = []): CvaAnd here is how it is used in Twig:
{% set button = cva(
base: { display: 'inline-flex', rounded: 'md' },
variants: {
variant: {
primary: { bg: 'blue.600', color: 'white' },
ghost: { bg: 'transparent', _hover: { bg: 'gray.100' } },
},
size: {
sm: { h: '8', px: '3' },
lg: { h: '12', px: '6' },
},
},
compoundVariants: [
{ variant: 'ghost', size: 'sm', css: { px: '2' } },
],
defaultVariants: { variant: 'primary', size: 'sm' },
) %}These are the completions I would expect (▌ is the cursor):
| Cursor | Expected items |
|---|---|
base: { ▌ } |
CSS properties: display, alignItems... |
base: { color: '▌' } |
color tokens: blue.600, gray.700... |
variants: { variant: { primary: { ▌ } } } |
CSS properties |
compoundVariants: [{ ▌ }] |
variant, size, css |
compoundVariants: [{ variant: '▌' }] |
primary, ghost |
compoundVariants: [{ variant: ['▌'] }] |
primary, ghost |
compoundVariants: [{ css: { ▌ } }] |
CSS properties |
defaultVariants: { ▌ } |
variant, size |
defaultVariants: { size: '▌' } |
sm, lg |
This needs four rules:
- A hash passed as a named argument,
fn(name: { ... }), completes from the type of the PHP parameter of that name. - Collection types
array<K, V>,list<V>andV[]are followed to their value type, also through@psalm-typeand@phpstan-typealiases. - In a sequence literal
[{ ... }], each item completes from the value type of the list. - When the PHP callable has
variants,compoundVariantsanddefaultVariantsparameters (ordefaultVariant, forhtml_cva()), the default variants and each compound variant also complete the variant names and the values declared in thevariantsargument of the same call.
Rules 1 to 3 apply to every Twig function, only rule 4 is specific to cva().
The PhpStorm plugin does the same in Haehnchen/idea-php-symfony2-plugin#2910.
Thanks!
Metadata
Metadata
Assignees
Labels
No labels
Activity