Skip to content

Commit

Permalink
fix: remove flicker in AccordionItem (carbon-design-system#9818)
Browse files Browse the repository at this point in the history
Removes the flickering effect when using AccordionItem as a controlled
component
  • Loading branch information
cesardlinx committed Oct 11, 2023
1 parent 16cf7a2 commit 725a8ea
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,15 @@
"contributions": [
"code"
]
},
{
"login": "cesardlinx",
"name": "David Padilla",
"avatar_url": "https://avatars.githubusercontent.com/u/25573926?v=4",
"profile": "https://www.davidpadilla.dev/",
"contributions": [
"code"
]
}
],
"commitConvention": "none"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ check out our [Contributing Guide](/.github/CONTRIBUTING.md) and our
<td align="center"><a href="https://github.com/cordesmj"><img src="https://avatars.githubusercontent.com/u/7409239?v=4?s=100" width="100px;" alt=""/><br /><sub><b>cordesmj</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=cordesmj" title="Code">💻</a></td>
<td align="center"><a href="https://med-aziz-chebbi.web.app/"><img src="https://avatars.githubusercontent.com/u/60013060?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Aziz Chebbi</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=azizChebbi" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/misiekhardcore"><img src="https://avatars.githubusercontent.com/u/58469680?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michał Konopski</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=misiekhardcore" title="Code">💻</a></td>
<td align="center"><a href="https://www.davidpadilla.dev/"><img src="https://avatars.githubusercontent.com/u/25573926?v=4?s=100" width="100px;" alt=""/><br /><sub><b>David Padilla</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=cesardlinx" title="Code">💻</a></td>
</tr>
</table>

Expand Down
45 changes: 44 additions & 1 deletion packages/react/src/components/Accordion/Accordion.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/* eslint-disable no-console */

import React from 'react';
import React, { useState } from 'react';
import {
default as Accordion,
AccordionItem,
Expand Down Expand Up @@ -108,6 +108,49 @@ export const _WithLayer = () => (
</WithLayer>
);

export const Controlled = () => {
const [open, setOpen] = useState(false);
return (
<Accordion>
<AccordionItem
title="Section 1 title"
open={open}
onHeadingClick={({ isOpen }) => setOpen(isOpen)}>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.
</p>
</AccordionItem>
</Accordion>
);
};

export const Skeleton = (args) => (
<AccordionSkeleton open count={4} {...args} />
);
Expand Down
10 changes: 1 addition & 9 deletions packages/react/src/components/Accordion/AccordionItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ function AccordionItem({
...rest
}: PropsWithChildren<AccordionItemProps>) {
const [isOpen, setIsOpen] = useState(open);
const [prevIsOpen, setPrevIsOpen] = useState(open);
const [animation, setAnimation] = useState('');
const accordionState = useContext(AccordionContext);

Expand All @@ -138,17 +137,11 @@ function AccordionItem({

const Toggle = renderToggle || renderExpando; // remove renderExpando in next major release

if (open !== prevIsOpen) {
setAnimation(isOpen ? 'collapsing' : 'expanding');
setIsOpen(open);
setPrevIsOpen(open);
}

// When the AccordionItem heading is clicked, toggle the open state of the
// panel
function onClick(event) {
const nextValue = !isOpen;
setAnimation(isOpen ? 'collapsing' : 'expanding');
setAnimation(isOpen ? '' : 'expanding');
setIsOpen(nextValue);
if (onHeadingClick) {
// TODO: normalize signature, potentially:
Expand All @@ -168,7 +161,6 @@ function AccordionItem({
if (handleAnimationEnd) {
handleAnimationEnd(event);
}
setAnimation('');
}

return (
Expand Down

0 comments on commit 725a8ea

Please sign in to comment.