Created
February 10, 2017 21:48
-
-
Save tuxsudo/a53a1a74926cb0223e6ca94fa977320f to your computer and use it in GitHub Desktop.
DrawerFaC.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {Component} from 'react'; | |
import join from 'join-classnames'; | |
import styles from './DrawerFaC.css'; | |
export default class DrawerFaC extends Component { | |
static defaultProps = { | |
open: false | |
}; | |
state = { | |
open: this.props.open, | |
maxHeight: "100vh" | |
}; | |
componentDidMount() { | |
if(typeof window === "object") { | |
this.setState({ | |
maxHeight: `${this.node ? this.node.scrollHeight : 1000}px` | |
}); | |
} | |
} | |
close = () => { | |
this.setState({open: false}); | |
this.props.onClose && this.props.onClose(); | |
}; | |
open = () => { | |
this.setState({open: true}); | |
this.props.onOpen && this.props.onOpen() | |
}; | |
toggle = () => { | |
this.setState( (state) => { | |
const open = !state.open; | |
const hook = open | |
? this.props.onOpen | |
: this.props.onClose; | |
if(hook) { hook(); } | |
return ({open}); | |
}) | |
}; | |
handleRef = (node) => ( | |
this.node = node | |
); | |
Drawer = ({className, ...props}) => ( | |
<div | |
{...props} | |
ref={this.handleRef} | |
style={{maxHeight: this.state.open ? this.state.maxHeight : 0}} | |
className={join(className, styles.Body)} | |
/> | |
); | |
render() { | |
const {children} = this.props; | |
const {open: isOpen} = this.state; | |
const {Drawer, toggle: doToggle, close: doClose, open: doOpen} = this; | |
return children({ | |
Drawer, | |
isOpen, | |
doToggle, | |
doOpen, | |
doClose | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment