Skip to content

Commit 8a6c775

Browse files
committed
v5.0.52
1 parent 57d2d94 commit 8a6c775

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

src/components/collapsible/index.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { memo, useMemo, useState } from "react"
1+
import React, { memo, useMemo, useState, useEffect } from "react"
22
import styled from "styled-components"
33
import useUpdateEffect from "@/hooks/useUpdateEffect"
44
import useForwardRef from "@/hooks/useForwardRef"
@@ -26,15 +26,33 @@ const Collapsible = ({
2626
closedValue = 0,
2727
overflow = "visible",
2828
ref: parentRef,
29+
initial,
2930
...rest
3031
}) => {
3132
const measurement = measurementByDimension[direction] || measurementByDimension.vertical
3233
duration = process.env.NODE_ENV === "test" ? 0 : duration
3334

34-
const [dimension, setDimension] = useState(open ? "initial" : `${closedValue}px`)
35+
const [dimension, setDimension] = useState(open ? initial : `${closedValue}px`)
3536
const [animatedOpen, setAnimatedOpen] = useState(open)
3637
const [ref, setRef] = useForwardRef(parentRef)
3738

39+
useEffect(() => {
40+
if (open && ref.current) {
41+
const size =
42+
measurement === measurementByDimension.vertical
43+
? ref.current.scrollHeight
44+
: ref.current.scrollWidth
45+
46+
setDimension(`${size}px`)
47+
48+
const id = setTimeout(() => {
49+
setDimension("initial") // let it grow naturally after animation
50+
}, duration)
51+
52+
return () => clearTimeout(id)
53+
}
54+
}, [])
55+
3856
useUpdateEffect(() => {
3957
if (!ref.current) return
4058

@@ -43,7 +61,7 @@ const Collapsible = ({
4361
? `${
4462
measurement === measurementByDimension.vertical
4563
? ref.current.scrollHeight
46-
: ref.current.scrollHeight
64+
: ref.current.scrollWidth
4765
}px`
4866
: `${closedValue}px`
4967
)
@@ -56,7 +74,7 @@ const Collapsible = ({
5674
? `${
5775
measurement === measurementByDimension.vertical
5876
? ref.current.scrollHeight
59-
: ref.current.scrollHeight
77+
: ref.current.scrollWidth
6078
}px`
6179
: `${closedValue}px`
6280
)

0 commit comments

Comments
 (0)