-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
jacksleight
wants to merge
2
commits into
gehrisandro:main
Choose a base branch
from
jacksleight:conditional-classes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@gehrisandro that would be a nice feature ! |
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();
}} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:Tests are included and I've added a couple of extra tests to ensure that multiple arguments all still work as expected.