Support getBoundingClientRect() #102
Replies: 10 comments
-
Not sure what you mean by "exact size". ResizeObservers support some box options now, maybe that's what you need? This library is intentionally resisting such feature and aims to be thin hook to use Features like these can be implemented in composing hooks easily: see throttle / debounce and breakpoint examples in the readme. Also, What I could do, however, is giving easy access to the observed element in the onResize callback, so that you can compose the hook and add this feature yourself. Pseudo-code: const useBoundingClientRect = () => {
const [size, setSize] = useState({ width: undefined, height: undefined });
const { ref } = useResizeObserver({
onResize: useMemo(({ element }) => {
const { width, height } = element.getBoundingClientRect(); // This trashes the layout
setSize({ width, height });
}),
});
return { ...size, ref };
}; I would probably combine this with throttling, to lessen the effects of layout trashing. This is already possible btw, you just don't have such an easy access to the element rn. |
Beta Was this translation helpful? Give feedback.
-
Thanks, I was thinking maybe this library will go in the direction like Solid.js primitive https://www.npmjs.com/package/@solid-primitives/resize-observer but totally ok if not, this is now solved for me and can be closed. Thanks |
Beta Was this translation helpful? Give feedback.
-
I can't see any instances of that lib using |
Beta Was this translation helpful? Give feedback.
-
I know but it goes in a direction where it is very likely they will. |
Beta Was this translation helpful? Give feedback.
-
Got it 👍 |
Beta Was this translation helpful? Give feedback.
-
@ZeeCoder I tried your snippet but that didn't work. You closed this issue as completed, but I think I missed the change related to this. How can I obtain the |
Beta Was this translation helpful? Give feedback.
-
It was closed, nothing was done, that snippet was only a potential implementation. I'd advise against using getBoundingClientRect though, and use the box option instead if possible. |
Beta Was this translation helpful? Give feedback.
-
I understand the concerns but some information can only be obtained from
👍 that's fine, just wondering if that was available and I was missing something. I tried both merging ref or passing in a ref and both worked to get the element. 👍 |
Beta Was this translation helpful? Give feedback.
-
@bpinto to be precise I won't return anything that's not what Getting the element through onResize does not go against either of those though, and I can see implementing something like this being a bit of a pain, so I'll change this issue to a discussion instead. |
Beta Was this translation helpful? Give feedback.
-
I believe that's accurate for all entries, except for the deprecated contentRect which returns a DOMRectReadOnly object that contains much of the same properties as
👍 Sorry, I didn't mean to say that the library should execute
😍 |
Beta Was this translation helpful? Give feedback.
-
The returned width and height should also be also available as what we get from
el.getBoundingClientRect().width
&el.getBoundingClientRect().height
sometimes we need exact sitze and i didn't see anything in the Readme that this library supports that already, please correct me if I'm wrong.Beta Was this translation helpful? Give feedback.
All reactions