Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve accessibility #112

Merged
merged 3 commits into from
May 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 46 additions & 11 deletions src/baguetteBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
var data = {};
// Array containing temporary images DOM elements
var imagesElements = [];
// The last focused element before opening the overlay
var documentLastFocus = null;
var overlayClickHandler = function(event) {
// Close the overlay when user clicks directly on the background
if (event.target.id.indexOf('baguette-img') !== -1) {
Expand Down Expand Up @@ -115,6 +117,13 @@
touchFlag = false;
};

var trapFocusInsideOverlay = function(event) {
if (overlay.style.display === 'block' && !overlay.contains(event.target)) {
event.stopPropagation();
initFocus();
}
};

// forEach polyfill for IE8
// http://stackoverflow.com/a/14827443/1077846
/* jshint ignore:start */
Expand Down Expand Up @@ -158,7 +167,7 @@
nodeList: galleryNodeList
};
data[selector] = selectorData;

[].forEach.call(galleryNodeList, function(galleryElement) {
if (userOptions && userOptions.filter) {
regex = userOptions.filter;
Expand All @@ -169,7 +178,7 @@
return regex.test(element.href);
});
var gallery = [];

if (tagsNodeList.length === 0) {
return;
}
Expand Down Expand Up @@ -198,7 +207,7 @@
}
}
}

function removeFromCache(selector) {
if (!data.hasOwnProperty(selector)) {
return;
Expand All @@ -208,12 +217,12 @@
[].forEach.call(gallery, function(imageItem) {
unbind(imageItem.imageElement, 'click', imageItem.eventHandler);
});

if (currentGallery === gallery) {
currentGallery = [];
}
});

delete data[selector];
}

Expand All @@ -229,6 +238,7 @@
}
// Create overlay element
overlay = create('div');
overlay.setAttribute('role', 'dialog');
overlay.id = 'baguetteBox-overlay';
document.getElementsByTagName('body')[0].appendChild(overlay);
// Create gallery slider element
Expand Down Expand Up @@ -284,6 +294,7 @@
bind(overlay, 'touchstart', touchstartHandler);
bind(overlay, 'touchmove', touchmoveHandler);
bind(overlay, 'touchend', touchendHandler);
bind(document, 'focus', trapFocusInsideOverlay, true);
}

function unbindEvents() {
Expand All @@ -294,6 +305,7 @@
unbind(overlay, 'touchstart', touchstartHandler);
unbind(overlay, 'touchmove', touchmoveHandler);
unbind(overlay, 'touchend', touchendHandler);
unbind(document, 'focus', trapFocusInsideOverlay, true);
}

function prepareOverlay(gallery, userOptions) {
Expand All @@ -309,14 +321,22 @@
slider.removeChild(slider.firstChild);
}
imagesElements.length = 0;
// Prepare and append images containers

var imagesFiguresIds = [];
var imagesCaptionsIds = [];
// Prepare and append images containers and populate figure and captions IDs arrays
for (var i = 0, fullImage; i < gallery.length; i++) {
fullImage = create('div');
fullImage.className = 'full-image';
fullImage.id = 'baguette-img-' + i;
imagesElements.push(fullImage);

imagesFiguresIds.push('baguetteBox-figure-' + i);
imagesCaptionsIds.push('baguetteBox-figcaption-' + i);
slider.appendChild(imagesElements[i]);
}
overlay.setAttribute('aria-labelledby', imagesFiguresIds.join(' '));
overlay.setAttribute('aria-describedby', imagesCaptionsIds.join(' '));
}

function setOptions(newOptions) {
Expand All @@ -343,7 +363,7 @@
// Set overlay color
try {
overlay.style.backgroundColor = options.overlayBackgroundColor;
} catch(e) {}
} catch (e) {}
}

function showOverlay(chosenImageIndex) {
Expand Down Expand Up @@ -376,6 +396,16 @@
if (options.onChange) {
options.onChange(currentIndex, imagesElements.length);
}
documentLastFocus = document.activeElement;
initFocus();
}

function initFocus() {
if (options.buttons) {
previousButton.focus();
} else {
closeButton.focus();
}
}

function enterFullScreen() {
Expand Down Expand Up @@ -416,6 +446,7 @@
options.afterHide();
}
}, 500);
documentLastFocus.focus();
}

function loadImage(index, callback) {
Expand All @@ -441,6 +472,10 @@
var figure = create('figure');
var image = create('img');
var figcaption = create('figcaption');

figure.id = 'baguetteBox-figure-' + index;
figcaption.id = 'baguetteBox-figcaption-' + index;

imageContainer.appendChild(figure);
// Add loader element
figure.innerHTML = '<div class="baguetteBox-spinner">' +
Expand Down Expand Up @@ -595,18 +630,18 @@
});
}

function bind(element, event, callback) {
function bind(element, event, callback, useCapture) {
if (element.addEventListener) {
element.addEventListener(event, callback, false);
element.addEventListener(event, callback, useCapture);
} else {
// IE8 fallback
element.attachEvent('on' + event, callback);
}
}

function unbind(element, event, callback) {
function unbind(element, event, callback, useCapture) {
if (element.removeEventListener) {
element.removeEventListener(event, callback, false);
element.removeEventListener(event, callback, useCapture);
} else {
// IE8 fallback
element.detachEvent('on' + event, callback);
Expand Down
1 change: 1 addition & 0 deletions src/baguetteBox.scss
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
font: 1.6em sans-serif;
transition: background-color .4s ease;

&:focus,
&:hover {
background-color: rgba(50,50,50,.9);
}
Expand Down