-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MOON-116: Add animation on opening/closing accordion #147
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||||||||||||||||
import React, {useContext} from 'react'; | ||||||||||||||||||||
import React, {useContext, useEffect, useState} from 'react'; | ||||||||||||||||||||
import PropTypes from 'prop-types'; | ||||||||||||||||||||
import classnames from 'clsx'; | ||||||||||||||||||||
import styles from './AccordionItem.scss'; | ||||||||||||||||||||
|
@@ -7,28 +7,33 @@ import {AccordionContext} from '~/components/Accordion/Accordion.context'; | |||||||||||||||||||
|
||||||||||||||||||||
export const AccordionItem = ({id, label, icon, onClick, children, className, ...props}) => { | ||||||||||||||||||||
const context = useContext(AccordionContext); | ||||||||||||||||||||
const [cssExpanded, setCssExpanded] = useState(null); | ||||||||||||||||||||
const open = context.currentItem === id; | ||||||||||||||||||||
|
||||||||||||||||||||
const handleClick = (e, open) => { | ||||||||||||||||||||
onClick(e, !open); | ||||||||||||||||||||
context.onSetOpenedItem(id); | ||||||||||||||||||||
}; | ||||||||||||||||||||
|
||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||
setCssExpanded({[styles.accordionItem_opened]: open}); | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand the need of a state here - why don't we use directly open ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's to trigger the animation after the render. We had some user feedback than they don't understand there is some accordion at the bottom of the page. We think to animate the opening of the accordion can improve this behavior. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it's only for the first render, if the accordion item is directly opened ? This has no impact when opening/closing the accordion after its first render ? |
||||||||||||||||||||
}, | ||||||||||||||||||||
[open]); | ||||||||||||||||||||
|
||||||||||||||||||||
Comment on lines
+18
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If only for the first render I would suggest something like that :
Suggested change
And replace the cssExpanded with just |
||||||||||||||||||||
return ( | ||||||||||||||||||||
<section | ||||||||||||||||||||
{...props} | ||||||||||||||||||||
className={classnames( | ||||||||||||||||||||
styles.accordionItem, | ||||||||||||||||||||
context.isReversed ? styles.accordionItem_reversed : null, | ||||||||||||||||||||
'flexCol', | ||||||||||||||||||||
open ? 'flexFluid' : null, | ||||||||||||||||||||
{[styles.accordionItem_reversed]: context.isReversed}, | ||||||||||||||||||||
cssExpanded, | ||||||||||||||||||||
className | ||||||||||||||||||||
)} | ||||||||||||||||||||
> | ||||||||||||||||||||
<header | ||||||||||||||||||||
className={classnames( | ||||||||||||||||||||
styles.accordionItem_header, | ||||||||||||||||||||
open ? classnames(styles.accordionItem_header_selected) : null, | ||||||||||||||||||||
{[styles.accordionItem_header_selected]: open}, | ||||||||||||||||||||
'flexRow', | ||||||||||||||||||||
'alignCenter' | ||||||||||||||||||||
)} | ||||||||||||||||||||
|
@@ -38,14 +43,14 @@ export const AccordionItem = ({id, label, icon, onClick, children, className, .. | |||||||||||||||||||
onClick={e => handleClick(e, open)} | ||||||||||||||||||||
> | ||||||||||||||||||||
{icon && | ||||||||||||||||||||
<div className={classnames( | ||||||||||||||||||||
<div className={classnames( | ||||||||||||||||||||
styles.accordionItem_iconContainer, | ||||||||||||||||||||
'flexRow_center', | ||||||||||||||||||||
'alignCenter' | ||||||||||||||||||||
)} | ||||||||||||||||||||
> | ||||||||||||||||||||
{icon && <icon.type {...icon.props} size="big"/>} | ||||||||||||||||||||
</div>} | ||||||||||||||||||||
> | ||||||||||||||||||||
{icon && <icon.type {...icon.props} size="big"/>} | ||||||||||||||||||||
</div>} | ||||||||||||||||||||
<Typography | ||||||||||||||||||||
isNowrap | ||||||||||||||||||||
variant="subheading" | ||||||||||||||||||||
|
@@ -57,15 +62,11 @@ export const AccordionItem = ({id, label, icon, onClick, children, className, .. | |||||||||||||||||||
</header> | ||||||||||||||||||||
|
||||||||||||||||||||
{/* Accordion content */} | ||||||||||||||||||||
{open && | ||||||||||||||||||||
<div className={classnames( | ||||||||||||||||||||
styles.accordionItem_content, | ||||||||||||||||||||
'flexFluid' | ||||||||||||||||||||
)} | ||||||||||||||||||||
role="region" | ||||||||||||||||||||
> | ||||||||||||||||||||
{children} | ||||||||||||||||||||
</div>} | ||||||||||||||||||||
<div className={classnames(styles.accordionItem_content)} | ||||||||||||||||||||
role="region" | ||||||||||||||||||||
> | ||||||||||||||||||||
{open && children} | ||||||||||||||||||||
</div> | ||||||||||||||||||||
</section> | ||||||||||||||||||||
); | ||||||||||||||||||||
}; | ||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
$accordionHeader_height: 48px; | ||
|
||
.accordionItem { | ||
display: flex; | ||
flex-direction: column; | ||
min-height: $accordionHeader_height; | ||
|
||
border-top: 1px solid var(--color-gray_light); | ||
border-bottom: 1px solid var(--color-gray_light); | ||
|
||
transition: all ease-in-out 0.7s; | ||
overflow: hidden; | ||
border-bottom: 1px solid var(--color-gray_light); | ||
|
||
&:only-child { | ||
border-bottom: none; | ||
|
@@ -23,6 +26,7 @@ $accordionHeader_height: 48px; | |
.accordionItem_header { | ||
width: 100%; | ||
height: $accordionHeader_height; | ||
|
||
padding: 0 var(--spacing-medium); | ||
|
||
background-color: var(--color-gray_light40); | ||
|
@@ -62,7 +66,6 @@ $accordionHeader_height: 48px; | |
//--- | ||
.accordionItem_content { | ||
max-width: 100%; | ||
|
||
overflow-x: hidden; | ||
overflow-y: auto; | ||
} | ||
|
@@ -95,3 +98,18 @@ $accordionHeader_height: 48px; | |
background-color: var(--color-dark60); | ||
} | ||
} | ||
|
||
// Animation | ||
.accordionItem_opened { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please follow conventions for modifiers - .accordionItem.opened ( .accordion { &.opened {} } |
||
flex: 1; | ||
|
||
&:last-child { | ||
border-bottom: 0; | ||
} | ||
|
||
.accordionItem_content { | ||
flex: 1; | ||
|
||
height: auto; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add callbacks for transition handling - onItemOpening / onItemOpened on Accordion before and after transition , also check what to do with onSetOpenedItem (currently called before transition)