1- import React , { memo , useMemo , useState } from "react"
1+ import React , { memo , useMemo , useState , useEffect } from "react"
22import styled from "styled-components"
33import useUpdateEffect from "@/hooks/useUpdateEffect"
44import 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