I have a Svelte 5 component who's position is calculated according to its size, which depends on its dynamic content:
<script> import { onMount } from "svelte" import { quintIn } from "svelte/easing" import { scale } from "svelte/transition" import { getRandomInt } from "$lib/js/utils.js" const { htmlContent = null } = $props() let thisDiv let left let top let clientWidth = $state() let clientHeight = $state() let positionCalculated = false onMount(async () => { left = getRandomInt(5, window.innerWidth - clientWidth - 5) top = getRandomInt(5, window.innerHeight - clientHeight - 5) thisDiv.style.left = `${left}px` thisDiv.style.top = `${top}px` thisDiv.style.zIndex = nb positionCalculated = true })</script>{#if positionCalculated}<div bind:this={thisDiv} bind:clientWidth bind:clientHeight in:scale={{ duration: 500, easing: quintIn }}> htmlContent</div>{/if}
T̶h̶e̶̶e̶a̶s̶e̶̶i̶n̶̶a̶n̶i̶m̶a̶t̶i̶o̶n̶̶d̶o̶e̶s̶n̶'̶t̶̶w̶o̶r̶k̶̶a̶t̶̶a̶l̶l̶̶o̶n̶̶t̶h̶i̶s̶̶c̶o̶m̶p̶o̶n̶e̶n̶t̶,̶̶w̶h̶o̶̶a̶p̶p̶e̶a̶r̶s̶̶s̶u̶d̶d̶e̶n̶l̶y̶,̶̶u̶n̶l̶i̶k̶e̶̶o̶t̶h̶e̶r̶̶c̶o̶m̶p̶o̶n̶e̶n̶t̶̶w̶i̶t̶h̶̶t̶h̶e̶̶s̶a̶m̶e̶̶e̶a̶s̶e̶-̶i̶n̶̶a̶n̶i̶m̶a̶t̶i̶o̶n̶.̶̶I̶'̶v̶e̶̶b̶e̶e̶n̶̶t̶r̶y̶i̶n̶g̶:̶̶-̶̶t̶o̶̶a̶d̶d̶̶a̶̶t̶e̶m̶p̶l̶a̶t̶e̶̶c̶o̶n̶d̶i̶t̶i̶o̶n̶̶t̶o̶̶d̶i̶s̶p̶l̶a̶y̶̶t̶h̶e̶̶c̶o̶m̶p̶o̶n̶e̶n̶t̶̶o̶n̶l̶y̶̶a̶f̶t̶e̶r̶̶i̶t̶s̶̶s̶t̶y̶l̶e̶̶i̶s̶̶m̶o̶d̶i̶f̶i̶e̶d̶̶(̶a̶t̶̶t̶h̶e̶̶e̶n̶d̶̶o̶f̶̶o̶n̶M̶o̶u̶n̶t̶)̶,̶̶b̶u̶t̶̶i̶n̶̶t̶h̶a̶t̶̶c̶a̶s̶e̶̶o̶n̶̶l̶o̶a̶d̶i̶n̶g̶̶i̶t̶̶c̶a̶n̶n̶o̶t̶̶f̶i̶n̶d̶̶t̶h̶e̶̶b̶i̶n̶d̶e̶d̶̶c̶o̶m̶p̶o̶n̶e̶n̶t̶̶b̶e̶f̶o̶r̶e̶h̶a̶n̶d̶,̶̶w̶h̶i̶c̶h̶̶m̶a̶k̶e̶s̶̶s̶e̶n̶s̶e̶̶-̶̶t̶o̶̶w̶a̶i̶t̶̶f̶o̶r̶̶a̶̶t̶i̶c̶k̶̶o̶n̶̶m̶o̶u̶n̶t̶,̶̶b̶u̶t̶̶i̶t̶̶d̶o̶e̶s̶n̶'̶t̶̶c̶h̶a̶n̶g̶e̶̶a̶n̶y̶t̶h̶i̶n̶g̶
EDIT after @brunnerh's remark:
With that conditional transition, binded values cannot be read before positionCalculated
is true
, causing an error.
Any other idea?