Created
June 29, 2017 05:19
-
-
Save chrispeterson3/45302bd1561ebe144f68f9e55d5635cb to your computer and use it in GitHub Desktop.
React CSS Accordion
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'; | |
class Content extends Component { | |
constructor (props) { | |
super(props); | |
this.state = { | |
isActive: this.props.isActive, | |
styles: { | |
height: 0, | |
overflow: 'hidden', | |
transition: '250ms ease-in-out' | |
} | |
}; | |
} | |
componentWillReceiveProps (nextProps) { | |
this.setState({ | |
isActive: nextProps.isActive, | |
styles: { | |
height: nextProps.isActive ? `${this.refs.content.clientHeight}px` : 0, | |
overflow: nextProps.isActive ? 'none' : 'hidden', | |
transition: '250ms ease-in-out' | |
} | |
}); | |
} | |
render () { | |
return ( | |
<div className="Accordion-container" style={this.state.styles} ref="container"> | |
<div className="Accordion-content" ref="content"> | |
{this.props.children} | |
</div> | |
</div> | |
); | |
} | |
} | |
export default Content; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment