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

[WIP] select: improve JAWS readability #1594

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions common.blocks/menu/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ provide(BEMDOM.decl({ block : this.name, baseBlock : Control }, /** @lends menu.
this._hoveredItem && this._hoveredItem.delMod('hovered');
this.domElem.attr('aria-activedescendant', itemId);
this._scrollToItem(this._hoveredItem = item);
this.emit('item-hover');
} else if(this._hoveredItem === item) {
this.domElem.removeAttr('aria-activedescendant');
this._hoveredItem = null;
Expand Down
9 changes: 9 additions & 0 deletions common.blocks/select/__button/select__button.bh.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ module.exports = function(bh) {
disabled : mods.disabled,
checked : mods.mode !== 'radio' && !!checkedOptions.length
},
attrs : {
role : 'listbox',
'aria-multiselectable' : String(mods.mode === 'check'),
'aria-disabled' : mods.disabled && 'true',
'aria-haspopup' : 'true',
'aria-expanded' : String(!!mods.opened),
'aria-controls' : ctx.tParam('popupId'),
'aria-label' : ctx.tParam('checkedText')
},
id : select.id,
tabIndex : select.tabIndex,
content : [
Expand Down
5 changes: 5 additions & 0 deletions common.blocks/select/__menu/select__menu.bh.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ module.exports = function(bh) {
var res = {
block : 'menu-item',
mods : { disabled : mods.disabled || option.disabled },
attrs : {
role : 'option',
'aria-checked' : null,
'aria-selected' : String(!!option.checked)
},
val : option.val,
js : { checkedText : option.checkedText },
content : option.text
Expand Down
2 changes: 1 addition & 1 deletion common.blocks/select/__menu/select__menu.deps.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
({
shouldDeps : 'menu'
mustDeps : 'menu'
})
23 changes: 23 additions & 0 deletions common.blocks/select/__menu/select__menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @module menu-item
*/

modules.define('menu-item', ['i-bem__dom'], function(provide, BEMDOM) {

/**
* @exports
* @class menu-item
* @bem
*
* @param val Value of item
*/
provide(BEMDOM.decl(this.name, /** @lends menu-item.prototype */{
onSetMod : {
'checked' : {
'*' : function(_, modVal) {
this.domElem.attr('aria-selected', !!modVal);
}
}
}
}));
});
17 changes: 10 additions & 7 deletions common.blocks/select/_mode/select_mode_check.bh.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ module.exports = function(bh) {
});

bh.match('select_mode_check__button', function(ctx) {
var checkedOptions = ctx.tParam('checkedOptions');

ctx.content({
elem : 'text',
content : checkedOptions.length === 1?
var checkedOptions = ctx.tParam('checkedOptions'),
checkedText = checkedOptions.length === 1?
checkedOptions[0].text :
checkedOptions.reduce(function(res, option) {
return res + (res? ', ' : '') + (option.checkedText || option.text);
}, '') ||
ctx.tParam('select').text
});
ctx.tParam('select').text;

ctx
.tParam('checkedText', checkedText)
.content({
elem : 'text',
content : checkedText
});
});

};
13 changes: 8 additions & 5 deletions common.blocks/select/_mode/select_mode_radio-check.bh.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ module.exports = function(bh) {
});

bh.match('select_mode_radio-check__button', function(ctx) {
var checkedOptions = ctx.tParam('checkedOptions');
var checkedOptions = ctx.tParam('checkedOptions'),
checkedText = (checkedOptions[0] || ctx.tParam('select')).text;

ctx.content({
elem : 'text',
content : (checkedOptions[0] || ctx.tParam('select')).text
});
ctx
.tParam('checkedText', checkedText)
.content({
elem : 'text',
content : checkedText
});
});

};
12 changes: 8 additions & 4 deletions common.blocks/select/_mode/select_mode_radio.bh.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ module.exports = function(bh) {
});

bh.match('select_mode_radio__button', function(ctx) {
ctx.content({
elem : 'text',
content : ctx.tParam('checkedOption').text
});
var checkedText = ctx.tParam('checkedOption').text;

ctx
.tParam('checkedText', checkedText)
.content({
elem : 'text',
content : checkedText
});
});

};
5 changes: 4 additions & 1 deletion common.blocks/select/select.bh.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = function(bh) {

var isValDef = typeof json.val !== 'undefined',
isModeCheck = ctx.mod('mode') === 'check',
firstOption, checkedOptions = [];
firstOption, checkedOptions = [],
popupId = ctx.generateId();

iterateOptions(json.options);

Expand All @@ -39,11 +40,13 @@ module.exports = function(bh) {
.tParam('select', json)
.tParam('firstOption', firstOption)
.tParam('checkedOptions', checkedOptions)
.tParam('popupId', popupId)
.content([
{ elem : 'button' },
{
block : 'popup',
mods : { target : 'anchor', theme : ctx.mod('theme'), autoclosable : true },
attrs : { id : popupId, role : 'presentation' },
directions : ['bottom-left', 'bottom-right', 'top-left', 'top-right'],
content : { block : json.block, mods : ctx.mods(), elem : 'menu' }
}
Expand Down
15 changes: 14 additions & 1 deletion common.blocks/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ provide(BEMDOM.decl(this.name, /** @lends select.prototype */{
this._menu = this._popup.findBlockInside('menu')
.on({
'change' : this._onMenuChange,
'item-click' : this._onMenuItemClick
'item-click' : this._onMenuItemClick,
'item-hover' : this._onItemHover
}, this);

this._isPointerPressInProgress = false;
Expand All @@ -65,6 +66,7 @@ provide(BEMDOM.decl(this.name, /** @lends select.prototype */{
'opened' : {
'*' : function(_, modVal) {
this._menu.setMod('focused', modVal);
this._button.domElem.attr('aria-expanded', !!modVal);
},

'true' : function() {
Expand All @@ -82,6 +84,7 @@ provide(BEMDOM.decl(this.name, /** @lends select.prototype */{
this
.unbindFromDoc('pointerpress', this._onDocPointerPress)
._popup.delMod('visible');
this._button.domElem.removeAttr('aria-activedescendant');
}
},

Expand All @@ -94,6 +97,11 @@ provide(BEMDOM.decl(this.name, /** @lends select.prototype */{

'true' : function() {
this._popup.delMod('visible');
this.domElem.attr('aria-disabled', 'true');
},

'' : function() {
this.domElem.removeAttr('aria-disabled');
}
}
},
Expand Down Expand Up @@ -216,6 +224,11 @@ provide(BEMDOM.decl(this.name, /** @lends select.prototype */{
this.emit('change');
},

_onItemHover : function() {
var activeDescendant = this._menu.domElem.attr('aria-activedescendant');
this.elem('button').attr('aria-activedescendant', activeDescendant);
},

_onMenuItemClick : function() {},

_updateControl : function() {},
Expand Down
33 changes: 31 additions & 2 deletions common.blocks/select/select.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
modules.define(
'spec',
['select', 'i-bem__dom', 'jquery', 'sinon', 'keyboard__codes', 'next-tick', 'BEMHTML'],
function(provide, Select, BEMDOM, $, sinon, keyCodes, nextTick, BEMHTML) {
['select', 'i-bem__dom', 'jquery', 'sinon', 'keyboard__codes', 'next-tick', 'BEMHTML', 'chai'],
function(provide, Select, BEMDOM, $, sinon, keyCodes, nextTick, BEMHTML, chai) {

var expect = chai.expect;
describe('select', function() {
var select, button, popup, menu;

Expand Down Expand Up @@ -193,6 +194,34 @@ describe('select', function() {
select.getName().should.be.equal('select1');
});
});

describe('a11y', function() {
it('should set/remove "aria-expanded" attribute for button on show/hide', function() {
select.setMod('opened');
button.domElem.attr('aria-expanded').should.be.equal('true');

select.delMod('opened');
button.domElem.attr('aria-expanded').should.be.equal('false');
});

it('should set "aria-disabled" attribute on disabled state', function() {
select
.setMod('disabled')
.domElem.attr('aria-disabled').should.be.equal('true');

select.delMod('disabled');
expect(select.domElem.attr('aria-disabled')).to.be.undefined;
});

it('should set/remove "aria-activedescendant" to button on "opened" state', function() {
select.setMod('opened');
var menuAriaAttr = menu.domElem.attr('aria-activedescendant');
button.domElem.attr('aria-activedescendant').should.be.equal(menuAriaAttr);

select.delMod('opened');
expect(button.domElem.attr('aria-activedescendant')).to.be.undefined;
});
});
});

provide();
Expand Down