Skip to content

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

UX: Auto-resize the AI bot docked composer with content (#39837)

Sam38ab1004f85a
diff --git a/app/assets/stylesheets/common/components/docked-composer.scss b/app/assets/stylesheets/common/components/docked-composer.scss
index 1c1e2da36e2..8826c4efcd6 100644
--- a/app/assets/stylesheets/common/components/docked-composer.scss
+++ b/app/assets/stylesheets/common/components/docked-composer.scss
@@ -152,6 +152,40 @@
}
}
+ &--auto-resize {
+ --docked-composer-auto-resize-max-height: min(
+ 70vh,
+ calc(
+ var(--docked-composer-input-min-height) +
+ var(--docked-composer-max-resize-offset, 400px)
+ )
+ );
+ }
+
+ &--auto-resize &__editor {
+ .d-editor-input {
+ flex: none;
+ height: auto;
+ max-height: var(--docked-composer-auto-resize-max-height);
+ min-height: var(--docked-composer-input-min-height);
+ overflow-y: auto;
+ }
+
+ textarea.d-editor-input {
+ field-sizing: content;
+ }
+
+ .ProseMirror-container {
+ height: auto;
+ max-height: var(--docked-composer-auto-resize-max-height);
+ }
+
+ .ProseMirror.d-editor-input {
+ flex: none;
+ overflow-y: visible;
+ }
+ }
+
// submit button yielded into DEditor's textarea column, positioned in
// the bottom-right corner of the input box (ChatGPT-style) — icon only.
// Consumers overriding the `<:submit>` block should reuse this class so
diff --git a/frontend/discourse/app/components/docked-composer.gjs b/frontend/discourse/app/components/docked-composer.gjs
index 0b0164c71d2..2b8db6acbe3 100644
--- a/frontend/discourse/app/components/docked-composer.gjs
+++ b/frontend/discourse/app/components/docked-composer.gjs
@@ -7,6 +7,7 @@ import { guidFor } from "@ember/object/internals";
import { getOwner } from "@ember/owner";
import { trackedArray } from "@ember/reactive/collections";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
+import didUpdate from "@ember/render-modifiers/modifiers/did-update";
import willDestroy from "@ember/render-modifiers/modifiers/will-destroy";
import { schedule } from "@ember/runloop";
import { service } from "@ember/service";
@@ -39,14 +40,14 @@ export default class DockedComposer extends Component {
@tracked reply = "";
@tracked uploads = trackedArray();
- textarea = null;
+ inputElement = null;
uppyUpload = null;
fileInputEl = null;
#dragStart = null;
#rootElement = null;
#handlePaste = (event) => {
- if (!this.textarea || document.activeElement !== this.textarea) {
+ if (!this.inputElement || document.activeElement !== this.inputElement) {
return;
}
const { canUpload, canPasteHtml, types } = clipboardHelpers(event, {
@@ -90,6 +91,14 @@ export default class DockedComposer extends Component {
}
};
+ get autoResize() {
+ return this.args.autoResize ?? false;
+ }
+
+ get resizable() {
+ return (this.args.resizable ?? false) && !this.autoResize;
+ }
+
get maxResizeOffset() {
return this.args.maxResizeOffset ?? null;
}
@@ -153,16 +162,24 @@ export default class DockedComposer extends Component {
@action
setupEditor(textManipulation) {
- this.textarea =
+ this.inputElement?.removeEventListener(
+ "keydown",
+ this.#handleKeyDown,
+ true
+ );
+ this.inputElement?.removeEventListener("paste", this.#handlePaste);
+
+ this.inputElement =
textManipulation?.textarea ??
// Scope the fallback to this instance's root so multiple docked
// composers on the same page can't cross-wire.
this.#rootElement?.querySelector(".d-editor-input") ??
null;
- if (this.textarea) {
+ if (this.inputElement) {
// capture phase so Enter-to-send wins over ItsATrap / smart-list handlers
- this.textarea.addEventListener("keydown", this.#handleKeyDown, true);
- this.textarea.addEventListener("paste", this.#handlePaste);
+ this.inputElement.addEventListener("keydown", this.#handleKeyDown, true);
+ this.inputElement.addEventListener("paste", this.#handlePaste);
+ this.syncAutoResizeHeight();
}
}
@@ -171,6 +188,7 @@ export default class DockedComposer extends Component {
const saved = this.keyValueStore.get(this.draftKey);
if (saved) {
this.reply = saved;
+ this.syncAutoResizeHeight();
}
}
@@ -188,6 +206,15 @@ export default class DockedComposer extends Component {
const value = event?.target?.value ?? "";
this.reply = value;
this.persistDraft(value);
+ this.syncAutoResizeHeight();
+ }
+
+ @action
+ updateMaxResizeOffset() {
+ this.#rootElement?.style.setProperty(
+ "--docked-composer-max-resize-offset",
+ `${this.maxResizeOffset ?? 400}px`
+ );
}
@action
@@ -199,6 +226,7 @@ export default class DockedComposer extends Component {
@action
setupContainer(element) {
this.#rootElement = element;
+ this.updateMaxResizeOffset();
this.args.onRegisterApi?.({ setReply: this.setReply, focus: this.focus });
this.loadDraft();
@@ -227,8 +255,12 @@ export default class DockedComposer extends Component {
@action
teardown() {
- this.textarea?.removeEventListener("keydown", this.#handleKeyDown, true);
- this.textarea?.removeEventListener("paste", this.#handlePaste);
+ this.inputElement?.removeEventListener(
+ "keydown",
+ this.#handleKeyDown,
+ true
+ );
+ this.inputElement?.removeEventListener("paste", this.#handlePaste);
this.uppyUpload?.teardown();
this.#rootElement = null;
}
@@ -289,7 +321,8 @@ export default class DockedComposer extends Component {
this.reply = "";
this.uploads = trackedArray();
this.persistDraft("");
- schedule("afterRender", () => this.textarea?.focus());
+ this.syncAutoResizeHeight();
+ schedule("afterRender", () => this.inputElement?.focus());
} catch (error) {
// Consumers can opt into custom error handling; otherwise we
// fall back to the generic ajax-error popup for network failures.
@@ -303,7 +336,30 @@ export default class DockedComposer extends Component {
@action
focus() {
- this.textarea?.focus();
+ this.inputElement?.focus();
+ }
+
+ @action
+ syncAutoResizeHeight() {
+ if (!this.autoResize || this.inputElement?.tagName !== "TEXTAREA") {
+ return;
+ }
+
+ schedule("afterRender", () => {
+ if (!this.inputElement?.isConnected) {
+ return;
+ }
+
+ // CSS `field-sizing: content` handles modern browsers using the same
+ // mechanism as ExpandingTextArea. This fallback keeps older browsers
+ // usable without affecting the RTE, which naturally grows with content.
+ if (globalThis.CSS?.supports?.("field-sizing", "content")) {
+ return;
+ }
+
+ this.inputElement.style.height = "auto";
+ this.inputElement.style.height = `${this.inputElement.scrollHeight}px`;
+ });
}
@action
@@ -381,14 +437,16 @@ export default class DockedComposer extends Component {
<div
class={{concatClass
"docked-composer"
- (if @resizable "docked-composer--resizable")
+ (if this.resizable "docked-composer--resizable")
+ (if this.autoResize "docked-composer--auto-resize")
@class
}}
...attributes
{{didInsert this.setupContainer}}
+ {{didUpdate this.updateMaxResizeOffset this.maxResizeOffset}}
{{willDestroy this.teardown}}
>
- {{#if @resizable}}
+ {{#if this.resizable}}
<div
class="docked-composer__resize-handle"
role="separator"
diff --git a/frontend/discourse/tests/integration/components/docked-composer-test.gjs b/frontend/discourse/tests/integration/components/docked-composer-test.gjs
index 4d7817d3920..0f5fcab83d0 100644
--- a/frontend/discourse/tests/integration/components/docked-composer-test.gjs
+++ b/frontend/discourse/tests/integration/components/docked-composer-test.gjs
@@ -113,6 +113,29 @@ module("Integration | Component | docked-composer", function (hooks) {
assert.true(submitted, "Meta+Enter submits when submitOnEnter is false");
});
+ test("auto resize does not render the manual resize handle", async function (assert) {
+ await render(
+ <template>
+ <DockedComposer
+ @autoResize={{true}}
+ @resizable={{true}}
+ @maxResizeOffset={{200}}
+ @composerEvents={{false}}
+ @draftKey="test-auto-resize"
+ />
+ </template>
+ );
+
+ assert.dom(".docked-composer").hasClass("docked-composer--auto-resize");
+ assert.dom(".docked-composer__resize-handle").doesNotExist();
+ assert.strictEqual(
+ document
+ .querySelector(".docked-composer")
+ .style.getPropertyValue("--docked-composer-max-resize-offset"),
+ "200px"
+ );
+ });
+
test("keyboard End key respects @maxResizeOffset when provided", async function (assert) {
await render(
<template>
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-docked-composer.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-docked-composer.gjs
index 5bbb7331ed0..0dca4122f35 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-docked-composer.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-docked-composer.gjs
@@ -56,15 +56,20 @@ export default class AiBotDockedComposer extends Component {
};
#onViewportChange = () => {
- if (!window.visualViewport) {
- return;
- }
- const keyboardOffset = window.innerHeight - window.visualViewport.height;
const composerEl = this.#composerEl;
if (!composerEl) {
return;
}
+ composerEl.style.setProperty(
+ "--docked-composer-max-resize-offset",
+ `${this.maxResizeOffset}px`
+ );
+
+ if (!window.visualViewport) {
+ return;
+ }
+ const keyboardOffset = window.innerHeight - window.visualViewport.height;
if (keyboardOffset > 100) {
if (!this.#keyboardOpen) {
this.#keyboardOpen = true;
@@ -233,6 +238,7 @@ export default class AiBotDockedComposer extends Component {
this.#checkScroll();
window.visualViewport?.addEventListener("resize", this.#onViewportChange);
window.visualViewport?.addEventListener("scroll", this.#onViewportChange);
+ window.addEventListener("resize", this.#onViewportChange);
this.appEvents.on("topic:edit-post", this, this.handleEditPost);
this.appEvents.on("topic:quote-post", this, this.handleQuotePost);
}
@@ -251,6 +257,7 @@ export default class AiBotDockedComposer extends Component {
"scroll",
this.#onViewportChange
);
+ window.removeEventListener("resize", this.#onViewportChange);
this.appEvents.off("topic:edit-post", this, this.handleEditPost);
this.appEvents.off("topic:quote-post", this, this.handleQuotePost);
}
@@ -280,7 +287,7 @@ export default class AiBotDockedComposer extends Component {
@onRegisterApi={{this.registerComposerApi}}
@isSubmitting={{this.aiBotDockedSubmit.loading}}
@disabled={{this.isStreaming}}
- @resizable={{true}}
+ @autoResize={{true}}
@maxResizeOffset={{this.maxResizeOffset}}
{{didInsert this.setupScrollListener}}
{{willDestroy this.teardownScrollListener}}
diff --git a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot-conversations/docked-composer.scss b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot-conversations/docked-composer.scss
index 4b3f8de14c1..131d845cb48 100644
--- a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot-conversations/docked-composer.scss
+++ b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot-conversations/docked-composer.scss
@@ -80,23 +80,6 @@ body.has-ai-bot-docked-composer {
}
}
-// Place the resize handle inside the bordered wrapper. The handle overlaps 1em
-// into the wrapper's padding-top zone, above the input area.
-.docked-composer.ai-bot-docked-composer.docked-composer--resizable {
- gap: 0;
-
- .docked-composer__resize-handle {
- height: 1em;
- position: relative;
- z-index: 2;
- margin-bottom: -1em;
-
- @include viewport.until(sm) {
- display: none;
- }
- }
-}
-
// Remove default border from the column; the wrapper gets the border instead.
.docked-composer.ai-bot-docked-composer .d-editor-textarea-column {
border: none;
@@ -120,11 +103,21 @@ body.has-ai-bot-docked-composer {
border-radius: var(--d-input-border-radius);
background: var(--d-input-bg-color);
transition: border-color 0.15s ease-in-out;
+ position: relative;
&.in-focus {
border-color: var(--d-input-focused-color) !important;
- outline: 2px solid var(--d-input-focused-color) !important;
- outline-offset: -2px;
+ outline: none !important;
+
+ &::after {
+ content: "";
+ position: absolute;
+ inset: -1px;
+ z-index: 1;
+ border: 2px solid var(--d-input-focused-color);
+ border-radius: var(--d-input-border-radius);
+ pointer-events: none;
+ }
}
}