Skip to content

Commit

Permalink
Support for HtmlString in Toasts (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet authored Mar 20, 2024
1 parent 6316e96 commit f409142
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
5 changes: 3 additions & 2 deletions app/app/Http/Controllers/ToastController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use Illuminate\Support\HtmlString;
use ProtoneMedia\Splade\Facades\Splade;
use ProtoneMedia\Splade\Facades\Toast;

Expand Down Expand Up @@ -31,8 +32,8 @@ public function infoRightTop()

public function infoLeftCenter()
{
Toast::title('Info Left Center')
->message('This is a message')
Toast::title(new HtmlString('Info Left Center'))
->message(new HtmlString('This is a message'))
->info()
->leftCenter();

Expand Down
6 changes: 3 additions & 3 deletions resources/views/components/toast.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@
'text-red-800' => $isDanger,
'text-blue-800' => $isInfo,
])>
{!! nl2br(e($title ?: $message)) !!}
{{ $title->isNotEmpty() ? $title : $message }}
</h3>

@if($title && $message)
@if($title->isNotEmpty() && $message->isNotEmpty())
<div @class([
'mt-2 text-sm',
'text-green-700' => $isSuccess,
'text-orange-700' => $isWarning,
'text-red-700' => $isDanger,
'text-blue-700' => $isInfo,
])>
<p>{!! nl2br(e($message)) !!}</p>
<p>{{ $message }}</p>
</div>
@endif
</div>
Expand Down
9 changes: 3 additions & 6 deletions src/SpladeCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use ProtoneMedia\Splade\Facades\Splade;
Expand Down Expand Up @@ -292,19 +293,15 @@ public function toastBuilder(): SpladeToastBuilder
/**
* Returns a new SpladeToast instance
*/
public static function toastOnEvent(string $message = ''): SpladeToast
public static function toastOnEvent(HtmlString|string $message = ''): SpladeToast
{
$newToast = new SpladeToast($message);

if ($factory = Splade::getCustomToastFactory()) {
call_user_func($factory, $newToast);
}

if (trim($message) !== '') {
$newToast->message($message);
}

return $newToast;
return $newToast->message($message);
}

/**
Expand Down
63 changes: 43 additions & 20 deletions src/SpladeToast.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ProtoneMedia\Splade;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\HtmlString;
use JsonSerializable;

class SpladeToast implements Arrayable, JsonSerializable
Expand All @@ -29,23 +30,33 @@ class SpladeToast implements Arrayable, JsonSerializable
* Creates a new toast instance.
*/
public function __construct(
private string $message,
private string $title = '',
private HtmlString|string $message,
private HtmlString|string $title = '',
private string $style = 'success',
private string $positionX = 'right',
private string $positionY = 'top',
private bool $backdrop = false,
private ?int $autoDismiss = null,
) {
$this->message($message);
$this->title($title);
}

/**
* Setter for the title of the toast.
*
* @return $this
*/
public function title(string $title): self
public function title(HtmlString|string $title = ''): self
{
if ($title instanceof HtmlString && trim($title->toHtml()) === '') {
$title = '';
}

if (is_string($title)) {
$title = new HtmlString(nl2br(e($title)));
}

$this->title = $title;

return $this;
Expand All @@ -56,9 +67,13 @@ public function title(string $title): self
*
* @return $this
*/
private function optionalMessage(string $message): self
private function optionalMessage(HtmlString|string $message = ''): self
{
if (trim($message) === '') {
if (is_string($message) && trim($message) === '') {
return $this;
}

if ($message instanceof HtmlString && trim($message->toHtml()) === '') {
return $this;
}

Expand All @@ -70,8 +85,16 @@ private function optionalMessage(string $message): self
*
* @return $this
*/
public function message(string $message): self
public function message(HtmlString|string $message = ''): self
{
if ($message instanceof HtmlString && trim($message->toHtml()) === '') {
$message = '';
}

if (is_string($message)) {
$message = new HtmlString(nl2br(e($message)));
}

$this->message = $message;

return $this;
Expand Down Expand Up @@ -119,7 +142,7 @@ public function position(string $x, string $y): self
*
* @return $this
*/
public function leftTop(string $message = ''): self
public function leftTop(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -131,7 +154,7 @@ public function leftTop(string $message = ''): self
*
* @return $this
*/
public function centerTop(string $message = ''): self
public function centerTop(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -143,7 +166,7 @@ public function centerTop(string $message = ''): self
*
* @return $this
*/
public function rightTop(string $message = ''): self
public function rightTop(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -155,7 +178,7 @@ public function rightTop(string $message = ''): self
*
* @return $this
*/
public function leftCenter(string $message = ''): self
public function leftCenter(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -167,7 +190,7 @@ public function leftCenter(string $message = ''): self
*
* @return $this
*/
public function center(string $message = ''): self
public function center(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -179,7 +202,7 @@ public function center(string $message = ''): self
*
* @return $this
*/
public function rightCenter(string $message = ''): self
public function rightCenter(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -191,7 +214,7 @@ public function rightCenter(string $message = ''): self
*
* @return $this
*/
public function leftBottom(string $message = ''): self
public function leftBottom(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -203,7 +226,7 @@ public function leftBottom(string $message = ''): self
*
* @return $this
*/
public function centerBottom(string $message = ''): self
public function centerBottom(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -215,7 +238,7 @@ public function centerBottom(string $message = ''): self
*
* @return $this
*/
public function rightBottom(string $message = ''): self
public function rightBottom(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -227,7 +250,7 @@ public function rightBottom(string $message = ''): self
*
* @return $this
*/
public function info(string $message = ''): self
public function info(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -239,7 +262,7 @@ public function info(string $message = ''): self
*
* @return $this
*/
public function success(string $message = ''): self
public function success(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -251,7 +274,7 @@ public function success(string $message = ''): self
*
* @return $this
*/
public function warning(string $message = ''): self
public function warning(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -263,7 +286,7 @@ public function warning(string $message = ''): self
*
* @return $this
*/
public function danger(string $message = ''): self
public function danger(HtmlString|string $message = ''): self
{
return $this
->optionalMessage($message)
Expand All @@ -275,7 +298,7 @@ public function danger(string $message = ''): self
*
* @return $this
*/
public function style(string $style): self
public function style(HtmlString|string $style): self
{
$this->style = $style;

Expand Down

0 comments on commit f409142

Please sign in to comment.