AlertComponent

3
Компонент для flash уведомлений
app/View/Components/AlertComponent.php
                            
<?php
 
namespace App\View\Components;
 
use Illuminate\View\Component;
 
class AlertComponent extends Component
{
public $type;
 
public $dismiss = false;
 
public $buttonLink = null;
 
public $buttonValue = null;
 
public $typeClasses = [
"info" => "indigo",
"error" => "red",
"warning" => "yellow"
];
 
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($type, $dismiss = false, $buttonLink = null, $buttonValue = null)
{
$this->type = $type;
 
$this->dismiss = $dismiss;
 
$this->buttonLink = $buttonLink;
$this->buttonValue = $buttonValue;
}
 
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
return view('components.alert-component');
}
}
resources/views/components/alert-component.blade.php
                            
<div class="bg-{{ $typeClasses[$type] ?? 'indigo' }}-600" x-data="{}" x-ref="alert">
<div class="max-w-7xl mx-auto py-3 px-3 sm:px-6 lg:px-8">
<div class="flex items-center justify-between flex-wrap">
<div class="w-0 flex-1 flex items-center">
<p class="ml-3 font-medium text-white truncate">
<span class="inline">
{{ $slot }}
</span>
</p>
</div>
 
@if($buttonLink)
<div class="order-3 mt-2 flex-shrink-0 w-full sm:order-2 sm:mt-0 sm:w-auto">
<a href="{{ $buttonLink }}" class="flex items-center justify-center px-4 py-2 border border-transparent
rounded-md shadow-sm text-sm font-medium text-{{ $typeClasses[$type] ?? 'indigo' }}-600 bg-white hover:bg-{{ $typeClasses[$type] ?? 'indigo' }}-50">
{{ $buttonValue ?? __("Show more") }}
</a>
</div>
@endif
 
@if($dismiss)
<div class="order-2 flex-shrink-0 sm:order-3 sm:ml-3">
<button x-on:click="$refs.alert.remove()" type="button" class="-mr-1 flex p-2 rounded-md hover:bg-{{ $typeClasses[$type] ?? 'indigo' }}-500 focus:outline-none focus:ring-2 focus:ring-white sm:-mr-2">
<span class="sr-only">{{ __("Dismiss") }}</span>
 
<svg class="h-6 w-6 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
</div>
@endif
</div>
</div>
</div>
resources/views/partials/alert.blade.php
                            
@if(session()->has('alert'))
<x-alert type="info" dismiss="true" buttonLink="/" buttonValue="Click me">
{{ session()->get('alert') }}
</x-alert>
@endif
 
@if(session()->has('warning'))
<x-alert type="warning" dismiss="false">
{{ session()->get('warning') }}
</x-alert>
@endif
 
@if(session()->has('error'))
<x-alert type="error" dismiss="true">
{{ session()->get('error') }}
</x-alert>
@endif
resources/views/layouts/app.blade.php
                            
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
 
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
 
<link href="{{ mix('css/app.css') }}" rel="stylesheet">
 
@yield('after-styles')
</head>
 
<body class="antialiased antialiased text-gray bg-white">
<main class="mb-20">
@include("partials.alert")
 
@yield('content')
</main>
 
<script src="{{ mix('js/app.js') }}"></script>
</body>
</html>
app/Providers/AppServiceProvider.php
                            
<?php
 
namespace App\Providers;
 
use App\View\Components\AlertComponent;
 
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
 
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
 
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if(!app()->runningInConsole()) {
Blade::withoutDoubleEncoding();
 
Blade::component('alert', AlertComponent::class);
}
}
}