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

Support conditional classes in Blade directive and attribute macro #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jacksleight
Copy link

@jacksleight jacksleight commented Jul 10, 2023

This PR adds support for conditional classes to the Blade directive and attribute macro, just like Blade's existing @class directive and $attrbutes->class() method support. Allowing you to do:

<div class="@twMerge([
    'h-4 h-6',
    'rounded-lg' => $someCondition,
])"></div>

Tests are included and I've added a couple of extra tests to ensure that multiple arguments all still work as expected.

@robsontenorio
Copy link

robsontenorio commented Jul 30, 2023

@gehrisandro that would be a nice feature !

@GoodM4ven
Copy link

Until the author merges this, here's a helper method me and ChatGPT came up with:

if (!function_exists('conditioned_classes')) {
    function conditioned_classes(array $classes)
    {
        $classString = '';

        foreach ($classes as $class => $condition) {
            if (is_int($class)) {
                // If the key is an integer, it means the class is unconditional
                $classString .= " $condition";
            } elseif ($condition) {
                // If the key is a string, and the condition is true, add the class
                $classString .= " $class";
            }
        }

        return $classString;
    }
}
{{
    $attributes
        ->twMerge(conditioned_classes([
            '-left-[2.8rem] translate-x-full' => $openFromTheRight,
            '-right-[2.8rem] -translate-x-full' => !$openFromTheRight,
            'grid h-10 w-10 rotate-45 items-center justify-center rounded-lg border-2 border-primary-light-500 shadow shadow-primary-light-500/30 transition-all dark:border-primary-dark-500 dark:shadow-primary-dark-500/30 ali-outline-to-ring bg-light dark:bg-dark',
        ]))
        ->merge();
}}

@chrillep
Copy link

chrillep commented Feb 12, 2024

Until the author merges this, here's a helper method me and ChatGPT came up with:

if (!function_exists('conditioned_classes')) {
    function conditioned_classes(array $classes)
    {
        $classString = '';

        foreach ($classes as $class => $condition) {
            if (is_int($class)) {
                // If the key is an integer, it means the class is unconditional
                $classString .= " $condition";
            } elseif ($condition) {
                // If the key is a string, and the condition is true, add the class
                $classString .= " $class";
            }
        }

        return $classString;
    }
}
{{
    $attributes
        ->twMerge(conditioned_classes([
            '-left-[2.8rem] translate-x-full' => $openFromTheRight,
            '-right-[2.8rem] -translate-x-full' => !$openFromTheRight,
            'grid h-10 w-10 rotate-45 items-center justify-center rounded-lg border-2 border-primary-light-500 shadow shadow-primary-light-500/30 transition-all dark:border-primary-dark-500 dark:shadow-primary-dark-500/30 ali-outline-to-ring bg-light dark:bg-dark',
        ]))
        ->merge();
}}

the Arr helper already has this ?

https://laravel.com/docs/10.x/helpers#method-array-to-css-classes

use Illuminate\Support\Arr;
 
$isActive = false;
$hasError = true;
 
$array = [
  'p-4', 
  'font-bold' => $isActive, 
  'bg-red' => $hasError
];
 
$classes = Arr::toCssClasses($array);
 
/*
    'p-4 bg-red'
*/

so pass the result of Arr::toCssClasses to twMerge ?

{{
    $attributes
        ->twMerge(Arr::toCssClasses([
            'grid h-10 w-10 rotate-45 items-center justify-center rounded-lg border-2 border-primary-light-500 shadow shadow-primary-light-500/30 transition-all dark:border-primary-dark-500 dark:shadow-primary-dark-500/30 ali-outline-to-ring bg-light dark:bg-dark',
            '-left-[2.8rem] translate-x-full' => $openFromTheRight,
            '-right-[2.8rem] -translate-x-full' => !$openFromTheRight,
        ]))
        ->merge();
}}

@clarkewing
Copy link

Hi @gehrisandro, any chance of getting this merged?

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 this pull request may close these issues.

5 participants