Skip to content

Commit

Permalink
Refactor toggleHeading to be clearer about it's intentions
Browse files Browse the repository at this point in the history
isOpen was initially set, to the *current* state. Subsequent controls
were inverting that. If we invert it at the beginning this becomes clearer
Note - also renamed to setOpen to signify intent.
  • Loading branch information
colinbm committed Jun 22, 2021
1 parent a462b92 commit 85257e9
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/assets/javascripts/_modules/collapsible-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@
}

function toggleHeading ($topLevelItem) {
var isOpen = $topLevelItem.hasClass('is-open')
var setOpen = !$topLevelItem.hasClass('is-open')

var $heading = $topLevelItem.find('> a')
var $button = $topLevelItem.find('.collapsible__toggle')
var $toggleLabel = $topLevelItem.find('.collapsible__toggle-label')

$topLevelItem.toggleClass('is-open', !isOpen)
$button.attr('aria-expanded', isOpen ? 'false' : 'true')
$toggleLabel.text(isOpen ? 'Expand ' + $heading.text() : 'Collapse ' + $heading.text())
$topLevelItem.toggleClass('is-open', setOpen)
$button.attr('aria-expanded', setOpen ? 'true' : 'false')
$toggleLabel.text(setOpen ? 'Collapse ' + $heading.text() : 'Expand ' + $heading.text())
}

function openActiveHeading () {
Expand Down

0 comments on commit 85257e9

Please sign in to comment.