Skip to content

Commit 6afaf75

Browse files
fix: synchronise element bindings (#10512)
fixes #10511 We used the animation frame dance previously where the `$effect` timing was slightly different and did not wait on its children before rerunning. With that changed now everything false into place nicely. --------- Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Simon Holthausen <[email protected]>
1 parent a2164ff commit 6afaf75

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

.changeset/quiet-berries-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: tweak initial `bind:clientWidth/clientHeight/offsetWidth/offsetHeight` update timing

packages/svelte/src/internal/client/render.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -970,11 +970,11 @@ export function bind_resize_observer(dom, type, update) {
970970
* @param {(size: number) => void} update
971971
*/
972972
export function bind_element_size(dom, type, update) {
973-
// We need to wait a few ticks to be sure that the element has been inserted and rendered
974-
// The alternative would be a mutation observer on the document but that's way to expensive
975-
requestAnimationFrame(() => requestAnimationFrame(() => update(dom[type])));
976973
const unsub = resize_observer_border_box.observe(dom, () => update(dom[type]));
977-
render_effect(() => unsub);
974+
effect(() => {
975+
untrack(() => update(dom[type]));
976+
return unsub;
977+
});
978978
}
979979

980980
/**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { test } from '../../assert';
2+
import { log } from './log.js';
3+
4+
export default test({
5+
async test({ assert }) {
6+
await new Promise((fulfil) => setTimeout(fulfil, 0));
7+
8+
assert.deepEqual(log, [
9+
[false, 0, 0],
10+
[true, 100, 100]
11+
]);
12+
}
13+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/** @type {any[]} */
2+
export const log = [];
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script>
2+
import { log } from './log.js';
3+
4+
let w = 0;
5+
let h = 0;
6+
7+
/** @type {HTMLElement} */
8+
let div;
9+
10+
$: {
11+
log.push([!!div, w, h]);
12+
}
13+
</script>
14+
15+
<div bind:this={div} bind:clientWidth={w} bind:clientHeight={h} class="box"></div>
16+
17+
<style>
18+
.box {
19+
width: 100px;
20+
height: 100px;
21+
background: #ff3e00;
22+
}
23+
</style>

0 commit comments

Comments
 (0)