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

              
                <my-accordion>
  <my-accordion-item title="Accordion 1">
    <svg class="svg-icon" 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;
}

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

my-accordion-item {
  display: block;

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

button {
  background: red;
}

              
            
!

JS

              
                import { LitElement, css, html } from "https://cdn.skypack.dev/lit";
import {
  customElement,
  property
} from "https://cdn.skypack.dev/lit/decorators.js";
import { classMap } from "https://cdn.skypack.dev/lit/directives/class-map.js";

@customElement("my-accordion")
export class MyAccordion extends LitElement {
  // Render the UI as a function of component state

  connectedCallback() {
    const { handleAccordionToggle } = this;
    super.connectedCallback();

    this.addEventListener("accordionToggle", handleAccordionToggle);
  }

  private handleAccordionToggle(event) {
    const { item: toggledItem, status: toggledItemStatus } = event.detail;

    if (toggledItemStatus) {
      Array.from(this.querySelectorAll("my-accordion-item"))
        .filter((item) => item !== toggledItem)
        .forEach((item) => {
          item.open = false;
        });
    }
  }

  render() {
    return html`
      <slot></slot>
    `;
  }
}

@customElement("my-accordion-item")
export class MyAccordionItem extends LitElement {
  @property({ attribute: "title", reflect: true })
  title?: string;

  @property()
  open: boolean = false;

  private clickHandler(event) {
    this.open = !this.open;

    const accordionToggleEvent = new CustomEvent("accordionToggle", {
      bubbles: true,
      cancelable: false,
      composed: true,
      detail: {
        item: this,
        status: this.open
      }
    });

    this.dispatchEvent(accordionToggleEvent);
  }

  render() {
    const { title, open } = this;

    const classes = {
      "accordion-content": true,
      "accordion-content--open": open
    };

    return html`
      <button @click=${this.clickHandler}>
        <h2>${title}</h2>
        <slot name="icon"></slot>
      </button>

      <div class="${classMap(classes)}">
        <slot></slot>
      </div>
    `;
  }

  static styles = css`
    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;
    }
  `;
}

              
            
!
999px

Console