Comments (9)
Not sure what you mean by "exact size". ResizeObservers support some box options now, maybe that's what you need?
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
from use-resize-observer.
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 ResizeObserver
as-is,
so this will definitely not be supported out of the box.
Features like these can be implemented in composing hooks easily: see throttle / debounce and breakpoint examples in the readme.
Also, getBoundingClientRect
is one of the calls you can make in JS land that causes layout trashing, avoiding which is one of the main selling points of using the native ResizeObserver.
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.
from use-resize-observer.
I can't see any instances of that lib using getBoundingClientRect
either.
from use-resize-observer.
I can't see any instances of that lib using
getBoundingClientRect
either.
I know but it goes in a direction where it is very likely they will.
from use-resize-observer.
Got it 👍
from use-resize-observer.
@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 element
?
from use-resize-observer.
It was closed, nothing was done, that snippet was only a potential implementation.
You should have access to the element already though, as you'd need to pass it in either as a ref object or call the callback ref with it.
I'd advise against using getBoundingClientRect though, and use the box option instead if possible.
from use-resize-observer.
I'd advise against using getBoundingClientRect though, and use the box option instead if possible.
I understand the concerns but some information can only be obtained from getBoundingClientRect
as it returns more than width
and height
. I read somewhere, I think, that you don't want to return more than just those two attributes regardless of what's available, right?
You should have access to the element already though, as you'd need to pass it in either as a ref object or call the callback ref with it.
👍 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. 👍
from use-resize-observer.
@bpinto to be precise I won't return anything that's not what ResizeObserver
gives you, which currently only width/height.
If I used getBoundingClientRect to return more things it would A) go against the philosophy of this hook being minimal to be as composable as possible (feature creep), and B) it would mean that you lose the benefit of ResizeObserver reporting size changes before layout / paint.
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.
Could be part of a future minor release.
from use-resize-observer.
Related Issues (20)
- Externalise useResolvedElement HOT 11
- Are the terms `blockSize` and `inlineSize` in the docs backwards? HOT 1
- Support for <img/> elements using `object-fit` HOT 5
- Future enhancement to return element coordinates
- Simply doesnt seem to want to work with a simple component. Crashes even in the simplest usage.
- SSR HTMLDivElement is not defined in 7.0.0 HOT 2
- Refs not being released on component dismounts leading to memory leaks HOT 7
- useResizeDetector resize method is not triggering in v7.0.1 HOT 7
- Using the same ResizeObserver instance for all hooks for better performance HOT 1
- Safari 15 has ResizeObserver API but it still does not work HOT 14
- ResizeObserverSize is not defined in typescript build
- onResize is not called on mount HOT 3
- Why is it suggested to instal as a dev dependency HOT 2
- No values reported when using React StrictMode HOT 5
- support for SVG elements HOT 9
- Basic usage is not working HOT 3
- Is there any reason we should save this as a dev dependency instead of a runtime dep? HOT 1
- Question regarding environments missing ResizeObserver HOT 6
- export TS types HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from use-resize-observer.