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

feat(a11y): improve the a11y of the example #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
feat(a11y): improve the a11y of the example
  • Loading branch information
felixzapata committed Apr 20, 2019
commit 9257f94a678b37f4d685561f32720966fda199bf
189 changes: 97 additions & 92 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -63,10 +63,10 @@ <h1>
<circle cx="16" cy="16" r="14" fill="none"></circle>
</svg>
</div>
<button class="remove-city">&times;</button>
<button class="remove-city" aria-label="remove the city">&times;</button>
<div class="city-key" hidden></div>
<div class="card-last-updated" hidden></div>
<div class="location">&nbsp;</div>
<h2 class="location">&nbsp;</h2>
<div class="date">&nbsp;</div>
<div class="description">&nbsp;</div>
<div class="current">
@@ -76,103 +76,107 @@ <h1>
<span class="value"></span><span class="scale">°F</span>
</div>
</div>
<div class="description">
<div class="humidity">
<span class="label">Humidity:</span>
<span class="value"></span><span class="scale">%</span>
</div>
<div class="wind">
<span class="label">Wind:</span>
<span class="value"></span>
<dl class="description">
<dt class="label">Humidity:</dt>
<dd class="humidity"><span class="value"></span><span class="scale">%</span></dd>
<dt><span class="label">Wind:</span></dt>
<dd class="wind"><span class="value"></span>
<span class="scale">mph</span>
<span class="direction"></span>°
</div>
<div class="sunrise">
<span class="label">Sunrise:</span>
<span class="value"></span>
</div>
<div class="sunset">
<span class="label">Sunrise:</span>
<span class="value"></span>
</div>
</div>
<span class="direction"></span>°</dd>
<dt><span class="label">Sunrise:</span></dt>
<dd class="sunrise"><span class="value"></span></dd>
<dt><span class="label">Sunset:</span></dt>
<dd class="sunset"><span class="value"></span></dd>
</dl>
</div>
<div class="future">
<div class="oneday">
<div class="date"></div>
<div class="icon"></div>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</div>
<div class="oneday">
<div class="date"></div>
<div class="icon"></div>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</div>
<div class="oneday">
<div class="date"></div>
<div class="icon"></div>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</div>
<div class="oneday">
<div class="date"></div>
<div class="icon"></div>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</div>
<div class="oneday">
<div class="date"></div>
<div class="icon"></div>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</div>
<div class="oneday">
<div class="date"></div>
<div class="icon"></div>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</div>
<div class="oneday">
<div class="date"></div>
<div class="icon"></div>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</div>
<table summary="the weather the next 7 days">
<thead>
<tr>
<th class="date"></th>
<th class="date"></th>
<th class="date"></th>
<th class="date"></th>
<th class="date"></th>
<th class="date"></th>
<th class="date"></th>
</tr>
</thead>
<tbody>
<tr>
<td class="icon"></td>
<td class="icon"></td>
<td class="icon"></td>
<td class="icon"></td>
<td class="icon"></td>
<td class="icon"></td>
<td class="icon"></td>
</tr>
<tr>
<td>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</td>
<td>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</td>
<td>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</td>
<td>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</td>
<td>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</td>
<td>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</td>
<td>
<div class="temp-high">
<span class="value"></span>°
</div>
<div class="temp-low">
<span class="value"></span>°
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</main>

<div id="addDialogContainer">
<div id="addDialogContainer" aria-hidden="true">
<div class="dialog">
<div class="dialog-title">Add new city</div>
<div class="dialog-body">
@@ -199,6 +203,7 @@ <h1>
</div>
</div>

<script src="/scripts/inert.min.js"></script>
<script src="/scripts/luxon-1.11.4.js"></script>
<script src="/scripts/app.js"></script>
<!-- CODELAB: Add the install script here -->
50 changes: 41 additions & 9 deletions public/scripts/app.js
Original file line number Diff line number Diff line change
@@ -22,11 +22,33 @@ const weatherApp = {
addDialogContainer: document.getElementById('addDialogContainer'),
};

/**
* It will keep the focus inside the dialog
* It uses the inert attribute
* https://github.com/WICG/inert
*/
function blockModal(dialog, value) {
Array.from(document.body.children).forEach(child => {
if(child !== dialog) {
child.inert = value;
}
});
}

/**
* Toggles the visibility of the add location dialog box.
*/
function toggleAddDialog() {
let ariaHidden = weatherApp.addDialogContainer.getAttribute('aria-hidden') === 'true' ? 'false' : 'true';
weatherApp.addDialogContainer.classList.toggle('visible');
weatherApp.addDialogContainer.setAttribute('aria-hidden', ariaHidden);
// the dialog is going to be visible
// we trap the focus inside the dialog
if(weatherApp.addDialogContainer.classList.contains('visible')) {
blockModal(weatherApp.addDialogContainer, true);
} else {
blockModal(weatherApp.addDialogContainer, false);
}
}

/**
@@ -65,6 +87,16 @@ function removeLocation(evt) {
}
}

/**
* Receives an icon name and returns the name
* For example, partly-cloudy-night returns partly cloudy night
*
* @param {String} icon
*/
function parseIcon(icon) {
return icon.replace(/-/g, ' ');
}

/**
* Renders the forecast data into the card element.
*
@@ -117,20 +149,20 @@ function renderForecast(card, data) {
card.querySelector('.current .sunset .value').textContent = sunset;

// Render the next 7 days.
const futureTiles = card.querySelectorAll('.future .oneday');
futureTiles.forEach((tile, index) => {
const futureTiles = card.querySelector('.future');
let index = 0;
for(; index < 7; index++) {
const forecast = data.daily.data[index + 1];
const forecastFor = luxon.DateTime
.fromSeconds(forecast.time)
.setZone(data.timezone)
.toFormat('ccc');
tile.querySelector('.date').textContent = forecastFor;
tile.querySelector('.icon').className = `icon ${forecast.icon}`;
tile.querySelector('.temp-high .value')
.textContent = Math.round(forecast.temperatureHigh);
tile.querySelector('.temp-low .value')
.textContent = Math.round(forecast.temperatureLow);
});
futureTiles.querySelectorAll('.date')[index].textContent = forecastFor;
futureTiles.querySelectorAll('.icon')[index].className = `icon ${forecast.icon}`;
futureTiles.querySelectorAll('.icon')[index].innerHTML = `<span class="sr-only">${parseIcon(forecast.icon)}</span>`;
futureTiles.querySelectorAll('.temp-high')[index].querySelector('.value').textContent = Math.round(forecast.temperatureHigh);
futureTiles.querySelectorAll('.temp-low')[index].querySelector('.value').textContent = Math.round(forecast.temperatureLow);
}

// If the loading spinner is still visible, remove it.
const spinner = card.querySelector('.card-spinner');
2 changes: 2 additions & 0 deletions public/scripts/inert.min.js

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

Loading