11import mixin from '../../globals/js/misc/mixin' ;
22import createComponent from '../../globals/js/mixins/create-component' ;
3- import initComponentBySearch
4- from '../../globals/js/mixins/init-component-by-search' ;
3+ import initComponentBySearch from '../../globals/js/mixins/init-component-by-search' ;
54import eventMatches from '../../globals/js/misc/event-matches' ;
65
76class Accordion extends mixin ( createComponent , initComponentBySearch ) {
@@ -13,19 +12,41 @@ class Accordion extends mixin(createComponent, initComponentBySearch) {
1312 */
1413 constructor ( element , options ) {
1514 super ( element , options ) ;
16- this . element . addEventListener ( 'click' , ( event ) => {
15+ this . element . addEventListener ( 'click' , event => {
1716 const item = eventMatches ( event , this . options . selectorAccordionItem ) ;
1817 if ( item && ! eventMatches ( event , this . options . selectorAccordionContent ) ) {
19- item . classList . toggle ( this . options . classActive ) ;
18+ this . _toggle ( item ) ;
2019 }
2120 } ) ;
2221
23- this . element . addEventListener ( 'keypress' , ( event ) => {
24- const item = eventMatches ( event , this . options . selectorAccordionItem ) ;
25- if ( item && ! eventMatches ( event , this . options . selectorAccordionContent ) ) {
26- this . _handleKeypress ( event ) ;
27- }
28- } ) ;
22+ /**
23+ *
24+ * DEPRECATE in v8
25+ *
26+ * Swapping to a button elemenet instead of a div
27+ * automatically maps click events to keypress as well
28+ * This event listener now is only added if user is using
29+ * the older markup
30+ */
31+
32+ if ( ! this . _checkIfButton ( ) ) {
33+ this . element . addEventListener ( 'keypress' , event => {
34+ const item = eventMatches ( event , this . options . selectorAccordionItem ) ;
35+
36+ if (
37+ item &&
38+ ! eventMatches ( event , this . options . selectorAccordionContent )
39+ ) {
40+ this . _handleKeypress ( event ) ;
41+ }
42+ } ) ;
43+ }
44+ }
45+
46+ _checkIfButton ( ) {
47+ return (
48+ this . element . firstElementChild . firstElementChild . nodeName === 'BUTTON'
49+ ) ;
2950 }
3051
3152 /**
@@ -34,10 +55,26 @@ class Accordion extends mixin(createComponent, initComponentBySearch) {
3455 */
3556 _handleKeypress ( event ) {
3657 if ( event . which === 13 || event . which === 32 ) {
37- event . target . classList . toggle ( this . options . classActive ) ;
58+ this . _toggle ( event . target ) ;
3859 }
3960 }
4061
62+ _toggle ( element ) {
63+ const heading = element . querySelector (
64+ this . options . selectorAccordionItemHeading
65+ ) ;
66+ const expanded = heading . getAttribute ( 'aria-expanded' ) ;
67+
68+ if ( expanded !== null ) {
69+ heading . setAttribute (
70+ 'aria-expanded' ,
71+ expanded === 'true' ? 'false' : 'true'
72+ ) ;
73+ }
74+
75+ element . classList . toggle ( this . options . classActive ) ;
76+ }
77+
4178 /**
4279 * The component options.
4380 * If `options` is specified in the constructor,
@@ -48,6 +85,7 @@ class Accordion extends mixin(createComponent, initComponentBySearch) {
4885 static options = {
4986 selectorInit : '[data-accordion]' ,
5087 selectorAccordionItem : '.bx--accordion__item' ,
88+ selectorAccordionItemHeading : '.bx--accordion__heading' ,
5189 selectorAccordionContent : '.bx--accordion__content' ,
5290 classActive : 'bx--accordion__item--active' ,
5391 } ;
0 commit comments