Skip to content

Complete hashes in named arguments and lists, and the variants of cva() #133

Open

Description

@Kocal

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 = []): Cva

And 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:

  1. A hash passed as a named argument, fn(name: { ... }), completes from the type of the PHP parameter of that name.
  2. Collection types array<K, V>, list<V> and V[] are followed to their value type, also through @psalm-type and @phpstan-type aliases.
  3. In a sequence literal [{ ... }], each item completes from the value type of the list.
  4. When the PHP callable has variants, compoundVariants and defaultVariants parameters (or defaultVariant, for html_cva()), the default variants and each compound variant also complete the variant names and the values declared in the variants argument 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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions