fix: size autosize textareas that start out hidden once they are shown
This commit is contained in:
1 parent
3c34cfa76a
commit
cd7cc7f8fc
1 file changed
+25
@@ -7,11 +7,36 @@ export default class extends Controller {
|
|||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
this.element.rows = this.minRowsValue
|
this.element.rows = this.minRowsValue
|
||||||
|
this.minHeight = null
|
||||||
|
|
||||||
|
// A textarea inside a hidden block (a questionnaire follow-up, a modal tab) has no layout
|
||||||
|
// yet: measured now, its floor would be 0 and it would collapse to nothing. It is measured
|
||||||
|
// once it is rendered instead - ResizeObserver reports the switch from display: none.
|
||||||
|
this.observer = new ResizeObserver(() => this.measure())
|
||||||
|
this.observer.observe(this.element)
|
||||||
|
this.measure()
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect() {
|
||||||
|
this.observer.disconnect()
|
||||||
|
}
|
||||||
|
|
||||||
|
measure() {
|
||||||
|
if (null !== this.minHeight || 0 === this.element.getClientRects().length) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
this.minHeight = this.element.scrollHeight
|
this.minHeight = this.element.scrollHeight
|
||||||
|
this.observer.disconnect()
|
||||||
this.resize()
|
this.resize()
|
||||||
}
|
}
|
||||||
|
|
||||||
resize() {
|
resize() {
|
||||||
|
// until it has been rendered there is nothing to size; its rows keep it at the floor
|
||||||
|
if (null === this.minHeight) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
this.element.style.height = 'auto'
|
this.element.style.height = 'auto'
|
||||||
this.element.style.height = `${Math.max(this.element.scrollHeight, this.minHeight)}px`
|
this.element.style.height = `${Math.max(this.element.scrollHeight, this.minHeight)}px`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user