-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12189 from craftcms/a11y/update-accordions
A11y/update accordions
- Loading branch information
Showing
18 changed files
with
284 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** global: Craft */ | ||
/** global: Garnish */ | ||
/** | ||
* Accordion | ||
*/ | ||
Craft.Accordion = Garnish.Base.extend({ | ||
$trigger: null, | ||
targetSelector: null, | ||
|
||
_$target: null, | ||
|
||
init: function (trigger) { | ||
this.$trigger = $(trigger); | ||
|
||
// Is this already an accordion trigger? | ||
if (this.$trigger.data('accordion')) { | ||
console.warn('Double-instantiating an accordion trigger on an element'); | ||
this.$trigger.data('accordion').destroy(); | ||
} | ||
|
||
this.$trigger.data('accordion', this); | ||
this.targetSelector = this.$trigger.attr('aria-controls') | ||
? `#${this.$trigger.attr('aria-controls')}` | ||
: null; | ||
|
||
if (this.targetSelector) { | ||
this._$target = $(this.targetSelector); | ||
} | ||
|
||
this.addListener(this.$trigger, 'click', 'onTriggerClick'); | ||
this.addListener(this.$trigger, 'keypress', (event) => { | ||
const key = event.keyCode; | ||
|
||
if (key === Garnish.SPACE_KEY || key === Garnish.RETURN_KEY) { | ||
event.preventDefault(); | ||
this.onTriggerClick(); | ||
} | ||
}); | ||
}, | ||
|
||
onTriggerClick: function () { | ||
const isOpen = this.$trigger.attr('aria-expanded') === 'true'; | ||
|
||
if (isOpen) { | ||
this.hideTarget(this._$target); | ||
} else { | ||
this.showTarget(this._$target); | ||
} | ||
}, | ||
|
||
showTarget: function ($target) { | ||
if ($target && $target.length) { | ||
this.showTarget._currentHeight = $target.height(); | ||
|
||
$target.removeClass('hidden'); | ||
|
||
this.$trigger | ||
.removeClass('collapsed') | ||
.addClass('expanded') | ||
.attr('aria-expanded', 'true'); | ||
|
||
for (let i = 0; i < $target.length; i++) { | ||
(($t) => { | ||
if ($t.prop('nodeName') !== 'SPAN') { | ||
$t.height('auto'); | ||
this.showTarget._targetHeight = $t.height(); | ||
$t.css({ | ||
height: this.showTarget._currentHeight, | ||
overflow: 'hidden', | ||
}); | ||
|
||
$t.velocity('stop'); | ||
|
||
$t.velocity( | ||
{height: this.showTarget._targetHeight}, | ||
Garnish.getUserPreferredAnimationDuration('fast'), | ||
function () { | ||
$t.css({ | ||
height: '', | ||
overflow: '', | ||
}); | ||
} | ||
); | ||
} | ||
})($target.eq(i)); | ||
} | ||
|
||
delete this.showTarget._targetHeight; | ||
delete this.showTarget._currentHeight; | ||
|
||
// Trigger a resize event in case there are any grids in the target that need to initialize | ||
Garnish.$win.trigger('resize'); | ||
} | ||
}, | ||
|
||
hideTarget: function ($target) { | ||
if ($target && $target.length) { | ||
this.$trigger | ||
.removeClass('expanded') | ||
.addClass('collapsed') | ||
.attr('aria-expanded', 'false'); | ||
|
||
for (let i = 0; i < $target.length; i++) { | ||
(($t) => { | ||
if ($t.hasClass('hidden')) { | ||
return; | ||
} | ||
if ($t.prop('nodeName') === 'SPAN') { | ||
$t.addClass('hidden'); | ||
} else { | ||
$t.css('overflow', 'hidden'); | ||
$t.velocity('stop'); | ||
$t.velocity( | ||
{height: 0}, | ||
Garnish.getUserPreferredAnimationDuration('fast'), | ||
function () { | ||
$t.addClass('hidden'); | ||
} | ||
); | ||
} | ||
})($target.eq(i)); | ||
} | ||
} | ||
}, | ||
|
||
destroy: function () { | ||
this.$trigger.removeData('accordion'); | ||
this.base(); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.