From cd7cc7f8fc3a7b3a063ef147b7dbf569ffd4fde4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Tue, 10 Nov 2026 16:38:51 +0100 Subject: [PATCH] fix: size autosize textareas that start out hidden once they are shown --- .../textarea_autosize_controller.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/assets/controllers/textarea_autosize_controller.js b/assets/controllers/textarea_autosize_controller.js index f8e000d..50a3e65 100644 --- a/assets/controllers/textarea_autosize_controller.js +++ b/assets/controllers/textarea_autosize_controller.js @@ -7,11 +7,36 @@ export default class extends Controller { connect() { 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.observer.disconnect() this.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 = `${Math.max(this.element.scrollHeight, this.minHeight)}px` }