Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add A image lightbox in imageColumn #9501

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/tables/docs/03-columns/04-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ ImageColumn::make('avatar')
->defaultImageUrl(url('/images/placeholder.png'))
```

## Image Lightbox

You can display a image lightbox by using `lightbox()`:

```php
use Filament\Tables\Columns\ImageColumn;

ImageColumn::make('header_image')
->lightbox()
```

## Stacking images

You may display multiple images as a stack of overlapping images by using `stacked()`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
$isCircular = $isCircular();
$isSquare = $isSquare();
$isStacked = $isStacked();
$hasLightbox = $hasLightBox() ?? false;
$overlap = $isStacked ? ($getOverlap() ?? 2) : null;
$ring = $isStacked ? ($getRing() ?? 2) : null;
$height = $getHeight() ?? ($isStacked ? '2rem' : '2.5rem');
Expand Down Expand Up @@ -41,6 +42,7 @@
@endphp

<div
@if($hasLightbox) x-data="SimpleImageLightBox" @endif
{{
$attributes
->merge($getExtraAttributes(), escape: false)
Expand Down Expand Up @@ -71,6 +73,7 @@
>
@foreach ($limitedState as $stateItem)
<img
@if($hasLightbox) @click="openSimpleImageLightBox($event)" @endif
src="{{ filled($stateItem) ? $getImageUrl($stateItem) : $defaultImageUrl }}"
{{
$getExtraImgAttributeBag()
Expand Down
53 changes: 53 additions & 0 deletions packages/tables/resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -1119,4 +1119,57 @@ class="p-4 sm:px-6"
</x-filament-tables::container>

<x-filament-actions::modals />

<script>
document.addEventListener('alpine:init', () => {
Alpine.data('SimpleImageLightBox', () => ({
openSimpleImageLightBox(e) {
e.preventDefault()
let src = e.target.src
let lightboxDiv = document.createElement('div')
lightboxDiv.innerHTML = `
<div
id="SimpleImageLightBox"
@keydown.escape.window="document.querySelector('#SimpleImageLightBox').remove()"
class="fixed inset-0 flex items-center justify-center p-4 bg-black/50 bg-opacity-75"
style="z-index: 10000;"
>
<div class="relative bg-white max-h-full max-w-2xl p-6 mx-auto rounded shadow"
onclick="event.stopPropagation()"
>
<img src="${src}" alt="Lightbox image" class="object-cover w-full h-full rounded" />
<button
class="absolute p-2 m-2 text-white rounded-full focus:outline-none"
style="top:0; right:0"
>
✖️
</button>
</div>
</div>
`
lightboxDiv.addEventListener('click', () =>
this.closeSimpleImageLightBox(),
)
lightboxDiv
.querySelector('button')
.addEventListener('click', (e) => {
e.stopPropagation()
this.closeSimpleImageLightBox()
})
lightboxDiv
.querySelector('div')
.addEventListener('keydown', (e) => {
if (e.key === 'Escape')
this.closeSimpleImageLightBox()
})

this.lightboxDiv = lightboxDiv
document.body.appendChild(lightboxDiv)
},
closeSimpleImageLightBox() {
this.lightboxDiv.remove()
},
}))
})
</script>
</div>
14 changes: 14 additions & 0 deletions packages/tables/src/Columns/ImageColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class ImageColumn extends Column

protected string | Closure | null $limitedRemainingTextSize = null;

protected bool | Closure $lightbox = false;

public function disk(string | Closure | null $disk): static
{
$this->disk = $disk;
Expand Down Expand Up @@ -283,6 +285,18 @@ public function getRing(): ?int
return $this->evaluate($this->ring);
}

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

return $this;
}

public function hasLightBox(): ?bool
{
return $this->evaluate($this->lightbox);
}

public function limit(int | Closure | null $limit = 3): static
{
$this->limit = $limit;
Expand Down