@@ -26,8 +26,12 @@ function Collapsible(props: {
26
26
27
27
// keyframe animations doesn't need data-starting/ending-style
28
28
29
- const [ height , setHeight ] = React . useState < number | undefined > ( undefined ) ;
29
+ function initializeHeight ( ) {
30
+ return open ? undefined : 0 ;
31
+ }
32
+ const [ height , setHeight ] = React . useState < number | undefined > ( initializeHeight ) ;
30
33
34
+ const latestAnimationNameRef = React . useRef < string > ( 'none' ) ;
31
35
// const shouldCancelInitialOpenTransitionRef = React.useRef(open);
32
36
33
37
const isHidden = React . useMemo ( ( ) => {
@@ -38,27 +42,24 @@ function Collapsible(props: {
38
42
return ! open && ! mounted ;
39
43
} , [ keepMounted , open , mounted ] ) ;
40
44
41
- const animationTypeRef = React . useRef < AnimationType > ( null ) ;
42
45
const panelRef : React . RefObject < HTMLElement | null > = React . useRef ( null ) ;
43
46
44
47
const handlePanelRef = useEventCallback ( ( element : HTMLElement ) => {
45
48
if ( ! element ) {
46
49
return ;
47
50
}
48
51
49
- element . style . animationDuration = '0s' ;
52
+ // element.style.animationDuration = '0s';
53
+ const panelStyles = getComputedStyle ( element ) ;
54
+ latestAnimationNameRef . current = panelStyles . animationName ;
50
55
51
- // if (height === undefined) {
52
- // // set` display: block !important` here to force layout
53
- // element.style.setProperty('display', 'block', 'important');
54
- // // measure the height
55
- // setHeight(element.scrollHeight);
56
- // element.style.removeProperty('display');
57
-
58
- // if (shouldCancelInitialOpenTransitionRef.current) {
59
- // element.style.animationDuration = '0s';
60
- // }
61
- // }
56
+ if ( height === undefined ) {
57
+ // set` display: block !important` here to force layout
58
+ element . style . setProperty ( 'display' , 'block' , 'important' ) ;
59
+ // measure the height
60
+ setHeight ( element . scrollHeight ) ;
61
+ element . style . removeProperty ( 'display' ) ;
62
+ }
62
63
63
64
// requestAnimationFrame(() => {
64
65
// shouldCancelInitialOpenTransitionRef.current = false;
@@ -86,18 +87,34 @@ function Collapsible(props: {
86
87
87
88
if ( nextOpen ) {
88
89
/* opening */
90
+
91
+ panel . style . removeProperty ( 'display' ) ;
92
+
93
+ /* opening */
94
+ panel . style . height = '0px' ;
95
+
96
+ requestAnimationFrame ( ( ) => {
97
+ panel . style . removeProperty ( 'height' ) ;
98
+ setHeight ( panel . scrollHeight ) ;
99
+ } ) ;
89
100
} else {
90
101
/* closing */
102
+ panel . style . height = `${ panel . scrollHeight } px` ;
103
+ // panel.style.removeProperty('display');
104
+ requestAnimationFrame ( ( ) => {
105
+ panel . style . removeProperty ( 'height' ) ;
106
+ setHeight ( 0 ) ;
107
+ } ) ;
91
108
}
92
109
} ) ;
93
110
94
111
return (
95
112
< div
96
113
className = { classes . Root }
97
114
style = { {
98
- // animationDuration: '0s',
99
115
// @ts -ignore
100
- '--collapsible-panel-height' : height !== undefined ? `${ height } px` : 'auto' ,
116
+ '--collapsible-panel-height' :
117
+ height !== undefined ? `${ height } px` : undefined ,
101
118
} }
102
119
>
103
120
< button
@@ -115,10 +132,9 @@ function Collapsible(props: {
115
132
// @ts -ignore
116
133
ref = { mergedRef }
117
134
className = { classes . Panel }
118
- // {...{ [open ? 'data-open' : 'data-closed']: '' }}
119
- data-open = ""
135
+ { ...{ [ open ? 'data-open' : 'data-closed' ] : '' } }
120
136
// {...styleHooks}
121
- hidden = { isHidden }
137
+ hidden = { ! open }
122
138
id = { id }
123
139
>
124
140
< div className = { classes . Content } >
0 commit comments