0

I have a css property transition set to height 3s. However, it isn't working. I m on react.js

When I try to do it, there is no transition on height.

Short part of code. Jsx:

function Form({showPopup}){
  const [height, setHeight] = useState("400px")
    function proceedWithOtp(message){
      setHeight("300px")
      setForm(<OtpForm message={message}/>)
    }
    const [form, setForm] = useState(<RegisterForm showPopup={showPopup} proceedWithOtp={proceedWithOtp}/>)

    return (
      // This system is used so that height animation can work
      <div className="register-form" style={{
        height: height
      }}>
        {form}
      </div>
    )
}

Css:

.Register .register-form form {
    width: 350px;
    padding: 25px;
    border-radius: 25px;
}

.Register .register-form {
    display: flex;
    justify-content: center;
    align-items: center;
    transition: height 5s;
}

.Register .register-form form * {
    padding: 5px;
}

This is what RegisterForm returns:

        <form onSubmit={handleSubmit}  className="bg-white text-dark email-form">
            <h1 className="text-center font-hagrid">Register</h1>
            <br />
            <div className="mb-3">
    <input type="text" value={name} onChange={e => setName(e.target.value)} required placeholder="Enter your Name" className="form-control"  />   
  </div>

  <div className="mb-3">
    <input type="email"value={email} onChange={e => setEmail(e.target.value)} required placeholder="Enter your email address" className="form-control" />   
    {/* <div id="emailHelp" className="form-text">Your passwords are 100% secure and encrypted with us!</div> */}
  </div>
  <div className="mb-3">
    <input placeholder="Enter your password" required value={password} onChange={e => setPassword(e.target.value)}type="password" className="form-control" />
  </div>
      <ReCAPTCHA
      sitekey={RECAPTCHA_SITE_KEY}
      size="invisible"
      ref={recaptchaRef}
      />
   <button disabled={buttonDisabled} type="submit"  className="btn btn-primary btn-submit text-dark">{buttonContent}</button>
</form>

OtpForm returns:

 <form className="bg-white text-dark otp-form" >
  <p className="text-center">{message}</p>
 <div className="input_field_box mb-3">
  <input ref={ref1} name="1" onChange={otpBoxChange}  type="number" />
  <input ref={ref2} name="2" onChange={otpBoxChange}  type="number" disabled />
  <input ref={ref3} name="3" onChange={otpBoxChange}  type="number" disabled />
  <input ref={ref4} name="4" onChange={otpBoxChange}  type="number" disabled />
</div>

<button disabled={false} type="submit"  className="btn btn-primary btn-submit text-dark">{"Submit"}</button> 
</form>

This is a form for register. The items are centered with a definite width. The height is fixed and I want it to animate. However, for some reason, its not animation with the transition property on css. How do I fix this now? I'm unable to figure it out.

  • add the RegisterForm Component also – Developer May 21 '23 at 15:22
  • RegisterForm component where –  May 21 '23 at 15:32
  • You are setting the RegisterForm component on form state. And you are rendering it inside div. So, for css issue to resolve, need to see the register form html. – Developer May 21 '23 at 15:34
  • there is a problem. How do you want your animation? DO you want that register form open with 5 sec gradually?? Right?? And this should be happen on page load?? right? – Developer May 21 '23 at 18:09
  • Well, I want it such that. The register form should open with no animation. But when the register form is completed and the results are submitted, I want its height to collapse to 300px and show the OTPForm –  May 21 '23 at 18:16
  • I tested the code. There is only one issue. Go to your css and see what is that .Register selector. There is no such selector in your html. And as I remove it, and just use the form selector it is working fine. – Developer May 21 '23 at 18:23
  • Did you test ??? – Developer May 21 '23 at 18:28
  • .Register selector is outside the react.js component. So even If i remove it, it'll function the same. I can't remove it as it'll effect other pages –  May 21 '23 at 18:30
  • Oky, then fine. But its working fine for me. One more thing, check the proceedWithOtp prop. Where are you using it inside the register component???.I didn't see in the code – Developer May 21 '23 at 18:31
  • @Developer I've edited the code and added it. If you'd like to help me in a chat, let me know –  May 21 '23 at 18:33
  • Because of your reputation, I can't move it to chat. I think so – Developer May 21 '23 at 18:35
  • @Developer how do we chat then? –  May 21 '23 at 18:38
  • chat is not possible with repo less than 20. Leave the chat . No problem. Can you tell me one thing, is your initial height working fine? Intial height of 400px – Developer May 21 '23 at 18:39
  • Yes, @Developer –  May 21 '23 at 18:41
  • I have a onSubmit event –  May 21 '23 at 18:46
  • Right. So, can you see that height change to 300px on submission??? – Developer May 21 '23 at 18:47
  • Leave it, I'll find another way –  May 21 '23 at 19:08

1 Answers1

1

CSS can't directly animate the height property when it is set with auto (https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties).
One thing you could try is setting the height via JavaScript using the scrollHeight property, or try one of the numerous solutions already discussed here.

  • I've updated the code which uses definite height. However, it still didn't work –  May 21 '23 at 17:30
  • The value in your `height` state should be a number. Try and see if it makes a change https://reactnative.dev/docs/height-and-width – Anthony Bertrand May 21 '23 at 18:03
  • I'm using react.js, not react native. I tried to change it to a number. still, no luck –  May 21 '23 at 18:26
  • Sorry to hear that. Try adding `overflow: hidden;position:relative;` to your container (`.register-form`). – Anthony Bertrand May 21 '23 at 18:31