Jump to content
Search Community

Sveltekit: Animate modal height with dynamic content

vexkiddy test
Moderator Tag

Recommended Posts

Total noob here :)Consider the following using sveltekit (svelte-5) and tailwindcss:

 

<div class="flex flex-col justify-center items-center gap-8 w-[350px]" bind:this={instance}>

  <img src="/logo/logo-web.svg" alt="" class="max-w-[64px]" />

  {#if loginUserStore.step === 'login'}
   <EmailPassword />
  {:else if loginUserStore.step === 'reset'}
   <Reset />
  {:else if loginUserStore.step === 'confirm'}
   <Confirm />
  {/if}
</div>

 

When loginUserStore.step changes, the content of the modal changes. The modal height is set to auto (its not actually defined) and has a fixed width. 

 

How can I use GSAP to animate the height of the modal when the dynamic content changes ? 

I made a svelte REPL here: https://svelte.dev/repl/2e81e9e7aba64e54bdbe45f7e5d193f4?version=4.2.15 to show how it currently works.

 

Link to comment
Share on other sites

What have you tried already? We love to see what you can come up with, that way we can see your thought process and thus better help you understand the tools!

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Hi @vexkiddy welcome to the forum!

 

Am I missing something? I don't see any GSAP code! Again we would love to see what you can come up with that way we can better help you understand the tools, so please try to get something working then someone here will definitely point you in the right direction.

 

If you're new to GSAP check out this awesome getting started guide https://gsap.com/resources/get-started/ 

 

Hope it helps and happy tweening! 

 

  • Like 1
Link to comment
Share on other sites

I don't know how to get it with auto, I think that only works if you set the hight to 0 and then animate it to it's full hight, but you can easily get the height of the elements on page load and then add it to an array an then tween to the height of the current index. Hope it helps and happy tweening! 

 

https://svelte.dev/repl/3461a39638864bfca41e6f473dff87cf?version=4.2.15

Link to comment
Share on other sites

ahh i see, so you have to hard code the values ? there's not a way for it to work dynamically with the height ? its just it probably needs to be responsive so that it adapts to screen size changes. hmmm 

Link to comment
Share on other sites

3 minutes ago, vexkiddy said:

so you have to hard code the values ?

No, that is why I said:

19 minutes ago, mvaneijgen said:

you can easily get the height of the elements on page load

Everything is hard coded right now, because that is the easiest way to demonstrate how the logic works. I have no idea how Svelt works, but I would get the elements as soon as they are ready and grap their heights and then add that to the array.

  • Like 1
Link to comment
Share on other sites

Hi,

 

The main issue here is that the height of the element is updated after the reactive property is changed, because of this the height of the element is not the correct one right after changing the value, you need to be sure that the DOM has been updated properly. For that you can use Svelte's tick method:

https://svelte.dev/docs/svelte#tick

 

Something like this:

const handleClick = async() => {
  i++
  if (i >= 3) {
    i = 0
  }
  loginUserStore = state[i]
  const height = instance.clientHeight;
  await tick();
  gsap.set(instance, { clearProps: "height" });
  gsap.from(instance, {
    height: height,
    ease: "power1.inOut",
  });
}

What that code does is to update the reactive properties, then get the current height of the element (before the DOM is updated), then wait for the DOM to be updated and finally animate the element from the height it had before the update.

 

Here is a fork of your demo:
https://svelte.dev/repl/bd10a0741c8b4aaa8664b11357a0e678?version=4.2.15

 

Finally I would suggest you to check the Flip Plugin for this, but since you're just starting it might be a good idea to get a good grasp of what that code is doing before moving onto more complex stuff.

 

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...