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

              
                  <button type="button" onclick="openMenu();">Open</button>
  <button type="button" onclick="closeMenu();">Close</button>
  <button type="button" onclick="focusMenu();">Focus</button>
  <button type="button" onclick="blurMenu();">Blur</button>
  <div>
    <menu-group id="test-target" rotate>
      <smart-button type="simple-text">Text 1</smart-button>
      <smart-button type="simple-text">Text 2</smart-button>
      <smart-button type="simple-text">Text 3</smart-button>
      <smart-button type="simple-text">Text 4</smart-button>
      <smart-button type="simple-text">Text 5</smart-button>
    </menu-group>
  </div>
              
            
!

CSS

              
                /**
 * Start of Simple Text Button
**/

smart-button[type="simple-text"] {
  display: inline-block;

  font-family: "Fira Sans";
  font-style: italic;
  font-size: 2.4rem;
  line-height: 8rem;
 
  transition-property: all;
  transition-timing-function: cubic-bezier(0.25, 0, 0, 1.0);
  transition-duration: 0.42s;
 
  color: #dddddd;
  background: none;
  border: none;
  border-radius: 0;

  width: auto;
  height: 8rem;

  padding-left: 2.6rem;
  padding-right: 2.6rem;

  -moz-user-select: none;
}

smart-button[type="simple-text"].focused {
  outline: 0;
  transform: scale(1.2);
  transition-duration: 0.42s;

  color: #ffffff;
  background: none;
}

smart-button[type="simple-text"].released {
  transition-duration: 0.16s;
}

smart-button[type="simple-text"].pressed {
  color: #00caf2;
  background: none;
  transform: scale(0.8);
  transition-duration: 0.06s;
}

/**
 * End of Simple Text Button
**/

/**
 * Button state:
 *    1. normal (class name: [none])
 *    2. enlarging (class name: enlarging)
 *    3. shrinking (class name: shrinking)
 *    4. opening (class name: opening)
 *    5. closing (class name: closing)
 *    6. closed (class name: closed)
**/

/**
 * focus: closed -> closed + enlarging -> closed + shrinking -> opening -> ''
 * close: '' -> closing -> closed;
**/

menu-group {
  outline: 0;
  display: inline-block;
  height: 8rem;
  padding-left: 8rem;
  padding-right: 4rem;
  opacity: 1;
  /* we need to set border to none to remove the button border */
  border: none;
  /* the radius is still make button as round even if we don't have border */
  border-radius: 4rem;
  background-color: rgba(0, 0, 0, 0.5);

  word-break: keep-all;
  white-space: nowrap;
  overflow: hidden;
  position: relative;

  transition-property: all;
  transition-timing-function: cubic-bezier(0.25, 0, 0, 1.0);
  transition-duration: 0.42s;
}

menu-group:before {
  content: " ";
  background-repeat: no-repeat;
  background-size: 5.4rem auto;
  background-position: 1.3rem center;
  width: 8rem;
  height: 8rem;
  display: inline-block;
  position: absolute;
  left: 0;

  transition-property: all;
  transition-timing-function: cubic-bezier(0.25, 0, 0, 1.0);
  transition-duration: 0.42s;
}

menu-group[rotate]:before {
  transform: rotate(360deg);
  transition-property: all;
  transition-timing-function: cubic-bezier(0.25, 0, 0, 1.0);
  transition-duration: 0.42s;
}

menu-group[data-icon]:before {
  width: 5.4rem;
  left: 1.3rem;
  font-size: 5.4rem;
  line-height: 8rem;
  text-align: center;
  color: #ffffff;
  white-space: normal;
  top: 0;
}

menu-group[data-icon].switching-icon:before {
  font-size: 5.4rem;
  line-height: 8rem;
  text-align: center;
  color: #ffffff;
  /**
   * According to visual spec, we have falling down transition while icon
   * change.
   *
   * The trick is:
   *   1. to put the next icon at the top of current and slide icons down.
   *   2. to set transition duration to 0 here to elimiate the slide up
   *      transition.
  **/
  top: -8rem;
  transition: none;
}

menu-group.closed {
  padding-right: 0;

  transition-duration: 0.5s;
  background-color: rgba(0, 0, 0, 0.5);
}

menu-group.closed > * {
  display: none;
}

menu-group[rotate].closed:before {
  transform: rotate(0deg);
}

menu-group.opening {
  transition-duration: 0.72s;
  /* add the same padding-right of normal to make it run transition */
  padding-right: 4rem;
}

menu-group.opening:before {
  transition-duration: 0.72s;
}

menu-group.closing {
  transition-duration: 0.5s;
  padding-right: 0;
  background-color: rgba(0, 0, 0, 0.5);
}

menu-group[rotate].closing:before {
  transform: rotate(0deg);
  transition-duration: 0.5s;
}

menu-group.enlarging {
  opacity: 1;
  background-color: #ffffff;
  transform: scale(1.2);
  transition-duration: 0.2s;
}

menu-group.shrinking {
  transition-duration: 0.23s;
}

/* example */
html,
body {
  margin: 0;
  padding: 0;
  font-size: 10px;
  background-color: gray;
}
menu-group:before {
  background-image: url('https://raw.githubusercontent.com/mozilla-b2g/gaia/master/tv_apps/smart-home/style/images/ic_setting%402x.png');
}
menu-group.enlarging:before {
  background-image: url('https://raw.githubusercontent.com/mozilla-b2g/gaia/master/tv_apps/smart-home/style/images/ic_setting.png');
}
              
            
!

JS

              
                'use strict';
/* global evt */

window.MenuGroup = (function(win) {

  // Extend from the HTMLElement prototype
  var proto = evt(Object.create(HTMLElement.prototype));

  proto.createdCallback = function() {
    this.style.width = '0';
    this.classList.add('closed');
  };

  proto.calculateChildWidth = function() {
    var child = this.firstElementChild;
    var childWidth = 0;
    var style;
    while(child) {
      style = window.getComputedStyle(child);
      childWidth += child.offsetWidth + parseInt(style.marginLeft, 10) +
                                        parseInt(style.marginRight, 10);
      child = child.nextElementSibling;
    }
    return childWidth;
  };

  proto.fireEvent = function(event, detail) {
    var evtObject = new CustomEvent(event, {
                                      bubbles: false,
                                      detail: detail || this
                                    });
    this.dispatchEvent(evtObject);
    this.fire(event, detail);
  };

  proto.focus = proto.open = function() {
    this.getAnimations().forEach((animation) => {
      animation.cancel();
    });
    
    this.fireEvent('will-open');
    
    this.animateEnlarge()
    .then(this.animateShrink.bind(this), this.resetToClose.bind(this))
    .then(this.animateOpen.bind(this), this.resetToClose.bind(this))
    .then( () => {
      this.classList.remove('opening');
      this.fireEvent('opened');
    }, () => {
      this.resetToOpen();
      this.fireEvent('opened'); 
    });
  };

  proto.blur = proto.close = function() {
    this.getAnimations().forEach((animation) => {
      animation.cancel();
    });
    
    this.fireEvent('will-close');
    this.animateClose()
    .then( () => {
      this.classList.remove('closing');
      this.classList.add('closed');
      // final state: closed
      this.fireEvent('closed');
    }, () => {
      this.classList.remove('closing');
      this.classList.add('closed');
      // final state: closed
      this.fireEvent('closed');
    });
  };
  
  proto.animateEnlarge = function() {
    console.log('animateEnlarge');
    // If we get focus when we closing the group, we need to cancel the closing
    // state.
    this.classList.remove('shrinking');
    this.classList.remove('opening');
    this.classList.remove('closing');
    this.classList.add('enlarging');
    
    return this.getTransition('background-color').finished;
  };
  
  proto.animateShrink = function() {
    console.log('aniamteShrink');
    this.classList.remove('enlarging');
    this.classList.add('shrinking');
    
    return this.getTransition('background-color').finished;
  };
  
  proto.animateOpen = function() {
    console.log('animateOpen');
    this.classList.remove('shrinking');
    this.classList.remove('closed');
    this.classList.add('opening');
    // change to opening
    this.style.width = this.calculateChildWidth() + 'px';
    
    return this.getTransition('width').finished;
  };
  
  proto.animateClose = function() {
    console.log('animateClose');
    
    if (this.classList.contains('closed')) {
      var promise = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve();
        });
      });
      return promise;
    }
    // We may call close at mid state, we need to reset all of them and go to
    // closing state
    this.classList.remove('enlarging');
    this.classList.remove('shrinking');
    this.classList.remove('opening');
    this.classList.add('closing');
    this.style.width = '0';
    
    var transition = this.getTransition('width');
    return transition ? transition.finished : this.getTransition('background-color').finished;
  };
  
  proto.getTransition = function(attr) {
    var transition;
    this.getAnimations().forEach( (animation) => {
      if (animation.transitionProperty === attr) {
        transition = animation;
      }
    });
    
    return transition;
  };
    
  proto.resetToOpen = function() {
 /*   this.classList.remove('shrinking');
    this.classList.remove('closed');*/
    this.classList.remove('opening');
  };
  
  proto.resetToClose = function() {
    this.classList.remove('enlarging');
    this.classList.remove('shrinking');
    this.classList.remove('opening');
    this.classList.remove('closing');
    this.classList.add('closed');
  };

  // Register and return the constructor
  return document.registerElement('menu-group', { prototype: proto });
})(window);

/* End of Menu Group component */

// Handle button clicks to trigger menu group behaviors
window.onload = function() {
  
  window.openMenu = function () {
    document.getElementById('test-target').open();
  };
  window.closeMenu = function () {
    document.getElementById('test-target').close();
  };
  window.focusMenu = function () {
    document.getElementById('test-target').focus();
  };
  window.blurMenu = function () {
    document.getElementById('test-target').blur();
  };
  
  ['opened', 'closed', 'will-open', 'will-close'].forEach(function(evt) {
    document.getElementById('test-target').addEventListener(evt,
      function() {
        console.log(evt + ' received!');
      });
  });
};
              
            
!
999px

Console