cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <h1>Accessible Accordion Widget</h1>
<ul>
    <li>Use up and down cursor keys to navigate through the "accordion triggers".</li>
    <li>Click  or press Space or Enter to open/close an accordion.</li>
    <li>Press Tab to jump to the open accordion panel.</li>
    <li>Applies correct <code>aria</code> states and stateful classes - so you can style them how you prefer</li>
    <li>Default open panels can be set with JS when instantiating, or by adding <code>.is-selected</code> to the accordion trigger</li>
    <li>Supports an accordion of 1 item (useful for "show advanced")</li>
</ul>

<div role="tablist" aria-multiselectable="true" class="accordion-interface">
    <h2>Multiple panels can be opened at once</h2>
    
    <a href="#accordion1" role="tab" aria-controls="accordion1" class="accordion-trigger js-accordion-trigger">Accordion 1</a>
    <div id="accordion1" role="tabpanel" class="accordion-panel js-accordion-panel">
        <div class="accordion-panel__inner">
            <h3>Accordion 1</h3>
            <p>Duis sagittis, est sit amet gravida tristique, purus lectus venenatis urna, id molestie magna risus ut nunc. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Vestibulum sit amet ipsum lacus. Suspendisse <em>potenti</em>. </p>
            <p>Nulla lobortis tempus commodo. Fusce ac sodales magna. Cras molestie risus a <q cite='http://www.heydonworks.com'>enim convallis vitae</q> luctus libero lacinia. Donec tempus tempus tellus, ac lacinia turpis mattis ac. Maecenas sit <q cite='http://www.heydonworks.com'>amet tellus nec mi gravida posuere</q> non pretium magna. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Aliquam tincidunt velit sit amet ante hendrerit tempus. Sed mauris arcu, aliquet ultrices malesuada sed, pretium id <kbd>CTRL</kbd> + <kbd>V</kbd> massa. </p>
        </div>
    </div>

    <a href="#accordion2" role="tab" aria-controls="accordion2" class="accordion-trigger js-accordion-trigger">Accordion 2</a>
    <div id="accordion2" role="tabpanel" class="accordion-panel js-accordion-panel">
        <div class="accordion-panel__inner">
            <h3>Accordion 2</h3>
            <p>Fusce ac sodales <abbr title='Cascading Style Sheets'>CSS</abbr> magna. Donec et nisi dictum felis sollicitudin congue. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Sed dapibus, lectus sit amet adipiscing egestas, mauris est viverra
                nibh, iaculis pretium sem orci aliquet mauris. Suspendisse <del>potenti</del>. Duis sagittis, est sit amet gravida tristique, purus lectus venenatis urna, id molestie magna risus ut nunc. Fusce ac sodales magna. Potenti et eros sed justo commodo bibendum
                non at nunc. </p>
            <p>Suspendisse potenti cras molestie, risus a enim convallis vitae luctus libero lacinia. Nulla vel magna sit amet dui <a href='#'>lobortis</a> commodo vitae vel nulla. Suspendisse potenti. Sed dapibus, lectus sit amet adipiscing egestas, mauris est viverra
                nibh, <a href='#'>iaculis pretium</a> sem orci aliquet mauris. Donec a congue leo. Vestibulum sit amet ipsum lacus. </p>
            </div>
    </div>

    <a href="#accordion3" role="tab" aria-controls="accordion3" class="accordion-trigger js-accordion-trigger">Accordion 3</a>
    <div id="accordion3" role="tabpanel" class="accordion-panel js-accordion-panel">
        <div class="accordion-panel__inner">
            <h3>Accordion 3</h3>
            <p>Potenti et eros sed justo commodo bibendum non at nunc. Maecenas sit amet tellus nec mi gravida posuere non pretium magna. Nulla auctor eleifend turpis consequat pharetra. </p>
            <p>Vestibulum sit amet ipsum lacus. Lorem ipsum dolor sit amet, consectetur <em>adipiscing elit</em>. Sed dapibus, lectus sit amet adipiscing egestas, mauris est viverra nibh, iaculis pretium sem orci aliquet mauris. Duis sagittis, est sit amet gravida
                tristique, purus lectus venenatis urna, id molestie magna risus ut nunc. Nulla vel magna sit amet dui <a href='#'>lobortis</a> commodo vitae vel nulla. </p>
            <p>Suspendisse potenti. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Lorem ipsum dolor sit amet, consectetur <em>adipiscing elit</em>. Suspendisse potenti H<sub>2</sub>0. </p>
            </div>
    </div>
</div>

<div role="tablist" class="accordion-interface">
    <h2>Only one panel can be opened</h2>
    
    <a href="#accordion1" role="tab" aria-controls="accordion1" class="accordion-trigger js-accordion-trigger">Accordion 1</a>
    <div id="accordion1" role="tabpanel" class="accordion-panel js-accordion-panel">
        <div class="accordion-panel__inner">
            <h3>Accordion 1</h3>
            <p>Duis sagittis, est sit amet gravida tristique, purus lectus venenatis urna, id molestie magna risus ut nunc. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Vestibulum sit amet ipsum lacus. Suspendisse <em>potenti</em>. </p>
            <p>Nulla lobortis tempus commodo. Fusce ac sodales magna. Cras molestie risus a <q cite='http://www.heydonworks.com'>enim convallis vitae</q> luctus libero lacinia. Donec tempus tempus tellus, ac lacinia turpis mattis ac. Maecenas sit <q cite='http://www.heydonworks.com'>amet tellus nec mi gravida posuere</q> non pretium magna. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Aliquam tincidunt velit sit amet ante hendrerit tempus. Sed mauris arcu, aliquet ultrices malesuada sed, pretium id <kbd>CTRL</kbd> + <kbd>V</kbd> massa. </p>
        </div>
    </div>

    <a href="#accordion2" role="tab" aria-controls="accordion2" class="accordion-trigger js-accordion-trigger">Accordion 2</a>
    <div id="accordion2" role="tabpanel" class="accordion-panel js-accordion-panel">
        <div class="accordion-panel__inner">
            <h3>Accordion 2</h3>
            <p>Fusce ac sodales <abbr title='Cascading Style Sheets'>CSS</abbr> magna. Donec et nisi dictum felis sollicitudin congue. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Sed dapibus, lectus sit amet adipiscing egestas, mauris est viverra
                nibh, iaculis pretium sem orci aliquet mauris. Suspendisse <del>potenti</del>. Duis sagittis, est sit amet gravida tristique, purus lectus venenatis urna, id molestie magna risus ut nunc. Fusce ac sodales magna. Potenti et eros sed justo commodo bibendum
                non at nunc. </p>
            <p>Suspendisse potenti cras molestie, risus a enim convallis vitae luctus libero lacinia. Nulla vel magna sit amet dui <a href='#'>lobortis</a> commodo vitae vel nulla. Suspendisse potenti. Sed dapibus, lectus sit amet adipiscing egestas, mauris est viverra
                nibh, <a href='#'>iaculis pretium</a> sem orci aliquet mauris. Donec a congue leo. Vestibulum sit amet ipsum lacus. </p>
            </div>
    </div>

    <a href="#accordion3" role="tab" aria-controls="accordion3" class="accordion-trigger js-accordion-trigger">Accordion 3</a>
    <div id="accordion3" role="tabpanel" class="accordion-panel js-accordion-panel">
        <div class="accordion-panel__inner">
            <h3>Accordion 3</h3>
            <p>Potenti et eros sed justo commodo bibendum non at nunc. Maecenas sit amet tellus nec mi gravida posuere non pretium magna. Nulla auctor eleifend turpis consequat pharetra. </p>
            <p>Vestibulum sit amet ipsum lacus. Lorem ipsum dolor sit amet, consectetur <em>adipiscing elit</em>. Sed dapibus, lectus sit amet adipiscing egestas, mauris est viverra nibh, iaculis pretium sem orci aliquet mauris. Duis sagittis, est sit amet gravida
                tristique, purus lectus venenatis urna, id molestie magna risus ut nunc. Nulla vel magna sit amet dui <a href='#'>lobortis</a> commodo vitae vel nulla. </p>
            <p>Suspendisse potenti. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Lorem ipsum dolor sit amet, consectetur <em>adipiscing elit</em>. Suspendisse potenti H<sub>2</sub>0. </p>
        </div>
    </div>
</div>
            
          
!
            
              .accordion-trigger {
    background: lightgrey;
    border: 1px solid;
    color: #000;
    display: block;
    font-weight: bold;
    padding: 10px;
    text-decoration: none;
    
    &.is-selected {
        background: lightblue;
    }
    
    &:before {
        // Don't use generated content for the indicator in production as it gets read to screenreaders
        content: "+";
        display: inline-block;
        width: 15px;
    }
    
    &.is-selected:before {
        content: "–";
    }
}

.accordion-panel {
    max-height: 1000px;
    opacity: 1;
    transform: scaleY(1);
    transform-origin: top;
    transition: transform ease 0.5s, opacity ease 0.5s, max-height ease 0.75s;
    visibility: visible;
    
    &.is-hidden {
        max-height: 0;
        opacity: 0;
        overflow: hidden;
        transform: scaleY(0);
        transition: all ease 0.5s;
        visibility: hidden;
    }
}

.accordion-panel__inner {
    border: 1px solid;
    border-top: none;
    padding: 10px;
}

body {
    font-family: sans-serif;
    margin: auto;
    padding: 30px;
    max-width: 960px;
    overflow-y: scroll;
}

li {
    margin: 0.25em 0;
}

h1, h2, h3 {
    margin-top: 0;
}

h1 {
    font-size: (25 / 16) * 1em;
}

h2 {
    font-size: (22 / 16) * 1em;
}

code {
    font-size: (20 / 16) * 1em;
}

.accordion-interface {
    float: left;
    margin: 0 2% 30px; 
    width: 46%
}

:focus {
    box-shadow: 0 0 4px dodgerblue;
    outline: none;
}
            
          
!
            
              function AccordionWidget (el, selectedIndex) {
    if (!el) {
        return;
    }

    this.el = el;

    this.accordionTriggers = this.el.getElementsByClassName('js-accordion-trigger');
    this.accordionPanels = this.el.getElementsByClassName('js-accordion-panel');

    if (this.accordionTriggers.length === 0 || this.accordionTriggers.length !== this.accordionPanels.length) {
        return;
    }

    this._init(selectedIndex);
}

AccordionWidget.prototype._init = function (selectedIndex) {
    this.accordionTriggersLength = this.accordionTriggers.length;
    this.expandedAccordions = new Array(this.accordionTriggersLength);
    this.multiSelectable = this.el.hasAttribute('aria-multiselectable');
    this.clickListener = this._clickEvent.bind(this);
    this.keydownListener = this._keydownEvent.bind(this);
    this.focusListener = this._focusEvent.bind(this);
    this.keys = {
        prev: 38,
        next: 40,
        space: 32
    };

    var initialSelectedIndex;

    for (var i = 0; i < this.accordionTriggersLength; i++) {
        this.accordionTriggers[i].index = i;
        this.accordionTriggers[i].addEventListener('click', this.clickListener, false);
        this.accordionTriggers[i].addEventListener('keydown', this.keydownListener, false);
        this.accordionTriggers[i].addEventListener('focus', this.focusListener, false);

        if (this.accordionTriggers[i].classList.contains('is-selected')) {
            this.expandedAccordions[i] = true;
        }
    }

    if (!isNaN(selectedIndex)) {
        initialSelectedIndex = selectedIndex < this.accordionTriggersLength ? selectedIndex : this.accordionTriggersLength - 1;
        this.expandedAccordions = new Array(this.accordionTriggersLength);
        this.expandedAccordions[initialSelectedIndex] = true;
    }
    else {
        initialSelectedIndex = this.expandedAccordions.lastIndexOf(true);

        if (!this.multiSelectable) {
            this.expandedAccordions = new Array(this.accordionTriggersLength);
            this.expandedAccordions[initialSelectedIndex] = true;
        }
    }

    this.setSelected(initialSelectedIndex);
    this.setExpanded();
    this.el.classList.add('is-initialized');
};

AccordionWidget.prototype._clickEvent = function (e) {
    e.preventDefault();

    this.setSelected(e.target.index, true);
    this.setExpanded(e.target.index, true);
};

AccordionWidget.prototype._keydownEvent = function (e) {
    var targetIndex;

    switch (e.keyCode) {
        case this.keys.space:
        case this.keys.prev:
        case this.keys.next:
            e.preventDefault();
            break;
        default:
            return;
    }

    if (e.keyCode === this.keys.space) {
        this.setExpanded(e.target.index, true);

        return;
    }
    else if (e.keyCode === this.keys.prev && e.target.index > 0) {
        targetIndex = e.target.index - 1;
    }
    else if (e.keyCode === this.keys.next && e.target.index < this.accordionTriggersLength - 1) {
        targetIndex = e.target.index + 1;
    }
    else {
        return;
    }

    this.setSelected(targetIndex, true);
};

AccordionWidget.prototype._focusEvent = function () {
    if (this.accordionTriggersLength === 1) {
        this.setSelected(0);
    }

    return;
};

AccordionWidget.prototype.setSelected = function (index, userInvoked) {
    if (index === -1) {
        this.accordionTriggers[0].setAttribute('tabindex', 0);

        return;
    }

    for (var i = 0; i < this.accordionTriggersLength; i++) {
        if (i === index) {
            this.accordionTriggers[i].classList.add('is-selected');
            this.accordionTriggers[i].setAttribute('aria-selected', true);
            this.accordionTriggers[i].setAttribute('tabindex', 0);

            if (userInvoked) {
                this.accordionTriggers[i].focus();
            }
        }
        else {
            this.accordionTriggers[i].classList.remove('is-selected');
            this.accordionTriggers[i].setAttribute('aria-selected', false);
            this.accordionTriggers[i].setAttribute('tabindex', -1);
        }
    }
};

AccordionWidget.prototype.setExpanded = function (index, userInvoked) {
    var i;

    if (userInvoked) {
        if (this.multiSelectable) {
            this.expandedAccordions[index] = !this.expandedAccordions[index];
        }
        else {
            for (i = 0; i < this.accordionTriggersLength; i++) {
                if (i === index) {
                    this.expandedAccordions[i] = !this.expandedAccordions[i];
                }
                else {
                    this.expandedAccordions[i] = false;
                }
            }
        }
    }

    for (i = 0; i < this.accordionTriggersLength; i++) {
        if (this.expandedAccordions[i]) {
            this.accordionTriggers[i].setAttribute('aria-expanded', true);
            this.accordionTriggers[i].classList.add('is-expanded');

            this.accordionPanels[i].setAttribute('aria-hidden', false);
            this.accordionPanels[i].classList.remove('is-hidden');
            this.accordionPanels[i].setAttribute('tabindex', 0);
        }
        else {
            this.accordionTriggers[i].setAttribute('aria-expanded', false);
            this.accordionTriggers[i].classList.remove('is-expanded');

            this.accordionPanels[i].setAttribute('aria-hidden', true);
            this.accordionPanels[i].classList.add('is-hidden');
            this.accordionPanels[i].setAttribute('tabindex', -1);
        }
    }
};

AccordionWidget.prototype.destroy = function () {
    this.el.classList.remove('is-initialized');

    for (var i = 0; i < this.accordionTriggersLength; i++) {
        this.accordionTriggers[i].removeAttribute('aria-expanded');
        this.accordionTriggers[i].removeAttribute('aria-selected');
        this.accordionTriggers[i].classList.remove('is-expanded');

        this.accordionPanels[i].removeAttribute('aria-hidden');
        this.accordionPanels[i].classList.remove('is-hidden');
        this.accordionPanels[i].removeAttribute('tabindex');

        this.accordionTriggers[i].removeEventListener('click', this.clickListener, false);
        this.accordionTriggers[i].removeEventListener('keydown', this.keydownListener, false);
        this.accordionTriggers[i].removeEventListener('focus', this.focusListener, false);

        delete this.accordionTriggers[i].index;
    }
};

new AccordionWidget(document.getElementsByClassName('accordion-interface')[0], 0);

new AccordionWidget(document.getElementsByClassName('accordion-interface')[1], 1);

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console