Skip to content

Commit 2017ab4

Browse files
authored
images: wrap images in a lightbox (#63)
* images: wrap images in a lightbox Wrap images in a lightbox, increasing them to full-screen size when clicked. There are JavaScript solutions available (e.g. lightbox2)[^0], though I opted for a purely CSS-based approach.[^1] [^0]: https://lokeshdhakar.com/projects/lightbox2/ [^1]: https://codesalad.dev/blog/how-to-create-an-image-lightbox-in-pure-css-25 Signed-off-by: Luca Zeuch <[email protected]> * perf: lazy-load lightbox duplicate The implementation of a lightbox requires that the image in question is rendered twice, once in the actual content and a second time as an off-screen image. Defer loading of that off-screen duplicate until it is needed. Signed-off-by: Luca Zeuch <[email protected]> * layouts/images: allow opt-out from lightbox Authors can now pass `lightbox=false` as a query parameter with their image URL to disable the lightbox effect; this is especially useful for smaller images that do not have a lot of detail. Signed-off-by: Luca Zeuch <[email protected]> * images: center lightbox on both axes when shown Signed-off-by: Luca Zeuch <[email protected]> --------- Signed-off-by: Luca Zeuch <[email protected]>
1 parent 4da89cb commit 2017ab4

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

assets/scss/components/_images.scss

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,42 @@ figcaption {
2525
margin-top: 0.5rem;
2626
font-style: italic;
2727
}
28+
29+
/* CSS-only lightbox. See https://codesalad.dev/blog/how-to-create-an-image-lightbox-in-pure-css-25 */
30+
.lightbox {
31+
display: none;
32+
33+
position: fixed;
34+
z-index: 99999;
35+
top: 0;
36+
left: 0;
37+
right: 0;
38+
bottom: 0;
39+
40+
padding: 1em;
41+
42+
background: rgba(0, 0, 0, 0.85);
43+
44+
/* center that thang, ya get me? */
45+
justify-content: center;
46+
align-items: center;
47+
text-align: center;
48+
white-space: nowrap;
49+
}
50+
51+
/* Unhide the lightbox when it's the target */
52+
.lightbox:target {
53+
display: flex;
54+
}
55+
56+
.lightbox span {
57+
/* Full width and height */
58+
display: block;
59+
width: 100%;
60+
height: 100%;
61+
62+
/* Size and position background image */
63+
background-position: center;
64+
background-repeat: no-repeat;
65+
background-size: contain;
66+
}

layouts/_default/_markup/render-image.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,33 @@
108108
{{- end }}
109109
{{- end }}
110110

111+
{{- $lightbox := true }}
112+
{{- if $u.RawQuery }}
113+
{{- if $u.Query.Has "lightbox" }}
114+
{{- $lightbox = ne ($u.Query.Get "lightbox") "false" }}
115+
{{- end }}
116+
{{- end }}
117+
111118
{{- /* Render image element. */ -}}
119+
{{- if $lightbox -}}
120+
<a href="#{{ print $id "-lightbox" }}">
121+
{{- end }}
112122
<img
113123
{{- range $k, $v := $attrs }}
114124
{{- if or $v (eq $k "alt") }}
115125
{{- printf " %s=%q" $k $v | safeHTMLAttr }}
116126
{{- end }}
117127
{{- end -}}
118128
>
129+
{{- if $lightbox -}}
130+
</a>
131+
<a href="javascript:history.back();" class="lightbox" id="{{ print $id "-lightbox" }}">
132+
<img loading="lazy"
133+
{{- range $k, $v := $attrs }}
134+
{{- if or $v (eq $k "alt") }}
135+
{{- printf " %s=%q" $k $v | safeHTMLAttr }}
136+
{{- end }}
137+
{{- end -}}
138+
>
139+
</a>
140+
{{- end }}

0 commit comments

Comments
 (0)