Skip to content

fix: synchronise element bindings #10512

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

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-berries-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: tweak initial `bind:clientWidth/clientHeight/offsetWidth/offsetHeight` update timing
8 changes: 4 additions & 4 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,11 +970,11 @@ export function bind_resize_observer(dom, type, update) {
* @param {(size: number) => void} update
*/
export function bind_element_size(dom, type, update) {
// We need to wait a few ticks to be sure that the element has been inserted and rendered
// The alternative would be a mutation observer on the document but that's way to expensive
requestAnimationFrame(() => requestAnimationFrame(() => update(dom[type])));
const unsub = resize_observer_border_box.observe(dom, () => update(dom[type]));
render_effect(() => unsub);
effect(() => {
untrack(() => update(dom[type]));
return unsub;
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { test } from '../../assert';
import { log } from './log.js';

export default test({
async test({ assert }) {
await new Promise((fulfil) => setTimeout(fulfil, 0));

assert.deepEqual(log, [
[false, 0, 0],
[true, 100, 100]
]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** @type {any[]} */
export const log = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
import { log } from './log.js';

let w = 0;
let h = 0;

/** @type {HTMLElement} */
let div;

$: {
log.push([!!div, w, h]);
}
</script>

<div bind:this={div} bind:clientWidth={w} bind:clientHeight={h} class="box"></div>

<style>
.box {
width: 100px;
height: 100px;
background: #ff3e00;
}
</style>