Skip to content

Instantly share code, notes, and snippets.

@tuxsudo
Created February 10, 2017 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxsudo/a53a1a74926cb0223e6ca94fa977320f to your computer and use it in GitHub Desktop.
Save tuxsudo/a53a1a74926cb0223e6ca94fa977320f to your computer and use it in GitHub Desktop.
DrawerFaC.js
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