Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TwigComponent] CVA - Allow passing array in classes #1586

Closed
seb-jean opened this issue Mar 6, 2024 · 3 comments · Fixed by #1600
Closed

[TwigComponent] CVA - Allow passing array in classes #1586

seb-jean opened this issue Mar 6, 2024 · 3 comments · Fixed by #1600

Comments

@seb-jean
Copy link
Contributor

seb-jean commented Mar 6, 2024

Currently, we can pass a string for classes (Example: "bg-red-100 opacity-50"). It would be interesting to be able to also pass an array in the class values. This would be useful when there are several classes.

Here is an example below:

With array:

{# templates/components/Button.html.twig #}
{% props color = 'blue' %}

{% set button = cva({
    base: [
      'relative isolate inline-flex items-center justify-center gap-x-2 rounded-lg border text-base/6 font-semibold',
      'px-[calc(theme(spacing[3.5])-1px)] py-[calc(theme(spacing[2.5])-1px)] sm:px-[calc(theme(spacing.3)-1px)] sm:py-[calc(theme(spacing[1.5])-1px)] sm:text-sm/6',
      'focus:outline-none data-[focus]:outline data-[focus]:outline-2 data-[focus]:outline-offset-2 data-[focus]:outline-blue-500',
      'data-[disabled]:opacity-50',
      '[&>[data-slot=icon]]:-mx-0.5 [&>[data-slot=icon]]:my-0.5 [&>[data-slot=icon]]:size-5 [&>[data-slot=icon]]:shrink-0 [&>[data-slot=icon]]:text-[--btn-icon] [&>[data-slot=icon]]:sm:my-1 [&>[data-slot=icon]]:sm:size-4 forced-colors:[--btn-icon:ButtonText] forced-colors:data-[hover]:[--btn-icon:ButtonText]',
    ],
    variants: {
        color: {
            blue: [
               'text-white [--btn-hover-overlay:theme(colors.white/10%)] [--btn-bg:theme(colors.blue.600)] [--btn-border:theme(colors.blue.700/90%)]',
               '[--btn-icon:theme(colors.blue.400)] data-[active]:[--btn-icon:theme(colors.blue.300)] data-[hover]:[--btn-icon:theme(colors.blue.300)]',
            ],
            red: [
                'text-white [--btn-hover-overlay:theme(colors.white/10%)] [--btn-bg:theme(colors.red.600)] [--btn-border:theme(colors.red.700/90%)]',
                '[--btn-icon:theme(colors.red.300)] data-[active]:[--btn-icon:theme(colors.red.200)] data-[hover]:[--btn-icon:theme(colors.red.200)]',
            ],
        },
    },
}) %}

<div class="{{ button.apply({color}) }}">
     {% block content %}{% endblock %}
</div>

Wihtout array:

{# templates/components/Button.html.twig #}
{% props color = 'blue' %}

{% set button = cva({
    base: 'relative isolate inline-flex items-center justify-center gap-x-2 rounded-lg border text-base/6 font-semibold px-[calc(theme(spacing[3.5])-1px)] py-[calc(theme(spacing[2.5])-1px)] sm:px-[calc(theme(spacing.3)-1px)] sm:py-[calc(theme(spacing[1.5])-1px)] sm:text-sm/6 focus:outline-none data-[focus]:outline data-[focus]:outline-2 data-[focus]:outline-offset-2 data-[focus]:outline-blue-500 data-[disabled]:opacity-50 [&>[data-slot=icon]]:-mx-0.5 [&>[data-slot=icon]]:my-0.5 [&>[data-slot=icon]]:size-5 [&>[data-slot=icon]]:shrink-0 [&>[data-slot=icon]]:text-[--btn-icon] [&>[data-slot=icon]]:sm:my-1 [&>[data-slot=icon]]:sm:size-4 forced-colors:[--btn-icon:ButtonText] forced-colors:data-[hover]:[--btn-icon:ButtonText]',
    variants: {
        color: {
            blue: 'text-white [--btn-hover-overlay:theme(colors.white/10%)] [--btn-bg:theme(colors.blue.600)] [--btn-border:theme(colors.blue.700/90%)] [--btn-icon:theme(colors.blue.400)] data-[active]:[--btn-icon:theme(colors.blue.300)] data-[hover]:[--btn-icon:theme(colors.blue.300)]',
            red: 'text-white [--btn-hover-overlay:theme(colors.white/10%)] [--btn-bg:theme(colors.red.600)] [--btn-border:theme(colors.red.700/90%)] [--btn-icon:theme(colors.red.300)] data-[active]:[--btn-icon:theme(colors.red.200)] data-[hover]:[--btn-icon:theme(colors.red.200)]',
        },
    },
}) %}

<div class="{{ button.apply({color}) }}">
     {% block content %}{% endblock %}
</div>

What do you think ?

@WebMamba
Copy link
Collaborator

WebMamba commented Mar 6, 2024

Yes it's a good idea, but I think if we do that for the base classes we should also do that for variants, and compound variants

@seb-jean
Copy link
Contributor Author

seb-jean commented Mar 6, 2024

Yes of course.

@seb-jean
Copy link
Contributor Author

seb-jean commented Mar 6, 2024

By the way, they do it at https://cva.style/docs/getting-started/variants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants