Courses

Tailwind CSS v4 for Laravel Developers

Laravel Helpers and Blade Components

Laravel has a powerful Blade components feature. Let's look at how they can be used in combination with helpers.


For example a text input component:

resources/views/components/text-input.blade.php:

@props(['disabled' => false])
 
<input {{ $disabled ? 'disabled' : '' }} {!! $attributes->merge(['class' => 'border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm']) !!}>

Tailwind classes are defined inside the component. Developers can easily reuse components without repeating CSS or writing custom classes.

<x-text-input type="text" />

Another benefit of this structure is quickly changing the component's appearance by adding or removing Tailwind classes without searching for the correct class in many CSS files.


Default / Merged Attributes

Sometimes, you need slight modifications to your components. For instance, text should be uppercase for that text-input component.

All Blade components in Laravel have a particular variable named $attributes, the attributes bag. It has the ->merge() method to define default classes.

The syntax is as follows:

{!! $attributes->merge(['class' => '...']) !!}

Now, we can add just a single Tailwind class to our component, and it will be merged with all default classes inside the component, rendering us an input field where all input is in uppercase.

<x-text-input type="text" class="uppercase" />

Custom Properties

You can define your custom properties on the component and use them as data variables using the @props Blade directive.

Let's modify the default text-input component and add more props:

resources/views/components/text-input.blade.php:

@props([
'disabled' => false,
'description' => '',
'default' => ''
])
 
<div>
<input
{{ $disabled ? 'disabled' : '' }}
{!! $attributes->merge(['class' => 'border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm']) !!}
value="{{ $default }}"
>
<div class="text-gray-600">{{ $description }}</div>
</div>

Now, in our view, we can define components like this:

@php
$customVariable = 'Laravel Daily';
@endphp
 
<x-text-input
type="text"
:default="$customVariable"
description="Your name in latin characters"
/>

It gives the following result:

Text Input Component

Notice that when we have a colon : prefix before the attribute, the value between double quotes "" is evaluated by PHP. That can be useful for more complex logic.


Conditional Classes

The @class directive conditionally compiles a CSS class string. It accepts an array of classes where the array key contains the classes you wish to add, while the value is a boolean expression.

@php
$isActive = true;
@endphp
 
<div @class([
'p-4',
'font-bold' => $isActive,
'text-gray-500' => ! $isActive,
])>This is active text</div>
 
 
@php
$isActive = false;
@endphp
 
<div @class([
'p-4',
'font-bold' => $isActive,
'text-gray-500' => ! $isActive,
])>This is inactive text</div>

Conditional Class Active Text

More information on Blade capabilities could be found in the Official Laravel Documentation.

Previous: Your Own Custom Classes
avatar

Hello there.

I have a function in a Service file called getBlendColor, something like this:

function getBlendColor($index): string
    {
        switch ($index) {
            case '4':
                $blendColor = 'bg-pink-200';
                break;
            case '9':
                $blendColor = 'bg-blue-200';
                break;
            case 'total':
                $blendColor = 'bg-yellow-200';
                break;
            default:
                $blendColor = '';
                break;
        }

        return $blendColor;
    }

And i'm calling it inside a blade file like this:

5° Ano

But it is not working. So I put the function on the top of the blade file, and call that function, and it works. I would prefer to have that function separated, but ok. But then i change the switch to match function, to its more elegant, but de colors didn't work properly with the match function, just one color worked.

$blendColor = match ($index) {
    '4' => 'bg-pink-200',
    '9' => 'bg-blue-200',
    'total' => 'bg-yellow-200',
    default => '',
};

So, 2 questions:

  1. Can't we use tailwind outside blade, like in a service file and the call then in the blade file?
  2. Why the tailwind did not work with the match funciton properly?
avatar

Simply add a path to your php file in tailwind.config.js file, so Tailwind knows which files to look for class names. Example:

content: [
    './app/Services/**/*.php',
],

Make sure you're always using complete class names and not constructing them dynamically: Dynamic class names.

avatar

Thanks for the reply. I tried this, but it doesn't work for some how. I tried both below and run npm run dev and npm run build:

'./app/Services/**/*.php',
'./app/Services/*.php',
avatar
You can use Markdown
avatar
You can use Markdown