Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <template data-component="my-accordion-item">
  <style>
    button {
      display: flex;
      width: 100%;
      justify-content: space-between;
      align-items: center;
    }

    .accordion-content {
      display: none;
    }

    .accordion-content--open {
      display: block;
    }

    ::slotted([slot="icon"]) {
      width: 30px;
      height: 30px;
    }

    :host([open]) ::slotted([slot="icon"]) {
      transform: rotate(-90deg);
    }
  </style>

  <button>
    <h2>title</h2>
    <slot name="icon"></slot>
  </button>

  <div class="accordion-content">
    <slot></slot>
  </div>
</template>

<my-accordion>
  <my-accordion-item open title="Accordion 1">
    <svg viewBox="0 0 20 20" slot="icon">
      <path fill="currentColor" d="M8.388,10.049l4.76-4.873c0.303-0.31,0.297-0.804-0.012-1.105c-0.309-0.304-0.803-0.293-1.105,0.012L6.726,9.516c-0.303,0.31-0.296,0.805,0.012,1.105l5.433,5.307c0.152,0.148,0.35,0.223,0.547,0.223c0.203,0,0.406-0.08,0.559-0.236c0.303-0.309,0.295-0.803-0.012-1.104L8.388,10.049z"></path>
    </svg>
    Lorem ipsum dolor sit amet consectetur, adipisicing elit. Cum quia ratione, doloremque nemo qui veritatis laudantium et libero eos minima. Aspernatur unde dolores facere illum doloremque, odio soluta minima laudantium vitae porro expedita, quidem commodi.
  </my-accordion-item>
  <my-accordion-item title="Accordion 2">
    Lorem ipsum dolor sit amet consectetur, adipisicing elit. Cum quia ratione, doloremque nemo qui veritatis laudantium et libero eos minima. Aspernatur unde dolores facere illum doloremque, odio soluta minima laudantium vitae porro expedita, quidem commodi.
  </my-accordion-item>
  <my-accordion-item title="Accordion 3">
    Lorem ipsum dolor sit amet consectetur, adipisicing elit. Cum quia ratione, doloremque nemo qui veritatis laudantium et libero eos minima. Aspernatur unde dolores facere illum doloremque, odio soluta minima laudantium vitae porro expedita, quidem commodi.
  </my-accordion-item>
  <my-accordion-item title="Accordion 4">
    Lorem ipsum dolor sit amet consectetur, adipisicing elit. Cum quia ratione, doloremque nemo qui veritatis laudantium et libero eos minima. Aspernatur unde dolores facere illum doloremque, odio soluta minima laudantium vitae porro expedita, quidem commodi.
  </my-accordion-item>
</my-accordion>
              
            
!

CSS

              
                :not(:defined) {
  display: none;
}

body {
  padding: 1rem;
}

my-accordion {
  display: block;
  max-width: 750px;
  margin: 0 auto;
}

my-accordion-item {
  display: block;

  & + & {
    margin-top: 1rem;
  }
}

button {
  background: red;
}

              
            
!

JS

              
                console.clear();

/**
 * Defines the <my-accordion> element
 */
class myAccordion extends HTMLElement {
  constructor() {
    super();
    this.addEventListener("accordionOpen", this.onAccordionOpen.bind(this));
  }

  /**
   * Catches the accordionOpen event to toggle other accordions closed
   */
  onAccordionOpen(event) {
    const items = Array.from(this.querySelectorAll("my-accordion-item"));

    const itemsToClose = items.filter((item) => item !== event.detail.item);

    itemsToClose.forEach((item) => {
      item.toggleState(false);
    });
  }
}

/**
 * Defines the <my-accordion-item> element
 */
class myAccordionItem extends HTMLElement {
  /**
   * Required to build the new element in the DOM
   */
  constructor() {
    super();

    // Get template to seed the shadowRoot.
    const template = document.querySelector(
      'template[data-component="my-accordion-item"]'
    );

    // Creates a shadowroot, injects template contents
    const root = this.attachShadow({
      mode: "open"
    });
    root.appendChild(template.content.cloneNode(true));

    // Store references to internal components
    this.heading = root.querySelector("button h2");
    this.content = root.querySelector(".accordion-content");

    // Wire up internal logic
    root
      .querySelector("button")
      .addEventListener("click", this.clickHandler.bind(this));

    // Set initial state
    this.open = this.hasAttribute("open") ? true : false;
    this.toggleState(this.open);

    this.heading.innerText = this.getAttribute("title");
  }

  /**
   * Toggles the open state of the accordion item.
   * If an argument is provided, it acts as a one-way toggle.
   */
  toggleState(forceState) {
    const toState = forceState !== undefined ? Boolean(forceState) : !this.open;

    this.open = toState;
    this.content.classList.toggle("accordion-content--open", toState);

    if (this.open) {
      this.setAttribute("open", "");
    } else {
      this.removeAttribute("open");
    }
  }

  /**
   * Toggles this accordion item and dispatches an event to
   * the accordion wrapper to close other items.
   */
  clickHandler(event) {
    this.toggleState();

    const toggleEvent = new CustomEvent("accordionOpen", {
      bubbles: true,
      cancelable: true,
      composed: true,
      detail: {
        item: this,
        open: this.open
      }
    });

    this.dispatchEvent(toggleEvent);
  }

  /**
   * Callback anytime an attribute changes
   * Only triggered if attribute is in list of this.observedAttributes
   */
  attributeChangedCallback(name, oldValue, newValue) {
    if (name === "title") {
      this.heading.innerText = newValue;
    }
  }

  /**
   * List of attributes to respond to with attributeChangedCallback
   */
  static get observedAttributes() {
    return ["title", "open"];
  }
}

/**
 * Define our custom elements in the registry
 *
 * https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define
 */
customElements.define("my-accordion", myAccordion);
customElements.define("my-accordion-item", myAccordionItem);

              
            
!
999px

Console