Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Auto-resizing textareas in FormKit (#39751)

Jarek Radosz79f5e56cf5d9
diff --git a/app/assets/stylesheets/common/components/_index.scss b/app/assets/stylesheets/common/components/_index.scss
index 7174aee0e12..1ce23f73ef6 100644
--- a/app/assets/stylesheets/common/components/_index.scss
+++ b/app/assets/stylesheets/common/components/_index.scss
@@ -29,6 +29,7 @@
@import "d-combo-button";
@import "drafts-dropdown-menu";
@import "empty-states";
+@import "expanding-text-area";
@import "feature-topic-modal";
@import "file-size-input";
@import "footer-nav";
diff --git a/app/assets/stylesheets/common/components/expanding-text-area.scss b/app/assets/stylesheets/common/components/expanding-text-area.scss
new file mode 100644
index 00000000000..a151c19794a
--- /dev/null
+++ b/app/assets/stylesheets/common/components/expanding-text-area.scss
@@ -0,0 +1,36 @@
+.expanding-text-area {
+ width: 100%;
+ min-width: 0;
+
+ > textarea {
+ field-sizing: content;
+ height: auto;
+ resize: none;
+ max-width: 100%;
+ min-height: var(--expanding-text-area-min-height, auto);
+ max-height: var(--expanding-text-area-max-height, none);
+ }
+
+ @supports not (field-sizing: content) {
+ display: grid;
+ grid-template-columns: minmax(0, 1fr);
+
+ > textarea {
+ grid-area: 1 / 1;
+ }
+
+ &::after {
+ content: attr(data-replicated-value) " ";
+ white-space: pre-wrap;
+ word-break: break-word;
+ visibility: hidden;
+ grid-area: 1 / 1;
+ box-sizing: border-box;
+ padding: 0.5em 0.65em;
+ border: 1px solid transparent;
+ font: inherit;
+ min-height: var(--expanding-text-area-min-height, auto);
+ max-height: var(--expanding-text-area-max-height, none);
+ }
+ }
+}
diff --git a/docs/developer-guides/docs/03-code-internals/21-form-kit.md b/docs/developer-guides/docs/03-code-internals/21-form-kit.md
index b4534320a7d..e03b2a62dda 100644
--- a/docs/developer-guides/docs/03-code-internals/21-form-kit.md
+++ b/docs/developer-guides/docs/03-code-internals/21-form-kit.md
@@ -1076,6 +1076,25 @@ Sets the height of the textarea.
</Form>
```
+### @autoResize
+
+Grows the textarea with its content (renders via `<ExpandingTextArea>`). Bounds can be set via the `--expanding-text-area-min-height` (default `auto`) and `--expanding-text-area-max-height` (default `none`) custom properties on any ancestor.
+
+**Example**
+
+```hbs
+<form.Field @name="description" @type="textarea" as |field|>
+ <field.Control @autoResize={{true}} />
+</form.Field>
+```
+
+```css
+[data-name="description"] {
+ --expanding-text-area-min-height: 10rem;
+ --expanding-text-area-max-height: 50rem;
+}
+```
+
## Toggle
Renders a `<DToggleSwitch />` component.
diff --git a/frontend/discourse/app/components/expanding-text-area.gjs b/frontend/discourse/app/components/expanding-text-area.gjs
index bd461475d12..86165a81d9d 100644
--- a/frontend/discourse/app/components/expanding-text-area.gjs
+++ b/frontend/discourse/app/components/expanding-text-area.gjs
@@ -1,71 +1,9 @@
-import Component from "@glimmer/component";
import { on } from "@ember/modifier";
-import { action } from "@ember/object";
-import { modifier } from "ember-modifier";
import autoFocus from "discourse/modifiers/auto-focus";
-export default class ExpandingTextArea extends Component {
- setTextarea = modifier((element) => {
- this.#textarea = element;
-
- const placeholderObserver = new MutationObserver((mutations) => {
- mutations.forEach((mutation) => {
- if (
- mutation.type === "attributes" &&
- mutation.attributeName === "placeholder"
- ) {
- this.autoResize();
- }
- });
- });
-
- placeholderObserver.observe(element, {
- attributes: true,
- attributeFilter: ["placeholder"],
- });
-
- const handleResize = () => this.autoResize();
- window.addEventListener("resize", handleResize);
-
- this.autoResize();
-
- return () => {
- placeholderObserver.disconnect();
- window.removeEventListener("resize", handleResize);
- this.#textarea = null;
- };
- });
-
- #textarea;
-
- @action
- autoResize() {
- setTimeout(() => {
- if (!this.#textarea) {
- return;
- }
-
- this.#textarea.style.height = "auto";
- this.#textarea.style.height = this.#textarea.scrollHeight + "px";
-
- // Get the max-height value from CSS (30vh)
- const maxHeight = parseInt(
- getComputedStyle(this.#textarea).maxHeight,
- 10
- );
-
- // Only enable scrolling if content exceeds max-height
- if (this.#textarea.scrollHeight > maxHeight) {
- this.#textarea.style.overflowY = "auto";
- } else {
- this.#textarea.style.overflowY = "hidden";
- }
- }, 50);
- }
-
- <template>
+const ExpandingTextArea = <template>
+ <div class="expanding-text-area" data-replicated-value={{@value}}>
<textarea
- {{this.setTextarea}}
{{(if @autoFocus autoFocus)}}
{{! deprecated args: }}
autocorrect={{@autocorrect}}
@@ -75,10 +13,9 @@ export default class ExpandingTextArea extends Component {
rows={{@rows}}
value={{@value}}
{{(if @input (modifier on "input" @input))}}
- {{on "input" this.autoResize}}
- {{on "focus" this.autoResize}}
- {{on "blur" this.autoResize}}
...attributes
></textarea>
- </template>
-}
+ </div>
+</template>;
+
+export default ExpandingTextArea;
diff --git a/frontend/discourse/app/form-kit/components/fk/control/textarea.gjs b/frontend/discourse/app/form-kit/components/fk/control/textarea.gjs
index b9d888e0b17..be494ca4575 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/textarea.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/textarea.gjs
@@ -1,7 +1,9 @@
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { trustHTML } from "@ember/template";
+import ExpandingTextArea from "discourse/components/expanding-text-area";
import FKBaseControl from "discourse/form-kit/components/fk/control/base";
+import element from "discourse/helpers/element";
import { escapeExpression } from "discourse/lib/utilities";
export default class FKControlTextarea extends FKBaseControl {
@@ -40,9 +42,14 @@ export default class FKControlTextarea extends FKBaseControl {
return trustHTML(`height: ${escapeExpression(this.args.height)}px`);
}
+ get textareaElement() {
+ return this.args.autoResize ? ExpandingTextArea : element("textarea");
+ }
+
<template>
- <textarea
- class="form-kit__control-textarea"
+ <this.textareaElement
+ {{on "input" this.handleInput}}
+ {{on "keydown" this.onKeyDown}}
style={{this.style}}
disabled={{@field.disabled}}
value={{@field.value}}
@@ -50,9 +57,8 @@ export default class FKControlTextarea extends FKBaseControl {
name={{@field.name}}
aria-invalid={{if @field.error "true"}}
aria-describedby={{if @field.error @field.errorId}}
+ class="form-kit__control-textarea"
...attributes
- {{on "input" this.handleInput}}
- {{on "keydown" this.onKeyDown}}
/>
</template>
}
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs
index dee6884a368..5debb54d14c 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs
@@ -594,11 +594,11 @@ export default class AgentEditor extends Component {
@title={{i18n "discourse_ai.ai_agent.system_prompt"}}
@validation="required|length:1,100000"
@disabled={{data.system}}
- @format="large"
+ @format="full"
@type="textarea"
as |field|
>
- <field.Control />
+ <field.Control @autoResize={{true}} />
</form.Field>
<AiAgentResponseFormatEditor @form={{form}} @data={{data}} />
diff --git a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-agent.scss b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-agent.scss
index 96a57b839ce..198d07aa6a4 100644
--- a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-agent.scss
+++ b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-agent.scss
@@ -235,6 +235,11 @@
}
}
+ [data-name="system_prompt"] {
+ --expanding-text-area-min-height: 13.5rem;
+ --expanding-text-area-max-height: 43.75rem;
+ }
+
&__tool-options {
padding: 1em;
border: 1px solid var(--content-border-color);