Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/components/Accordion/AccordionItem/AccordionItem.jsx
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';
Expand All @@ -7,28 +7,33 @@ import {AccordionContext} from '~/components/Accordion/Accordion.context';

export const AccordionItem = ({id, label, icon, onClick, children, className, ...props}) => {
Copy link
Contributor

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)

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});
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
useEffect(() => {
setCssExpanded({[styles.accordionItem_opened]: open});
},
[open]);
const [initialized, setInitialized] = useState(false);
useEffect(() => {
setInitialized(true)
}, []);
const isExpanded = open && initialized

And replace the cssExpanded with just {[styles.expanded]: isExpanded}

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'
)}
Expand All @@ -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"
Expand All @@ -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>
);
};
Expand Down
22 changes: 20 additions & 2 deletions src/components/Accordion/AccordionItem/AccordionItem.scss
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;
Expand All @@ -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);
Expand Down Expand Up @@ -62,7 +66,6 @@ $accordionHeader_height: 48px;
//---
.accordionItem_content {
max-width: 100%;

overflow-x: hidden;
overflow-y: auto;
}
Expand Down Expand Up @@ -95,3 +98,18 @@ $accordionHeader_height: 48px;
background-color: var(--color-dark60);
}
}

// Animation
.accordionItem_opened {
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
}