Skip to content

Commit 0c4ec2d

Browse files
fix(web): return focus to the composer after saving a citation note
Saving or cancelling a citation comment now puts the caret right after the chip instead of on the edit button, so Enter sends. Shift+Tab from just after a chip reaches its edit button, and Tab from that button returns to the caret.
1 parent cb1a3f3 commit 0c4ec2d

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

‎apps/web/src/components/ComposerPromptEditorTiptap.tsx‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
useMemo,
3030
useRef,
3131
useState,
32+
type KeyboardEvent as ReactKeyboardEvent,
3233
} from "react";
3334
import { EditorContent, useEditor } from "@tiptap/react";
3435

@@ -371,13 +372,39 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
371372
.run();
372373
}, [editor, nodePos]);
373374

375+
// Put the caret right after the chip so Enter sends and typing continues the prompt.
376+
const onRestoreFocus = useCallback(() => {
377+
if (!editor.isEditable) return;
378+
const pos = nodePos();
379+
if (pos === null) return;
380+
const current = editor.state.doc.nodeAt(pos);
381+
if (!current || current.type.name !== "composer-citation") return;
382+
editor.commands.focus(pos + current.nodeSize);
383+
}, [editor, nodePos]);
384+
374385
return (
375386
<NodeViewWrapper
376387
as="span"
377388
className="inline-flex min-w-0 max-w-full select-none"
378389
contentEditable={false}
379390
spellCheck={false}
380391
data-composer-citation-chip="true"
392+
onKeyDown={(event: ReactKeyboardEvent<HTMLElement>) => {
393+
// Tab from the comment button returns to the caret after the chip.
394+
if (
395+
event.key !== "Tab" ||
396+
event.shiftKey ||
397+
event.altKey ||
398+
event.metaKey ||
399+
event.ctrlKey ||
400+
!(event.target instanceof HTMLElement) ||
401+
event.target.dataset.citationCommentTrigger === undefined
402+
) {
403+
return;
404+
}
405+
event.preventDefault();
406+
onRestoreFocus();
407+
}}
381408
>
382409
<AssistantCitationChip
383410
citation={citation}
@@ -396,6 +423,7 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
396423
commentContext.onSubmitAndSend();
397424
return true;
398425
},
426+
onRestoreFocus,
399427
}}
400428
/>
401429
</NodeViewWrapper>
@@ -856,6 +884,32 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
856884
return true;
857885
}
858886
}
887+
// Shift+Tab from just after a citation reaches its comment button, which
888+
// native tab order skips because the chip lives inside the editor.
889+
if (
890+
event.key === "Tab" &&
891+
event.shiftKey &&
892+
!event.altKey &&
893+
!event.metaKey &&
894+
!event.ctrlKey &&
895+
view.state.selection.empty
896+
) {
897+
const { $from } = view.state.selection;
898+
const citation = $from.nodeBefore;
899+
if (citation?.type.name === "composer-citation") {
900+
const chip = view.nodeDOM($from.pos - citation.nodeSize);
901+
const commentButton =
902+
chip instanceof HTMLElement
903+
? chip.querySelector<HTMLElement>("[data-citation-comment-trigger]")
904+
: null;
905+
if (commentButton) {
906+
event.preventDefault();
907+
event.stopPropagation();
908+
commentButton.focus();
909+
return true;
910+
}
911+
}
912+
}
859913
if (event.key === "Enter" && (event.isComposing || event.keyCode === 229)) {
860914
event.stopPropagation();
861915
return true;

‎apps/web/src/components/chat/AssistantCitationChip.tsx‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
2323
import { AssistantCitationCommentEditor } from "./AssistantCitationCommentEditor";
2424
import { resolveAssistantCitationCommentDismissal } from "./assistantCitationCommentDismissal";
2525
import { observeAssistantCitationCommentSource } from "./AssistantCitationSource";
26-
import { composerFloatingLayerProps } from "./composerEventScope";
26+
import { composerFloatingLayerProps, isInsideComposerFloatingLayer } from "./composerEventScope";
2727

2828
export function AssistantCitationChip({
2929
citation,
@@ -39,6 +39,8 @@ export function AssistantCitationChip({
3939
onCancel?: () => void;
4040
onSave: (comment: string) => boolean;
4141
onSaveAndSend?: (comment: string) => boolean;
42+
/** Returns focus to the host editor when the popover closes instead of to the pencil trigger. */
43+
onRestoreFocus?: () => void;
4244
};
4345
}) {
4446
const navigate = useNavigate();
@@ -155,6 +157,7 @@ export function AssistantCitationChip({
155157
>
156158
<PopoverTrigger
157159
aria-label={citation.comment ? "Edit citation comment" : "Add comment to citation"}
160+
data-citation-comment-trigger="true"
158161
render={<ContextChipAction />}
159162
>
160163
<PencilIcon aria-hidden="true" />
@@ -169,6 +172,21 @@ export function AssistantCitationChip({
169172
commentInputRef.current?.focus({ preventScroll: true });
170173
return false;
171174
}}
175+
finalFocus={
176+
commentEditor.onRestoreFocus
177+
? () => {
178+
// Leave focus alone when the user closed the popover by moving to another control.
179+
const activeElement = document.activeElement;
180+
if (
181+
activeElement === document.body ||
182+
isInsideComposerFloatingLayer(activeElement)
183+
) {
184+
commentEditor.onRestoreFocus?.();
185+
}
186+
return false;
187+
}
188+
: undefined
189+
}
172190
aria-label="Edit citation comment"
173191
width="md"
174192
padding="compact"

0 commit comments

Comments
 (0)