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

[8.x] Conditionally merge classes into a Blade Component attribute bag #36131

Merged
merged 2 commits into from
Feb 4, 2021
Merged

[8.x] Conditionally merge classes into a Blade Component attribute bag #36131

merged 2 commits into from
Feb 4, 2021

Conversation

pascalbaljet
Copy link
Contributor

Inspired by Vue's :class syntax, this PR adds a class() method to the ComponentAttributeBag class. It merges the given classes into the attribute bag.

It accepts an array of classes, and you can specify a constraint whether to merge the class or not:

<div {{ $attributes->class(['p-4', 'bg-red' => $hasError]) }}>

It also accepts a string:

<div {{ $attributes->class('p-4') }}>

@taylorotwell
Copy link
Member

What's your suggest usage with this if you also need to pass other attributes to the parent? Do you use $attributes->class() and then $attributes->except('class')->merge()?

@taylorotwell taylorotwell merged commit 2ee67d0 into laravel:8.x Feb 4, 2021
@pascalbaljet
Copy link
Contributor Author

Thanks for merging! I would chain the class and merge methods.

Blade Component view:

<button {{ $attributes->class(['bg-red' => true])->merge(['type' => 'submit']) }}>
    {{ $slot }}
</button>

Consuming the component:

<x-button class="justify-self-end" type="reset" disabled>
    Reset form
</x-button>

The rendered HTML:

<button class="bg-red justify-self-end" type="reset" disabled="disabled">
    Reset form
</button>

@driesvints
Copy link
Member

@pascalbaljet thanks for this! Can you also send in a PR to the docs?

@pascalbaljet
Copy link
Contributor Author

@pascalbaljet thanks for this! Can you also send in a PR to the docs?

Yes, working on it :)

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.

4 participants