Skip to content

Added accessibility features for tabbing and aria labels #257

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
63 changes: 59 additions & 4 deletions src/vex.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ var addClasses = function addClasses (el, classStr) {
var animationEndEvent = (function detectAnimationEndEvent () {
var el = document.createElement('div')
var eventNames = {
'animation': 'animationend',
'WebkitAnimation': 'webkitAnimationEnd',
'MozAnimation': 'animationend',
'OAnimation': 'oanimationend',
'msAnimation': 'MSAnimationEnd'
'msAnimation': 'MSAnimationEnd',
'animation': 'animationend'
}
for (var i in eventNames) {
if (el.style[i] !== undefined) {
Expand Down Expand Up @@ -162,6 +162,12 @@ var vex = {
if (Object.keys(vexes).length === 0) {
document.body.classList.remove(baseClassNames.open)
}
//Give focus back to initial element
if (options.giveBackFocus) {
focusedElementBefore.focus();
}
document.querySelector(options.ariaContentMain).setAttribute('aria-hidden', false)

}.bind(this)

// Close the vex
Expand Down Expand Up @@ -208,6 +214,7 @@ var vex = {
// Overlay
var overlayEl = vexInstance.overlayEl = document.createElement('div')
overlayEl.classList.add(baseClassNames.overlay)
overlayEl.tabIndex = -1
addClasses(overlayEl, options.overlayClassName)
if (options.overlayClosesOnClick) {
rootEl.addEventListener('click', function overlayClickListener (e) {
Expand All @@ -221,13 +228,58 @@ var vex = {
// Content
var contentEl = vexInstance.contentEl = document.createElement('div')
contentEl.classList.add(baseClassNames.content)
contentEl.setAttribute('role', 'dialog')
contentEl.setAttribute('aria-hidden', false)
contentEl.setAttribute('aria-labelledBy', options.message)
addClasses(contentEl, options.contentClassName)
contentEl.appendChild(options.content instanceof window.Node ? options.content : domify(options.content))
rootEl.appendChild(contentEl)

//Accessibility features
//Get initial element
var focusedElementBefore = document.activeElement;

document.querySelector(options.ariaContentMain).setAttribute('aria-hidden', true)

if (options.trapTabKey) {
contentEl.onkeydown = function(event) {
var focusableElementsString = "a[href], area[href], input:not([disabled]), select:not([disabled]), textarea, button, iframe, object, embed, *[tabindex], *[contenteditable]"
var focusableElements = contentEl.querySelectorAll(focusableElementsString)

//Tab key pressed
if (event.which == 9) {
console.log(focusableElements.length)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep console.log's out of production code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conole.log's have been removed

var visibleFocusableElements = focusableElements

var focusedElement = document.activeElement
var numberOfFocusableELements = visibleFocusableElements.length

var focusedElementIndex = Array.prototype.indexOf.call(visibleFocusableElements, focusedElement)

if (event.shiftKey) {
//back tab
// if focused on first item and user preses back-tab, go to the last focusable item
if (focusedElementIndex == 0) {
visibleFocusableElements.item(numberOfFocusableELements - 1).focus()
event.preventDefault()
}
} else {
console.log("tab overrided")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep console.log's out of production code.

//forward tab
// if focused on the last item and user preses tab, go to the first focusable item
if (focusedElementIndex == numberOfFocusableELements - 1) {
visibleFocusableElements.item(0).focus();
event.preventDefault();
}
}
}
}
}

// Close button
if (options.showCloseButton) {
var closeEl = vexInstance.closeEl = document.createElement('div')
var closeEl = vexInstance.closeEl = document.createElement('button')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a sanity check on our CSS, does this break any styles?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changed the appearance of several styles so it was changed back to div

closeEl.setAttribute('aria-label', 'close')
closeEl.classList.add(baseClassNames.close)
addClasses(closeEl, options.closeClassName)
closeEl.addEventListener('click', vexInstance.close.bind(vexInstance))
Expand Down Expand Up @@ -319,7 +371,10 @@ vex.defaultOptions = {
overlayClassName: '',
contentClassName: '',
closeClassName: '',
closeAllOnPopState: true
closeAllOnPopState: true,
giveBackFocus: true,
trapTabKey: true,
ariaContentMain: 'body:not([vex])'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the original PR, this selector was 'body > :not(.vex)'. The selector you have selects a very different thing. What's the reason for the change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ariaContentMain was changed back to the correct selector

}

// TODO Loading symbols?
Expand Down