A Pen by Artur Sedlukha on CodePen.
Created
July 19, 2019 10:55
-
-
Save alersenkevich/9461798f2eb14dc22333baac7605c818 to your computer and use it in GitHub Desktop.
Accordion react component with animation
This file contains hidden or 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
<div id="app"></app> |
This file contains hidden or 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
const paragraph = 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Eveniet natus sint provident vel ab reprehenderit cum soluta, suscipit facere nisi sed earum repellendus fuga debitis, nam molestiae minima voluptates possimus.' | |
const data = [ | |
{ | |
title: 'Pricing plans', | |
paragraph | |
}, | |
{ | |
title: 'How to apply', | |
paragraph | |
}, | |
{ | |
title: 'Purchasing process', | |
paragraph | |
}, | |
{ | |
title: 'Usage guides', | |
paragraph | |
} | |
] | |
class Accordion extends React.Component { | |
render () { | |
return ( | |
<div {...{ className: 'wrapper' }}> | |
<ul {...{ className: 'accordion-list' }}> | |
{data.map((data, key) => { | |
return ( | |
<li {...{ className: 'accordion-list__item', key }}> | |
<AccordionItem {...data} /> | |
</li> | |
) | |
})} | |
</ul> | |
</div> | |
) | |
} | |
} | |
class AccordionItem extends React.Component { | |
state = { | |
opened: false | |
} | |
render () { | |
const { | |
props: { | |
paragraph, | |
title | |
}, | |
state: { | |
opened | |
} | |
} = this | |
return ( | |
<div | |
{...{ | |
className: `accordion-item, ${opened && 'accordion-item--opened'}`, | |
onClick: () => { this.setState({ opened: !opened }) } | |
}} | |
> | |
<div {...{ className: 'accordion-item__line' }}> | |
<h3 {...{ className: 'accordion-item__title' }}> | |
{title} | |
</h3> | |
<span {...{ className: 'accordion-item__icon' }}/> | |
</div> | |
<div {...{ className: 'accordion-item__inner' }}> | |
<div {...{ className: 'accordion-item__content' }}> | |
<p {...{ className: 'accordion-item__paragraph' }}> | |
{paragraph} | |
</p> | |
</div> | |
</div> | |
</div> | |
) | |
} | |
} | |
ReactDOM.render(<Accordion />, document.getElementById('app')); |
This file contains hidden or 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
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.7.0/umd/react.production.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.7.0/umd/react-dom.production.min.js"></script> |
This file contains hidden or 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
$cAccent: #3978EF; | |
$cBlack: #121212; | |
$cGray: #333; | |
$cLightGray: rgba(#000, 0.1); | |
html { | |
font-size: 16px; | |
height: 100%; | |
} | |
body { | |
min-height: calc(100% - 10rem); | |
background: rgba($cAccent, 0.16); | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
font-family: 'Open Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; | |
padding: 5rem 0; | |
} | |
.accordion-list{ | |
list-style: none; | |
margin: 0; | |
padding: 0; | |
background-color: #fff; | |
max-width: 30rem; | |
border-radius: 0.4rem; | |
overflow: hidden; | |
box-shadow: | |
0 0 0.8rem 0.1rem rgba(darken($cAccent, 20%), 0.06), | |
0 20px 30px -10px rgba(darken($cAccent, 20%), 0.2); | |
&__item{ | |
&+&{ | |
border-top: 1px solid $cLightGray; | |
} | |
} | |
} | |
.accordion-item{ | |
$self: &; | |
&--opened{ | |
#{ $self }__icon{ | |
transform: rotate(180deg); | |
} | |
#{ $self }__inner{ | |
max-height: 100rem; | |
transition-timing-function: cubic-bezier(0.895, 0.03, 0.685, 0.22); | |
transition-duration: 0.5s; | |
transition-property: max-height; | |
} | |
#{ $self }__content{ | |
opacity: 1; | |
transform: translateY(0); | |
transition-delay: 0.2s; | |
transition-timing-function: ease-in-out; | |
transition-duration: 0.2s; | |
transition-property: opacity, transform; | |
} | |
} | |
&__line{ | |
display: block; | |
padding: 0.8rem 1.2rem; | |
display: flex; | |
justify-content: space-between; | |
align-items: center; | |
background-color: #fff; | |
z-index: 2; | |
position: relative; | |
} | |
&__title{ | |
font-size: 1.6rem; | |
margin: 0; | |
font-weight: 700; | |
color: $cBlack; | |
} | |
&__icon{ | |
width: 1.2rem; | |
height: 1.2rem; | |
transition: transform 0.3s ease-in-out; | |
background-size: contain; | |
background-repeat: no-repeat; | |
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAQAAABIkb+zAAABGklEQVR4Ae3RAcZCQRiF4buDfwshBGi+2UQgcIGAVtpSIuS/KyilG+UTcbk6zIH3GQBm3mM6AAAAAAAAAACA+eqf/yZBXcV/2XeCVPYx1FXj/FjGUMd45AQp/1HHGGLZNL+e61jHnKDmv8652YT1IvPfE2LX/Sh27/ycsF60yT/lk58JYn6eU4MJccjnlAmZ/33i0OAH4jg9Qcw/5g9YJpS+m6n0xvzpCfVe+nn59S7kGyYo+YYJWz3fO+E2PaFs9XzPhMy/6fmWCXq+YUJs9HzrhLh+JsQmrnq+bYKeb52g53snXPR88wQ93z9Bz/dP0PP9E/R89wQ93zpBz7dO0POtE/R86wQ93zpBzzdP+MoHAAAAAAAAAADAExTnTW20AtjhAAAAAElFTkSuQmCC); | |
opacity: 0.6; | |
} | |
&__inner{ | |
max-height: 0; | |
overflow: hidden; | |
text-transform: cubic-bezier(0.95, 0.05, 0.795, 0.035); | |
transition-duration: 0.5s; | |
transition-property: max-height; | |
z-index: 1; | |
position: relative; | |
} | |
&__content{ | |
opacity: 0; | |
transform: translateY(-1rem); | |
transition-timing-function: linear, ease; | |
transition-duration: 0.1s; | |
transition-property: opacity, transform; | |
transition-delay: 0.5s; | |
padding: 0 1.2rem 1.2rem; | |
} | |
&__paragraph{ | |
margin: 0; | |
font-size: 1rem; | |
color: $cGray; | |
font-weight: 300; | |
line-height: 1.3; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment