Skip to content

Commit

Permalink
Update 2022-03-31-stimulus-textarea-autogrow.md
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Jul 14, 2023
1 parent a069a12 commit 7519c38
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions _posts/2022-03-31-stimulus-textarea-autogrow.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,51 @@ thumbnail: /assets/thumbnails/stimulus-logo.png

![text area with autogrow](/assets/images/with-autogrow-good.gif)

How it works:
### 14.06.2023 updated version:

Just connect the below stimulus controller to a `<textarea>` and you're good to go!

```sh
rails g stimulus autogrow
```

StimulusJS controller inspired by [MDN HTMLTextAreaElement example](https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement#autogrowing_textarea_example):

```js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
connect() {
this.element.style.overflow = 'hidden';
this.grow();
}

grow() {
const textarea = this.this.element;
if (textarea.scrollHeight > textarea.clientHeight) {
textarea.style.height = `${textarea.scrollHeight}px`;
}
}

// bad approach:
// grow() {
// this.element.style.height = 'auto';
// this.element.style.height = `${this.element.scrollHeight}px`;
// }
}
```

Usage with `html.erb`:

```ruby
<%= form.text_area :content,
# rows: 5,
data: { controller: 'autogrow',
action: "input->autogrow#grow" } %>
```
### 31.03.2022 version:
```js
// app/javascript/controllers/autogrow.js
import { Controller } from "@hotwired/stimulus"
Expand Down Expand Up @@ -53,4 +90,4 @@ export default class extends Controller {
}
```

Based on the fantastic [@guillaumebriday's stimulus-textarea-autogrow](https://github.com/stimulus-components/stimulus-textarea-autogrow/blob/master/src/index.ts){:target="blank"}
This old version is based on the fantastic [@guillaumebriday's stimulus-textarea-autogrow](https://github.com/stimulus-components/stimulus-textarea-autogrow/blob/master/src/index.ts){:target="blank"}

0 comments on commit 7519c38

Please sign in to comment.