1
1
import clsx from 'clsx' ;
2
- import React , { useEffect , useRef , useState } from 'react' ;
2
+ import React , { useCallback , useEffect , useRef , useState } from 'react' ;
3
3
import type { CollapseProps } from './types' ;
4
4
5
5
const MDBCollapse : React . FC < CollapseProps > = ( {
@@ -28,6 +28,18 @@ const MDBCollapse: React.FC<CollapseProps> = ({
28
28
) ;
29
29
const refCollapse = useRef < HTMLElement > ( null ) ;
30
30
31
+ const handleResize = useCallback ( ( ) => {
32
+ if ( showCollapse || statement ) {
33
+ setCollapseHeight ( undefined ) ;
34
+ }
35
+ } , [ showCollapse , statement ] ) ;
36
+
37
+ useEffect ( ( ) => {
38
+ if ( collapseHeight === undefined && ( showCollapse || statement ) ) {
39
+ setCollapseHeight ( refCollapse ?. current ?. scrollHeight ) ;
40
+ }
41
+ } , [ collapseHeight , showCollapse , statement ] ) ;
42
+
31
43
useEffect ( ( ) => {
32
44
if ( typeof show === 'string' ) {
33
45
setShowCollapseString ( show ) ;
@@ -47,7 +59,7 @@ const MDBCollapse: React.FC<CollapseProps> = ({
47
59
return ( ) => {
48
60
clearTimeout ( timer ) ;
49
61
} ;
50
- } , [ collapseHeight , show , showCollapse , id , showCollapseString , statement ] ) ;
62
+ } , [ show , showCollapse , id , showCollapseString , statement ] ) ;
51
63
52
64
useEffect ( ( ) => {
53
65
if ( showCollapse || statement ) {
@@ -57,6 +69,14 @@ const MDBCollapse: React.FC<CollapseProps> = ({
57
69
}
58
70
} , [ showCollapse , statement ] ) ;
59
71
72
+ useEffect ( ( ) => {
73
+ window . addEventListener ( 'resize' , handleResize ) ;
74
+
75
+ return ( ) => {
76
+ window . removeEventListener ( 'resize' , handleResize ) ;
77
+ } ;
78
+ } , [ handleResize ] ) ;
79
+
60
80
return (
61
81
< Tag style = { { height : collapseHeight , ...style } } id = { id } className = { classes } { ...props } ref = { refCollapse } >
62
82
{ children }
0 commit comments