@@ -15,7 +15,7 @@ type SubscriberResponse = SubscriberCleanup | void;
1515// refs, but then host hooks / components could not opt out of renders.
1616// This could've been exported to its own module, but the current build doesn't
1717// 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 > (
1919 subscriber : ( element : T ) => SubscriberResponse ,
2020 refOrElement ?: T | RefObject < T > | null
2121) : RefCallback < T > {
@@ -26,12 +26,15 @@ function useResolvedElement<T extends HTMLElement>(
2626 } | null > ( null ) ;
2727 const cleanupRef = useRef < SubscriberResponse | null > ( ) ;
2828
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 ;
2932 const callSubscriber = useCallback ( ( ) => {
3033 let element = null ;
3134 if ( callbackRefElement . current ) {
3235 element = callbackRefElement . current ;
3336 } else if ( refOrElement ) {
34- if ( refOrElement instanceof HTMLElement ) {
37+ if ( refOrElement instanceof Element ) {
3538 element = refOrElement ;
3639 } else {
3740 element = refOrElement . current ;
@@ -60,7 +63,7 @@ function useResolvedElement<T extends HTMLElement>(
6063 if ( element ) {
6164 cleanupRef . current = subscriber ( element ) ;
6265 }
63- } , [ refOrElement , subscriber ] ) ;
66+ } , [ refOrElement , refElement , subscriber ] ) ;
6467
6568 // On each render, we check whether a ref changed, or if we got a new raw
6669 // element.
@@ -88,7 +91,7 @@ type ObservedSize = {
8891
8992type ResizeHandler = ( size : ObservedSize ) => void ;
9093
91- type HookResponse < T extends HTMLElement > = {
94+ type HookResponse < T extends Element > = {
9295 ref : RefCallback < T > ;
9396} & ObservedSize ;
9497
@@ -159,7 +162,7 @@ const extractSize = (
159162
160163type RoundingFunction = ( n : number ) => number ;
161164
162- function useResizeObserver < T extends HTMLElement > (
165+ function useResizeObserver < T extends Element > (
163166 opts : {
164167 ref ?: RefObject < T > | T | null | undefined ;
165168 onResize ?: ResizeHandler ;
@@ -194,6 +197,8 @@ function useResizeObserver<T extends HTMLElement>(
194197 // the component unmounted.
195198 const didUnmount = useRef ( false ) ;
196199 useEffect ( ( ) => {
200+ didUnmount . current = false ;
201+
197202 return ( ) => {
198203 didUnmount . current = true ;
199204 } ;
0 commit comments