Skip to content

Commit d85e7d5

Browse files
authored
Add native image option support (#175)
* add image option * Update README.md
1 parent 5ced1bb commit d85e7d5

5 files changed

Lines changed: 97 additions & 0 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,25 @@ public function changesSaved()
407407
}
408408
```
409409

410+
### Image
411+
412+
You can use `imageUrl()`, `imageWidth()`, `imageHeight()`, and `imageAlt()` methods to define custom image into your alert.
413+
``` php
414+
LivewireAlert::imageUrl('/images/example.png');
415+
```
416+
417+
``` php
418+
LivewireAlert::imageWidth(200);
419+
```
420+
421+
``` php
422+
LivewireAlert::imageHeight(200);
423+
```
424+
425+
``` php
426+
LivewireAlert::imageAlt('Simple Alt');
427+
```
428+
410429
### Options
411430

412431
The `withOptions()` method allows you to extend the alert’s configuration with any SweetAlert2-compatible options, giving you full control to customize its appearance, behavior, or functionality beyond the built-in methods. This is ideal for advanced use cases like adding inputs, modifying styles, or setting custom SweetAlert2 features.

src/Contracts/Alertable.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public function cancelButtonColor(string $color): self;
5252

5353
public function denyButtonColor(string $color): self;
5454

55+
public function imageUrl(string $url): self;
56+
57+
public function imageWidth(int $width): self;
58+
59+
public function imageHeight(int $height): self;
60+
61+
public function imageAlt(string $alt): self;
62+
5563
public function asConfirm(): self;
5664

5765
/**

src/Facades/LivewireAlert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
* @method static \Jantinnerezo\LivewireAlert\LivewireAlert confirmButtonColor(string $color)
3030
* @method static \Jantinnerezo\LivewireAlert\LivewireAlert cancelButtonColor(string $color)
3131
* @method static \Jantinnerezo\LivewireAlert\LivewireAlert denyButtonColor(string $color)
32+
* @method static \Jantinnerezo\LivewireAlert\LivewireAlert imageUrl(string $url)
33+
* @method static \Jantinnerezo\LivewireAlert\LivewireAlert imageWidth(int $width)
34+
* @method static \Jantinnerezo\LivewireAlert\LivewireAlert imageHeight(int $height)
35+
* @method static \Jantinnerezo\LivewireAlert\LivewireAlert imageAlt(string $alt)
3236
* @method static \Jantinnerezo\LivewireAlert\LivewireAlert asConfirm()
3337
* @method static \Jantinnerezo\LivewireAlert\LivewireAlert onConfirm(string $action, mixed $data = null)
3438
* @method static \Jantinnerezo\LivewireAlert\LivewireAlert onDeny(string $action, mixed $data = null)

src/LivewireAlert.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,35 @@ public function denyButtonColor(string $color): self
228228
return $this;
229229
}
230230

231+
public function imageUrl(string $url): Alertable
232+
{
233+
$this->options[Enums\Option::ImageUrl->value] = $url;
234+
235+
return $this;
236+
}
237+
238+
public function imageWidth(int $width): Alertable
239+
{
240+
$this->options[Enums\Option::ImageWidth->value] = $width;
241+
242+
return $this;
243+
}
244+
245+
public function imageHeight(int $height): Alertable
246+
{
247+
$this->options[Enums\Option::ImageHeight->value] = $height;
248+
249+
return $this;
250+
}
251+
252+
public function imageAlt(string $alt): Alertable
253+
{
254+
$this->options[Enums\Option::ImageAlt->value] = $alt;
255+
256+
return $this;
257+
}
258+
259+
231260
public function asConfirm(): self
232261
{
233262
$this->question();

tests/LivewireAlertTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,41 @@ protected function livewireAlert(): LivewireAlert
579579
Livewire::test(TestComponent::class)->instance()
580580
);
581581
}
582+
583+
#[Test]
584+
public function it_sets_image_url_correctly():void
585+
{
586+
$imageUrl = 'https://picsum.photos/200/300';
587+
$alert = $this->livewireAlert();
588+
$alert->imageUrl($imageUrl);
589+
590+
$this->assertEquals($imageUrl, $alert->getOptions()['imageUrl']);
591+
}
592+
593+
#[Test]
594+
public function it_sets_image_width_correctly():void
595+
{
596+
$alert = $this->livewireAlert();
597+
$alert->imageWidth(200);
598+
599+
$this->assertEquals(200, $alert->getOptions()['imageWidth']);
600+
}
601+
602+
#[Test]
603+
public function it_sets_image_height_correctly():void
604+
{
605+
$alert = $this->livewireAlert();
606+
$alert->imageHeight(200);
607+
608+
$this->assertEquals(200, $alert->getOptions()['imageHeight']);
609+
}
610+
611+
#[Test]
612+
public function it_sets_image_alt_correctly():void
613+
{
614+
$alert = $this->livewireAlert();
615+
$alert->imageAlt('Test Alt Text');
616+
617+
$this->assertEquals('Test Alt Text', $alert->getOptions()['imageAlt']);
618+
}
582619
}

0 commit comments

Comments
 (0)