ZeeCoder / use-resize-observer

A React hook that allows you to use a ResizeObserver to measure an element's size.
MIT License
651 stars 42 forks source link

support for SVG elements #91

Closed khusamov closed 2 years ago

khusamov commented 2 years ago

Will there be support for SVG elements or not?

Now the error appears. Since the SVG element is not compatible with the HTML element.

TS2322: Type '(instance: HTMLElement | null) => void' is not assignable to type 'LegacyRef<SVGSVGElement>'.   
Type '(instance: HTMLElement | null) => void' is not assignable to type '(instance: SVGSVGElement | null) => void'.     
Types of parameters 'instance' and 'instance' are incompatible.       
Type 'SVGSVGElement | null' is not assignable to type 'HTMLElement | null'.         
Type 'SVGSVGElement' is missing the following properties from type 'HTMLElement': 
accessKey, accessKeyLabel, autocapitalize, dir, and 19 more.
ZeeCoder commented 2 years ago

Seems like the TS types will need to change from using HTMLElement to simply Element, as ResizeObserver itself can observe any Element.

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 9.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

khusamov commented 2 years ago

Thank you very much!

khusamov commented 2 years ago

I checked working with SVG. Does not work. The height variable is always zero.

ZeeCoder commented 2 years ago

I checked working with SVG. Does not work. The height variable is always zero.

Could you please provide reproduction in codesandbox? it would be even better if you could check using raw ResizeObserver first, as it's known to be a bit quirky with svgs.

khusamov commented 2 years ago

https://codesandbox.io/s/use-resize-observer-hsxycd?file=/src/App.tsx

It is required to display a square in the middle of the browser. But instead of the browser dimensions, the dimensions of the square are substituted in height, width.

ZeeCoder commented 2 years ago

Here's a slightly modified version of that sandbox: https://codesandbox.io/s/use-resize-observer-forked-29q3fg?file=/src/App.tsx

As you can see the hook reports the same exact values that a raw ResizeObserver reports, so I see no issues with the hook based on this example.

khusamov commented 2 years ago

Why doesn't it return the dimensions of the SVG container?

ZeeCoder commented 2 years ago

Not 100% sure, the ResizeObserver spec around SVGs is a bit weird.

Here's some stuff I found, hope it helps:

Also I wonder if you could just wrap your SVG in a div and measure that instead? Might help you depending on your exact use-case.