Skip to content

Commit 6b11c5c

Browse files
committed
client: Switch to yet-another-react-lightbox
It is bit more maintained and looks better.
1 parent 28acf11 commit 6b11c5c

4 files changed

Lines changed: 64 additions & 36 deletions

File tree

assets/js/templates/Item.jsx

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import PropTypes from 'prop-types';
33
import { Link, useHistory, useLocation } from 'react-router-dom';
44
import { usePreviousImmediate } from 'rooks';
@@ -12,30 +12,26 @@ import * as icons from '../icons';
1212
import { ConfigurationContext } from '../helpers/configuration';
1313
import { LocalizationContext } from '../helpers/i18n';
1414
import { useSharers } from '../sharers';
15-
import SimpleLightbox from 'simplelightbox/dist/simple-lightbox.esm';
15+
import Lightbox from 'yet-another-react-lightbox';
1616

17-
function setupLightbox(element) {
17+
function setupLightbox({
18+
element,
19+
setSlides,
20+
setSelectedPhotoIndex,
21+
}) {
1822
let images = element.querySelectorAll('a[href$=".jpg"], a[href$=".jpeg"], a[href$=".png"], a[href$=".gif"], a[href$=".jpg:large"], a[href$=".jpeg:large"], a[href$=".png:large"], a[href$=".gif:large"]');
1923

20-
if (images.length === 0) {
21-
return;
22-
}
23-
const gallery = new SimpleLightbox(
24-
images,
25-
{
26-
loop: false,
27-
// react-router already handles this.
28-
history: false,
29-
},
30-
);
24+
setSlides(Array.from(images).map((link, index) => {
25+
link.addEventListener('click', (event) => {
26+
event.preventDefault();
3127

32-
gallery.on('show.simplelightbox', function() {
33-
selfoss.lightboxActive.update(true);
34-
});
28+
setSelectedPhotoIndex(index);
29+
});
3530

36-
gallery.on('close.simplelightbox', function() {
37-
selfoss.lightboxActive.update(false);
38-
});
31+
return {
32+
src: link.getAttribute('href'),
33+
};
34+
}));
3935
}
4036

4137
function stopPropagation(event) {
@@ -252,6 +248,9 @@ export default function Item({ currentTime, item, selected, expanded, setNavExpa
252248
const previouslyExpanded = usePreviousImmediate(expanded);
253249
const configuration = React.useContext(ConfigurationContext);
254250

251+
const [slides, setSlides] = useState([]);
252+
const [selectedPhotoIndex, setSelectedPhotoIndex] = useState(null);
253+
255254
React.useEffect(() => {
256255
// Handle entry becoming/ceasing to be expanded.
257256
const parent = document.querySelector(`.entry[data-entry-id="${item.id}"]`);
@@ -301,7 +300,11 @@ export default function Item({ currentTime, item, selected, expanded, setNavExpa
301300
} else {
302301
if (firstExpansion) {
303302
// setup fancyBox image viewer
304-
setupLightbox(contentBlock.current);
303+
setupLightbox({
304+
element: contentBlock.current,
305+
setSlides,
306+
setSelectedPhotoIndex,
307+
});
305308
}
306309

307310
// scroll to article header
@@ -501,6 +504,23 @@ export default function Item({ currentTime, item, selected, expanded, setNavExpa
501504

502505
{/* content */}
503506
<div className={classNames({'entry-content': true, 'entry-content-nocolumns': item.lengthWithoutTags < 500})}>
507+
<Lightbox
508+
open={selectedPhotoIndex !== null}
509+
index={selectedPhotoIndex}
510+
close={() => setSelectedPhotoIndex(null)}
511+
carousel={{
512+
finite: true,
513+
}}
514+
controller={{
515+
closeOnBackdropClick: true,
516+
}}
517+
on={{
518+
entered: () => selfoss.lightboxActive.update(true),
519+
exited: () => selfoss.lightboxActive.update(false),
520+
}}
521+
slides={slides}
522+
/>
523+
504524
<div ref={contentBlock} />
505525

506526
<div className="entry-smartphone-share">

assets/package-lock.json

Lines changed: 20 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
"react-router-dom": "^5.2.0",
2727
"reset-css": "^5.0.1",
2828
"rooks": "^7.1.1",
29-
"simplelightbox": "^2.10.3",
3029
"tinykeys": "^1.1.1",
3130
"unreset-css": "^1.0.1",
32-
"use-state-with-deps": "^1.1.1"
31+
"use-state-with-deps": "^1.1.1",
32+
"yet-another-react-lightbox": "^2.1.1"
3333
},
3434
"devDependencies": {
3535
"@parcel/packager-raw-url": "^2.0.0",

assets/styles/main.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@use 'sass:color';
22

3-
@import 'npm:simplelightbox/src/simple-lightbox';
3+
@import 'npm:yet-another-react-lightbox/dist/styles.css';
44
@import 'npm:reset-css/sass/reset';
55
@import 'mixins/visually-hidden';
66

0 commit comments

Comments
 (0)