Google Material Design Growing Textarea while typing

10,677

Solution 1

You are creating all the Material Design CSS & Jquery by yourself?

Otherwise, I found Material Design textarea like you mentioned in here:

Source: https://materializecss.com/text-inputs.html#textarea

Check out their Textarea part.

Solution 2

Actually, to obtain this level of control, and work around the fact that a textarea, on most web browsers, can be resized by hand, you'll want to use a div with the contenteditable attribute.

See the HTML doctor entry on contenteditable for more.

Further, to calculate font sizes and overflow, you might want to use the canvas measureText method, for example using canvas as an offscreen substitute (where you input exactly the same text that is typed inside your contenteditable element).

Finally, while the css lineHeight attribute can somewhat facilitate those calculations, there are a few javascript libraries out there that are dedicated to the purpose. I found Font.js, haven't tested it at the time of this writing.

Share:
10,677
nclsvh
Author by

nclsvh

Updated on June 16, 2022

Comments

  • nclsvh
    nclsvh almost 2 years

    I'm trying to implement Google Material Design Guidelines in my forms and it's working great, until I bumped into the textarea. What I want is this: When you focus on the textarea there is only one line, but when you reach the end of the line (while typing) it automatically adds another line and continues typing there.

    I have found this on codepen, but this uses an inputfield, not a textarea. This just scrolls horizontally... Demo

      <input type="text" required>
    

    Anyone who has this code and is willing to share? Thanks

  • nclsvh
    nclsvh almost 9 years
    Oh this is great man, thanks. Exactly what I was looking for!! I was writing it myself, but with this I can adjust it to my likes. Can't believe I never came across this site while searching.
  • yeyene
    yeyene almost 9 years
    Good news! Now Google is offering their MD framework for websites ( like Bootstrap ). Check it out here... getmdl.io/components/index.html
  • Darth Coder
    Darth Coder almost 7 years
    Exactly. A textarea only takes you so far and even frameworks like materialize.css limit you by using a textarea and not a contenteditable div. Do you know a way to make a contenteditable div work/behave visually the same way as a material textarea? I'm trying to achieve the material effects with added functionality (highlighting extra characters beyond text limit, etc.) but haven't been successful so far.
  • Mauro Colella
    Mauro Colella almost 7 years
    Sorry, haven't looked into that part yet.
  • Iman  Mohamadi
    Iman Mohamadi over 5 years
    it's a 404 for me