Skip to content

Commit 4be9091

Browse files
refactor(ui): unify composer menu active-option scrolling (#124256)
* refactor(ui): unify composer menu active-option scrolling The slash-command and skill-mention composer menus each kept their own byte-identical algorithm for scrolling the keyboard-active option into view inside their ".slash-menu__scroll" viewport. Move that algorithm into the shared chat-composer-dom module as scrollActiveMenuOptionIntoView(activeId), and have each menu owner keep resolving its own active-option id while delegating the scroll math to the shared helper. Fixes #124238 * refactor(ui): inline forwarding scroll wrappers in composer menus scrollActiveSkillMenuOptionIntoView and scrollActiveSlashMenuOptionIntoView only forwarded their state/paneId args to getActive*MenuOptionId and the shared scrollActiveMenuOptionIntoView helper. Delete both wrappers and have the chat-composer.ts keydown call sites resolve the active option id and call the shared geometry helper directly, since both id resolvers were already exported and consumed elsewhere.
1 parent 644d93c commit 4be9091

4 files changed

Lines changed: 31 additions & 55 deletions

File tree

‎ui/src/pages/chat/components/chat-composer-dom.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,27 @@ export function restoreHistoryCaret(target: HTMLTextAreaElement, direction: "up"
245245
target.selectionEnd = caret;
246246
});
247247
}
248+
249+
// Shared by the slash and skill composer menus, which resolve their own
250+
// active-option id but scroll the same ".slash-menu__scroll" viewport shape.
251+
export function scrollActiveMenuOptionIntoView(activeId: string | null): void {
252+
if (!activeId) {
253+
return;
254+
}
255+
requestAnimationFrame(() => {
256+
const activeOption = document.getElementById(activeId);
257+
const scrollRegion = activeOption?.closest<HTMLElement>(".slash-menu__scroll");
258+
if (!activeOption || !scrollRegion) {
259+
return;
260+
}
261+
const menuBounds = scrollRegion.getBoundingClientRect();
262+
const optionBounds = activeOption.getBoundingClientRect();
263+
// scrollIntoView also moves the short-landscape composer and page. Keep
264+
// keyboard navigation owned by the menu so textarea focus stays stable.
265+
if (optionBounds.top < menuBounds.top) {
266+
scrollRegion.scrollTop -= menuBounds.top - optionBounds.top;
267+
} else if (optionBounds.bottom > menuBounds.bottom) {
268+
scrollRegion.scrollTop += optionBounds.bottom - menuBounds.bottom;
269+
}
270+
});
271+
}

‎ui/src/pages/chat/components/chat-composer-skill-menu.ts‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -179,30 +179,6 @@ export function getActiveSkillMenuOptionLabel(state: ChatComposerState): string
179179
return command ? `${getSkillDisplayName(command)} ${getSlashCommandDescription(command)}` : "";
180180
}
181181

182-
export function scrollActiveSkillMenuOptionIntoView(
183-
state: ChatComposerState,
184-
paneId: string,
185-
): void {
186-
const activeId = getActiveSkillMenuOptionId(state, paneId);
187-
if (!activeId) {
188-
return;
189-
}
190-
requestAnimationFrame(() => {
191-
const activeOption = document.getElementById(activeId);
192-
const scrollRegion = activeOption?.closest<HTMLElement>(".slash-menu__scroll");
193-
if (!activeOption || !scrollRegion) {
194-
return;
195-
}
196-
const menuBounds = scrollRegion.getBoundingClientRect();
197-
const optionBounds = activeOption.getBoundingClientRect();
198-
if (optionBounds.top < menuBounds.top) {
199-
scrollRegion.scrollTop -= menuBounds.top - optionBounds.top;
200-
} else if (optionBounds.bottom > menuBounds.bottom) {
201-
scrollRegion.scrollTop += optionBounds.bottom - menuBounds.bottom;
202-
}
203-
});
204-
}
205-
206182
export function selectSkillMention(
207183
command: SlashCommandDef,
208184
props: ChatComposerProps,

‎ui/src/pages/chat/components/chat-composer-slash-menu.ts‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -259,32 +259,6 @@ export function getActiveSlashMenuOptionLabel(state: ChatComposerState): string
259259
return `${command} ${getSlashCommandDescription(cmd)}`;
260260
}
261261

262-
export function scrollActiveSlashMenuOptionIntoView(
263-
state: ChatComposerState,
264-
paneId: string,
265-
): void {
266-
const activeId = getActiveSlashMenuOptionId(state, paneId);
267-
if (!activeId) {
268-
return;
269-
}
270-
requestAnimationFrame(() => {
271-
const activeOption = document.getElementById(activeId);
272-
const scrollRegion = activeOption?.closest<HTMLElement>(".slash-menu__scroll");
273-
if (!activeOption || !scrollRegion) {
274-
return;
275-
}
276-
const menuBounds = scrollRegion.getBoundingClientRect();
277-
const optionBounds = activeOption.getBoundingClientRect();
278-
// scrollIntoView also moves the short-landscape composer and page. Keep
279-
// keyboard navigation owned by the menu so textarea focus stays stable.
280-
if (optionBounds.top < menuBounds.top) {
281-
scrollRegion.scrollTop -= menuBounds.top - optionBounds.top;
282-
} else if (optionBounds.bottom > menuBounds.bottom) {
283-
scrollRegion.scrollTop += optionBounds.bottom - menuBounds.bottom;
284-
}
285-
});
286-
}
287-
288262
function renderSlashIcon(name: string) {
289263
return icons[name as IconName] ?? icons.terminal;
290264
}

‎ui/src/pages/chat/components/chat-composer.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import {
1717
replaceComposerPopoverAnchor,
1818
restoreHistoryCaret,
1919
scheduleTextareaHeightAdjustment,
20+
scrollActiveMenuOptionIntoView,
2021
} from "./chat-composer-dom.ts";
2122
import {
2223
getActiveSkillMenuOptionId,
2324
getActiveSkillMenuOptionLabel,
2425
isSkillMenuVisible,
2526
resetSkillMenuState,
26-
scrollActiveSkillMenuOptionIntoView,
2727
selectSkillMention,
2828
updateSkillMenu,
2929
} from "./chat-composer-skill-menu.ts";
@@ -34,7 +34,6 @@ import {
3434
isSlashMenuVisible,
3535
paneDomId,
3636
resetSlashMenuState,
37-
scrollActiveSlashMenuOptionIntoView,
3837
selectSlashArg,
3938
selectSlashCommand,
4039
tabCompleteSlashCommand,
@@ -322,7 +321,8 @@ export function renderChatComposer(props: ChatComposerProps) {
322321
props.paneId,
323322
requestUpdate,
324323
(command) => selectSkillMention(command, props, requestUpdate),
325-
scrollActiveSkillMenuOptionIntoView,
324+
(menuState, paneId) =>
325+
scrollActiveMenuOptionIntoView(getActiveSkillMenuOptionId(menuState, paneId)),
326326
"skill",
327327
)
328328
) {
@@ -344,7 +344,8 @@ export function renderChatComposer(props: ChatComposerProps) {
344344
props.paneId,
345345
requestUpdate,
346346
(arg, submit) => selectSlashArg(arg, props, requestUpdate, submit),
347-
scrollActiveSlashMenuOptionIntoView,
347+
(menuState, paneId) =>
348+
scrollActiveMenuOptionIntoView(getActiveSlashMenuOptionId(menuState, paneId)),
348349
)
349350
) {
350351
return;
@@ -363,7 +364,8 @@ export function renderChatComposer(props: ChatComposerProps) {
363364
submit
364365
? selectSlashCommand(command, props, requestUpdate)
365366
: tabCompleteSlashCommand(command, props, requestUpdate),
366-
scrollActiveSlashMenuOptionIntoView,
367+
(menuState, paneId) =>
368+
scrollActiveMenuOptionIntoView(getActiveSlashMenuOptionId(menuState, paneId)),
367369
)
368370
) {
369371
return;

0 commit comments

Comments
 (0)