@@ -15,7 +15,7 @@ type SubscriberResponse = SubscriberCleanup | void;
15
15
// refs, but then host hooks / components could not opt out of renders.
16
16
// This could've been exported to its own module, but the current build doesn't
17
17
// seem to work with module imports and I had no more time to spend on this...
18
- function useResolvedElement < T extends HTMLElement > (
18
+ function useResolvedElement < T extends Element > (
19
19
subscriber : ( element : T ) => SubscriberResponse ,
20
20
refOrElement ?: T | RefObject < T > | null
21
21
) : RefCallback < T > {
@@ -26,12 +26,15 @@ function useResolvedElement<T extends HTMLElement>(
26
26
} | null > ( null ) ;
27
27
const cleanupRef = useRef < SubscriberResponse | null > ( ) ;
28
28
29
+ // Resolving ".current" purely so that a new callSubscriber instance is created when needed.
30
+ const refElement =
31
+ refOrElement && "current" in refOrElement ? refOrElement . current : null ;
29
32
const callSubscriber = useCallback ( ( ) => {
30
33
let element = null ;
31
34
if ( callbackRefElement . current ) {
32
35
element = callbackRefElement . current ;
33
36
} else if ( refOrElement ) {
34
- if ( refOrElement instanceof HTMLElement ) {
37
+ if ( refOrElement instanceof Element ) {
35
38
element = refOrElement ;
36
39
} else {
37
40
element = refOrElement . current ;
@@ -60,7 +63,7 @@ function useResolvedElement<T extends HTMLElement>(
60
63
if ( element ) {
61
64
cleanupRef . current = subscriber ( element ) ;
62
65
}
63
- } , [ refOrElement , subscriber ] ) ;
66
+ } , [ refOrElement , refElement , subscriber ] ) ;
64
67
65
68
// On each render, we check whether a ref changed, or if we got a new raw
66
69
// element.
@@ -88,7 +91,7 @@ type ObservedSize = {
88
91
89
92
type ResizeHandler = ( size : ObservedSize ) => void ;
90
93
91
- type HookResponse < T extends HTMLElement > = {
94
+ type HookResponse < T extends Element > = {
92
95
ref : RefCallback < T > ;
93
96
} & ObservedSize ;
94
97
@@ -159,7 +162,7 @@ const extractSize = (
159
162
160
163
type RoundingFunction = ( n : number ) => number ;
161
164
162
- function useResizeObserver < T extends HTMLElement > (
165
+ function useResizeObserver < T extends Element > (
163
166
opts : {
164
167
ref ?: RefObject < T > | T | null | undefined ;
165
168
onResize ?: ResizeHandler ;
@@ -194,6 +197,8 @@ function useResizeObserver<T extends HTMLElement>(
194
197
// the component unmounted.
195
198
const didUnmount = useRef ( false ) ;
196
199
useEffect ( ( ) => {
200
+ didUnmount . current = false ;
201
+
197
202
return ( ) => {
198
203
didUnmount . current = true ;
199
204
} ;
0 commit comments