Per jjeff's suggestion #431888: Part of Core for D7 I've created a patch for misc/textarea.js that incorporates textarea_expander.module's functionality.

This patch makes resizable textarea fields automatically expand and shrink as text is entered or removed, much like the comment fields on Basecamp.

Manual resizing is a chore. This patch makes sure you never have to see any scroll bars. It also means that the node form body field doesn't need to take up so much of the screen if JavaScript is enabled.

I've done my best to clean it up, but JavaScript is not my specialty so any help is very much appreciated.

A couple of notes:

  • The textarea height defaults to 150px (if empty or less than 5 rows) and a line-height of 20px is assumed. I've experimented a bit with detecting the line-height and calculating the default height from that, but it quickly gets very complicated since both the line-height and the font-size (which could be used as an alternative measure) can be defined in lots of different ways ("normal", "auto", and in px, %, em or pt).
  • The height is changed in intervals. Using "current height + 3" or something similar would be easier but I think it would be distracting if the field expands every time you hit enter.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tstoeckler’s picture

Two questions:

  1. Does this patch convert the text field in core to use this resizing or the body that is provided by node.module? Would be great to keep an eye on #372743: Body and teaser as fields.
  2. Is there any way to disable this? I could imagine if you have huge amounts of text, you don't want to endlessly scroll the whole page, but just the textfield (current behaviour). And, related, what happends with JavaScript disabled?
henrrrik’s picture

FileSize
2.1 KB
  1. It just grabs any textarea with the .resizable class, so any field that would feature a drag handle at the bottom.
  2. Currently the only way to turn it off is by setting #resizable to FALSE when defining the textarea field. If JavaScript is disabled it degrades to a normal plain textarea field (as with the drag handles). I've rolled a new patch that caps the field if the height has increased to 600px or more. Good thinking, thanks!
tstoeckler’s picture

...now I get it: +1 on the general concept.

Just tried it and a few things came up.

(Concerning 2. above:) If your browser doesn't fill the whole screen the 600px really should be less for the whole stop-resizing-at-a-certain-point thing to work. I have no idea how to solve this problem just wanted to note that.

Also it might be worth discussing, whether the gray bar that indicates that the textarea is resizable makes any sense with this patch applied. I personally would favor removing it, but I'm not sure if there's not a use-case for that or even a usability issue with removing the visual signal of that textarea being resizable.

So leaving at NR for now.

zoo33’s picture

This works very well in Safari, and it's a nice UI improvement.

The combination of the resize bar/handle and the automatic resizing is a little awkward however. If you resize the textarea manually to either a smaller or larger size and then type something, the automatic resizing kicks in and overrides your chosen size.

I think I agree that with this feature we don't need the resize bar at all, but some may not like such a change. So how about a setting like this:

Resizable text areas
(*) Automatically resize textareas to its content.
    If you choose this option, the height of text areas will automatically be adjusted to fit the contained 
    text while the user types.
( ) Allow users to manually resize textareas.
    If you choose this option, a resize handle will be shown at the bottom of text areas which allows 
    users to manually adjust the height of the text areas (the standard behavior in Drupal 6).
( ) Disable resizable text areas.
    Choose this option if you don't want to allow for any resizing of text areas.

Or would this be adding too much cruft to the configuration screens?

A code review is forthcoming, once we sort out these questions.

webchick’s picture

Sounds cool! Haven't had a chance to try it out yet.

Could we get a couple of screenshots or a demo site, please, so that people who can't apply patches could give it a visual review?

I notice the patch is only adding things; if we're removing the old resizing behaviour shouldn't it also be removing some code/images/etc?

catch’s picture

There's already an issue for this at #111308: Add auto-expand feature to textareas which had a fair bit of review and discussion already but hasn't been updated for six months. Not sure it makes sense to mark this one as duplicate since I haven't compared the approaches yet so leaving them both open for now.

@zoo33 I think exposing those settings would be too much cruft, having them defined by (unexposed) variables or easy to override with contrib makes sense though.

henrrrik’s picture

@webchick: Here's a demo: http://textareademo.sixshooter.se/ I've tried it with IE 6, IE 7, Safari 4b, Firefox 3 and the latest Opera.

The patch currently doesn't touch the grippie drag handle functionality, so nothing is removed. The question going forward is whether to remove it or somehow make them play nice (e.g. turning off autoresizing if a drag has been performed).

@catch: I wasn't aware of that. I took a cursory glance at the code and it differs somewhat in both approach and behavior.

Steven's patch uses a containing div to track height and increases it on every newline. It also turns off autoresizing when the user has manually resized with a grippie (which is probably the way to go if the grippie stays).

My patch tracks height by counting newlines and rows longer than 70 characters and increases height by several rows in steps. I much prefer increasing in steps since typing against the lower edge of the box feels a bit claustrophobic (for lack of a better word). It also allows for the much lower precision when tracking the height of the content (but this really needs testing).

Sidenote: As I set up the demo site I noticed that in my CVS snapshot Drupal.behaviours.textarea doesn't fire on the node form unless the user has "administer nodes" access. I first thought my patch was broken, but it's the same using a plain textarea.js. This means no resizable texareas and no teaser join/split button. Is this by design or have I found a bug?

Dave Reid’s picture

Status: Needs review » Needs work

Sounds like this needs work. Why also can't we adjust the "rows" parameter instead of manually setting a CSS height?

kkaefer’s picture

Why is it beneficial to never see scrollbars on a node body field?

tstoeckler’s picture

Well never is probably saying too much, especially for larger texts, as discussed above. But having the form adapt according to the user input makes sense. This makes node/edit pages much nicer (even more than node/add) because the text fills the whole textbox, and there is neither a lot of unused space nor a scrollbar for just a few lines, which could have easily fit onto the page.
The height of that textbox is something you can't solve correctly for every use-case, therefore making it adapt to the user is a great usability improvement. Therefore +1 on the concept.

sun.core’s picture

Version: 7.x-dev » 8.x-dev
modstore’s picture

Does anything else need to be done to make this work? I applied the patch, but my forms don't dynamically resize.

I noticed too, that misc/textarea.js wasn't actually loaded in my source, so I added drupal_add_js('misc/textarea.js') to my template.php file, but still no go.

Do I need to load anything else, or do it some other way? (drupal 7 btw).

yoroy’s picture

It would be nice to see an updated demo of this in action. Maybe we can make it work in a good way, but that will take some iterations to refine it. We have to experience it to know if it works.

RobW’s picture

I was looking around to see if this functionality was in a module before I rolled my own. It seems like it would be a great UI add for many situations, but should probably be configurable for times when it would not be. This issue has been dead for a while, so let's take stock.

If we are combining #111308: Add auto-expand feature to textareas, it seems like the functionality wish list is:

  1. Add new lines as the content fills the text area.
  2. Stop adding lines when the text area reaches the height of the browser window, minus some padding.
  3. Shrink textarea as content is removed.

The ux wish list is:

  1. Ease/animate new rows into view.

And the questions are:

  1. Remove handle for resize, or allow user resize?
  2. Remove added lines on deletion of content?
  3. Two to three line buffer between bottom of textarea or allow text to lie flush?
  4. What js approach to use: counting rows, container div and newline addition, or other?
  5. What fields should this be applied to? Configurability?

Recently there was an article on A List Apart that demo'd a method for resizing that doesn't require font size or line height assumptions or calculations, instead using a mirrored <pre> tag. This is what I was going to experiment with -- I don't know performance implications if there are 20 textareas on a node edit page, but it seems like a valid alternative to look into re: question #4.

Realistically I'm not going to play around with this for another month or so, just adding this comment if the ops want to resurrect this issue, and if not for my own safe keeping.

[edit] Looks like there's a sandbox project using the ALA method here: http://drupal.org/sandbox/minorOffense/1328318.

Bojhan’s picture

Issue tags: -Needs usability review

Nothing to review

nod_’s picture

Status: Needs work » Postponed

I'm not sure this should be in core. It's really slick to use but it's something easy for contrib to add. How well does this work on mobile? I'm postponing since I don' t have time to work on it. If someone is up for it feel free to grab it :)

nod_’s picture

Status: Postponed » Active

Last call for this feature I guess.

modstore’s picture

Issue summary: View changes

I've created a D7 module for this: https://www.drupal.org/sandbox/modstore/2299225 which uses this library: http://www.jacklmoore.com/autosize/

If anyone finds it useful, review here to get it approved as a full project: https://www.drupal.org/node/2299275. Cheers.

nod_’s picture

Version: 8.0.x-dev » 8.1.x-dev

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

yoroy’s picture

Status: Active » Closed (duplicate)
Related issues: +#2239419: Include CKEditor's AutoGrow plug-in
hawkeye.twolf’s picture

Version: 8.2.x-dev » 8.5.x-dev
Status: Closed (duplicate) » Needs review

@yoroy, I don't think #2239419: Include CKEditor's AutoGrow plug-in supersedes this one, as this deals with textareas while the other with CKEditor.

dnotes’s picture

Status: Needs review » Needs work

I also think this is not a duplicate, and I think Drupal core would be better for this. However, the approach above will not work for Drupal 8, since textarea.js has been removed from core in favor of CSS resize property (https://www.drupal.org/node/1465840). Is there still interest in this? I could put together a proposal and possibly a patch for D8 core.

hawkeye.twolf’s picture

@dnotes, thanks for jumping in, I would be very happy to see an outline of the approach.

dnotes’s picture

This won't be on my task list until December, but if there's no motion on it before then I'll put something together. A demo would be e.g. here: https://codepen.io/vsync/pen/frudD

hawkeye.twolf’s picture

FYI, this module will be available on drupal.org just as soon as my colleague, gremy, publishes it on d.o:

https://github.com/kalamuna/autogrow_textarea

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

hawkeye.twolf’s picture

There's also the Expanding Textareas module now.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

nod_’s picture

Status: Needs work » Closed (won't fix)

There is a contrib module with support of drupal 7 to 9 and this point hasn't come up in usability reviews as far as I know.