Search code examples
laravelcomponents

merge attributes - how do I get them to function properly?


i have the following in a component stored in resources/views/components/green-button.blade.php in laravel 8.

<button {{ $attributes->merge(['type' => 'button', 'class' => 'px-4 inline-flex justify-center py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500']) }}>
    {{ $slot }}
</button>

I use it :

<x-green-button class="px-0"
                title="Click to show or hide data entry for {{$person->firstname}}."
                wire:click="toggleVisibility({{$person->id}})">
  <h3 class="my-1">{{$person->FullName_fh}}</h3>
</x-green-button>

The component has an x axis padding of px-4. I pass px-0, but there is no effect. What am I doing wrong?

rbd


Solution

  • You can use @props() to achieve the goal.

    // In your component
    
    @props(['customClass' => ''])
    
    <button {{ $attributes->merge([
        'type'  => 'button', 
        'class' => 'all-your-classes ' . $customClass
    ]) }}>
        {{ $slot }}
    </button>
    
    // In your blade file
    
    <x-green-button customClass="px-0">
        {{ $person->FullName_fh }}
    </x-green-button>