Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chrispeterson3/45302bd1561ebe144f68f9e55d5635cb to your computer and use it in GitHub Desktop.
Save chrispeterson3/45302bd1561ebe144f68f9e55d5635cb to your computer and use it in GitHub Desktop.
React CSS Accordion
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