Trying to create a reusable collapse component, but having a smooth transition on the element getting a problem. So when the collapse item is clicked i want have a smooth transition
Here is the main part of what i have tried so far.
index.js
const Collapse = ({ title, text, child, ...props }) => {
const [isOpen, setIsOpen] = useState(false);
const toggleCollapse = () => setIsOpen((isOpen) => !isOpen);
const closeCollapse = () => setIsOpen(false);
const content = useRef(null);
const isParentOpen = props.isParentOpen;
useEffect(() => {
if (!isParentOpen) closeCollapse();
}, [isParentOpen]);
const height = !isOpen ? "0px" : `auto`; // ${content.current?.scrollHeight}px
return (
<CollapseContainer>
<CollapseButton isOpen={isOpen} onClick={toggleCollapse}>
<CollapseTitleWrapper>{title}</CollapseTitleWrapper>
</CollapseButton>
<CollapseContent ref={content} max_height={height}>
<CollapseText>
{text}
{child?.map((datumn, index) => (
<Collapse
{...datumn}
key={`collapse-child-${index}`}
isParentOpen={isOpen}
/>
))}
</CollapseText>
</CollapseContent>
</CollapseContainer>
);
};
export default Collapse;
So i am able to calculate the height of the content dynamically using ref, but smooth transition will happen but i will get a scroll inside the child collapse nested that i don't want. Is there way to apply transition on height:auto.
Here is the working codesandbox