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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <h3>This is accordion</h3>
<div class="app"></div>
              
            
!

CSS

              
                body
  background-color: #2f2f31

h3
  text-align: center
  color: #ddd

.accordion
  display: block
  width: 400px
  margin: 50px auto
  padding: 10px
  border: 5px solid rgba(255, 255, 255, .3)
  border-radius: 6px
  box-shadow: 0px 0px 300px 1px #333
  
  ul 
    margin: 0px
    padding: 0px
    
  
  .accordion-item
    background-color: rgba(255, 255, 255, .1)
    border-radius: 4px
    list-style: none
    padding: 15px
    margin-bottom: 5px
    overflow: hidden
    &:hover
      cursor: pointer
    &:active
      background-color: #2f2f31
      
    header
      padding: 0px
      margin: 0px
      i
        transition: 0.3s
      span 
        color: #ddd
        display: inline-block
        vertical-align: middle
        &:hover 
          cursor: pointer
          color: #fff

      span:first-child 
        padding-top: 5px
        
        
      span:last-child
        padding-left: 5px
      
    p
      border-top: 1px solid #555
      padding-top: 15px
      color: #999
    
    .accordion-content
      display: none

    .active
      display: block
              
            
!

JS

              
                const { Component, PropTypes } = React;

const acc_items = [
  {
    id: 0,
    title: "This is item 1",
    text: "Lorem ipsum dolor sit amet",
  },
  {
    id: 1,
    title: "This is item 2",
    text: "Lorem ipsum dolor sit amet",
  },
  {
    id: 2,
    title: "This is item 3",
    text: "Lorem ipsum dolor sit amet",
  },
  {
    id: 3,
    title: "This is item 4",
    text: "Lorem ipsum dolor sit amet",
  },
];

class AccordionItem extends Component {
  render() {
    const { title, text, isOpened } = this.props.data;
    const { togleItem } = this.props.show_content;
    
    return (
       <li  id={ this.props.data.id } 
            className="accordion-item">
        <header onClick={ event => togleItem(event) }>
          <span><i className="material-icons">expand_more</i></span>
          <span>{ title }</span>
        </header>
        <div className={ "accordion-content animated " + (isOpened? "active fadeInDown": "") }>
          <p>{ text }</p>
        </div>
      </li>
    );
  }
}

class Accordion extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: this.props.data,
      togleItem: this.togleItem.bind(this),
    };
  }
  
  togleItem(event) {
    let objs = this.state.items;
    
    for (const i in objs) {
      objs[i].isOpened = false;
      
      if (Number(event.target.parentNode.id) === objs[i].id) {
        objs[i].isOpened = true;
      }
    }
    
    this.setState({
      items: objs,
    });
    
    console.log(event.target.parentNode);
  }
  
  render() {
    return (
      <div className="accordion">
        <ul>
          { this.state.items.map((item, index) =>
            <AccordionItem 
              id={ index } 
              data={ item } 
              show_content={ this.state }/> 
          )}
        </ul>
      </div>
    );
  }
}

Accordion.propTypes = {
  items: PropTypes.array.isRequired
}

ReactDOM.render(
  <Accordion data={ acc_items }/>,
  document.querySelector('.app'));

              
            
!
999px

Console