Skip to content

Commit

Permalink
LuminousGallery onChange callback
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendolenLynch authored and frederickfogerty committed Nov 10, 2021
1 parent bfa5447 commit 96863e0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ LuminousGallery supports two sets of options arguments. The first set is specifi
```javascript
var galleryOpts = {
// Whether pressing the arrow keys should move to the next/previous slide.
arrowNavigation: true
arrowNavigation: true,
// A callback triggered when the image changes that is passed the image HTML element
onChange: ({ imgEl }) => { … },
};

var luminousOpts = {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"homepage": "https://github.com/imgix/luminous#readme",
"contributors": [
"Frederick Fogerty <[email protected]> (https://github.com/frederickfogerty)",
"Jakub Duras <[email protected]> (https://duras.me)"
"Jakub Duras <[email protected]> (https://duras.me)",
"Gwendolen Lynch <[email protected]> (https://github.com/GwendolenLynch)"
],
"main": "lib/lum.js",
"module": "es/lum.js",
Expand Down
2 changes: 2 additions & 0 deletions src/js/Lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ export default class Lightbox {
this._updateImgSrc();
this._updateCaption();
this._sizeImgWrapperEl();
this.settings._gallery.onChange({ imgEl: this.imgEl });
}

/**
Expand All @@ -321,6 +322,7 @@ export default class Lightbox {
this._updateImgSrc();
this._updateCaption();
this._sizeImgWrapperEl();
this.settings._gallery.onChange({ imgEl: this.imgEl });
}

/**
Expand Down
15 changes: 14 additions & 1 deletion src/js/LuminousGallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default class LuminousGallery {
*/
constructor(triggers, options = {}, luminousOpts = {}) {
const optionsDefaults = {
arrowNavigation: true
arrowNavigation: true,
onChange: null,
};

this.settings = Object.assign({}, optionsDefaults, options);
Expand Down Expand Up @@ -68,6 +69,18 @@ export default class LuminousGallery {
: this.triggers[prevTriggerIndex];
}

/**
* Callback called when current image is changed
* @param {Object} params
* @param {!Element} params.imgEl New image element
*/
onChange({ imgEl }) {
const onChange = this.settings.onChange;
if (onChange && typeof onChange === "function") {
onChange({ imgEl });
}
}

/**
* Destroys the internal luminous instances
* @return {void}
Expand Down
34 changes: 34 additions & 0 deletions test/testLuminousGallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,38 @@ describe("LuminousGallery", () => {

expect(document.body.querySelector(".lum-img").src).toBe(genLink(0));
});
it("should call onChange when current image moves right", () => {
let current = null;
new LuminousGallery(document.querySelectorAll(".test-gallery-anchor"), {
arrowNavigation: true,
onChange: ({ imgEl }) => current = imgEl,
});

openLuminous(0);

const nextButtonEl = document.body.querySelector(".lum-next-button");
if (!nextButtonEl) {
throw new Error("Navigation button not found in DOM.");
}
nextButtonEl.click();

expect(current.src).toBe(genLink(1));
});
it("should call onChange when current image moves left", () => {
let current = null;
new LuminousGallery(document.querySelectorAll(".test-gallery-anchor"), {
arrowNavigation: true,
onChange: ({ imgEl }) => current = imgEl,
});

openLuminous(1);

const prevButtonEl = document.body.querySelector(".lum-previous-button");
if (!prevButtonEl) {
throw new Error("Navigation button not found in DOM.");
}
prevButtonEl.click();

expect(current.src).toBe(genLink(0));
});
});

0 comments on commit 96863e0

Please sign in to comment.