Skip to content
This repository was archived by the owner on Oct 20, 2025. It is now read-only.

Commit d386f36

Browse files
authored
Better Toast DX + Styling fixes (#55)
1 parent cdcb263 commit d386f36

File tree

11 files changed

+141
-50
lines changed

11 files changed

+141
-50
lines changed

app/tests/Unit/ToastTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use ProtoneMedia\Splade\Facades\Splade;
6+
use ProtoneMedia\Splade\SpladeToast;
7+
use Tests\TestCase;
8+
9+
class ToastTest extends TestCase
10+
{
11+
/** @test */
12+
public function it_can_set_a_default_toast()
13+
{
14+
$toastA = Splade::toast('Message A')->toArray();
15+
16+
$this->assertEquals('Message A', $toastA['message']);
17+
$this->assertEquals('success', $toastA['style']);
18+
$this->assertEquals('right', $toastA['positionX']);
19+
$this->assertEquals('top', $toastA['positionY']);
20+
21+
Splade::defaultToast(function (SpladeToast $toast) {
22+
$toast->warning()->message('Hey!')->leftBottom()->autoDismiss(5);
23+
});
24+
25+
$toastB = Splade::toast()->toArray();
26+
27+
$this->assertEquals('Hey!', $toastB['message']);
28+
$this->assertEquals('warning', $toastB['style']);
29+
$this->assertEquals('left', $toastB['positionX']);
30+
$this->assertEquals('bottom', $toastB['positionY']);
31+
32+
$toastC = Splade::toast('Custom message')->toArray();
33+
34+
$this->assertEquals('Custom message', $toastC['message']);
35+
}
36+
}

resources/views/confirm.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
is="transition"
1212
child
1313
animation="opacity"
14-
class="fixed inset-0 bg-black/75"
14+
class="fixed z-30 inset-0 bg-black/75"
1515
/>
1616

17-
<div class="fixed z-30 inset-0 overflow-y-auto">
17+
<div class="fixed z-40 inset-0 overflow-y-auto">
1818
<div class="flex items-end sm:items-center justify-center min-h-full p-4 text-center sm:p-0">
1919
<x-splade-component is="transition" child animation="fade" after-leave="confirm.emitClose">
2020
<x-splade-component is="dialog" panel class="relative bg-white rounded-lg px-4 pt-5 pb-4 text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:max-w-lg sm:w-full sm:p-6">

resources/views/modal-wrapper.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
is="transition" child
99
v-if="modal.stack === 1"
1010
animation="opacity">
11-
<div v-show="modal.onTopOfStack" class="fixed inset-0 bg-black/75" />
11+
<div v-show="modal.onTopOfStack" class="fixed z-30 inset-0 bg-black/75" />
1212
</x-splade-component>
1313

14-
<div v-if="modal.stack > 1 && modal.onTopOfStack" class="fixed inset-0 bg-black/75" />
14+
<div v-if="modal.stack > 1 && modal.onTopOfStack" class="fixed z-30 inset-0 bg-black/75" />
1515

1616
{{ $slot }}
1717
</x-splade-component>

resources/views/modal.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<x-splade-component is="modal-wrapper" :base-attributes="$attributes" :key="$modalKey" :close-button="$closeButton">
22
<!-- Full-screen scrollable container -->
3-
<div class="fixed inset-0 overflow-y-auto p-4">
3+
<div class="fixed inset-0 z-40 overflow-y-auto p-4">
44
<!-- Container to center the panel -->
55
<div class="flex min-h-full items-center justify-center">
66
<!-- The actual dialog panel -->

resources/views/slideover.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<x-splade-component is="modal-wrapper" :base-attributes="$attributes" :key="$modalKey" :close-button="$closeButton">
22
<!-- Full-screen scrollable container -->
3-
<div class="fixed inset-0 overflow-y-auto">
3+
<div class="fixed inset-0 z-40 overflow-y-auto">
44
<!-- Container to center the panel -->
55
<div class="flex min-h-full items-center justify-end">
66
<!-- The actual dialog panel -->

resources/views/toast-wrapper.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
animation="opacity"
66
appear
77
show="toasts.hasBackdrop"
8-
class="fixed inset-0 bg-black/75"
8+
class="fixed z-30 inset-0 bg-black/75"
99
/>
1010

11-
<div class="fixed inset-0 grid grid-cols-3 grid-flow-row-3 z-10 pointer-events-none">
11+
<div class="fixed z-40 inset-0 grid grid-cols-3 grid-flow-row-3 pointer-events-none">
1212
<div v-for="position in toasts.positions" class="relative">
1313
<div
1414
:class="{

resources/views/toast.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
</svg>
3030
@endif
3131
</div>
32-
<div class="ml-3 break-all">
32+
<div class="ml-3 break-words">
3333
<h3 @class([
3434
'text-sm font-medium',
3535
'text-green-800' => $isSuccess,

src/Facades/Splade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @method static int getLazyComponentKey()
2323
* @method static mixed onInit($value)
2424
* @method static mixed onLazy($value)
25+
* @method static self defaultToast(callable $toastFactory):
2526
* @method static self reset()
2627
* @method static self setModalKey(string $key)
2728
* @method static self setRootView(string $view)

src/Facades/Toast.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66

77
/**
88
* @method static self title(string $title)
9-
* @method static self message(string $title)
9+
* @method static self message(string $message)
1010
* @method static self backdrop(bool $value = true)
1111
* @method static self autoDismiss(int $afterSeconds = 15)
1212
* @method static self position(string $x, string $y)
13-
* @method static self leftTop()
14-
* @method static self centerTop()
15-
* @method static self rightTop()
16-
* @method static self leftCenter()
17-
* @method static self center()
18-
* @method static self rightCenter()
19-
* @method static self leftBottom()
20-
* @method static self centerBottom()
21-
* @method static self rightBottom()
22-
* @method static self info()
23-
* @method static self success()
24-
* @method static self warning()
25-
* @method static self danger()
13+
* @method static self leftTop(string $message = '')
14+
* @method static self centerTop(string $message = '')
15+
* @method static self rightTop(string $message = '')
16+
* @method static self leftCenter(string $message = '')
17+
* @method static self center(string $message = '')
18+
* @method static self rightCenter(string $message = '')
19+
* @method static self leftBottom(string $message = '')
20+
* @method static self centerBottom(string $message = '')
21+
* @method static self rightBottom(string $message = '')
22+
* @method static self info(string $message = '')
23+
* @method static self success(string $message = '')
24+
* @method static self warning(string $message = '')
25+
* @method static self danger(string $message = '')
2626
* @method static self style(string $style)
2727
*
2828
* @see \ProtoneMedia\Splade\SpladeToast

src/SpladeCore.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class SpladeCore
3636

3737
private Head $head;
3838

39+
private $customToastFactory;
40+
3941
public function getRootView(): string
4042
{
4143
return $this->rootView;
@@ -91,6 +93,13 @@ public function setModalKey(string $key): self
9193
return $this;
9294
}
9395

96+
public function defaultToast(callable $toastFactory): self
97+
{
98+
$this->customToastFactory = $toastFactory;
99+
100+
return $this;
101+
}
102+
94103
public function onInit($value)
95104
{
96105
return ResolvableData::from($value)->resolveIf(!$this->isLazyRequest());
@@ -133,7 +142,17 @@ public function head(): Head
133142

134143
public function toast(string $message = ''): SpladeToast
135144
{
136-
return $this->toasts[] = new SpladeToast($message);
145+
$newToast = new SpladeToast($message);
146+
147+
if ($this->customToastFactory) {
148+
call_user_func($this->customToastFactory, $newToast);
149+
}
150+
151+
if (trim($message) !== '') {
152+
$newToast->message($message);
153+
}
154+
155+
return $this->toasts[] = $newToast;
137156
}
138157

139158
public static function toastOnEvent(string $message = ''): SpladeToast

0 commit comments

Comments
 (0)