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

              
                <details-accordion title="Click to expand this details with a WAAPI sliding effect">
  <p>
    Lorem, ipsum dolor sit amet consectetur adipisicing elit. Modi unde, ex rem voluptates autem aliquid veniam quis temporibus repudiandae illo, nostrum, pariatur quae! At animi modi dignissimos corrupti placeat voluptatum!
  </p>
  <img src="https://placebear.com/400/200" alt="a bear">
  <p>
    Facilis ducimus iure officia quos possimus quaerat iusto, quas, laboriosam sapiente autem ab assumenda eligendi voluptatum nisi eius cumque, tempore reprehenderit optio placeat praesentium non sint repellendus consequuntur? Nihil, soluta.
  </p>
</details-accordion>


              
            
!

CSS

              
                body {
  background: #fafafa;
  max-width: 500px;
  margin: 15px;
}
details-accordion {
  --accordian-font: 1em 'Helvetica';
  --accordian-title-font: bold 1em cursive;
  --accordian-title-background: #8dd792;
  --accordian-title-background-open: green;
  --accordian-content-border: 2px solid #8dd792;
}
p {
  margin: 0;
  padding-bottom: 10px;
}
p:last-child {
  padding: 0;
}
img {
  max-width: 100%;
}
              
            
!

JS

              
                import { html, css, LitElement } from 'https://unpkg.com/lit-element/lit-element.js?module';

/*
 * See https://css-tricks.com/how-to-animate-the-details-element-using-waapi/
 */

class DetailsAccordion extends LitElement {
  static get styles() {
    return css`
      :host {
        display: block;
        --accordian-color: black;
        --accordian-background: white;
        --accordian-font: 1em sans-serif;
        --accordian-title-color: black;
        --accordian-title-color-open: white;
        --accordian-title-background: lightgrey;
        --accordian-title-background-open: black;
        --accordian-title-border: 2px solid transparent;
        --accordian-title-focus-border-color: black;
        --accordian-title-font: 1em sans-serif;
        --accordian-content-border: 2px solid lightgray;
      }
      details {
        color: var(--accordian-color);
        background: var(--accordian-background);
        font: var(--accordian-font);
        box-sizing: border-box;
      }
      summary {
        color: var(--accordian-title-color);
        background: var(--accordian-title-background);
        font: var(--accordian-title-font);
        padding: 5px;
        border: var(--accordian-title-border);
        outline: none;
      }
      summary:focus {
        border-color: var(--accordian-title-focus-border-color);
      }
      summary[open] {
        color: var(--accordian-title-color-open);
        background: var(--accordian-title-background-open);
      }
      .content {
        border-top: none;
        padding: 10px;
        border: var(--accordian-content-border);
        border-top: none;
      }
    `;
  }

  render() {
    return html`
      <details ?open="${this.opened}" @click="${this._click}">
        <summary ?open="${this.opened}">${this.title}</summary>
        <div class="content">
          <slot></slot>
        </div>
      </details>
    `;
  }

  static get properties() {
    return {
      title: { type: String },
      opened: { type: Boolean },
    };
  }

  constructor() {
    super();
    this.title = '';
    this.opened = false;

    this._animation = null;
    this._isClosing = false;
    this._isExpanding = false;
  }

  _click(evt) {
    // Stop default behaviour from the browser
    evt.preventDefault();
    // Add an overflow on the <details> to avoid content overflowing
    const details = this.shadowRoot.querySelector('details');
    details.style.overflow = 'hidden';
    // Check if the element is being closed or is already closed
    if (this._isClosing || !this.opened) {
      this._open();
      // Check if the element is being openned or is already open
    } else if (this._isExpanding || this.opened) {
      this._shrink();
    }
  }

  _shrink() {
    // Set the element as "being closed"
    this._isClosing = true;

    const details = this.shadowRoot.querySelector('details');
    const summary = this.shadowRoot.querySelector('summary');
    // Store the current height of the element
    const startHeight = `${details.offsetHeight}px`;
    // Calculate the height of the summary
    const endHeight = `${summary.offsetHeight}px`;

    if (this._animation) {
      // Cancel the current animation
      this._animation.cancel();
    }

    // Start a WAAPI animation
    this._animation = details.animate({
      // Set the keyframes from the startHeight to endHeight
      height: [startHeight, endHeight],
    }, {
      // If the duration is too slow or fast, you can change it here
      duration: 400,
      // You can also change the ease of the animation
      easing: 'ease-out',
    });

    // When the animation is complete, call onAnimationFinish()
    this._animation.onfinish = () => this._animationFinish(false);
    // If the animation is cancelled, isClosing variable is set to false
    this._animation.oncancel = () => { this._isClosing = false };
  }

  _open() {
    const details = this.shadowRoot.querySelector('details');
    // Apply a fixed height on the element
    details.style.height = `${details.offsetHeight}px`;
    // Force the [open] attribute on the details element
    this.opened = true;
    // Wait for the next frame to call the expand function
    window.requestAnimationFrame(() => this._expand());
  }

  _expand() {
    const details = this.shadowRoot.querySelector('details');
    const summary = this.shadowRoot.querySelector('summary');
    const content = this.shadowRoot.querySelector('.content');
    // Set the element as "being expanding"
    this._isExpanding = true;
    // Get the current fixed height of the element
    const startHeight = `${details.offsetHeight}px`;
    // Calculate the open height of the element (summary height + content height)
    const endHeight = `${summary.offsetHeight + content.offsetHeight}px`;

    // If there is already an animation running
    if (this._animation) {
      // Cancel the current animation
      this._animation.cancel();
    }

    // Start a WAAPI animation
    this._animation = details.animate({
      // Set the keyframes from the startHeight to endHeight
      height: [startHeight, endHeight]
    }, {
      // If the duration is too slow of fast, you can change it here
      duration: 400,
      // You can also change the ease of the animation
      easing: 'ease-out'
    });
    // When the animation is complete, call onAnimationFinish()
    this._animation.onfinish = () => this._animationFinish(true);
    // If the animation is cancelled, isExpanding variable is set to false
    this._animation.oncancel = () => { this._isExpanding = false };
  }

  _animationFinish(open) {
    // Set the open attribute based on the parameter
    this.opened = open;
    // Clear the stored animation
    this._animation = null;
    // Reset isClosing & isExpanding
    this._isClosing = false;
    this._isExpanding = false;
    // Remove the overflow hidden and the fixed height
    const details = this.shadowRoot.querySelector('details');
    details.style.height = '';
    details.style.overflow = '';
  }
}

window.customElements.define('details-accordion', DetailsAccordion);
              
            
!
999px

Console