L o a d i n g
Laravel Toast Component using Tailwind and Alpine BackEnd Development

Laravel Toast Component using Tailwind and Alpine

In this Article, we will see how to create a Toast Component and make it customisable by allowing for different types and positions.

First of all we are going to create a Component File in resources/views/components/ directory. We are going to call it alert.blade.php and we can call it in Blade File using below syntax

<x alert></x alert>
Enter fullscreen mode Exit fullscreen mode

We are going to pass the message that we want to display in between the 2 tags.

<x alert>You are Logged in!</x alert>
Enter fullscreen mode Exit fullscreen mode

This message will be available to the component as $slot variable.

<div>
    {{$slot}}
</div>
Enter fullscreen mode Exit fullscreen mode

Let us design it using some Tailwind CSS Classes.

<div>
    <div class="bg blue 500 border blue 700 max w xs text white rounded lg px 4 py 2">
        {{$slot}}
    </div>
</div>
Enter fullscreen mode Exit fullscreen mode

We have applied following classes:

  • bg blue 500: This gives a background color of Blue Shade.
  • border blue 700: This gives a border of slightly darker Blue Shade.
  • max w xs: This limits the maximum widht that div can take.
  • text white: This gives the white color to the message
  • rounded lg: This gives the border radius to the div.
  • px 4 py 2: They give a padding to the div.

At this stage our alert component is displayed depending upon where it is rendered in the Blade File. However, we want to display it in a fixed position at bottom right of the screen. To do this, we will give Tailwind CSS Classes to the outer div.

<div class="bottom 4 right 4 fixed">
    <div class="bg blue 500 border blue 700 max w xs text white rounded lg px 4 py 2">
        {{$slot}}
    </div>
</div>
Enter fullscreen mode Exit fullscreen mode

fixed class gives it a fixed Position and bottom 4 and right 4 place it at the bottom right of the screen. At this stage we can include our Component anywhere in our Blade File and it will display at the bottom right of the screen.

Lets make our Component a bit more flexible. Currently it only display every message in Blue Shade. However, we want to support error, warning and success message type along with the info message type which will be default. We are going to add type as @props. And we are going to change the CSS classes based on the type property.

@props(['type' => 'info'])

@php
    $typeClasses = [
        'info' => 'bg blue 500 border blue 700',
        'warning' => 'bg yellow 500 border yellow 700',
        'error' => 'bg red 500 border red 700',
        'success' => 'bg green 500 border green 700',
    ][$type];
@endphp

<div class="bottom 4 right 4 fixed">
    <div class="{{$typeClasses}} max w xs text white rounded lg px 4 py 2">
        {{$slot}}
    </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Now we can call our Alert Component by passing type as follows:

<x alert type="error">Record was not Saved.</x alert>
Enter fullscreen mode Exit fullscreen mode

And the Component will be displayed with a shade of red color. If you do not pass any type, it will default to info.

Similarly, we are going to allow our Component to take position as props. We will support 4 positions, bottom right, bottom left, top right, top left. Our Component will look like this.

@props(['type' => 'info', 'position' => 'bottom right'])

@php
    $typeClasses = [
        'info' => 'bg blue 500 border blue 700',
        'warning' => 'bg yellow 500 border yellow 700',
        'error' => 'bg red 500 border red 700',
        'success' => 'bg green 500 border green 700',
    ][$type];

    $positionClasses = [
        'bottom right' => 'bottom 4 right 4',
        'bottom left' => 'bottom 4 left 4',
        'top right' => 'top 4 right 4',
        'top left' => 'top 4 left 4',
    ][$position]
@endphp

<div class="{{$positionClasses}} fixed">
    <div class="{{$typeClasses}} max w xs text white rounded lg px 4 py 2">
        {{$slot}}
    </div>
</div>
Enter fullscreen mode Exit fullscreen mode

You can try by calling the component as follows:

<x alert type="error" position="top right">
    Profile was saved successfully.
</x alert>
Enter fullscreen mode Exit fullscreen mode

And we also want User to close this Notification when they click on it. we are going to use AlpineJs for it. We will create a show property and use x show directive to change the display of our Component. Using the @click directive we will change the value of show to false. We will also use the class cursor pointer to change the Cursor to Pointer.

<div class="{{$positionClasses}} fixed cursor pointer"
    x data="{show:true}"
    x show="show"
    @click="show=false"
>
Enter fullscreen mode Exit fullscreen mode

And with that our Component is complete and we can use this Component anywhere in our Laravel Project. If you are interested in more complex implementation using Livewire, please check out this package.

Hope you have enjoyed this tutorial. For similar articles, you can follow me on Twitter

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started