Skip to content

Commit

Permalink
Add styles for the gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Dec 5, 2023
1 parent c37ec66 commit 122d28d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 35 deletions.
10 changes: 10 additions & 0 deletions assets/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions site/snippets/blocks/gallery.php
Original file line number Diff line number Diff line change
@@ -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>
55 changes: 20 additions & 35 deletions site/snippets/blocks/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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();
$src = null;

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>
Expand Down
12 changes: 12 additions & 0 deletions site/snippets/image.php
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 122d28d

Please sign in to comment.