Add styles for the gallery

This commit is contained in:
Bastian Allgeier
2023-12-05 13:03:59 +01:00
parent c37ec66ab6
commit 122d28dbcb
4 changed files with 64 additions and 34 deletions

View File

@@ -239,6 +239,16 @@ h1, h2, h3, h4, h5, h6 {
padding-top: .75rem;
color: var(--color-text-grey);
}
.text figure ul {
line-height: 0;
display: grid;
gap: 1.5rem;
margin: 0;
grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
}
.text figure ul li {
list-style: none;
}
hr {
border: 0;

View File

@@ -0,0 +1,23 @@
<?php
/** @var \Kirby\Cms\Block $block */
?>
<figure class="gallery">
<ul>
<?php foreach ($block->images()->toFiles() as $image): ?>
<li>
<?php snippet('image', [
'alt' => $image->alt(),
'contain' => $block->crop()->isTrue(),
'lightbox' => true,
'ratio' => $block->ratio()->or('auto'),
'src' => $image->url(),
]) ?>
</li>
<?php endforeach ?>
</ul>
<?php if ($block->caption()->isNotEmpty()): ?>
<figcaption>
<?= $block->caption() ?>
</figcaption>
<?php endif ?>
</figure>

View File

@@ -13,47 +13,32 @@
https://getkirby.com/docs/guide/templates/snippets
*/
$alt = $block->alt();
$caption = $block->caption();
$contain = $block->crop()->isFalse();
$link = $block->link();
$ratio = $block->ratio()->or('auto');
$class = $ratio != 'auto' ? 'img' : 'auto';
$src = null;
$lightbox = $link->isEmpty();
$src = null;
if ($block->location() == 'web') {
$src = $block->src();
$srcValue = $src->escape('attr');
} elseif ($image = $block->image()->toFile()) {
$alt = $alt->or($image->alt());
$src = $srcValue = $image->url();
if ($block->location()->value() === 'web') {
$alt = $block->alt();
$src = $block->src();
} else if ($image = $block->image()->toFile()) {
$alt = $block->alt()->or($image->alt());
$src = $image->url();
}
if ($ratio !== 'auto') {
$ratio = Str::split($ratio, '/');
$w = $ratio[0] ?? 1;
$h = $ratio[1] ?? 1;
}
$attrs = attr([
'class' => $class,
'data-contain' => $contain,
'data-lightbox' => $lightbox,
'href' => $link->or($src),
'style' => '--w:' . $w . '; --h:' . $h,
]);
?>
<?php if ($srcValue): ?>
<?php if ($src): ?>
<figure>
<a <?= $attrs ?>>
<img src="<?= $srcValue ?>" alt="<?= esc($alt, 'attr') ?>">
</a>
<?php if ($caption->isNotEmpty()): ?>
<?php snippet('image', [
'alt' => $alt,
'contain' => $block->crop()->isFalse(),
'lightbox' => $block->link()->isEmpty(),
'href' => $block->link()->or($src),
'src' => $src,
'ratio' => $block->ratio()->or('auto')
]) ?>
<?php if ($block->caption()->isNotEmpty()): ?>
<figcaption class="img-caption">
<?= $caption ?>
<?= $block->caption() ?>
</figcaption>
<?php endif ?>
</figure>

12
site/snippets/image.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
$attrs = attr([
'data-contain' => $contain ?? false,
'data-lightbox' => $lightbox ?? false,
'href' => $href ?? $src,
]);
?>
<a <?= $attrs ?>>
<img src="<?= esc($src, 'attr') ?>" alt="<?= esc($alt, 'attr') ?>" style="aspect-ratio: <?= $ratio ?? 'auto' ?>">
</a>