Skip to content

Instantly share code, notes, and snippets.

@iamchristough
Created February 7, 2019 17:12
Show Gist options
  • Save iamchristough/60719ae40662014cee4ab316a9dcc3aa to your computer and use it in GitHub Desktop.
Save iamchristough/60719ae40662014cee4ab316a9dcc3aa to your computer and use it in GitHub Desktop.
Collapse React Component
class Collapse extends React.Component {
constructor(props) {
super(props);
this.state = {
collapsed: !!props.collapsed
};
this.style = {
collapsed: {
display: 'none'
},
expanded: {
display: 'block'
},
buttonStyle: {
display: 'block',
width: '100%'
}
};
this.toggleCollapse = this.toggleCollapse.bind(this);
}
toggleCollapse() {
this.setState(state => ({ collapsed: !state.collapsed }));
}
render() {
return (
<div>
<button style={this.style.buttonStyle} onClick={this.toggleCollapse}>
Show/Hide Content
</button>
<div
style= {this.state.collapsed ? this.style.collapsed : this.style.expanded}
aria-expanded = {this.state.collapsed}
>
{this.props.children}
</div>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment