Skip to content

Commit

Permalink
fix: ensure bind:offsetHeight updates (#8096)
Browse files Browse the repository at this point in the history
fixes #4233 by calling the callback after the iframe loads, which may be asynchronous

---------

Co-authored-by: Yuichiro Yamashita <xydybaseball@gmail.com>
  • Loading branch information
josdejong and baseballyama committed Feb 23, 2023
1 parent 709264a commit e5b0b62
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ export function add_resize_listener(node: HTMLElement, fn: () => void) {
iframe.src = 'about:blank';
iframe.onload = () => {
unsubscribe = listen(iframe.contentWindow, 'resize', fn);

// make sure an initial resize event is fired _after_ the iframe is loaded (which is asynchronous)
// see https://github.com/sveltejs/svelte/issues/4233
fn();
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
async test({ assert, component }) {
assert.equal(component.toggle, true);
assert.equal(component.offsetHeight, 800);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
export let offsetHeight;
export let offsetWidth;
export let toggle = false;
$: if (offsetWidth) {
toggle = true;
}
</script>

<div class:toggle>
<div bind:offsetHeight bind:offsetWidth>{offsetHeight}</div>
</div>

<style>
.toggle > div {
height: 800px;
}
</style>

0 comments on commit e5b0b62

Please sign in to comment.