Skip to content

Commit 517bf4a

Browse files
authored
Merge pull request #14 from Doctave/lightbox
Lightbox implementation
2 parents ccab92c + f94e5b7 commit 517bf4a

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

crates/libdoctave/src/templates/components/markdown/image.html.jinja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
src="{{ node.kind.data.url }}"
33
alt="{{ node.kind.data.alt }}"
44
title="{{ node.kind.data.title }}"
5+
data-zoomable="true"
56
/>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.app-lightbox {
2+
position: fixed;
3+
top: 0;
4+
z-index: 99999;
5+
width: 100%;
6+
height: 100%;
7+
pointer-events: none;
8+
cursor: zoom-out;
9+
background-color: var(--bg-color, hsl(0, 0%, 0%, 0.85));
10+
opacity: 0;
11+
transition: opacity 0.35s;
12+
}
13+
14+
.app-lightbox.is-active {
15+
pointer-events: initial;
16+
opacity: 1;
17+
}
18+
19+
:global([data-zoomable]) {
20+
cursor: zoom-in;
21+
}
22+
23+
.portal {
24+
display: flex;
25+
align-items: center;
26+
justify-content: center;
27+
width: 100%;
28+
height: 100%;
29+
transition: opacity 0.35s, transform 0.35s;
30+
transform: scale(0.2);
31+
}
32+
33+
.app-lightbox.is-active .portal {
34+
transform: scale(0.75);
35+
}
36+
37+
@media (max-width: 1024px) {
38+
.app-lightbox.is-active .portal {
39+
transform: scale(0.9);
40+
}
41+
}
42+
43+
.portal img {
44+
width: 100%;
45+
height: auto;
46+
border-radius: var(--radius-6);
47+
}

crates/libdoctave/src/templates/css-prose.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@
115115
border-radius: initial;
116116
margin-top: initial;
117117
margin-bottom: initial;
118+
cursor: initial;
118119
{% else %}
119120
max-width: 100%;
120121
border-radius: var(--space-2);
121122
margin-top: calc(2em * var(--prose-scale));
122123
margin-bottom: calc(2em * var(--prose-scale));
124+
cursor: zoom-in;
123125
{% endif %}
124126
}
125127

@@ -381,6 +383,7 @@
381383
{% if not reset %}
382384
margin-top: 0;
383385
margin-bottom: 0;
386+
cursor: zoom-in;
384387
{% endif %}
385388
}
386389

crates/libdoctave/src/templates/css.html.jinja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,5 +668,6 @@
668668
{% include "css-builtin-components.css" %}
669669
{% include "css-openapi.css" %}
670670
{% include "css-search-modal.css" %}
671+
{% include "css-lightbox.css" %}
671672
</style>
672673
{# prettier-ignore-end #}

crates/libdoctave/src/templates/layouts/base.html.jinja

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,7 @@
145145
{% endif %}
146146
</div>
147147
</div>
148+
149+
{% include "lightbox.html.jinja" %}
148150
</body>
149151
</html>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<div class="app-lightbox">
2+
<div class="portal"></div>
3+
</div>
4+
5+
<script>
6+
const lbTriggers = document.querySelectorAll('[data-zoomable="true"]');
7+
8+
const lightbox = document.querySelector('.app-lightbox');
9+
const portal = lightbox.querySelector('.portal');
10+
11+
for (const trig of lbTriggers) {
12+
trig.addEventListener('click', () => {
13+
portal.innerHTML = trig.outerHTML;
14+
setTimeout(() => {
15+
const img = portal.querySelectorAll('img')[0];
16+
const ratio = img.naturalWidth / img.naturalHeight;
17+
img.sizes = `${window.innerHeight * ratio}px`;
18+
}, 352);
19+
lightbox.classList.add('is-active');
20+
document.documentElement.classList.add('scrollIsLocked');
21+
});
22+
}
23+
24+
lightbox.addEventListener('click', () => {
25+
lightbox.classList.remove('is-active');
26+
document.documentElement.classList.remove('scrollIsLocked');
27+
});
28+
29+
document.addEventListener('keydown', (e) => {
30+
if (e.key === 'Escape') {
31+
lightbox.classList.remove('is-active');
32+
document.documentElement.classList.remove('scrollIsLocked');
33+
}
34+
});
35+
</script>

0 commit comments

Comments
 (0)