Skip to content

Commit

Permalink
Use pull request foundation#11052 from DanielRuf/fix/toggler-aria-exp…
Browse files Browse the repository at this point in the history
…anded-on-trigger-11049 for v6.5.0

fbc69f1 fix: add aria-expanded to the toggler triggers
e84e7d1 fix: apply aria-expanded to the toggler triggers in _updateARIA
dd96a42 tests: replace duplicate code in the toggler test

Signed-off-by: Nicolas Coden <nicolas@ncoden.fr>
  • Loading branch information
DanielRuf authored and ncoden committed Jun 16, 2018
1 parent d5c66f8 commit ca8a1af
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
13 changes: 9 additions & 4 deletions js/foundation.toggler.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ class Toggler extends Plugin {
// Add ARIA attributes to triggers
var id = this.$element[0].id;
$(`[data-open="${id}"], [data-close="${id}"], [data-toggle="${id}"]`)
.attr('aria-controls', id);
// If the target is hidden, add aria-hidden
this.$element.attr('aria-expanded', this.$element.is(':hidden') ? false : true);
.attr({
'aria-controls': id,
'aria-expanded': this.$element.is(':hidden') ? false : true
});
}

/**
Expand Down Expand Up @@ -125,7 +126,11 @@ class Toggler extends Plugin {
}

_updateARIA(isOn) {
this.$element.attr('aria-expanded', isOn ? true : false);
var id = this.$element[0].id;
$(`[data-open="${id}"], [data-close="${id}"], [data-toggle="${id}"]`)
.attr({
'aria-expanded': isOn ? true : false
});
}

/**
Expand Down
35 changes: 20 additions & 15 deletions test/javascript/components/toggler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ describe('Toggler', function() {
$html.remove();
});

function appendTriggers() {
return $(`<div>
<a data-open="toggler">Open</a>
<a data-close="toggler">Close</a>
<a data-toggle="toggler">Toggle</a>
</div>`).appendTo('body')
}

describe('constructor()', function() {
it('stores the element and plugin options', function() {
$html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
Expand Down Expand Up @@ -42,33 +50,29 @@ describe('Toggler', function() {

it('adds Aria attributes to click triggers', function() {
$html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
var $triggers = $(`<div>
<a data-open="toggler">Open</a>
<a data-close="toggler">Close</a>
<a data-toggle="toggler">Toggle</a>
</div>`).appendTo('body');
var $triggers = appendTriggers();
plugin = new Foundation.Toggler($html, {});

$triggers.find('[data-open]').should.have.attr('aria-controls', 'toggler');
$triggers.find('[data-close]').should.have.attr('aria-controls', 'toggler');
$triggers.find('[data-toggle]').should.have.attr('aria-controls', 'toggler');
$triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-controls', 'toggler');

$triggers.remove();
});

it('sets aria-expanded to true if the element is visible', function() {
$html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
var $triggers = appendTriggers();
plugin = new Foundation.Toggler($html, {});

$('#toggler').should.have.attr('aria-expanded', 'true');
$triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'true');
});

it('sets aria-expanded to false if the element is invisible', function() {
var $css = $('<style>#toggler { display: none }</style>').appendTo('body');
$html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
var $triggers = appendTriggers();
plugin = new Foundation.Toggler($html, {});

$('#toggler').should.have.attr('aria-expanded', 'false');
$triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'false');
$css.remove();
});
});
Expand All @@ -92,13 +96,14 @@ describe('Toggler', function() {

it('updates aria-expanded after the class is toggled', function() {
$html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
var $triggers = appendTriggers();
plugin = new Foundation.Toggler($html, {});

plugin._toggleClass();
$('#toggler').should.have.attr('aria-expanded', 'true');
$triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'true');

plugin._toggleClass();
$('#toggler').should.have.attr('aria-expanded', 'false');
$triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'false');
});
});

Expand All @@ -107,25 +112,25 @@ describe('Toggler', function() {
it('animates an invisible element in', function(done) {
var $css = $('<style>#toggler { display: none; }</style>').appendTo('body');
$html = $('<div id="toggler" data-toggler data-animate="fade-in fade-out"></div>').appendTo('body');

plugin = new Foundation.Toggler($html, {});

$html.on('on.zf.toggler', function() {
$('#toggler').should.be.visible;
$('#toggler').should.have.attr('aria-expanded', 'true');
$css.remove();
done();
});

plugin._toggleAnimate();
});

it('animates an visible element out', function(done) {
it('animates a visible element out', function(done) {
$html = $('<div id="toggler" data-toggler data-animate="fade-in fade-out"></div>').appendTo('body');

plugin = new Foundation.Toggler($html, {});

$html.on('off.zf.toggler', function() {
$('#toggler').should.be.hidden;
$('#toggler').should.have.attr('aria-expanded', 'false');
done();
});

Expand Down

0 comments on commit ca8a1af

Please sign in to comment.