Skip to content

Commit

Permalink
fix: improved screenreader experience
Browse files Browse the repository at this point in the history
  • Loading branch information
NickColley authored and christophwitzko committed Mar 21, 2016
1 parent d6cb35d commit e77e2db
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
2 changes: 1 addition & 1 deletion public/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tracker</title>
<title>Tracker | About</title>
<link rel="shortcut icon" href="assets/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="assets/app.css" />
</head>
Expand Down
14 changes: 11 additions & 3 deletions public/account.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tracker</title>
<title>Tracker | Account</title>
<link rel="shortcut icon" href="assets/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="assets/app.css" />
</head>
Expand All @@ -25,7 +25,10 @@ <h1><a href="/">Tracker</a></h1>
</header>

<main>
<a href="/">← Return to the tracker</a>
<a href="/">
<span aria-hidden="true"></span>
Return to the tracker
</a>

<div data-show="signin" data-hide-if="signed-in">
<form class="signup" action="signup">
Expand All @@ -50,7 +53,10 @@ <h2>Sign up</h2>
<form class="signin" action="signin">
<div class="signup-link">
<h3>New account?</h3>
<a href="#" class="btn" data-action="show-signup">click here to sign up!</a>
<a href="#" class="btn" data-action="show-signup">
<span class="sr-only">Need an account?</span>
Sign up!
</a>
</div>

<h2>Log in</h2>
Expand Down Expand Up @@ -142,6 +148,8 @@ <h2>Account settings</h2>
</div>
</main>

<span id="a11y-announcer" class="sr-only" aria-live="polite"></span>
<script src="assets/js/a11y-announce.js"></script>
<script src="/hoodie/client.js"></script>
<script src="assets/js/common.js"></script>
<script src="assets/js/account.js"></script>
Expand Down
12 changes: 12 additions & 0 deletions public/assets/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,15 @@ img.low-profile-dog {
height: 160px;
float: right;
}

/* Show content for screenreaders only */
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
border: 0;
}
15 changes: 15 additions & 0 deletions public/assets/js/a11y-announce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
!(function (global, document) {
var announceTimer
var $announcer = document.getElementById('a11y-announcer')
var announce = function (message, tone) {
tone = tone || 'polite'
$announcer.setAttribute('aria-live', 'off')
$announcer.innerHTML = ''
clearTimeout(announceTimer)
announceTimer = setTimeout(function () {
$announcer.setAttribute('aria-live', tone)
$announcer.innerHTML = message
}, 500)
}
global.announce = announce
})(window, document)
36 changes: 35 additions & 1 deletion public/assets/js/account.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var originalDocumentTitle = document.title
var locationHash = window.location.hash.replace('#', '')

/**
* Find all elements with a data-show attribute and apply a click handler
* in which we check if an element with a data-action attribute was clicked.
Expand All @@ -14,8 +17,14 @@
}

event.preventDefault()
$formsContainer.dataset.show = action.substr('show-'.length)
var showTarget = action.substr('show-'.length)
$formsContainer.dataset.show = showTarget
setHashState(showTarget)
})
if (locationHash) {
el.dataset.show = locationHash
setHashState(locationHash)
}
})

/**
Expand All @@ -39,6 +48,8 @@ document.querySelector('form.signup').addEventListener('submit', function (event
})
})

.then(setHashState)

.catch(handleError)
})

Expand All @@ -56,6 +67,8 @@ document.querySelector('form.signin').addEventListener('submit', function (event
password: password
})

.then(setHashState)

.catch(handleError)
})

Expand Down Expand Up @@ -90,6 +103,7 @@ document.querySelector('form.password-reset').addEventListener('submit', functio
.then(function () {
alert('Email sent to ' + email)
document.querySelector('[data-show="password-reset"]').dataset.show = 'signin'
setHashState('signin')
})

.catch(handleError)
Expand Down Expand Up @@ -134,6 +148,8 @@ document.querySelector('[data-action="show-password-reset"]').addEventListener('
document.querySelector('[data-action=signout]').addEventListener('click', function (event) {
event.preventDefault()
hoodie.account.signOut()

.then(setHashState)
})

/**
Expand All @@ -149,4 +165,22 @@ function handleError (error) {
alert(error)
}

function setHashState (hash) {
var url = window.location.origin + window.location.pathname
var title = originalDocumentTitle
if (typeof hash === 'string') {
url += '#' + hash
var formattedHash = hash
.split('-')
.map(function (string) {
return string.charAt(0).toUpperCase() + string.slice(1)
})
.join(' ')
title += ' | ' + formattedHash
}
document.title = title
window.history.replaceState(null, null, url)
window.announce(document.title + ': Page loaded', 'assertive')
}

/* global hoodie, alert */

0 comments on commit e77e2db

Please sign in to comment.