Skip to content

Commit

Permalink
Fixed aria-controls attribute
Browse files Browse the repository at this point in the history
When using multiple nested unique IDs the aria relationship broke. This
change adds the list of the nested elements in the `aria-controls` attribute.
  • Loading branch information
Jakub Miarka committed Nov 15, 2019
1 parent c186288 commit 41c0956
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/assets/javascripts/_modules/collapsible-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
continue
}
$topLevelItem.addClass('collapsible')
var arrayOfIds = []
$listing.each(function (i) {
var uniqueId = id + '-' + i
arrayOfIds.push(uniqueId)
$(this).addClass('collapsible__body')
.attr('id', id + '-' + i)
.attr('id', uniqueId)
.attr('aria-expanded', 'false')
})
$heading.addClass('collapsible__heading')
.after('<button class="collapsible__toggle" aria-controls="' + id + '"><span class="collapsible__toggle-label">Expand ' + $heading.text() + '</span><span class="collapsible__toggle-icon" aria-hidden="true"></button>')
.after('<button class="collapsible__toggle" aria-controls="' + arrayOfIds.join(' ') + '"><span class="collapsible__toggle-label">Expand ' + $heading.text() + '</span><span class="collapsible__toggle-icon" aria-hidden="true"></button>')
$topLevelItem.on('click', '.collapsible__toggle', function (e) {
e.preventDefault()
var $parent = $(this).parent()
Expand Down
6 changes: 6 additions & 0 deletions spec/javascripts/collapsible-navigation-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ describe('Collapsible navigation', function () {
expect($(this)[0].id).toEqual('toc-nested-page-' + i)
})
})

it('aria-controls on the button lists all the nested IDs', function () {
$navigation.find('button').each(function (i) {
expect($navigation.find('button').attr('aria-controls')).toEqual('toc-nested-page-0 toc-nested-page-1')
})
})
})

0 comments on commit 41c0956

Please sign in to comment.