Help forum

Auto adjust height of an input field as text gets added?

  • Devin McPaul

    2 years ago

    There are two input fields here. The first is in the blue bubble. The second is in the white bubble.

    The blue bubble's input field isn't shrinking properly to wrap around the text. Seems like it has a set width even though I haven't set one yet. It can get bigger, but doesn't get smaller if there's less text. My goal is to have it perfectly wrap the text you input into it no matter how long or short it is.

    The white bubble input field also isn't handling the text properly. I want this to expand down as the user writes more text. But currently it's just truncating the visible text instead of wrapping it like a normal text editor.

    Does anyone know how to set these up?
    1224202327129325608-Screenshot_2024-03-31_at_11.39.53_PM.png
  • Devin McPaul

    2 years ago

    This is how I designed it in Figma, but the same settings aren't working in Toddle when I set the text to input fields instead of normal text/span.
    1224202652364046518-Screenshot_2024-03-31_at_11.43.39_PM.png
  • Devin McPaul

    2 years ago

    Tried editing the wrap, overflow, and flex/text layouts.

    No dice.

    Don't have any hard heights/widths set for these, but when I shrink/expand those hard heights/widths it allows me to adjust them the way I want.

    Just doesn't handle auto scaling based on the text input length properly.
  • Max

    2 years ago

    Hi @Devin McPaul! I haven't done that in Toddle but in standard CSS/HTML. Those resources helped me a lot:
    https://css-tricks.com/the-cleanest-trick-for-autogrowing-textareas/
    https://css-tricks.com/auto-growing-inputs-textareas/

    I haven't checked but I'm pretty sure you can translate that to Toddle.
    โค๏ธ1
  • Tod

    2 years ago

    Great energy @Max! Your continuous contribution to the toddle Community just made you advance to Community Level 2!
  • Devin McPaul

    2 years ago

    There's a CSS editor in toddle, but I can't seem to get this to work @Max. Can you test & let me know how you get it to work?
  • Max

    2 years ago

    I can give it a go later this week if that's enough for you
  • Devin McPaul

    2 years ago

    Yea that'd be great, thanks!
  • Max

    2 years ago

    Hi @Devin McPaul! I created a proof of concept:

    https://auto_textarea.toddle.site/

    https://app.nordcraft.com/projects/auto_textarea

    Let me know if that helps and if this is what you are looking for. The basic idea is: You have a shadow text beneath the textarea with the same styling as your input. This text will change the height/width of the div. The textarea has "position: absolute" so that it fills the container. It is still a little rough around the edges and there are corner cases that you have to consider (empty value, line breaks...) but it should give you a good starting point ๐Ÿ™‚
  • Andreas Mรธller

    2 years ago

    Nice!
    An other approach is to render the textarea on top of a paragraph with the same font, size, and weight as the textarea.

    The set the textarea height to 100%
  • That way the paragraph will make sure the container is the right size
  • Devin McPaul

    2 years ago

    Let me give both of these a try, thanks!
  • @Max
    Hi @Devin McPaul! I created a proof of concept:

    https://auto_textarea.toddle.site/

    https://app.nordcraft.com/projects/auto_textarea

    Let me know if that helps and if this is what you are looking for. The basic idea is: You have a shadow text beneath the textarea with the same styling as your input. This text will change the height/width of the div. The textarea has "position: absolute" so that it fills the container. It is still a little rough around the edges and there are corner cases that you have to consider (empty value, line breaks...) but it should give you a good starting point ๐Ÿ™‚

    Armand

    2 years ago

    @Max would it be possible to grant me access to the auto_textarea project? I'm trying to figure out how to set this up for an input element as well, but I'm not able to get it working based on the explanations here. So I figured that when I see it, I will understand.
  • Max

    2 years ago

    Unfortunately no... I deleted that project ๐Ÿ˜… What are you struggeling with?
  • Armand

    2 years ago

    Ok, no worries.

    Well, I am not sure where to put each element (what goes inside what) and how to make sure they are "on top" of each other. And also, should the Input element become a Textarea for this to work?
  • this is what I'm trying now, but that isn't working.
    1266299786961879153-Screenshot_2567-07-26_at_14.42.55.png
  • Max

    2 years ago

    It should be a textarea. There should be a surrounding div with height set to "fit-content", a "p", that has the text, like you did already. And a textarea with position: absolute and height 100%
  • Armand

    2 years ago

    So:
    Div: fit-content
    p (with text)
    textarea: position:absolute; height 100%

    Just want to make sure I got it correct
  • Max

    2 years ago

    Yep! There are some corner cases that you will have to catch programmatically. I don't remember all of them but I am sure that line-breaks don't behave as expected.
  • Tod

    2 years ago

    Great energy @Armand! Your continuous contribution to the toddle Community just made you advance to Community Level 4!
  • Armand

    2 years ago

    Got it to work. @Max thanks a lot! ๐Ÿ™
    1266313897086812191-Screenshot_2567-07-26_at_15.39.00.png
  • Max

    2 years ago

    Yes, nice! ๐Ÿ’ช๐Ÿผ
  • Stockton

    2 years ago

    Is this still working?
    1270773369142247495-task_manager_-_start_Toddle_editor_2024-08-07_at_9.59.35_AM.jpg
  • My textarea doesn't want to resize
  • Max

    2 years ago

    The approach still works, yes. Did you set the position to absolute and the height to 100% an the textarea?
  • Stockton

    2 years ago

    Got it! Thank you!

    Man, text areas are tough ๐Ÿ˜…
  • Max

    2 years ago

    They are ๐Ÿ˜„