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

              
                <div class="slidable-flip-demo"></div>
              
            
!

CSS

              
                $base-color: #f0f0d8;
$background-color: darken($base-color, 5%);
$border-color: #604848;
$filler-color: darken($base-color, 20%);
$text-color: #6f6e5a;

$rhythm: 20px;

// Slidable component-specific styles
.Slidable {
  .slidable-container {
    overflow: visible;
    position: relative;
  }

  .slidable-content {
    position: absolute;
    width: 100%;

    > * {
      position: relative;
      z-index: 10;
    }
  }

  .previous-children {
    position: absolute;
    top: 0;
    width: 100%;
    z-index: 1;
  }
}



// demo styles
.slidable-flip-demo {
  background: $background-color;
  border: 1px solid #ddd;
  color: $text-color;
  font-family: Helvetica;
  margin: 0 auto;
  max-width: 800px;
  min-height: 200px;
  text-align: center;
  position: relative;
  width: 100%;
  
  header {
    cursor: pointer;
    font-size: 24px;
    font-weight: bold;
    line-height: $rhythm * 2;
    padding: 10px 0;
    
    &:hover {
      background: darken($background-color, 5%);
    }
  }
  
  strong {
    font-weight: bold;
  }
  
  a {
    &, &:visited {
      color: inherit;
      
      &:hover {
        color: darken($text-color, 10%);
      }
    }
  }
  
  h3 {
    font-size: 20px;
    font-weight: bold;
    line-height: $rhythm * 2;
    padding: $rhythm 0 0;
  }
  
  p {
    margin: 20px auto;
    line-height: $rhythm;
    width: 80%;
  }
  
  ul {
    display: flex;
    flex-flow: row wrap;
    width: 100%;
        
    li {
      border: 5px solid $border-color;
      flex: 1 1 25%;
      
      &:nth-child(2n) {
        border-color: lighten($border-color, 20%);
      }
      
      img {
        display: block;
        width: 100%;
      }
    }
  }
  
  .drawer {
    background: $base-color; 
  }
  
  .other-things-on-the-page {
    font-size: 0;
    position: relative;
      
    img {
      // this is just a hack for prefetching the image since our list elements in the slidable are auto-height
      display: none;
    }
    
    > div {
      background: $filler-color;
      display: inline-block;
      height: 5px;
      margin: 0;
      padding: 0;
      width: 5px;
    }
  }
}
              
            
!

JS

              
                const IMG_SOURCE = 'https://gfp-nnw0orriao.netdna-ssl.com/wp-content/uploads/2016/07/Bernese-Mountain-Dog-600x600.jpg';

class Slidable extends React.Component {
  constructor(props) {
    super();
    
    this.state = {
      /** {number} - The pixel height of the container. Or `null` to set the height to auto. */
      height: null,
      /**
       * {React.Children} - We now cycle through two children, children0 and children1, and
       * update the classname of their container based on which one is currently the previous
       * children. Initially, children0 is current and children1 is previous, which is null.
       * After the transition, we set our previous children position to null.
       */
      children0: props.children,
      children1: null,
      /** {number} - reference to the which children set are the previous children */
      previousChildrenPosition: 1,
      /**
        * {number} - The previous pixel height of the container, used to determine when to update.
        *    To minimize forced layouts, we saved the current height as the previous height after
        *    that height's animation.
        */
      prevHeight: 0,
      /** {boolean} - Whether or not the component is in the process of changing height. */
      transitioning: false
    };
  }

  componentWillReceiveProps(nextProps) {
    var newChildrenNumber = (this._arePreviousChildrenPositionOne()) ? 1 : 0,
        newPreviousChildrenPosition = (this._arePreviousChildrenPositionOne()) ? 0 : 1;

    if (this.props.updateTriggerCondition !== nextProps.updateTriggerCondition) {
      // if previous children is currently child number 2, replace it with new children
      // which would be `this.props` after update
      this.setState({
        [`children${newChildrenNumber}`]: nextProps.children,
        previousChildrenPosition: newPreviousChildrenPosition,
        prevHeight: ReactDOM.findDOMNode(this.refs.content).offsetHeight,
        transitioning: false
      });
    } else {
      // we have to update children props every time
      this.setState({
        [`children${newPreviousChildrenPosition}`]: nextProps.children
      });
    }
  }

  componentDidUpdate(prevProps) {
    var containerEl = ReactDOM.findDOMNode(this.refs.container),
        contentHeight,
        translateHeight,
        flipEls;

    if (this.props.updateTriggerCondition !== prevProps.updateTriggerCondition) {
      window.requestAnimationFrame(() => {
        // this.state.prevHeight is First. here’s our Last
        contentHeight = ReactDOM.findDOMNode(this.refs.content).offsetHeight;
        // this is how much we are going to Invert by
        translateHeight = this.state.prevHeight - contentHeight;

        if (contentHeight !== this.state.prevHeight && !this.state.transitioning) {
          this.setState({
            height: !this.props.FLIPSelector ? this.state.prevHeight : null,
            transitioning: true
          }, () => {
            if (this.props.FLIPSelector) {
              flipEls = document.querySelectorAll(this.props.FLIPSelector);
              [].forEach.call(flipEls, (el) => {
                el.style.position = 'relative';
                el.style.transform = `translateY(${translateHeight}px)`;
                el.style.willChange = 'transform';
                el.style.zIndex = 100000;
              });

              containerEl.style.overflowY = 'visible';

              // request another frame so that the new styles have a chance to take effect
              window.requestAnimationFrame(() => {
                [].forEach.call(flipEls, (el) => {
                  // and this is where we Play!
                  el.style.transform = 'none';
                  el.style.transition = `transform ${this.props.transitionDuration}ms`;
                });
                
                // use setTimeout because transitionend events fire inconsistently across browsers 🙁
                window.setTimeout(this.onTransitionEnd.bind(this), this.props.transitionDuration);
              });
            } else {
              // we still default to animating by height
             window.requestAnimationFrame(() => {
               window.setTimeout(this.onTransitionEnd.bind(this), this.props.transitionDuration);
               this.setState({height: contentHeight})    
             });
           }
         });
       }
      });
    }
  }

  /**
   * Called when the height transition ends on the container. It removes the
   * content from the DOM when the container is done sliding out, and sets
   * the height to 'auto' after the container is done sliding in.
   */
  onTransitionEnd() {
    var childrenNumberToRemove = (this._arePreviousChildrenPositionOne()) ? 1 : 0,
        containerEl = ReactDOM.findDOMNode(this.refs.container);
    
    if (this.props.FLIPSelector) {
      // unset all changed CSS on any of the FLIPping elements
      [].forEach.call(document.querySelectorAll(this.props.FLIPSelector), (el) => {
        el.style.position = '';
        el.style.transform = '';
        el.style.transition = '';
        el.style.willChange = '';
        el.style.zIndex = '';
      });

      containerEl.style.overflowY = '';
    }

    this.setState({
      [`children${childrenNumberToRemove}`]: null,
      height: null
    });
  }

  _arePreviousChildrenPositionOne() {
    return this.state.previousChildrenPosition === 1;
  }
  
  render() {
    var contentStyle = {
          position: (this.state.height === null) ? 'relative' : 'absolute'
        },
        containerStyle = {
          height: (this.state.height === null) ? 'auto' : this.state.height,
          transition: `height ${this.props.transitionDuration}ms`
        };

    return (
      <div className='Slidable'>
        <div ref='container' style={containerStyle} className='slidable-container'>
          <div ref='content' style={contentStyle} className='slidable-content'>
            {/* treat each child set identically and only edit the classname for each */}
            <div className={!this._arePreviousChildrenPositionOne() ? 'previous-children' : ''}>
              {this.state.children0}
            </div>
            <div className={this._arePreviousChildrenPositionOne() ? 'previous-children' : ''}>
              {this.state.children1}
            </div>
          </div>
        </div>
      </div>
    );
  }
}


class SlidableFLIPDemo extends React.Component {
  constructor() {
    super();
    
    this.state = {drawerOpen: false};
  }
 
  onHeaderClick() {
    this.setState({drawerOpen: !this.state.drawerOpen});
  }
  
  render() {
    return (
      <div className='slidable-flip-demo'>
        <header onClick={this.onHeaderClick.bind(this)}>
          <h1>Toggle me for a surprise!</h1>
        </header>
        <Slidable
          FLIPSelector='.other-things-on-the-page'
          transitionDuration={400}
          updateTriggerCondition={this.state.drawerOpen}
        >
          {this.state.drawerOpen ? (
            <div className='drawer'>
              <h3>...it's PUPPIEEEEES!!!</h3>
              <p>
                We've added a bunch of dummy elements in <strong>.other-things-on-the-page</strong> below to mimic the work the browser has to do in the real-world. But by animating only the <strong>transform</strong> property, the work required is much, much less, and it's easier to hit 60fps.  FLIP FTW!
              </p>
              <p>
                To learn more about FLIP, <a href='https://aerotwist.com/blog/flip-your-animations/'>take it straight from the master, Paul Lewis</a>.
              </p>
              <ul>
                <li><img src={IMG_SOURCE} /></li>
                <li><img src={IMG_SOURCE} /></li>
                <li><img src={IMG_SOURCE} /></li>
              </ul>
            </div>
          ) : null}
        </Slidable>
        <section className='other-things-on-the-page'>
          <img src={IMG_SOURCE} />
          {Array.apply(null, new Array(20000)).map(() => <div />)}
        </section>
      </div>
    )
  }
}

ReactDOM.render(<SlidableFLIPDemo />, document.querySelector('.slidable-flip-demo'));
              
            
!
999px

Console