1
1
import classNames from 'classnames' ;
2
- import React , { ElementType , PropsWithChildren , useCallback , useState } from 'react' ;
2
+ import React , { ElementType , PropsWithChildren , useCallback , useRef , useState } from 'react' ;
3
3
import { Transition } from 'react-transition-group' ;
4
4
import type { TransitionProps } from 'react-transition-group/Transition' ;
5
5
import { TransitionTimeouts , TransitionsKeys } from '../transitions' ;
@@ -39,43 +39,53 @@ export const AccordionBody = ({
39
39
...attributes
40
40
} : PropsWithChildren < AccordionBodyProps > ) => {
41
41
const [ height , setHeight ] = useState < null | number > ( null ) ;
42
-
42
+ const nodeRef = useRef < HTMLElement > ( null ) ;
43
43
const onEntering = useCallback (
44
- ( node : HTMLElement , isAppearing : boolean ) => {
45
- setHeight ( getHeight ( node ) ) ;
46
- attributes . onEntering ?.( node , isAppearing ) ;
44
+ ( isAppearing : boolean ) => {
45
+ if ( nodeRef . current ) {
46
+ setHeight ( getHeight ( nodeRef . current ) ) ;
47
+ attributes . onEntering ?.( nodeRef . current , isAppearing ) ;
48
+ }
47
49
} ,
48
50
[ attributes . onEntering ]
49
51
) ;
50
52
const onEntered = useCallback (
51
- ( node : HTMLElement , isAppearing : boolean ) => {
53
+ ( isAppearing : boolean ) => {
52
54
setHeight ( null ) ;
53
- attributes . onEntered ?.( node , isAppearing ) ;
55
+ if ( nodeRef . current ) {
56
+ attributes . onEntered ?.( nodeRef . current , isAppearing ) ;
57
+ }
54
58
} ,
55
59
[ attributes . onEntered ]
56
60
) ;
57
61
const onExit = useCallback (
58
- ( node : HTMLElement ) => {
59
- setHeight ( getHeight ( node ) ) ;
60
- attributes . onExit ?.( node ) ;
62
+ ( ) => {
63
+ if ( nodeRef . current ) {
64
+ setHeight ( getHeight ( nodeRef . current ) ) ;
65
+ attributes . onExit ?.( nodeRef . current ) ;
66
+ }
61
67
} ,
62
68
[ attributes . onExit ]
63
69
) ;
64
70
const onExiting = useCallback (
65
- ( node : HTMLElement ) => {
66
- // getting this variable triggers a reflow
67
- // @ts -expect-error variabile non usata
68
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
69
- const _unused = node . offsetHeight ;
70
- setHeight ( 0 ) ;
71
- attributes . onExiting ?.( node ) ;
71
+ ( ) => {
72
+ if ( nodeRef . current ) {
73
+ // getting this variable triggers a reflow
74
+ // @ts -expect-error variabile non usata
75
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
76
+ const _unused = nodeRef . current . offsetHeight ;
77
+ setHeight ( 0 ) ;
78
+ attributes . onExiting ?.( nodeRef . current ) ;
79
+ }
72
80
} ,
73
81
[ attributes . onExiting ]
74
82
) ;
75
83
const onExited = useCallback (
76
- ( node : HTMLElement ) => {
84
+ ( ) => {
77
85
setHeight ( null ) ;
78
- attributes . onExited ?.( node ) ;
86
+ if ( nodeRef . current ) {
87
+ attributes . onExited ?.( nodeRef . current ) ;
88
+ }
79
89
} ,
80
90
[ attributes . onExited ]
81
91
) ;
@@ -86,14 +96,15 @@ export const AccordionBody = ({
86
96
87
97
return (
88
98
< Transition
89
- { ... transitionProps }
99
+ nodeRef = { nodeRef }
90
100
timeout = { timeout }
91
101
in = { active }
92
102
onEntering = { onEntering }
93
103
onEntered = { onEntered }
94
104
onExit = { onExit }
95
105
onExiting = { onExiting }
96
106
onExited = { onExited }
107
+ { ...transitionProps }
97
108
>
98
109
{ ( status ) => {
99
110
const transitionClass = getTransitionClass ( status ) ;
@@ -102,12 +113,11 @@ export const AccordionBody = ({
102
113
const style = height == null ? null : { height } ;
103
114
104
115
return (
105
- < Tag className = { classes } style = { { ...childProps . style , ...style } } { ...childProps } >
116
+ < Tag className = { classes } ref = { nodeRef } style = { { ...childProps . style , ...style } } { ...childProps } >
106
117
< div className = { listClasses } > { children } </ div >
107
118
</ Tag >
108
119
) ;
109
120
} }
110
121
</ Transition >
111
122
) ;
112
123
} ;
113
- // }
0 commit comments