An open source design system that's fully customizable and agent ready
TypeScript
fix(ChatComposer): a click inside an open Tooltip or HoverCard doesn't focus the input (#9375)
Cindy Zhang42754d567722
diff --git a/.changeset/chatcomposer-surface-press.md b/.changeset/chatcomposer-surface-press.mdnew file mode 100644index 0000000000..608d7fd0ad--- /dev/null+++ b/.changeset/chatcomposer-surface-press.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] A click inside an open Tooltip or HoverCard in a ChatComposer's body no longer moves focus to the composer's input. A click on the body's empty space still focuses it.++@cixzhangdiff --git a/docs/specs/components/Chat/ChatComposer.spec.md b/docs/specs/components/Chat/ChatComposer.spec.mdindex 3426c4b8e3..0294b75d05 100644--- a/docs/specs/components/Chat/ChatComposer.spec.md+++ b/docs/specs/components/Chat/ChatComposer.spec.md@@ -122,19 +122,19 @@ Consumer migration instructions belong in consumer docs and release notes.## Behavioral and layout contract-| ID | Invariant | Basis |-| ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------- |-| FR1 | Controlled mode MUST report changes through `onChange`; uncontrolled mode MUST retain them locally. Every successful submit MUST trim and deliver the value supplied to that operation, then clear through the same value channel. | public types and docs; `architecture:public-component-api/INV3, INV9, INV12`; focused tests |-| FR2 | The default editor MUST serialize its current text/tokens, submit Enter without Shift, preserve Shift+Enter, defer during IME composition, and allow a caller key handler to prevent the default operation. | implementation, docs, and focused tests |-| FR3 | `isDisabled` MUST prevent draft edits and submissions and MUST expose `aria-disabled="true"` on the default textbox; removing the prop MUST remove that state. | `spec:AST-020/FR1` (WCAG 2.2 4.1.2); `architecture:interaction-modality/INV5`; red/green focused test |-| FR4 | When `isStopShown` is true, the default primary action MUST remain an enabled, named Stop button and call `onStop`; if editing is also disabled, ancestor styling MUST NOT block its pointer, touch, or keyboard activation. | `architecture:interaction-modality/INV4, INV7, INV9`; red/green focused and browser-story tests |-| FR5 | Omitted header slots MUST omit the header row; the drawer precedes the body; input fills the body between header and footer; footer actions precede send actions and the primary action; status follows `statusPosition`. | implementation, props, stories, and blocks |-| FR6 | `compact` reduces body padding and gap; `balanced` and `spacious` produce the same body spacing. Whether `spacious` should differ is open (OQ3). | implementation and published union; no current owner selects distinct spacious geometry |-| FR7 | `low` applies low rest and medium hover and focus-within elevation; `none` applies no shadow and a border. Implementation gap: the public target does not reach the painted body (Current-state impact; OQ1). | implementation; `architecture:component-theming-surface/INV4, INV6`; the `chat-composer` target sits on the frame (Current-state impact) |-| FR8 | Keyboard focus entering the registered/default editor MUST add the shared focus indication to the body; pointer focus MUST not add that extra ring. Clicking non-interactive body space MUST focus the registered control, then a textarea fallback. | implementation; `architecture:interaction-modality/INV1–INV3, INV9`; focused tests |-| FR9 | Error status is announced assertively and warning status politely through the shared announcement channel, once per occasion: one state transition, which a rerender or an update to the same value does not repeat (`spec:AST-086` FR6); the status bar itself carries no live role. Both pair a semantic icon with color and can render above or below the body. | implementation, docs, and stories; `spec:AST-020/FR1`; `spec:AST-086/FR2, FR5, FR6` |-| FR10 | `ref`, DOM props and events, `className`, and `style` reach the outer frame; `xstyle` reaches the painted body. Implementation gap: the styling inputs split across two elements (Current-state impact; OQ2). | implementation; `architecture:public-component-api/INV5–INV6, INV8` |-| FR11 | The exported context MUST expose the current value, change and submit operations, placeholder, disabled/stop/send state, stop callback, and mutable input focus control to custom composition. | public export, types, docs, stories, and focused tests; `architecture:public-component-api/INV12` |+| ID | Invariant | Basis |+| ---- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |+| FR1 | Controlled mode MUST report changes through `onChange`; uncontrolled mode MUST retain them locally. Every successful submit MUST trim and deliver the value supplied to that operation, then clear through the same value channel. | public types and docs; `architecture:public-component-api/INV3, INV9, INV12`; focused tests |+| FR2 | The default editor MUST serialize its current text/tokens, submit Enter without Shift, preserve Shift+Enter, defer during IME composition, and allow a caller key handler to prevent the default operation. | implementation, docs, and focused tests |+| FR3 | `isDisabled` MUST prevent draft edits and submissions and MUST expose `aria-disabled="true"` on the default textbox; removing the prop MUST remove that state. | `spec:AST-020/FR1` (WCAG 2.2 4.1.2); `architecture:interaction-modality/INV5`; red/green focused test |+| FR4 | When `isStopShown` is true, the default primary action MUST remain an enabled, named Stop button and call `onStop`; if editing is also disabled, ancestor styling MUST NOT block its pointer, touch, or keyboard activation. | `architecture:interaction-modality/INV4, INV7, INV9`; red/green focused and browser-story tests |+| FR5 | Omitted header slots MUST omit the header row; the drawer precedes the body; input fills the body between header and footer; footer actions precede send actions and the primary action; status follows `statusPosition`. | implementation, props, stories, and blocks |+| FR6 | `compact` reduces body padding and gap; `balanced` and `spacious` produce the same body spacing. Whether `spacious` should differ is open (OQ3). | implementation and published union; no current owner selects distinct spacious geometry |+| FR7 | `low` applies low rest and medium hover and focus-within elevation; `none` applies no shadow and a border. Implementation gap: the public target does not reach the painted body (Current-state impact; OQ1). | implementation; `architecture:component-theming-surface/INV4, INV6`; the `chat-composer` target sits on the frame (Current-state impact) |+| FR8 | Keyboard focus entering the registered/default editor MUST add the shared focus indication to the body; pointer focus MUST not add that extra ring. Clicking non-interactive body space MUST focus the registered control, then a textarea fallback; a click inside an open Tooltip or HoverCard surface is not body space (`component:Tooltip` FR11, `component:HoverCard` FR11). | implementation; `architecture:interaction-modality/INV1–INV3, INV9`; focused tests |+| FR9 | Error status is announced assertively and warning status politely through the shared announcement channel, once per occasion: one state transition, which a rerender or an update to the same value does not repeat (`spec:AST-086` FR6); the status bar itself carries no live role. Both pair a semantic icon with color and can render above or below the body. | implementation, docs, and stories; `spec:AST-020/FR1`; `spec:AST-086/FR2, FR5, FR6` |+| FR10 | `ref`, DOM props and events, `className`, and `style` reach the outer frame; `xstyle` reaches the painted body. Implementation gap: the styling inputs split across two elements (Current-state impact; OQ2). | implementation; `architecture:public-component-api/INV5–INV6, INV8` |+| FR11 | The exported context MUST expose the current value, change and submit operations, placeholder, disabled/stop/send state, stop callback, and mutable input focus control to custom composition. | public export, types, docs, stories, and focused tests; `architecture:public-component-api/INV12` |### Allowed variation@@ -214,7 +214,7 @@ yet; the block is optional during migration, so the map is deferred.| FR3, AR1 | `ChatComposer.test.tsx` "exposes disabled state on the default textbox and clears it when enabled" | disabled and re-enabled default textbox | A textbox that loses or keeps its disabled state wrongly fails. | `audit:ChatComposer/accessibility` || FR4, AR2 | `ChatComposer.test.tsx` "keeps the Stop action pointer-operable while editing is disabled"; the `DisabledStreamingNarrow` story play in `ChatComposer.stories.tsx` | disabled editing with Stop shown; a narrow viewport with no horizontal overflow | A Stop action an ancestor blocks, or one that does not call `onStop`, fails. | `audit:ChatComposer/modality` || FR7 | `ChatComposer.test.tsx` elevation tests | `low` default, `none` | Levels that share a body class, or a default other than `low`, fail. | `audit:ChatComposer/theming` |-| FR8, AR4 | `ChatComposer.test.tsx` focus indication tests and custom-input focus tests | keyboard, pointer, programmatic after each, internal button, body click | A ring on pointer focus, a missing ring on keyboard focus, or a body click that skips the control fails. | `audit:ChatComposer/focus` |+| FR8, AR4 | `ChatComposer.test.tsx` focus indication tests and custom-input focus tests; "keeps a click on an open tooltip in the body from focusing the input" | keyboard, pointer, programmatic after each, internal button, body click | A ring on pointer focus, a missing ring on keyboard focus, or a body click that skips the control fails. | `audit:ChatComposer/focus` || FR9, AR5 | `ChatComposer.test.tsx` status announcement tests | error, warning, StrictMode mount, repeat after clear, no message, page load | A live role on the bar, a repeated or missing announcement, or the wrong urgency fails. | `audit:ChatComposer/accessibility` |Read from source, with no focused test yet:diff --git a/packages/core/src/Chat/ChatComposer.test.tsx b/packages/core/src/Chat/ChatComposer.test.tsxindex 6d9f0fc052..77a11d96aa 100644--- a/packages/core/src/Chat/ChatComposer.test.tsx+++ b/packages/core/src/Chat/ChatComposer.test.tsx@@ -6,6 +6,7 @@ import userEvent from '@testing-library/user-event';import {StrictMode, useEffect, useRef} from 'react';import {ChatComposer} from './ChatComposer';import {ChatComposerInput} from './ChatComposerInput';+import {Tooltip} from '../Tooltip/Tooltip';import {useChatComposerContext} from './ChatContext';import {createStaticSource} from '../Typeahead/createStaticSource';import {__resetInteractionModalityForTest} from '../utils/interactionModality';@@ -279,6 +280,28 @@ describe('ChatComposer input composition contract', () => {expect(focusSpy).toHaveBeenCalled();});+ it('keeps a click on an open tooltip in the body from focusing the input (Tooltip FR11)', () => {+ const {getByTestId} = render(+ <ChatComposer+ onSubmit={() => {}}+ input={+ <div>+ <textarea data-testid="bare-textarea" />+ <Tooltip content="Tooltip text" isOpen>+ <span tabIndex={0}>Info</span>+ </Tooltip>+ </div>+ }+ />,+ );+ const textarea = getByTestId('bare-textarea') as HTMLTextAreaElement;+ fireEvent.click(screen.getByText('Tooltip text'));+ expect(document.activeElement).not.toBe(textarea);+ // Positive control: a click on the body's own space still focuses it.+ fireEvent.click(textarea.parentElement!.parentElement!.parentElement!);+ expect(document.activeElement).toBe(textarea);+ });+it('falls back to focusing a bare textarea when no control is registered', () => {const {getByTestId} = render(<ChatComposerdiff --git a/packages/core/src/Chat/ChatComposer.tsx b/packages/core/src/Chat/ChatComposer.tsxindex 92b131148c..cb218a5306 100644--- a/packages/core/src/Chat/ChatComposer.tsx+++ b/packages/core/src/Chat/ChatComposer.tsx@@ -55,6 +55,8 @@ import {themeProps} from '../utils/themeProps';import {useTranslator} from '../i18n';import {useAnnounceRenderedText} from '../hooks/useAnnounceRenderedText';import {useMergedRefs} from '../hooks/useMergedRefs';+import {innermostInteractive} from '../hooks/useClickableContainer';+import {isLayerSurface} from '../Layer/layerSurfacePress';import {focusOutlineStyles} from '../utils/focusOutline.stylex';import {useInteractionModalityTracking} from '../utils/interactionModality';@@ -431,11 +433,14 @@ export function ChatComposer(props: ChatComposerProps) {const handleBodyClick = useCallback((e: MouseEvent<HTMLDivElement>) => {// Focus the input when clicking empty space in the body.// Skip if the click target is a button, link, or interactive element.+ // A press inside an open Tooltip or HoverCard stays in that surface+ // (component:Tooltip FR11, component:HoverCard FR11).const target = e.target as HTMLElement;if (target.closest('button, a, [role="button"], [contenteditable="true"], [data-astryx-token]',- )+ ) ||+ innermostInteractive(target, e.currentTarget, isLayerSurface) != null) {return;}