Skip to content

Commit 61998f3

Browse files
committed
Refactor simpleLightbox method to accept URL as default value
1 parent dd2b2c0 commit 61998f3

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ Tables\Columns\TextColumn::make('pdf_url')
5656
->simpleLightbox("Your Url address"),
5757
```
5858

59+
You can pass parameter to generate url, also the url can be used as default value (`urlAsDefault` default is true):
60+
```php
61+
Tables\Columns\ImageColumn::make('image')
62+
->simpleLightbox(fn ($record) => $record?->image ?? "Your Image Url address", urlAsDefault: true),
63+
```
64+
```php
65+
Tables\Columns\TextColumn::make('url')
66+
->simpleLightbox(fn ($record) => $record?->image ?? "Your Image Url address", urlAsDefault: true),
67+
```
68+
```php
69+
InfoLists\Components\ImageEntry::make('image')
70+
->simpleLightbox(fn ($record) => $record?->image ?? "Your Image Url address", urlAsDefault: true),
71+
```
72+
```php
73+
Tables\Columns\ImageColumn::make('image')
74+
->simpleLightbox(fn ($record) => $record?->image ?? "Your Image Url address", urlAsDefault: true),
75+
```
5976
## Preview
6077

6178

src/SimpleLightBoxPlugin.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ public function boot(Panel $panel): void
3939
return $url;
4040
};
4141

42-
ImageColumn::macro('simpleLightbox', macro: function ($url = null) use ($ensureLightBoxUrl) {
42+
ImageColumn::macro('simpleLightbox', macro: function ($url = null, $urlAsDefault = true) use ($ensureLightBoxUrl) {
4343

4444
$url = $ensureLightBoxUrl($url, $this);
4545

4646
$extraAttributes = $this->extraAttributes[0] ?? [];
4747
$extraImgAttributes = $this->extraImgAttributes[0] ?? [];
4848

49+
if ($urlAsDefault) {
50+
$this->defaultImageUrl($url);
51+
}
4952
/** @phpstan-ignore-next-line */
5053
return $this
5154
->openUrlInNewTab()
@@ -54,37 +57,50 @@ public function boot(Panel $panel): void
5457
->extraImgAttributes(array_merge($extraImgAttributes, ['class' => 'simple-light-box-img-indicator']));
5558
});
5659

57-
ImageEntry::macro('simpleLightbox', function ($url = null) use ($ensureLightBoxUrl) {
60+
61+
ImageEntry::macro('simpleLightbox', function ($url = null, $urlAsDefault = true) use ($ensureLightBoxUrl) {
5862

5963
$url = $ensureLightBoxUrl($url, $this);
6064

6165
$extraAttributes = $this->extraAttributes[0] ?? [];
6266
$extraImgAttributes = $this->extraImgAttributes[0] ?? [];
6367

68+
if ($urlAsDefault) {
69+
$this->defaultImageUrl($url);
70+
}
71+
6472
/** @phpstan-ignore-next-line */
6573
return $this
6674
->openUrlInNewTab()
6775
->extraAttributes(array_merge($extraAttributes, ['x-on:click' => 'SimpleLightBox.open(event, \'' . $url . '\')']))
6876
->extraImgAttributes(array_merge($extraImgAttributes, ['class' => 'simple-light-box-img-indicator']));
6977
});
7078

71-
TextColumn::macro('simpleLightbox', function ($url = null) use ($ensureLightBoxUrl) {
79+
TextColumn::macro('simpleLightbox', function ($url = null, $urlAsDefault = true) use ($ensureLightBoxUrl) {
7280

7381
$url = $ensureLightBoxUrl($url, $this);
7482

7583
$extraAttributes = $this->extraAttributes[0] ?? [];
7684

85+
if ($urlAsDefault) {
86+
$this->default($url);
87+
}
88+
7789
/** @phpstan-ignore-next-line */
7890
return $this
7991
->openUrlInNewTab()
8092
->url($url)
8193
->extraAttributes(array_merge($extraAttributes, ['x-on:click' => 'SimpleLightBox.open(event, \'' . $url . '\')']));
8294
});
8395

84-
TextEntry::macro('simpleLightbox', function ($url = null) {
96+
TextEntry::macro('simpleLightbox', function ($url = null, $urlAsDefault = true) {
8597

8698
$extraAttributes = $this->extraAttributes[0] ?? [];
8799

100+
if ($urlAsDefault) {
101+
$this->default($url);
102+
}
103+
88104
/** @phpstan-ignore-next-line */
89105
return $this
90106
->openUrlInNewTab()

0 commit comments

Comments
 (0)