Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                %div.container
    %h1 a11y  React.js accordions!
    %h3 Click or use 'Enter' to expand/collapse
    %h5 The attributes on the accordions refer to the state of the content area inside

    #accordions


              
            
!

CSS

              
                @import "bourbon";

// Colors
$slate: #222;
$red: #BA394C;
$blue: #1F6190;
$lightBlue: #3683BA;
$yellow: #FFAD28;

body {
    min-height: 100vh;
    @include linear-gradient(to top, #f5f5f5, #fff);
    @include display(flex);
    @include justify-content(center);
}

.container {
    width: 100%;
    max-width: 500px;
}

// Self contained components
// Semantically structured
.accordion {
    width: 100%;
    height: auto;
    margin: 10px 0px;
    
    // Default Theme
    .accordion-item {
        // Elements
        header {
            padding: 20px;
            background: #222;
            color: #fff;
            cursor: pointer;
            font-size: 0.9em;
            border-bottom: solid 1px #111;
            @include display(flex);
            @include justify-content(space-between);
            &:hover {
                background: #111;
            }
        }

        section {
            background: #fff;
            padding: 40px;
        }

        // State
        &.is-expanded {
            section {
               display: block;
            }
        }
        
        &.is-collapsed {
            section {
                display: none;
            }
        } 
    } // End Accordion Item
    
    // Themes
    @mixin accordionTheme($color) {
        $itemColor: $color;
        $shades: 5;
        .accordion-item {
            
            @for $i from 1 through $shades {
                &:nth-child(#{$i}) {
                    header {
                        $amount: $i * 3%;
                        background: darken($itemColor,  $amount ); 
                        border-bottom: solid 5px darken($itemColor,  $amount * 2);
                        &:hover {
                            background: darken($itemColor,  10%);
                        }
                    }
                }   
            }
            
            section {
                h2 {
                   color: $color;
                }
            }
        }
    }
    
    &.dark {
        @include accordionTheme($slate);
    }
    
    &.blue {
         @include accordionTheme($blue);
    }
    
    &.light-blue {
         @include accordionTheme($lightBlue);
    }
    
    &.red {
        @include accordionTheme($red);
    }
    
    &.yellow {
        @include accordionTheme($yellow);
    }
} // End Accordion

              
            
!

JS

              
                // Here is a little example for everyone
// Let's build an accordion that is a11y compliant!
// If there are any React purist out there, go easy on me, I am learning too :)
// I have to say, every change I have had to make so far has been SIGNIFICANTLY less painful than maintaining this through the jQuery spaghetti version

// Select the node to render the React component into
// The "Parent Element" or "Container"
// This is just an HTML element that React will render the state of our accordion into
var renderNode = document.getElementById("accordions");

// This enables react components to work well on touch devices
React.initializeTouchEvents(true);

// In react everything is organized as a self contained object.

/////////////////////////////////////
// Accordion Component
/////////////////////////////////////
var Accordion = React.createClass({
    render: function() {
        // Look into this vvvv. I believe there is a better way than concatenating a string to dynamic properties. Probably some sort of binding?
        var classes = "accordion " + this.props.theme;
        return(
            <div className={classes}>
                {this.props.children}
            </div>
        );
    }
});

/////////////////////////////////////
// Accordion Item Component
////////////////////////////////////
var AccordionItem = React.createClass({
    
    // Initial setup
    getInitialState: function() {
        return {
          expanded: this.props.expanded
        };
    },
    
    // Actions   
    expand: function() {
        this.setState({expanded: true});
    },
    collapse: function() {
        this.setState({expanded: false});
    },
    toggleExpansion: function() {
        this.setState({expanded: !this.state.expanded});
    },
    
    // Events
    handleClick: function() {
        this.toggleExpansion();
    },
    
    handleKeyPress: function(ev) {
       var key = ev.keyCode;
       console.log(ev);
        
       // Enter
       if (key === 13) {
          this.toggleExpansion(); 
       }
        
       // Esc
       if (key === 27) {
          this.collapse();
       }
    },
    
    // Render 
    render: function() {
        // You can think of the render function as the "Template"
        // Here is where we set our state on the actual DOM element
        // We detect via the expanded state of the component what state
        // things like aria-hidden and tab-indexs should be in as well as
        // adjusting class names to change appearance.
        // A REAL nice way to build UI, give it a try.
        // DO NOTE: This is not the render function that is responsible for actually placing the element on the screen. That comes later :)
        
        var expandState = this.state.expanded ? 'is-expanded' : 'is-collapsed',
            hiddenState = this.state.expanded ? '' : 'hidden',
            ariaHiddenState = this.state.expanded ? 'false' : 'true',
            tabState = this.state.expanded ? '0' : '-1',
            classes = "accordion-item " + expandState;
        
        // OK, now take all that state and tell React how to render it into a template
        return (
            <div className={classes}>
                <header onClick={this.handleClick} onKeyDown={this.handleKeyPress} tabIndex="0">
                  <span>Tab-Index = {tabState}</span><span>Aria-Hidden = {ariaHiddenState}</span><span>Hidden = {hiddenState} </span>
                </header>
                <section aria-hidden={ariaHiddenState} hidden={hiddenState} tabIndex={tabState} onKeyDown={this.handleKeyPress}>
                    <h2>Content</h2> 
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Enim inventore velit sint quod blanditiis, sapiente voluptatibus, molestiae, dolore ipsam labore quaerat veritatis fuga libero! Explicabo aperiam sapiente optio consectetur placeat.
                </section>
            </div>
        );
    }
});

// Where the "Magic" happens. 
React.render(
    <div>
        <Accordion theme="red" toggleType="all">
            <AccordionItem />
            <AccordionItem expanded="true" />
            <AccordionItem />
            <AccordionItem />
        </Accordion>
    
          <Accordion theme="light-blue" toggleType="single">
            <AccordionItem />
            <AccordionItem />
            <AccordionItem />
            <AccordionItem />
         </Accordion>
    </div>    
   , renderNode);
              
            
!
999px

Console