Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(chips): Add basic accessibility support.
Browse files Browse the repository at this point in the history
Previously, chips had no, or very broken, support for accessibility.

Make many updates to fix broken functionality and add required features.

 - Remove existing `aria-hidden` attributes.

 - Dynamically apply required `role` and `aria-owns` attributes.

 - Ensure `tabindex` is set properly on all elements. Particularly,
   you can now tab to, and navigate through, a set of chips in
   readonly mode.

 - Provide new `input-aria-label` option to identify the inputs.

 - Provide new `container-hint` option used when the element is in
   readonly mode.

 - Fix issue with `delete-hint` never being read by screen readers
   because it was outside of the chip.

 - Fix keyboard navigation to properly wrap when moving both left
   or right.

 - Fix keyboard navigation when in readonly mode.

 - Fix issue where the wrong chip may be focused because the old
   chip was still in the DOM.

BREAKING CHANGES

We consider the following to be minor breaking changes since we never
expected these attributes/elements to be utilized by developers.

Nonetheless, we want to ensure that they are documented.

 - The `role` of the `.md-chip-content` has been modified from `button`
   to `option` so that it works with the new `listbox` role of it's
   parent.

   If you rely on this role being `button`, you should update your code
   accordingly.

- The delete hint on removable chips now resides inside of the
  `div.md-chip-content` rather than the parent `md-chip` element where
  it could not be read by screen readers.

  If you interact with this element (in CSS or JS) please update your
  selectors/code to account for the new DOM hierarchy.

Fixes #9391.
  • Loading branch information
topherfangio committed Oct 10, 2016
1 parent 7e49c78 commit 1217121
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 50 deletions.
4 changes: 2 additions & 2 deletions docs/config/template/index.template.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html ng-app="docsApp" ng-controller="DocsCtrl" lang="en" ng-strict-di >
<html ng-app="docsApp" ng-controller="DocsCtrl" lang="en" ng-strict-di>
<head>
<base href="/">
<title ng-bind="'Angular Material - ' + menu.currentSection.name +
Expand All @@ -12,7 +12,7 @@
<link rel="stylesheet" href="angular-material.min.css">
<link rel="stylesheet" href="docs.css">
</head>
<body class="docs-body" layout="row" ng-cloak>
<body class="docs-body" layout="row" ng-cloak aria-label="Angular Material Docs">

<md-sidenav class="site-sidenav md-sidenav-left md-whiteframe-z2"
md-component-id="left" hide-print
Expand Down
80 changes: 79 additions & 1 deletion src/components/chips/chips.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ describe('<md-chips>', function() {

var updatedChips = getChipElements(element);

expect(chips.length).not.toBe(updatedChips.length);
expect(updatedChips.length).toBe(chips.length - 1);
}));

it('should set removable to true by default', function() {
Expand Down Expand Up @@ -1331,6 +1331,84 @@ describe('<md-chips>', function() {

});
});

describe('keyboard navigation', function() {
var leftEvent, rightEvent;

beforeEach(inject(function($mdConstant) {
leftEvent = {
type: 'keydown',
keyCode: $mdConstant.KEY_CODE.LEFT_ARROW,
which: $mdConstant.KEY_CODE.LEFT_ARROW
};
rightEvent = {
type: 'keydown',
keyCode: $mdConstant.KEY_CODE.RIGHT_ARROW,
which: $mdConstant.KEY_CODE.RIGHT_ARROW
};
}));

describe('when readonly', function() {
// TODO: Add readonly specific tests
});

describe('when we have an input', function() {
it('clears the selected chip when the input is focused', inject(function($timeout) {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');

// Focus the input
ctrl.focusInput();
$timeout.flush();

// Expect no chip to be selected
expect(ctrl.selectedChip).toBe(-1);
}));

it('selects the previous chip', inject(function($timeout) {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
var chips = getChipElements(element);

// Select the second chip
ctrl.selectAndFocusChipSafe(1);
$timeout.flush();

expect(ctrl.selectedChip).toBe(1);

// Select the 1st chip
element.find('md-chips-wrap').triggerHandler(angular.copy(leftEvent));
$timeout.flush();

expect(ctrl.selectedChip).toBe(0);
}));

it('and the first chip is selected, selects the input', inject(function($timeout) {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
var chips = getChipElements(element);

// Append so we can focus the input
angular.element(document.body).append(element);

// Select the second chip
ctrl.selectAndFocusChipSafe(0);
$timeout.flush();

expect(ctrl.selectedChip).toBe(0);

// Selecting past the first should wrap back to the input
element.find('md-chips-wrap').triggerHandler(angular.copy(leftEvent));
$timeout.flush();

expect(ctrl.selectedChip).toBe(-1);
expect(document.activeElement).toBe(element.find('input')[0]);

// Cleanup after ourselves
element.remove();
}));
});
});
});

describe('with $interpolate.start/endSymbol override', function() {
Expand Down
2 changes: 1 addition & 1 deletion src/components/chips/demoBasicUsage/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h2 class="md-title">Use a custom chip template.</h2>

<form name="fruitForm">
<md-chips ng-model="ctrl.roFruitNames" name="fruitName" readonly="ctrl.readonly"
md-removable="ctrl.removable" md-max-chips="5">
md-removable="ctrl.removable" md-max-chips="5" placeholder="Enter a fruit...">
<md-chip-template>
<strong>{{$chip}}</strong>
<em>(fruit)</em>
Expand Down
17 changes: 15 additions & 2 deletions src/components/chips/demoContactChips/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
(function () {
'use strict';

// If we do not have CryptoJS defined; import it
if (typeof CryptoJS == 'undefined') {
var cryptoSrc = '//cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/md5.js';
var scriptTag = document.createElement('script');
scriptTag.setAttribute('src', cryptoSrc);
document.body.appendChild(scriptTag);
}

angular
.module('contactChipsDemo', ['ngMaterial'])
.controller('ContactChipDemoCtrl', DemoCtrl);
Expand Down Expand Up @@ -92,10 +101,14 @@

return contacts.map(function (c, index) {
var cParts = c.split(' ');
var email = cParts[0][0].toLowerCase() + '.' + cParts[1].toLowerCase() + '@example.com';
var hash = CryptoJS.MD5(email);

var contact = {
name: c,
email: cParts[0][0].toLowerCase() + '.' + cParts[1].toLowerCase() + '@example.com',
image: 'http://lorempixel.com/50/50/people?' + index
email: email,
image: '//www.gravatar.com/avatar/' + hash + '?s=50&d=retro'
//image: 'http://lorempixel.com/50/50/people?' + index
};
contact._lowername = contact.name.toLowerCase();
return contact;
Expand Down
54 changes: 32 additions & 22 deletions src/components/chips/js/chipDirective.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular
.module('material.components.chips')
.directive('mdChip', MdChip);
.module('material.components.chips')
.directive('mdChip', MdChip);

/**
* @ngdoc directive
Expand Down Expand Up @@ -33,36 +33,46 @@ var DELETE_HINT_TEMPLATE = '\
* @param $mdUtil
* @ngInject
*/
function MdChip($mdTheming, $mdUtil) {
var hintTemplate = $mdUtil.processTemplate(DELETE_HINT_TEMPLATE);
function MdChip($mdTheming, $mdUtil, $compile, $timeout) {
var deleteHintTemplate = $mdUtil.processTemplate(DELETE_HINT_TEMPLATE);

return {
restrict: 'E',
require: ['^?mdChips', 'mdChip'],
compile: compile,
link: postLink,
controller: 'MdChipCtrl'
};

function compile(element, attr) {
// Append the delete template
element.append($mdUtil.processTemplate(hintTemplate));
function postLink(scope, element, attr, ctrls) {
var chipsController = ctrls.shift();
var chipController = ctrls.shift();
var chipContentElement = angular.element(element[0].querySelector('.md-chip-content'));

return function postLink(scope, element, attr, ctrls) {
var chipsController = ctrls.shift();
var chipController = ctrls.shift();
$mdTheming(element);
$mdTheming(element);

if (chipsController) {
chipController.init(chipsController);
if (chipsController) {
chipController.init(chipsController);

angular
.element(element[0]
.querySelector('.md-chip-content'))
.on('blur', function () {
chipsController.resetSelectedChip();
chipsController.$scope.$applyAsync();
});
// Append our delete hint to the div.md-chip-content (which does not exist at compile time)
chipContentElement.append($compile(deleteHintTemplate)(scope));

// When a chip is blurred, make sure to unset (or reset) the selected chip so that tabbing
// through elements works properly
chipContentElement.on('blur', function() {
chipsController.resetSelectedChip();
chipsController.$scope.$applyAsync();
});
}

// Use $timeout to ensure we run AFTER the element has been added to the DOM so we can focus it.
$timeout(function() {
if (!chipsController) {
return;
}

if (chipsController.shouldFocusLastChip) {
chipsController.focusLastChipThenInput();
}
};
});
}
}
Loading

0 comments on commit 1217121

Please sign in to comment.