-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bind:boundingClientRect
#13412
Comments
That issue doesnt seem to mention boundingClientRect. I know that svelte already has bindings for contentRect among other things but these do not provide the same functionality. Unless I'm mistaken |
Can you explain what exactly the difference is for your use-case? |
Im not aware of any of the existing bindings giving absolute x, y positions of the bounded to div. ContentRect just gives the dimensions of the div without taking into account its position at all. Binding to boundingClientRect would also allow you to get the clientWidth and clientHeight at the same time as the absolute x and y pos without making extra bindings for those separately. This is another area where contentRect differs from boundingClientRect where contentRect doesnt include the padding in the width and height of the object which forces you to either calculate it or use clientWidth and clientHeight bindings. |
This sounds like something you can solve through an action. |
Which is what I did eventually. Thought it would be nicer to have this built-in though since it can be confusing as to why it doesn't exist. If theres no drawbacks to having this included is there a reason not to? |
Don't you need to poll this? How did you implement it? Maybe having this as a binding is a footgun when you should only call |
Not necessarily, the existing bindings use a |
|
Yes you do have to poll because |
The size bindings also used polling as a fallback (but will switch to |
Yeah but boundingClientRect doesnt only give you size information but positional. I dont believe a ResizeObserver would be able to detect changes to position no? Please correct me if I'm wrong. |
Yes, I was just pointing out the precedent for polling and its implications. |
Right. So what I'm getting is you wouldnt be against adding this even though its implemented using polling right? If thats the case I could maybe try to navigate myself through the source code and start figuring out what needs to be added. |
I'm not a maintainer, @dummdidumm is, and he suggested to use an action. In earlier versions, the ergonomics of having an action that updates a value were not great. Usually you would have to do things like using a separate store or maybe pass a setter function around. With runes you can create a utility that hosts an action and the value. So the end user code really does not change all that much, e.g. <script>
+ import { createClientRectTracker } from './client-rect-tracker.svelte.js';
- let clientRect = $state(null);
+ const clientRectTracker = createClientRectTracker();
</script>
- X: {clientRect?.x} <br>
- Y: {clientRect?.y} <br>
+ X: {clientRectTracker.value?.x} <br>
+ Y: {clientRectTracker.value?.y} <br>
- <div bind:boundingClientRect={clientRect}> ... </div>
+ <div use:clientRectTracker.action> ... </div> Example action (Try this in Firefox. Chromium throttles the polling due to some broken out-of-focus detection or something like that.) With a separate action you also have more control, you can e.g. provide an option to set the polling interval. |
Hey so I came across this implementation in the |
There are countless ways you can break this:
Screencast.from.2025-01-29.10-21-04.mp4Zooming in your browser also breaks it. |
Oh ok so its a broken implementation. Sounds good thanks. |
I think that might work if the entire window is watched deeply for mutations, for resizes & scroll as well, not sure if that has a significant performance impact. |
That sounds like it would be worse than polling lol. |
Describe the problem
I was working on creating user movable applets and came across many scenarios were I needed to reactively have the boundingClientRect of a div. This is not too hard to implement manually but would be really nice to have built into Svelte to avoid the boilerplate.
Describe the proposed solution
Add the ability to bind to the boundingClientRect of an html element using
bind:boundingClientRect
.Importance
nice to have
The text was updated successfully, but these errors were encountered: