Commit Graph

175 Commits

Author SHA1 Message Date
Fabien Potencier 0bfef5b300 feature #4006 Introduce CVA to html-extra (WebMamba)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Introduce CVA to html-extra

Hey! This PR introduces CVA to Twig. All of this has already been merged into SymfonyUX (https://github.com/symfony/ux/pull/1416), but `@kbond` suggested that this repo can be a better place for this feature.

Here is a description from the PR merged in to SymfonyUX:

------------------------------

This PR introduces a new concept CVA (Class Variance Authority), by adding a1 twig function, to help you manage your class in your component.

Let's take an example an Alert component. In your app, an alert can have a lot of different styles one for success, one for alert, one for warning, and different sizes, with icons or not... You need something that lets you completely change the style of your component without creating a new component, and without creating too much complexity in your template.

Here is the reason came CVA.

Your Alert component can now look like this:

```twig
{% props color = 'blue', size = 'md' %}

{% set alert =  html_cva(
      'alert rounded-lg',
     {
        color: {
            blue: 'text-blue-800 bg-blue-50 dark:bg-gray-800 dark:text-blue-400',
            red: 'text-red-800 bg-red-50 dark:bg-gray-800 dark:text-red-400',
            green: 'text-green-800 bg-green-50 dark:bg-gray-800 dark:text-green-400',
            yellow: 'text-yellow-800 bg-yellow-50 dark:bg-gray-800 dark:text-yellow-400',
        },
        size: {
            sm: 'px-4 py-3 text-sm',
            md: 'px-6 py-4 text-base',
            lg: 'px-8 py-5 text-lg',
        }
    },
    [{
           color: ['red'],
           size: ['lg'],
          class: 'font-semibold'
    }],
   {
           rounded: 'md'
    }
}) %}

<div class="{{ cva.apply({color, size}, attribute.render('class'), 'flex p-4') }}">
    ...
</div>
```

So here you have a `cva` function that lets you define different variants of your component.

You can now use your component like this:
```twig
<twig:Alert color="red" size="md"/>

<twig:Alert color="green" size="sm"/>

<twig:Alert color="yellow" size="lg"/>

<twig:Alert color="red" size="md" class="dark:bg-gray-800"/>
```

And then you get the following result:

<img width="1269" alt="Capture d’écran 2024-01-24 à 00 52 33" src="https://github.com/symfony/ux/assets/32077734/6a5e25be-5b81-4ae7-8385-0fa5422d0396">

If you want to know more about the concept I implement here you can look at:
- CVA (js version): https://cva.style/docs
- tailwind merge: https://github.com/gehrisandro/tailwind-merge-php, https://github.com/dcastil/tailwind-merge
- this implementation by using tailwind-merge and cva is inspired a lot by: https://ui.shadcn.com/ (shadcn is the most starred library on github in 2023)
- a really good article that explains the philosophy behind https://manupa.dev/blog/anatomy-of-shadcn-ui
- this PR works great in a LASTstack: https://symfonycasts.com/screencast/last-stack/last-stack

Tell me what you think about it! Thanks for your time! Cheers 🧡

------------

Commits
-------

e3ac14e1 Introduce CVA to html-extra
2024-08-18 09:36:20 +02:00
Matheo Daninos e3ac14e1c8 Introduce CVA to html-extra 2024-08-18 09:36:16 +02:00
Fabien Potencier 0c30e78b1c Add support for named arguments on special functions 2024-08-16 08:37:24 +02:00
Christophe Coevoet ff4d01f940 Implement the enum_cases function
The implementation contains an optimized implementation of the function
for the common case of using a string literal as the argument. It will
validate the enum existence during compilation and compile the code to
use `MyEnum::cases()` directly.
2024-08-09 21:21:58 +02:00
Fabien Potencier c44be99a84 Rename array to sequence 2024-06-22 19:37:51 +02:00
Fabien Potencier 8f8d67b489 Add some missing functions in docs 2022-12-27 13:34:33 +01:00
seb-jean b376bdd0a9 Add function twig *_name for intl list 2022-12-27 12:49:56 +01:00
Fabien Potencier e5c87f8821 Merge branch '2.x' into 3.x
* 2.x:
  Add output
  Fix the drupal testing script
  Add missing argument for the cycle function
2022-12-15 11:46:42 +01:00
Katharina Luthe 1e3b126cf2 Add output 2022-12-15 11:46:17 +01:00
Christophe Coevoet 883fb333b3 Add missing argument for the cycle function 2022-12-15 11:26:46 +01:00
Fabien Potencier be33323ab1 Merge branch '2.x' into 3.x
* 2.x:
  Fix doc CS
  Adding installation instructions for Symfony
2022-09-28 08:41:08 +02:00
Fabien Potencier 9170edfa35 Fix doc CS 2022-09-28 08:40:57 +02:00
Thomas Landauer c034c1d248 Adding installation instructions for Symfony
Wording is taken from https://twig.symfony.com/doc/3.x/filters/format_datetime.html
2022-09-28 08:37:59 +02:00
Kévin Dunglas 0d6bd45844 add support for the class constant on objects 2022-03-25 09:58:48 +01:00
Fabien Potencier 4659a99cdf Merge branch '2.x' into 3.x
* 2.x:
  Fix URLs
  Replace secure.php.net with www.php.net
2021-05-19 09:38:07 +02:00
Fabien Potencier 398487c101 Merge branch '1.x' into 2.x
* 1.x:
  Replace secure.php.net with www.php.net
2021-05-19 09:14:37 +02:00
Maurício Meneghini Fauth 1783298bb8 Replace secure.php.net with www.php.net
See: https://news-web.php.net/php.internals/104533

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
2021-05-18 16:07:51 -03:00
Fabien Potencier eda44caf9b Merge branch '2.x' into 3.x
* 2.x:
  Tweak wording on docs
  Adding link to RFC
2021-04-10 09:46:12 +02:00
Fabien Potencier 33729ab9a3 Tweak wording on docs 2021-04-10 09:41:46 +02:00
Fabien Potencier 22537dd1c4 Merge branch '2.x' into 3.x
* 2.x:
  Fix small typo
  Fix: Typo
  Enhancement: Use GithubActions instead of Travis for tests
  Fix: DOCtor-RST build
  Enhancement: Use no_explicit_use_of_code_block_php rule
  Fix: Highlighting
  Enhancement: Introduce DOCtor-RST config + Sphinx build + GithubActions
  Enhancement: Use GithubActions instead of Travis for tests
  Fix `odd` not working for negative numbers
  Bump version to 2.14.2-DEV
  Prepare the 2.14.1 release
  Update CHANGELOG
  Bump version to 1.44.2-DEV
  Prepare the 1.44.1 release
2020-11-05 18:38:00 +01:00
Fabien Potencier 4afea9870d Merge branch '1.x' into 2.x
* 1.x:
  Enhancement: Use no_explicit_use_of_code_block_php rule
2020-11-05 18:30:36 +01:00
Oskar Stark 5ceda707ed Fix: DOCtor-RST build 2020-11-05 13:57:55 +01:00
Oskar Stark 87eb1396ba Enhancement: Use no_explicit_use_of_code_block_php rule 2020-11-05 13:36:29 +01:00
Fabien Potencier c2a264a91d Merge branch '1.x' into 2.x
* 1.x:
  Enhancement: Introduce DOCtor-RST config + Sphinx build + GithubActions
  Enhancement: Use GithubActions instead of Travis for tests
  Fix `odd` not working for negative numbers
  Bump version to 1.44.2-DEV
  Prepare the 1.44.1 release
2020-11-05 13:31:01 +01:00
Oskar Stark c9328ad613 Enhancement: Introduce DOCtor-RST config + Sphinx build + GithubActions 2020-11-05 13:09:07 +01:00
Fabien Potencier df7471fed6 Merge branch '2.x' into 3.x
* 2.x:
  Fix PHP 8 support
2020-10-21 14:27:08 +02:00
Fabien Potencier 53650c55c1 Merge branch '1.x' into 2.x
* 1.x:
  Fix PHP 8 support
2020-10-21 14:27:01 +02:00
Fabien Potencier b1c9faa33e Fix PHP 8 support 2020-10-21 14:08:16 +02:00
Fabien Potencier 3c56677c85 Merge branch '2.x' into 3.x
* 2.x:
  Fix PHP 8 support
2020-10-21 13:43:41 +02:00
Fabien Potencier 2ffab581fe Merge branch '1.x' into 2.x
* 1.x:
  Fix PHP 8 support
2020-10-21 13:43:35 +02:00
Fabien Potencier d46aeb0724 Fix PHP 8 support 2020-10-21 13:43:26 +02:00
Fabien Potencier 050fdf54b8 Merge branch '2.x' into 3.x
* 2.x:
  Update api.rst
  Fix typo
  Fix typos
  Fix CI on PHP5
2020-04-24 08:31:57 +02:00
Pierre Grimaud 0273057622 Fix typos 2020-04-24 08:22:30 +02:00
fancyweb e6d3402c71 Remove versionadded in 2.x in 3.x doc 2020-01-11 12:38:48 +01:00
fancyweb 6b25dccf33 Fix versionadded in doc 2020-01-11 12:21:32 +01:00
Fabien Potencier ab735c7a1d Merge branch '2.x' into 3.x
* 2.x:
  Move country_timezone to a function
  Renamed inky to inky_to_html
  Minor fix in a filter description
  Minor fixes in the docs of some intl filters
2019-09-07 14:46:43 +02:00
Fabien Potencier c046e69e67 Move country_timezone to a function 2019-09-07 08:58:31 +02:00
Fabien Potencier d706fc259d Merge branch '2.x' into 3.x
* 2.x:
  added the cssinliner and inky extensions
  tweaked doc
2019-08-11 12:21:19 +02:00
Fabien Potencier 499a6eeb13 tweaked doc 2019-08-11 11:46:30 +02:00
Fabien Potencier 5b1f41b855 Merge branch '2.x' into 3.x
* 2.x:
  fixed markup in docs
  fixed markup in docs
  fixed typo
  fixed CS
  fixed typo
2019-08-10 16:16:55 +02:00
Fabien Potencier 5168c0e2fe fixed markup in docs 2019-08-10 16:16:47 +02:00
Fabien Potencier c4279abb1f Merge branch '2.x' into 3.x
* 2.x:
  disabled some tests that are not compatible with pHp 7.4 yet
  fixed typos
2019-08-10 15:49:59 +02:00
Fabien Potencier 3d5d053238 fixed typos 2019-08-10 15:14:08 +02:00
Fabien Potencier 6d14d9c9db Merge branch '2.x' into 3.x
* 2.x:
  fixed HTML extension
2019-08-10 11:13:02 +02:00
Fabien Potencier 5cc40a06f1 fixed HTML extension 2019-08-10 11:07:38 +02:00
Fabien Potencier 41ab8b9699 Merge branch '2.x' into 3.x
* 2.x:
  moved the HTML extension to an external Composer package
2019-08-10 07:48:25 +02:00
Fabien Potencier 59bb9d1033 moved the HTML extension to an external Composer package 2019-08-08 18:10:45 +02:00
Fabien Potencier 52c633a7be Merge branch '2.x' into 3.x
* 2.x:
  fixed the "empty" test on Traversable instances
  avoid confusion between .. and ..
  fixed phpdocs
  fixed CHANGELOG
  moved css_class function to html_classes filter
  Added support for `css_class` function
  added missing docs
  fixed wrong registration
  added the data_uri filter
  bumped version
  Update CoreExtension.php
2019-08-07 17:09:29 +02:00
Fabien Potencier 6ce9beb9fd moved css_class function to html_classes filter 2019-08-07 16:09:15 +02:00
Grégoire Pineau 2200d837ef Added support for css_class function 2019-08-07 15:57:33 +02:00