Auto Growing text-area [Stimulus] [Rails]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Some form --> | |
<div data-controller="auto-grow-textarea"> | |
<%= form.text_area :body, cols: 20, row: 2, placeholder: 'Bir yorum yazın...', class:'form-control', data: { action: 'input->auto-grow-textarea#resize', target: 'auto-grow-textarea.input' } %> | |
</div> | |
<!-- Some form continued --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Controller} from "stimulus"; | |
export default class extends Controller { | |
static targets = ["input"]; | |
connect() { | |
console.log('auto-grow-textarea controller connected...'); | |
this.inputTarget.style.resize = 'none'; | |
this.inputTarget.style.minHeight = `${this.inputTarget.scrollHeight}px`; | |
} | |
resize(event){ | |
event.target.style.height = '5px'; | |
event.target.style.height = `${event.target.scrollHeight}px`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Small improvement: you can add
this.inputTarget.style.overflow = 'hidden';
at the end of theconnect
method to prevent the scrollbar from appearing when adding text.