Skip to content

Instantly share code, notes, and snippets.

@eloytoro
Created March 15, 2017 19:38
Show Gist options
  • Save eloytoro/ba1e5e87969bb4c302ff0b906b8fc621 to your computer and use it in GitHub Desktop.
Save eloytoro/ba1e5e87969bb4c302ff0b906b8fc621 to your computer and use it in GitHub Desktop.
import React, { Component, PropTypes } from 'react';
import { findDOMNode } from 'react-dom';
import { Layout } from 'app/components/base/Layout';
import classnames from 'classnames';
import style from './style.css';
const nextFrame = window.requestAnimationFrame || (cb => setTimeout(cb, 0));
class Drawer extends Component {
state = {
height: 0,
animation: true
}
componentWillReceiveProps(nextProps) {
if (this.props.open !== nextProps.open) {
if (!nextProps.open) {
nextFrame(() => this.setState({ height: 0 }));
}
this.setState({
height: this.drawer.offsetHeight,
animation: true
});
}
}
handleTransitionEnd = (event) => {
event.stopPropagation();
this.setState({
height: this.props.open ? 'auto' : 0,
animation: !this.props.open
});
}
render() {
const { height, animation } = this.state;
const { children, className } = this.props;
return (
<Layout
onTransitionEnd={this.handleTransitionEnd}
className={classnames(style.viewport, {
[style.animation]: animation
})}
style={{ height }}
>
<div
ref={node => this.drawer = findDOMNode(node)}
className={classnames(style.drawer, className, {
[style.float]: height !== 'auto'
})}
>
{children}
</div>
</Layout>
);
}
}
Drawer.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
open: PropTypes.bool
};
export default Drawer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment