Skip to content

Commit

Permalink
Add caption support.
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
paulstraw committed Jan 13, 2016
1 parent 479a39d commit d73bf08
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ var options = {
namespace: null,
// Which attribute to pull the lightbox image source from.
sourceAttribute: 'href',
// Which attribute to pull the caption from, if any.
captionAttribute: null,
// The event to listen to on the _trigger_ element: triggers opening.
openTrigger: 'click',
// The event to listen to on the _lightbox_ element: triggers closing.
Expand Down
18 changes: 16 additions & 2 deletions dist/luminous-basic.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
}

.lum-lightbox {
background: rgba(0, 0, 0, 0.5);
background: rgba(0, 0, 0, 0.6);
}

.lum-lightbox-inner {
Expand All @@ -53,6 +53,13 @@
position: relative;
}

.lum-lightbox-inner .lum-lightbox-caption {
margin: 0 auto;
color: #fff;
max-width: 700px;
text-align: center;
}

.lum-lightbox-loader {
display: block;
position: absolute;
Expand Down Expand Up @@ -107,11 +114,18 @@

/* This media query makes screens less than 460px wide display in a "fullscreen"-esque mode. Users can then scroll around inside the lightbox to see the entire image. */
@media (max-width: 460px) {
.lum-lightbox-inner {
.lum-lightbox-image-wrapper {
display: block;
overflow: auto;
-webkit-overflow-scrolling: touch;
}

.lum-lightbox-caption {
width: 100%;
position: absolute;
bottom: 0;
}

.lum-lightbox-inner img {
max-width: none;
max-height: none;
Expand Down
2 changes: 1 addition & 1 deletion dist/luminous-basic.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 24 additions & 3 deletions dist/luminous.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var Lightbox = (function () {
this._sizeImgWrapperEl = function () {
var style = _this.imgWrapperEl.style;
style.width = _this.innerEl.clientWidth + 'px';
style.height = _this.innerEl.clientHeight + 'px';
style.height = _this.innerEl.clientHeight - _this.captionEl.clientHeight + 'px';
};

this._completeOpen = function () {
Expand All @@ -56,10 +56,12 @@ var Lightbox = (function () {
var triggerEl = _options$triggerEl === undefined ? (0, _throwIfMissing2.default)() : _options$triggerEl;
var _options$sourceAttrib = options.sourceAttribute;
var sourceAttribute = _options$sourceAttrib === undefined ? (0, _throwIfMissing2.default)() : _options$sourceAttrib;
var _options$captionAttri = options.captionAttribute;
var captionAttribute = _options$captionAttri === undefined ? (0, _throwIfMissing2.default)() : _options$captionAttri;
var _options$includeImgix = options.includeImgixJSClass;
var includeImgixJSClass = _options$includeImgix === undefined ? false : _options$includeImgix;

this.settings = { namespace: namespace, parentEl: parentEl, triggerEl: triggerEl, sourceAttribute: sourceAttribute, includeImgixJSClass: includeImgixJSClass };
this.settings = { namespace: namespace, parentEl: parentEl, triggerEl: triggerEl, sourceAttribute: sourceAttribute, captionAttribute: captionAttribute, includeImgixJSClass: includeImgixJSClass };

if (!(0, _dom.isDOMElement)(this.settings.parentEl)) {
throw new TypeError('`new Lightbox` requires a DOM element passed as `parentEl`.');
Expand Down Expand Up @@ -109,14 +111,27 @@ var Lightbox = (function () {
this.imgEl = document.createElement('img');
positionHelperEl.appendChild(this.imgEl);

this.captionEl = document.createElement('p');
(0, _dom.addClasses)(this.captionEl, this._buildClasses('lightbox-caption'));
positionHelperEl.appendChild(this.captionEl);

this.settings.parentEl.appendChild(this.el);

this._updateImgSrc();
this._updateCaption();

if (this.settings.includeImgixJSClass) {
this.imgEl.classList.add('imgix-fluid');
}
}
}, {
key: '_updateCaption',
value: function _updateCaption() {
var captionAttr = this.settings.captionAttribute;
if (captionAttr) {
this.captionEl.innerText = this.settings.triggerEl.getAttribute(captionAttr);
}
}
}, {
key: '_updateImgSrc',
value: function _updateImgSrc() {
Expand All @@ -139,6 +154,7 @@ var Lightbox = (function () {
// Make sure to re-set the `img` `src`, in case it's been changed
// by someone/something else.
this._updateImgSrc();
this._updateCaption();

(0, _dom.addClasses)(this.el, this.openClasses);

Expand Down Expand Up @@ -230,6 +246,10 @@ var Luminous = (function () {
var
// Which attribute to pull the lightbox image source from.
sourceAttribute = _options$sourceAttrib === undefined ? 'href' : _options$sourceAttrib;
var _options$captionAttri = options.captionAttribute;
var
// Which attribute to pull the caption from, if any.
captionAttribute = _options$captionAttri === undefined ? null : _options$captionAttri;
var _options$openTrigger = options.openTrigger;
var
// The event to listen to on the _trigger_ element: triggers opening.
Expand Down Expand Up @@ -272,7 +292,7 @@ var Luminous = (function () {
// section of README.md for more information.
injectBaseStyles = _options$injectBaseSt === undefined ? true : _options$injectBaseSt;

this.settings = { namespace: namespace, sourceAttribute: sourceAttribute, openTrigger: openTrigger, closeTrigger: closeTrigger, closeWithEscape: closeWithEscape, closeOnScroll: closeOnScroll, appendToSelector: appendToSelector, onOpen: onOpen, onClose: onClose, includeImgixJSClass: includeImgixJSClass, injectBaseStyles: injectBaseStyles };
this.settings = { namespace: namespace, sourceAttribute: sourceAttribute, captionAttribute: captionAttribute, openTrigger: openTrigger, closeTrigger: closeTrigger, closeWithEscape: closeWithEscape, closeOnScroll: closeOnScroll, appendToSelector: appendToSelector, onOpen: onOpen, onClose: onClose, includeImgixJSClass: includeImgixJSClass, injectBaseStyles: injectBaseStyles };

if (this.settings.injectBaseStyles) {
(0, _injectBaseStylesheet2.default)();
Expand All @@ -290,6 +310,7 @@ var Luminous = (function () {
parentEl: document.querySelector(this.settings.appendToSelector),
triggerEl: this.trigger,
sourceAttribute: this.settings.sourceAttribute,
captionAttribute: this.settings.captionAttribute,
includeImgixJSClass: this.settings.includeImgixJSClass
});
}
Expand Down
2 changes: 1 addition & 1 deletion dist/luminous.min.js

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions src/css/luminous-basic.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
}

.lum-lightbox {
background: rgba(0, 0, 0, 0.5);
background: rgba(0, 0, 0, 0.6);
}

.lum-lightbox-inner {
Expand All @@ -53,6 +53,13 @@
position: relative;
}

.lum-lightbox-inner .lum-lightbox-caption {
margin: 0 auto;
color: #fff;
max-width: 700px;
text-align: center;
}

.lum-lightbox-loader {
display: block;
position: absolute;
Expand Down Expand Up @@ -107,11 +114,18 @@

/* This media query makes screens less than 460px wide display in a "fullscreen"-esque mode. Users can then scroll around inside the lightbox to see the entire image. */
@media (max-width: 460px) {
.lum-lightbox-inner {
.lum-lightbox-image-wrapper {
display: block;
overflow: auto;
-webkit-overflow-scrolling: touch;
}

.lum-lightbox-caption {
width: 100%;
position: absolute;
bottom: 0;
}

.lum-lightbox-inner img {
max-width: none;
max-height: none;
Expand Down
18 changes: 16 additions & 2 deletions src/js/Lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ export default class Lightbox {
parentEl = throwIfMissing(),
triggerEl = throwIfMissing(),
sourceAttribute = throwIfMissing(),
captionAttribute = throwIfMissing(),
includeImgixJSClass = false,
} = options;

this.settings = { namespace, parentEl, triggerEl, sourceAttribute, includeImgixJSClass };
this.settings = { namespace, parentEl, triggerEl, sourceAttribute, captionAttribute, includeImgixJSClass };

if (!isDOMElement(this.settings.parentEl)) {
throw new TypeError('`new Lightbox` requires a DOM element passed as `parentEl`.');
Expand Down Expand Up @@ -62,9 +63,14 @@ export default class Lightbox {
this.imgEl = document.createElement('img');
positionHelperEl.appendChild(this.imgEl);

this.captionEl = document.createElement('p');
addClasses(this.captionEl, this._buildClasses('lightbox-caption'));
positionHelperEl.appendChild(this.captionEl);

this.settings.parentEl.appendChild(this.el);

this._updateImgSrc();
this._updateCaption();

if (this.settings.includeImgixJSClass) {
this.imgEl.classList.add('imgix-fluid');
Expand All @@ -74,9 +80,16 @@ export default class Lightbox {
_sizeImgWrapperEl = () => {
let style = this.imgWrapperEl.style;
style.width = `${this.innerEl.clientWidth}px`
style.height = `${this.innerEl.clientHeight}px`
style.height = `${this.innerEl.clientHeight - this.captionEl.clientHeight}px`
};

_updateCaption() {
let captionAttr = this.settings.captionAttribute;
if (captionAttr) {
this.captionEl.innerText = this.settings.triggerEl.getAttribute(captionAttr);
}
}

_updateImgSrc() {
let imageURL = this.settings.triggerEl.getAttribute(this.settings.sourceAttribute);

Expand All @@ -96,6 +109,7 @@ export default class Lightbox {
// Make sure to re-set the `img` `src`, in case it's been changed
// by someone/something else.
this._updateImgSrc();
this._updateCaption();

addClasses(this.el, this.openClasses);

Expand Down
5 changes: 4 additions & 1 deletion src/js/Luminous.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default class Luminous {
namespace = null,
// Which attribute to pull the lightbox image source from.
sourceAttribute = 'href',
// Which attribute to pull the caption from, if any.
captionAttribute = null,
// The event to listen to on the _trigger_ element: triggers opening.
openTrigger = 'click',
// The event to listen to on the _lightbox_ element: triggers closing.
Expand All @@ -49,7 +51,7 @@ export default class Luminous {
injectBaseStyles = true,
} = options

this.settings = { namespace, sourceAttribute, openTrigger, closeTrigger, closeWithEscape, closeOnScroll, appendToSelector, onOpen, onClose, includeImgixJSClass, injectBaseStyles };
this.settings = { namespace, sourceAttribute, captionAttribute, openTrigger, closeTrigger, closeWithEscape, closeOnScroll, appendToSelector, onOpen, onClose, includeImgixJSClass, injectBaseStyles };

if (this.settings.injectBaseStyles) {
injectBaseStylesheet();
Expand Down Expand Up @@ -109,6 +111,7 @@ export default class Luminous {
parentEl: document.querySelector(this.settings.appendToSelector),
triggerEl: this.trigger,
sourceAttribute: this.settings.sourceAttribute,
captionAttribute: this.settings.captionAttribute,
includeImgixJSClass: this.settings.includeImgixJSClass,
});
}
Expand Down
12 changes: 6 additions & 6 deletions test/lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ describe('Lightbox', () => {
expect(() => {
let triggerEl = document.querySelector('.test-anchor');

new Lightbox({namespace: 'test', parentEl: document.body, triggerEl: triggerEl, sourceAttribute: 'href'});
new Lightbox({namespace: 'test', parentEl: document.body, triggerEl: triggerEl, sourceAttribute: 'href', captionAttribute: null});
}).not.toThrowError();
});

it('throws if passed `parentEl` is not a DOM element', () => {
expect(() => {
let triggerEl = document.querySelector('.test-anchor');

new Lightbox({namespace: 'test', parentEl: '.not-an-element', triggerEl: triggerEl, sourceAttribute: 'href'});
new Lightbox({namespace: 'test', parentEl: '.not-an-element', triggerEl: triggerEl, sourceAttribute: 'href', captionAttribute: null});
}).toThrowError(TypeError, '`new Lightbox` requires a DOM element passed as `parentEl`.');
});

it('assigns the correct class to its element', () => {
let triggerEl = document.querySelector('.test-anchor');

let lightbox = new Lightbox({namespace: 'test-namespace', parentEl: document.body, triggerEl: triggerEl, sourceAttribute: 'href'});
let lightbox = new Lightbox({namespace: 'test-namespace', parentEl: document.body, triggerEl: triggerEl, sourceAttribute: 'href', captionAttribute: null});
lightbox.open();
lightbox.close();

Expand All @@ -61,7 +61,7 @@ describe('Lightbox', () => {

let triggerEl = document.querySelector('.test-anchor');

let lightbox = new Lightbox({namespace: 'lum', parentEl: demoDiv, triggerEl: triggerEl, sourceAttribute: 'href'});
let lightbox = new Lightbox({namespace: 'lum', parentEl: demoDiv, triggerEl: triggerEl, sourceAttribute: 'href', captionAttribute: null});
lightbox.open();
lightbox.close();

Expand All @@ -71,7 +71,7 @@ describe('Lightbox', () => {
it('cleans up its element when destroyed', () => {
let triggerEl = document.querySelector('.test-anchor');

let lightbox = new Lightbox({namespace: 'to-destroy', parentEl: document.body, triggerEl: triggerEl, sourceAttribute: 'href'});
let lightbox = new Lightbox({namespace: 'to-destroy', parentEl: document.body, triggerEl: triggerEl, sourceAttribute: 'href', captionAttribute: null});
lightbox.open();
lightbox.close();
lightbox.destroy();
Expand All @@ -82,7 +82,7 @@ describe('Lightbox', () => {
it('adds the `imgix-fluid` param if configured', () => {
let triggerEl = document.querySelector('.test-anchor');

let lightbox = new Lightbox({namespace: 'fluid', parentEl: document.body, triggerEl: triggerEl, sourceAttribute: 'href', includeImgixJSClass: true});
let lightbox = new Lightbox({namespace: 'fluid', parentEl: document.body, triggerEl: triggerEl, sourceAttribute: 'href', captionAttribute: null, includeImgixJSClass: true});
lightbox.open();
lightbox.close();

Expand Down

0 comments on commit d73bf08

Please sign in to comment.