Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(ChatComposerInput): a press in the editor keeps the trigger menu open (#9503)

Cindy Zhang8835deca3ee7
diff --git a/.changeset/chatcomposer-trigger-press-keeps-menu.md b/.changeset/chatcomposer-trigger-press-keeps-menu.md
new file mode 100644
index 0000000000..c172ebf8fe
--- /dev/null
+++ b/.changeset/chatcomposer-trigger-press-keeps-menu.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A press in a ChatComposerInput's own editor no longer closes its open trigger menu (the `@` mention or `/` command list). Clicking inside the query keeps the menu showing; a click that moves the caret out of the query, a press anywhere else on the page and Escape still close it.
+
+@cixzhang
diff --git a/docs/architecture/layer-runtime.md b/docs/architecture/layer-runtime.md
index c9d3cc537c..4758058426 100644
--- a/docs/architecture/layer-runtime.md
+++ b/docs/architecture/layer-runtime.md
@@ -300,14 +300,14 @@ during a press so native light dismissal does not mistake them for outside
interaction. Their click action is cancelled as an invoker toggle so the control
can perform its own action without closing the layer.
-A text input cannot be an invoker, so BaseTypeahead's list and DateTimeInput's
-time list, which their text fields own, are manual popovers. A press that starts and ends on the field's input or
-its buttons, or in the list, is inside, judged across open shadow roots; a press
+A text input cannot be an invoker, so BaseTypeahead's list, DateTimeInput's
+time list and ChatComposerInput's trigger menu, which their text fields own,
+are manual popovers. A press that starts and ends on the field's input or its
+buttons, or in the list, is inside, judged across open shadow roots; a press
that starts and ends anywhere else closes the list, a scroll or pinch that
starts outside does not, and Escape stays on the shared stack. Implementation
-gap: DateInput's calendar and ChatComposerInput's trigger menu are still auto
-popovers with native light dismiss, so a press in
-their own text input closes them.
+gap: DateInput's calendar is still an auto popover with native light dismiss,
+so a press in its own text input closes it.
### Current dismissal plumbing
@@ -333,8 +333,8 @@ Outside interaction is not coordinated by the shared stack. Current paths are
independent:
- auto popovers use native light dismiss;
-- BaseTypeahead's list and DateTimeInput's time list, manual popovers, use
- Layer's outside-press watch,
+- BaseTypeahead's list, DateTimeInput's time list and ChatComposerInput's
+ trigger menu, manual popovers, use Layer's outside-press watch,
which closes on a press that starts and ends outside the field and the list
and forgets a cancelled press (a scroll);
- native dialogs handle backdrop clicks locally;
@@ -661,7 +661,8 @@ is staged as `[breaking]` (`spec:AST-017` FR32) and `component:SelectableCard`
- `Layer/useTouchTrigger.ts` owns touch trigger classification and the current
touch-only outside listener for Tooltip/HoverCard.
- `Layer/outsidePress.ts` owns the outside-press watch for a text field's
- manual layer, used by BaseTypeahead and DateTimeInput.
+ manual layer, used by BaseTypeahead, DateTimeInput and ChatComposerInput's
+ trigger menu.
- `DropdownMenuContext` and `DropdownMenuSubMenu` own the current local
menu-cascade parent-close chain.
- Dialog families own native modal/backdrop presentation and their local channel
diff --git a/docs/specs/components/Chat/ChatComposerInput.spec.md b/docs/specs/components/Chat/ChatComposerInput.spec.md
index 083f84f146..4c4e3f18b2 100644
--- a/docs/specs/components/Chat/ChatComposerInput.spec.md
+++ b/docs/specs/components/Chat/ChatComposerInput.spec.md
@@ -115,7 +115,7 @@ imperative editing handle.
| FR13 | While a newer search for the active query is pending, Enter without Shift submits the draft as typed and MUST NOT pick a row; no key picks a row returned for an earlier query. Results for a superseded query, or arriving after a pick, dismissal, submission, or the query ending, are discarded, so the menu never reopens over a sent or cleared draft. This applies to every trigger. | DEC-1; reference chat composer behavior. |
| FR14 | When a multi-word query settles with no matches, the menu is hidden unless the trigger supplies `emptySearchText`. A single-token query with no matches shows the empty message. | DEC-1. |
| FR15 | Keys pressed during IME composition MUST NOT pick a row or move the highlighted row. | DEC-1; FR7. |
-| FR16 | Escape on a visible menu dismisses it, and the menu stays hidden while the same query keeps growing from the same trigger. | DEC-1. |
+| FR16 | Escape on a visible menu dismisses it, and the menu stays hidden while the same query keeps growing from the same trigger. While open, the menu stays open for a press on the editor or in the menu and closes for a press elsewhere (`architecture:layer-runtime`). | DEC-1. |
| FR17 | The editor carries `enterkeyhint`: the `enterKeyHint` value when one is set, otherwise `send`, because Enter sends (FR2). `enterKeyHint` takes the closed union `EnterKeyHint` (`component:TextInput` FR16), so TypeScript rejects any other value. The hint only labels the key: FR2 and FR3 decide what Enter and Shift+Enter do whatever it says, so a composer whose `onKeyDown` makes Enter insert a line break passes `enter`. ChatComposer's default input carries the same default. Implementation gap: the editor sets no `enterkeyhint`, and ChatComposerInput accepts no `enterKeyHint`. | DEC-2; `component:TextInput` DEC-1 |
### Allowed variation
@@ -185,7 +185,7 @@ add a theming target or decide a new visual state.
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------- |
| FR1, ORD1–ORD2 | focused imperative insertion and controlled-sync tests; `ImperativeInsertion` play | empty → inserted text; controlled echo; external override | Text appears only in the DOM, placeholder remains, callback is stale, or the caret jumps. | `audit:ChatComposerInput/behavior` |
| FR2–FR3, FR7–FR8, AR3 | focused keyboard, IME, trigger, and history tests | Enter; Shift+Enter; composition; cancelled key; boundary and mid-text arrows | A key submits twice, submits during composition, or steals native editing. | `audit:ChatComposerInput/interaction` |
-| FR10–FR16, AR5 | focused trigger-query and trigger keyboard tests | single-token; multi-word; line break; length bound; pending search; no matches; IME; Escape | A query ends early or alters text, Enter picks an earlier query's row or fails to submit while a search is pending, a late result reopens the menu after a send, or a hidden menu consumes keys. | `audit:ChatComposerInput/interaction` |
+| FR10–FR16, AR5 | focused trigger-query and trigger keyboard tests; "keeps the trigger menu open on a press in the editor or the menu, closes it on a press elsewhere" | single-token; multi-word; line break; length bound; pending search; no matches; IME; Escape | A query ends early or alters text, Enter picks an earlier query's row or fails to submit while a search is pending, a late result reopens the menu after a send, or a hidden menu consumes keys. | `audit:ChatComposerInput/interaction` |
| FR5, ORD4 | focused long-paste interception tests | consumer handled; token conversion; plain insertion | Consumer interception is skipped or multiple insertion paths run. | `audit:ChatComposerInput/behavior` |
| FR6, ORD5, AR2 | focused paste/drop tests and `FileDrop` play | paste; drop; disabled | Files navigate away, insert into the editor, bypass `onFiles`, or emit while disabled. | `audit:ChatComposerInput/behavior` |
| AR1, AR4 | role/name/ARIA tests plus exact-head accessibility and visual gates | textbox; combobox; disabled; keyboard focus | Name, role, multiline, expanded, disabled, or visible focus semantics regress. | `audit:ChatComposerInput/accessibility` |
diff --git a/packages/core/src/Chat/ChatComposerInput.test.tsx b/packages/core/src/Chat/ChatComposerInput.test.tsx
index 6699d614a9..eafcc79f8a 100644
--- a/packages/core/src/Chat/ChatComposerInput.test.tsx
+++ b/packages/core/src/Chat/ChatComposerInput.test.tsx
@@ -1534,6 +1534,79 @@ describe('ChatComposerInput', () => {
fireEvent.input(editable);
}
+ it('keeps the trigger menu open on a press in the editor or the menu, closes it on a press elsewhere', async () => {
+ render(
+ <>
+ <ChatComposerInput
+ triggers={[
+ createMentionTrigger({searchSource: createStaticSource(USERS)}),
+ ]}
+ debounceMs={0}
+ />
+ <button type="button">Elsewhere</button>
+ </>,
+ );
+ const editable = screen.getByRole('combobox');
+ editable.focus();
+ typeAtEnd(editable, '@c');
+ await waitFor(() => {
+ expect(editable).toHaveAttribute('aria-expanded', 'true');
+ });
+ const listbox = screen.getByRole('listbox', {hidden: true});
+ // A text editor cannot be an invoker, so native light dismiss would
+ // close the menu on this press; the menu is a manual popover.
+ expect(listbox.closest('[popover]')).toHaveAttribute('popover', 'manual');
+
+ const pressOn = (target: Element) => {
+ fireEvent.pointerDown(target);
+ fireEvent.pointerUp(target);
+ };
+ pressOn(editable);
+ expect(editable).toHaveAttribute('aria-expanded', 'true');
+ pressOn(listbox);
+ expect(editable).toHaveAttribute('aria-expanded', 'true');
+
+ pressOn(screen.getByRole('button', {name: 'Elsewhere'}));
+ await waitFor(() => {
+ expect(editable).toHaveAttribute('aria-expanded', 'false');
+ });
+ });
+
+ it('closes the trigger menu when a click in the editor moves the caret out of the query, and Enter then sends the draft', async () => {
+ const onSubmit = vi.fn();
+ render(
+ <ChatComposerInput
+ triggers={[
+ createMentionTrigger({searchSource: createStaticSource(USERS)}),
+ ]}
+ debounceMs={0}
+ onSubmit={onSubmit}
+ />,
+ );
+ const editable = screen.getByRole('combobox');
+ editable.focus();
+ typeAtEnd(editable, 'hello @c');
+ await waitFor(() => {
+ expect(editable).toHaveAttribute('aria-expanded', 'true');
+ });
+ // The click puts the caret between "he" and "llo".
+ const text = editable.firstChild as Text;
+ fireEvent.pointerDown(editable);
+ const range = document.createRange();
+ range.setStart(text, 2);
+ range.collapse(true);
+ const selection = window.getSelection()!;
+ selection.removeAllRanges();
+ selection.addRange(range);
+ fireEvent.pointerUp(editable);
+ fireEvent.click(editable);
+ await waitFor(() => {
+ expect(editable).toHaveAttribute('aria-expanded', 'false');
+ });
+ fireEvent.keyDown(editable, {key: 'Enter'});
+ expect(onSubmit).toHaveBeenCalledWith('hello @c');
+ });
+
it('Enter while a newer trigger search is pending sends the typed text, picks no row, and no menu reopens after the send', async () => {
const onSubmit = vi.fn();
let resolveSlow: (items: SearchableItem[]) => void = () => {};
diff --git a/packages/core/src/Chat/ChatComposerInput.tsx b/packages/core/src/Chat/ChatComposerInput.tsx
index 7e9f5a413f..88988c7438 100644
--- a/packages/core/src/Chat/ChatComposerInput.tsx
+++ b/packages/core/src/Chat/ChatComposerInput.tsx
@@ -656,6 +656,14 @@ export function ChatComposerInput(props: ChatComposerInputProps) {
triggerMenu.handleInput();
}, [isDisabled, emitChange, triggerMenu]);
+ // A click inside the editor keeps the menu open, but it can move the caret
+ // out of the query; the trigger is checked again at the new caret.
+ const handleEditorClick = useCallback(() => {
+ if (!isDisabled && triggerMenu.state.isActive) {
+ triggerMenu.handleInput();
+ }
+ }, [isDisabled, triggerMenu]);
+
const handleKeyDown = useCallback(
(e: KeyboardEvent<HTMLDivElement>) => {
if (isDisabled) {
@@ -935,6 +943,7 @@ export function ChatComposerInput(props: ChatComposerInputProps) {
contentEditable={!isDisabled}
suppressContentEditableWarning
onInput={handleInput}
+ onClick={handleEditorClick}
onKeyDown={handleKeyDown}
onPaste={handlePaste}
onDragOver={handleDragOver}
diff --git a/packages/core/src/Chat/useTriggerMenu.tsx b/packages/core/src/Chat/useTriggerMenu.tsx
index f743c7bd8f..ebfe74c518 100644
--- a/packages/core/src/Chat/useTriggerMenu.tsx
+++ b/packages/core/src/Chat/useTriggerMenu.tsx
@@ -32,6 +32,7 @@ import {
} from 'react';
import * as stylex from '@stylexjs/stylex';
import {usePopover} from '../Popover/usePopover';
+import {useOutsidePress} from '../Layer/outsidePress';
import {useBusyAnnouncement} from '../hooks/useBusyAnnouncement';
import {useHighlightedOptionScroll} from '../hooks/useHighlightedOptionScroll';
import {
@@ -402,8 +403,30 @@ export function useTriggerMenu(
}
}, []);
+ const outsidePress = useOutsidePress();
const popover = usePopover({
+ // A text input cannot be an invoker, so native light dismiss would count
+ // a press in the editor as outside and close the menu.
+ onShow: () => {
+ const editable = editableRef.current;
+ if (editable == null) {
+ return;
+ }
+ outsidePress.start(
+ editable,
+ // The menu is hosted outside the editor, so it counts on its own.
+ path =>
+ path.some(
+ target =>
+ target === editable ||
+ (target instanceof Element && target.id === popover.id),
+ ),
+ // Called after usePopover has returned, so `popover` is assigned.
+ () => popover.hide(),
+ );
+ },
onHide: useCallback(() => {
+ outsidePress.stop();
if (isMenuSuppressedRef.current) {
return;
}
@@ -425,8 +448,8 @@ export function useTriggerMenu(
isLoading: false,
isHidden: false,
}));
- }, [removeAnchorSpan]),
- hasLightDismiss: true,
+ }, [outsidePress, removeAnchorSpan]),
+ hasLightDismiss: false,
hasCloseButton: false,
hasAutoFocus: false,
// The popup's own role="listbox" is the exposed semantics; focus stays in
@@ -573,8 +596,8 @@ export function useTriggerMenu(
isLoading: false,
isHidden,
}));
- // A hidden menu closes its popover, which also releases the popover's
- // Escape and light-dismiss handling; matches reopen it at the caret.
+ // A hidden menu closes its popover. That also stops its Escape and
+ // outside-press handling. Matches reopen it at the caret.
if (isHidden && !isMenuSuppressedRef.current) {
isMenuSuppressedRef.current = true;
hidePopover();