Livewire - Notifier
Livewire Notifier is a simple notifications system with zero dependencies above TALL-stack (Tailwind CSS, Alpine.JS, Laravel and Livewire).
Requirements
Make sure that Livewire and Alpine.JS are installed properly. The easiest way to do it is to install Laravel Jetstream with Livewire stack (post-install command php artisan jetstream:install livewire
).
Alpine.JS must be imported in resources/js/app.js
package:
import 'alpinejs'
Livewire scripts and styles tags must be present in the layout file:
…
<head>
…
<livewire:styles/>
…
head>
<body>
…
<livewire:scripts/>
body>
html>
Installation
Via Composer
$ composer require codespb/livewire-notifier
Proceed with installation process:
$ php artisan livewire-notifier:install
Afterwards the package config can be found at config/livewire-notifier.php
and views at resources/views/vendor/livewire-notifier/
.
It's required because of Tailwind config is needed to be extended with new purge paths.
Otherwise you won't see messages styled properly.
Usage
Put Livewire-component
into the app layout. Make sure to insert it into correct context because it may be positioned absolutely.
Now you can use it from frontend and backend both.
Message options
Message added at backend (from any Livewire component) must have type of array
. Message added at frontend (from JavaScript) must have type of object
.
$message = [
'text' => __('Post is saved!'),
'title' => '', // Optional
'type' => 'success', // Optional. By default: success | optional: error (or fail), warning (or warn), info
// you can find all types options in the config file
'icon' => '', // Optional. It replaces the default type icon. Can be pure html or svg code
// Attention! The following options override ones from the config file
'duration' => 5000, // Optional. The time of message to be shown. To show infinitely set to 0
'msgClass' => 'bg-gradient-to-r from-green-200 to-green-300', // Optional. Tailwind class for message container
'progressClass' => 'bg-green-500', // Optional. Tailwind class for progress bar. If null progress bar won't be shown
'closable' => false, // Optional. True by default. Whether message is closable by click on message or Esc key press on window
]
let message = {
text: 'Post is saved!'
}
Backend
Livewire event
From any Livewire component push emit
trigger to render new message.
public function save(){
...
$this->emitTo('notifier','message',['text'=>__('The post is saved!')]);
}
Session flash variable
public function save(){
...
session()->flash('notifier',['text'=>__('The post is saved!')]);
return $this->redirect(route('posts'));
}
Frontend
Add new message from javascript:
Livewire.emitTo('notifier','message',{text:'The post is saved!'})
Example
Try to put the following code (with Laravel Jetstream w/Livewire stack installed):
<div class="flex flex-row space-x-4"> <x-jet-button x-data="{}" @click="Livewire.emitTo('notifier','message',{text:'Success',type:'success'})">Successx-jet-button> <x-jet-button x-data="{}" @click="Livewire.emitTo('notifier','message',{text:'Error',type:'error'})">Errorx-jet-button> <x-jet-button x-data="{}" @click="Livewire.emitTo('notifier','message',{text:'Warning',type:'warning'})">Warningx-jet-button> <x-jet-button x-data="{}" @click="Livewire.emitTo('notifier','message',{text:'Info',type:'info'})">Infox-jet-button> <x-jet-button x-data="{}" @click="Livewire.emitTo('notifier','message',{text:'Default',type:'default'})">Defaultx-jet-button> div>