Skip to content

Commit

Permalink
cleanup blade file and add "closeable" property
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy132 committed Jan 17, 2025
1 parent f3cb04b commit c8afdeb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 47 deletions.
53 changes: 40 additions & 13 deletions app/Livewire/AlertBanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace App\Livewire;

use Closure;
use Filament\Notifications\Concerns;
use Filament\Support\Concerns\EvaluatesClosures;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Str;
use Livewire\Wireable;

final class AlertBanner implements Arrayable
final class AlertBanner implements Wireable
{
use Concerns\HasBody;
use Concerns\HasIcon;
Expand All @@ -16,42 +17,68 @@ final class AlertBanner implements Arrayable
use Concerns\HasTitle;
use EvaluatesClosures;

public static function make(?string $id = null): static
protected bool|Closure $closable = false;

public static function make(?string $id = null): AlertBanner
{
$static = new self();
$static->id($id ?? Str::orderedUuid());

return $static;
}

public function toArray(): array
public function toLivewire(): array
{
return [
'id' => $this->getId(),
'title' => $this->getTitle(),
'body' => $this->getBody(),
'status' => $this->getStatus(),
'icon' => $this->getIcon(),
'closeable' => $this->isCloseable(),
];
}

public static function fromArray(array $data): static
public static function fromLivewire(mixed $value): AlertBanner
{
$static = static::make();
$static = AlertBanner::make();

$static->id($data['id']);
$static->title($data['title']);
$static->body($data['body']);
$static->status($data['status']);
$static->icon($data['icon']);
$static->id($value['id']);
$static->title($value['title']);
$static->body($value['body']);
$static->status($value['status']);
$static->icon($value['icon']);
$static->closable($value['closeable']);

return $static;
}

public function send(): static
public function closable(bool|Closure $closable = true): AlertBanner
{
$this->closable = $closable;

return $this;
}

public function isCloseable(): bool
{
session()->push('alert-banners', $this->toArray());
return $this->evaluate($this->closable);
}

public function send(): AlertBanner
{
session()->push('alert-banners', $this->toLivewire());

return $this;
}

public function getColorClasses(): string
{
return match ($this->getStatus()) {
'success' => 'text-success-600 dark:text-success-500',
'warning' => 'text-warning-600 dark:text-warning-500',
'danger' => 'text-danger-600 dark:text-danger-500',
default => 'text-info-600 dark:text-info-500',
};
}
}
3 changes: 2 additions & 1 deletion app/Livewire/AlertBannerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function mount(): void
public function pullFromSession(): void
{
foreach (session()->pull('alert-banners', []) as $alertBanner) {
$this->alertBanners[$alertBanner['id']] = $alertBanner;
$alertBanner = AlertBanner::fromLivewire($alertBanner);
$this->alertBanners[$alertBanner->getId()] = $alertBanner;
}
}

Expand Down
53 changes: 20 additions & 33 deletions resources/views/livewire/alerts/alert-banner.blade.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
@props(['alertBanner'])

@isset ($alertBanner)
@php
$status = $alertBanner['status'];
@php
$icon = $alertBanner->getIcon();
$title = $alertBanner->getTitle();
$body = $alertBanner->getBody();
@endphp

$title = $alertBanner['title'];
$body = $alertBanner['body'];
<div class="{{$alertBanner->getColorClasses()}} flex p-4 mt-3 rounded-xl shadow-lg bg-white dark:bg-gray-900 ring-1 ring-gray-950/5 dark:ring-white/10">
@if (filled($icon))
<x-filament::icon :icon="$icon" class="h-8 w-8 mr-2" color="{{$alertBanner->getStatus()}}" />
@endif

$icon = $alertBanner['icon'] ?? match ($status) {
"success" => "tabler-circle-check",
"warning" => "tabler-exclamation-circle",
"danger" => "tabler-circle-x",
default => "tabler-info-circle",
};
$colorClasses = match ($status) {
"success" => "text-success-600 dark:text-success-500",
"warning" => "text-warning-600 dark:text-warning-500",
"danger" => "text-danger-600 dark:text-danger-500",
default => "text-info-600 dark:text-info-500",
};
@endphp

<div class="{{$colorClasses}} flex p-4 mt-3 rounded-xl shadow-lg bg-white dark:bg-gray-900 ring-1 ring-gray-950/5 dark:ring-white/10">
@if (filled($icon))
<x-filament::icon :icon="$icon" class="h-8 w-8 mr-2" color="{{$status}}" />
<div class="flex flex-col flex-grow">
@if (filled($title))
<p class="font-bold">{{str($title)->sanitizeHtml()->toHtmlString()}}</p>
@endif

<div class="flex flex-col flex-grow">
@if (filled($title))
<p class="font-bold">{{str($title)->sanitizeHtml()->toHtmlString()}}</p>
@endif

@if (filled($body))
<p class="font-normal">{{str($body)->sanitizeHtml()->toHtmlString()}}</p>
@endif
</div>
@if (filled($body))
<p class="font-normal">{{str($body)->sanitizeHtml()->toHtmlString()}}</p>
@endif
</div>
@endisset

@if ($alertBanner->isCloseable())
<x-filament::icon-button color="gray" icon="tabler-x" wire:click="remove('{{$alertBanner->getID()}}')" />
@endif
</div>

0 comments on commit c8afdeb

Please sign in to comment.