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

              
                <div class="expando js-expando">
    <div class="expando__inner js-expando-inner">
      <div class="expando__inner-inverter js-expando-inner-inverter">
        <div class="expando__content js-content">
          <ul class="expando__content-list">
            <li>One</li>
            <li>Two</li>
            <li>Three</li>
            <li>Four</li>
          </ul>

          <button class="expando__close js-expando-collapse-btn">Close</button>
        </div>

        <button class="expando__btn js-expando-expand-btn">
          <svg fill="#000000" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
            <path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
            <path d="M0 0h24v24H0z" fill="none"/>
          </svg>
        </button>
      </div>
    </div>
  </div>
              
            
!

CSS

              
                
* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: Arial, Helvetica, sans-serif;
}

body {
  background: #333;
}

.expando {
  border-radius: 2px;
  overflow: hidden;
  position: fixed;
  top: 20px;
  left: 20px;
  will-change: transform;
}

.expando__inner {
  top: 10px;
  left: 10px;
  border-radius: 50%;
  overflow: hidden;
  background: #FFF;
  position: absolute;
  will-change: transform;
}

.expando__inner-inverter {
  will-change: transform;
}

.expando__btn {
  width: 48px;
  height: 48px;
  position: absolute;
  top: 0;
  left: 0;
  background: none;
  border: none;
  outline: none;
  pointer-events: auto;
  cursor: pointer;
  will-change: transform;
  transition:
      transform 0.1s cubic-bezier(0, 0, 0.31, 1),
      opacity 0.2s cubic-bezier(0, 0, 0.31, 1);
}

.expando__content {
  position: absolute;
  left: -10px;
  top: -10px;
  pointer-events: none;
  opacity: 0;
  will-change: transform;
  transform: translateY(20px);
  transition:
      transform 0.3s cubic-bezier(0, 0, 0.31, 1),
      opacity 0.3s cubic-bezier(0, 0, 0.31, 1);
}

.expando__close {
  position: absolute;
  right: 10px;
  top: 10px;
  outline: none;
  border: none;
  background: none;
  cursor: pointer;
}

.expando__content-list {
  width: 140px;
  margin: 0;
  padding: 30px 0 20px 0;
  list-style: none;
  overflow: hidden;
}

li {
  line-height: 1.9;
  padding: 0 20px;
}

li:hover {
  background: #555;
  color: #FFF;
}

.item--expanded {
  animation-name: expandAnimation;
  animation-duration: 0.7s;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
}

.item__contents--expanded {
  animation-name: expandContentsAnimation;
  animation-duration: 0.7s;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
}

.item--collapsed {
  animation-name: collapseAnimation;
  animation-duration: 0.2s;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
}

.item__contents--collapsed {
  animation-name: collapseContentsAnimation;
  animation-duration: 0.2s;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
}

.item__contents--expanded .expando__content {
  transform: translateY(0px);
  opacity: 1;
  pointer-events: auto;
}

.item__contents--expanded .expando__btn {
  transform: rotate(180deg);
  opacity: 0;
  pointer-events: none;
}
              
            
!

JS

              
                
'use strict';

class Expando {
  constructor () {
    this._el = document.querySelector('.js-expando');
    this._elInner = this._el.querySelector('.js-expando-inner');
    this._elInnerInverter = this._el.querySelector('.js-expando-inner-inverter');
    this._expandBtn = this._el.querySelector('.js-expando-expand-btn');
    this._collapseBtn = this._el.querySelector('.js-expando-collapse-btn');
    this._content = this._el.querySelector('.js-content');

    this.toggle = this.toggle.bind(this);
    this.expand = this.expand.bind(this);
    this.collapse = this.collapse.bind(this);

    this._calculate();
    this._createEaseAnimations();

    this._expandBtn.addEventListener('click', this.expand);
    this._collapseBtn.addEventListener('click', this.collapse);
  }

  expand () {
    if (this._isExpanded) {
      return;
    }
    this._isExpanded = true;
    this._applyAnimation({expand: true});
  }

  collapse () {
    if (!this._isExpanded) {
      return;
    }
    this._isExpanded = false;
    this._applyAnimation({expand: false});
  }

  toggle () {
    if (this._isExpanded) {
      return this.collapse();
    }

    this.expand();
  }

  _applyAnimation ({expand}=opts) {
    this._elInner.classList.remove('item--expanded');
    this._elInner.classList.remove('item--collapsed');
    this._elInnerInverter.classList.remove('item__contents--expanded');
    this._elInnerInverter.classList.remove('item__contents--collapsed');

    // Force a recalc styles here so the classes take hold.
    window.getComputedStyle(this._elInner).transform;

    if (expand) {
      this._elInner.classList.add('item--expanded');
      this._elInnerInverter.classList.add('item__contents--expanded');
      return;
    }

    this._elInner.classList.add('item--collapsed');
    this._elInnerInverter.classList.add('item__contents--collapsed');
  }

  _calculate () {
    const elBCR = this._el.getBoundingClientRect();
    const collapsed = this._expandBtn.getBoundingClientRect();
    const expanded = this._content.getBoundingClientRect();

    const expandedWidth = Math.abs(expanded.right - elBCR.left);
    const expandedHeight = Math.abs(expanded.bottom - elBCR.top);

    const collapsedWidth = collapsed.width;
    const collapsedHeight = collapsed.height;

    const exRadius = Math.sqrt(expandedWidth * expandedWidth +
        expandedHeight * expandedHeight);
    const colRadius = collapsedWidth * 0.5;

    this._scale = (exRadius - colRadius) / colRadius;

    // Set initial sizes.
    this._el.style.width = `${expandedWidth}px`;
    this._el.style.height = `${expandedHeight}px`;

    this._elInner.style.width = `${collapsedWidth}px`;
    this._elInner.style.height = `${collapsedHeight}px`;

    this._elInner.style.transformOrigin =
        `${collapsedWidth * 0.5}px ${collapsedHeight * 0.5}px`;
    this._elInnerInverter.style.transformOrigin =
        `${collapsedWidth * 0.5}px ${collapsedHeight * 0.5}px`;

  }

  _createEaseAnimations () {
    let ease = document.querySelector('.ease');
    if (ease) {
      return ease;
    }

    ease = document.createElement('style');
    ease.classList.add('ease');

    const expandAnimation = [];
    const expandContentsAnimation = [];
    const expandCircleAnimation = [];
    const collapseAnimation = [];
    const collapseContentsAnimation = [];
    const collapseCircleAnimation = [];
    for (let i = 0; i <= 100; i++) {
      const step = this._ease(i/100);

      // Expand animation.
      this._append({
        i,
        step,
        start: 1,
        end: this._scale,
        outerAnimation: expandAnimation,
        innerAnimation: expandContentsAnimation
      });

      // Collapse animation.
      this._append({
        i,
        step,
        start: this._scale,
        end: 1,
        outerAnimation: collapseAnimation,
        innerAnimation: collapseContentsAnimation
      });
    }

    ease.textContent = `
      @keyframes expandAnimation {
        ${expandAnimation.join('')}
      }
      @keyframes expandContentsAnimation {
        ${expandContentsAnimation.join('')}
      }
      @keyframes collapseAnimation {
        ${collapseAnimation.join('')}
      }
      @keyframes collapseContentsAnimation {
        ${collapseContentsAnimation.join('')}
      }`;

    document.head.appendChild(ease);
    return ease;
  }

  _append ({
        i,
        step,
        start,
        end,
        outerAnimation,
        innerAnimation}=opts) {

    const scale = start + (end - start) * step;
    const invScale = 1 / scale;

    outerAnimation.push(`
      ${i}% {
        transform: scale(${scale});
      }`);

    innerAnimation.push(`
      ${i}% {
        transform: scale(${invScale});
      }`);
  }

  _clamp (value, min, max) {
    return Math.max(min, Math.min(max, value));
  }

  _ease (v, pow=4) {
    v = this._clamp(v, 0, 1);

    return 1 - Math.pow(1 - v, pow);
  }
}

new Expando();
              
            
!
999px

Console