Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

Fix: Channel keyboard navigation (#1130)

Peter Chinman44260f471723
diff --git a/js/app/packages/block-channel/component/Channel.tsx b/js/app/packages/block-channel/component/Channel.tsx
index 2dbd905c24..c9a407bb5d 100644
--- a/js/app/packages/block-channel/component/Channel.tsx
+++ b/js/app/packages/block-channel/component/Channel.tsx
@@ -53,14 +53,17 @@ import {
createRenderEffect,
createSignal,
on,
- onCleanup,
onMount,
Suspense,
} from 'solid-js';
import { createStore } from 'solid-js/store';
import { type FocusableElement, tabbable } from 'tabbable';
import { ChannelInput } from './ChannelInput';
-import { MessageList, type TargetMessageInfo } from './MessageList/MessageList';
+import {
+ MessageList,
+ type MessageListNavigation,
+ type TargetMessageInfo,
+} from './MessageList/MessageList';
import { Top } from './Top';
import { useSplitPanelOrThrow } from '@app/component/split-layout/layoutUtils';
@@ -150,10 +153,13 @@ export function Channel(props: {
},
});
- const [focusedMessageId, setFocusedMessageId] = createSignal<
+ const [selectedMessageId, setSelectedMessageId] = createSignal<
string | undefined
>(undefined);
+ const [messageListNav, setMessageListNav] =
+ createSignal<MessageListNavigation>();
+
onMount(() => {
updateActivityOnOpen();
@@ -210,28 +216,54 @@ export function Channel(props: {
});
});
- const focusPrevious = () => {
- const tabbableEls = tabbable(blockRef()!);
- const fromEl = document.activeElement;
-
- const fromElIndex = tabbableEls.indexOf(fromEl as FocusableElement);
- if (fromElIndex !== -1) {
- const prevIndex = fromElIndex - 1;
- if (prevIndex < 0) return false;
- const prevEl = tabbableEls[prevIndex];
- if (!prevEl) return false;
- prevEl.focus();
- return true;
- } else {
- tabbableEls.at(-1)?.focus();
- return true;
+ /**
+ * Hybrid navigation: tries DOM-based tabbable navigation first,
+ * falls back to message ID navigation if element was unmounted (which can happen if you're moving rapidly with virtualization)
+ */
+ const navigateChannel = (direction: 'previous' | 'next') => {
+ const block = blockRef();
+ if (!block) return false;
+
+ const tabbableEls = tabbable(block);
+ let activeEl = document.activeElement;
+
+ const selectedMessageEl = block.querySelector(
+ `[data-message-id="${selectedMessageId()}"]`
+ );
+
+ if (selectedMessageEl && !selectedMessageEl?.contains(activeEl)) {
+ // Selected message gets set on hover without actually becoming focused. If the active element is not inside the selected message, it is probably because the user has hovered over a new message, and so we should proceed as if the selected message were the active element.
+ activeEl = selectedMessageEl;
+ }
+
+ const activeElIndex = tabbableEls.indexOf(activeEl as FocusableElement);
+
+ // DOM-based navigation: element is in tabbable list
+ if (activeElIndex !== -1) {
+ const targetIndex =
+ direction === 'previous' ? activeElIndex - 1 : activeElIndex + 1;
+
+ if (targetIndex >= 0 && targetIndex < tabbableEls.length) {
+ const targetEl = tabbableEls[targetIndex];
+ targetEl?.focus();
+ return;
+ }
+ }
+
+ // Fallback: element not in tabbable list (this can happen if moving too quickly thru the virtualized list)
+ // Use message ID-based navigation
+ const nav = messageListNav();
+ if (nav) {
+ return direction === 'previous'
+ ? nav.navigatePrevious()
+ : nav.navigateNext();
}
};
const onChannelInputFocusLeaveStart = (e: KeyboardEvent) => {
e.preventDefault();
e.stopPropagation();
- return focusPrevious();
+ return navigateChannel('previous');
};
registerHotkey({
@@ -254,7 +286,8 @@ export function Channel(props: {
scopeId: scopeId(),
description: 'Focus previous',
keyDownHandler: () => {
- return focusPrevious();
+ navigateChannel('previous');
+ return true;
},
hotkeyToken: TOKENS.channel.focusPreviousMessage,
hide: true,
@@ -265,18 +298,8 @@ export function Channel(props: {
scopeId: scopeId(),
description: 'Focus next',
keyDownHandler: () => {
- const tabbableEls = tabbable(blockRef()!);
- const activeEl = document.activeElement;
- const activeElIndex = tabbableEls.indexOf(activeEl as FocusableElement);
- if (activeElIndex !== -1) {
- const nextIndex = activeElIndex + 1;
- if (nextIndex >= tabbableEls.length) return false;
- const nextEl = tabbableEls[nextIndex];
- if (!nextEl) return false;
- nextEl.focus();
- return true;
- }
- return false;
+ navigateChannel('next');
+ return true;
},
hotkeyToken: TOKENS.channel.focusNextMessage,
hide: true,
@@ -284,9 +307,6 @@ export function Channel(props: {
const [channelInputRef, setChannelInputRef] = createSignal<
HTMLDivElement | undefined
>();
- const [lastMessageRef, setLastMessageRef] = createSignal<
- HTMLDivElement | undefined
- >();
const [autoFocusOnMount, setAutoFocusOnMount] = createSignal(true);
const { navigatedFromJK } = useNavigatedFromJK();
@@ -297,32 +317,6 @@ export function Channel(props: {
}
});
- let initialFocusSet = false;
- createEffect(
- on(lastMessageRef, () => {
- if (autoFocusOnMount()) return;
- if (!navigatedFromJK()) return;
- if (initialFocusSet) return;
-
- const lastMessage = lastMessageRef();
- if (!lastMessage) return;
- // Using Resize Observer here so that we only focus when the lastMessageRef
- // is actually connected to DOM
- const resizeObserver = new ResizeObserver(() => {
- const lastMessageId = lastMessage?.getAttribute('data-message-body-id');
- if (!lastMessageId) return;
- setTimeout(() => lastMessage.focus(), 0);
- setFocusedMessageId(lastMessageId);
- initialFocusSet = true;
- });
- resizeObserver.observe(lastMessage);
-
- onCleanup(() => {
- resizeObserver.disconnect();
- });
- })
- );
-
const debouncedMarkAsRead = makeDebouncedChannelNotificationReadMarker({
notificationSource: notificationSource,
channelId,
@@ -410,13 +404,13 @@ export function Channel(props: {
<MessageList
channelId={channelId}
messages={channelStoreData.messages}
- focusedMessageId={focusedMessageId}
- setFocusedMessageId={setFocusedMessageId}
+ focusedMessageId={selectedMessageId}
+ setFocusedMessageId={setSelectedMessageId}
targetMessage={targetMessage}
latestActivity={latestActivity()}
orderedMessages={orderedMessages}
setOrderedMessages={setOrderedMessages}
- setLastMessageRef={setLastMessageRef}
+ onNavigationReady={setMessageListNav}
/>
<div class="shrink-0 w-full px-4 pb-2">
{/* seamus: note this element is below the scroll so we translate it back to account for the scroll above */}
diff --git a/js/app/packages/block-channel/component/Message/ActionMenu.tsx b/js/app/packages/block-channel/component/Message/ActionMenu.tsx
index 066e8ec14e..35acd9865c 100644
--- a/js/app/packages/block-channel/component/Message/ActionMenu.tsx
+++ b/js/app/packages/block-channel/component/Message/ActionMenu.tsx
@@ -33,7 +33,7 @@ export function ActionMenu(props: {
<DeprecatedIconButton
onMouseDown={() => react(emoji)}
icon={() => <span>{emoji}</span>}
- tabIndex={0}
+ tabIndex={-1}
/>
)}
</For>
@@ -54,7 +54,7 @@ export function ActionMenu(props: {
onMouseDown={a.onClick}
icon={a.icon}
tooltip={{ label: a.text, delayOverride: 0 }}
- tabIndex={0}
+ tabIndex={-1}
/>
)}
</For>
diff --git a/js/app/packages/block-channel/component/Message/MessageContainer.tsx b/js/app/packages/block-channel/component/Message/MessageContainer.tsx
index 983dc4b5bc..bf3cfc9a58 100644
--- a/js/app/packages/block-channel/component/Message/MessageContainer.tsx
+++ b/js/app/packages/block-channel/component/Message/MessageContainer.tsx
@@ -54,6 +54,7 @@ import { EditMessageInput } from './EditMessageInput';
import { MessageAttachments } from './MessageAttachments';
import { MessageReactions } from './MessageReactions';
import { ThreadReplyIndicator } from './ThreadReplyIndicator';
+import { useIsKeyPressActive } from '@core/util/useIsKeyPressActive';
type MessageFlagProps = {
text: string;
@@ -110,7 +111,6 @@ type MessageProps = {
container?: HTMLDivElement;
listContext: MessageListContext;
setMessageContainerRef?: Setter<HTMLDivElement | undefined>;
- setLastMessageRef?: Setter<HTMLDivElement | undefined>;
isTarget: boolean;
};
@@ -123,20 +123,7 @@ export function MessageContainer(props: MessageProps) {
const [contextMenuOpen, setContextMenuOpen] = createSignal(false);
const [reactionSearchOpen, setReactionSearchOpen] = createSignal(false);
const [topBarEmojiMenuOpen, setTopBarEmojiMenuOpen] = createSignal(false);
- const [messageBodyRef, setMessageBodyRefInner] =
- createSignal<HTMLDivElement>();
-
- const setMessageBodyRef = ((
- value?:
- | HTMLDivElement
- | ((prev?: HTMLDivElement) => HTMLDivElement | undefined)
- ): undefined => {
- setMessageBodyRefInner(value);
- if (isLastMessage()) {
- props.setLastMessageRef?.(value);
- }
- return undefined;
- }) satisfies typeof setMessageBodyRefInner;
+ const [messageBodyRef, setMessageBodyRef] = createSignal<HTMLDivElement>();
const editMessageMutation = usePatchMessageMutation();
@@ -480,6 +467,17 @@ export function MessageContainer(props: MessageProps) {
listContext.toggleThread(message.thread_id);
};
+ const { isKeypressActive } = useIsKeyPressActive();
+
+ const setSelectedMessage = () => {
+ listContext.setFocusedMessageId(message.id);
+ };
+
+ const setSelectedMessageFromMouse = () => {
+ if (isKeypressActive()) return;
+ listContext.setFocusedMessageId(message.id);
+ };
+
return (
<div
class={`shrink-0 flex justify-center w-full ${isTouchDevice() ? 'no-select-children' : ''}`}
@@ -487,6 +485,13 @@ export function MessageContainer(props: MessageProps) {
props.setMessageContainerRef?.(el);
messageContainerRef = el;
}}
+ onFocusIn={() => {
+ setSelectedMessage();
+ }}
+ onMouseMove={() => {
+ if (isTouchDevice()) return;
+ setSelectedMessageFromMouse();
+ }}
data-message-id={message.id}
>
<div class="macro-message-width w-full">
diff --git a/js/app/packages/block-channel/component/MessageList/MessageList.tsx b/js/app/packages/block-channel/component/MessageList/MessageList.tsx
index b53924870c..718f0285df 100644
--- a/js/app/packages/block-channel/component/MessageList/MessageList.tsx
+++ b/js/app/packages/block-channel/component/MessageList/MessageList.tsx
@@ -29,7 +29,6 @@ import type { Activity as ChannelActivity } from '@service-comms/generated/model
import type { Message } from '@service-comms/generated/models/message';
import { useUserId } from '@core/context/user';
import { debounce } from '@solid-primitives/scheduled';
-import { activeElement } from 'app/signal/focus';
import {
type Accessor,
createContext,
@@ -66,6 +65,7 @@ type RowModel = {
const BASE_ITEM_SIZE = 50;
type MessageListContentContextValues = {
+ setFocusedMessageId: Setter<string | undefined>;
registerVirtualHandle: (handle: VirtualizerHandle) => void;
scrollContainerRef: Accessor<HTMLElement | undefined>;
registerScrollContainer: (el: HTMLElement) => void;
@@ -92,6 +92,15 @@ export const useMessageListContext = () => {
export type TargetMessageInfo = { messageId: string; threadId?: string };
+export type MessageListNavigation = {
+ /** Navigate to previous message (up in the list) */
+ navigatePrevious: () => boolean;
+ /** Navigate to next message (down in the list) */
+ navigateNext: () => boolean;
+ /** Navigate to a specific message by ID */
+ navigateToMessage: (messageId: string) => boolean;
+};
+
export type MessageListProps = {
channelId: string;
messages: Message[];
@@ -100,9 +109,10 @@ export type MessageListProps = {
targetMessage: Accessor<TargetMessageInfo | undefined>;
focusedMessageId: Accessor<string | undefined>;
setFocusedMessageId: Setter<string | undefined>;
+ /** Callback to expose navigation methods to parent */
+ onNavigationReady?: (nav: MessageListNavigation) => void;
orderedMessages: Accessor<Message[]>;
setOrderedMessages: Setter<Message[]>;
- setLastMessageRef?: Setter<HTMLDivElement | undefined>;
};
function EmptyMessageList() {
@@ -132,6 +142,7 @@ export function MessageList(props: MessageListProps) {
};
const context: MessageListContentContextValues = {
+ setFocusedMessageId: props.setFocusedMessageId,
registerVirtualHandle: setVirtualHandle,
scrollContainerRef,
registerScrollContainer: setScrollContainerRef,
@@ -249,6 +260,64 @@ function MessageListImpl(props: MessageListProps) {
return length - 1 - index;
};
+ // Navigation methods for keyboard navigation
+ const navigateToMessage = (messageId: string): boolean => {
+ const messages = props.orderedMessages();
+ const index = messages.findIndex((m) => m.id === messageId);
+ if (index === -1) return false;
+
+ const handle = virtualHandle();
+ if (!handle) return false;
+
+ // Scroll to the message
+ handle.scrollToIndex(normalizeIndex(index), { align: 'nearest' });
+
+ // Focus after scroll completes
+ requestAnimationFrame(() => {
+ const targetEl = containerRef()?.querySelector<HTMLElement>(
+ `[data-message-body-id="${messageId}"]`
+ );
+ targetEl?.focus();
+ });
+
+ return true;
+ };
+
+ const navigateByOffset = (
+ direction: -1 | 1,
+ fallbackToLast: boolean
+ ): boolean => {
+ const messages = props.orderedMessages();
+ const currentId = props.focusedMessageId();
+ const currentIndex = currentId
+ ? messages.findIndex((m) => m.id === currentId)
+ : -1;
+
+ if (currentIndex === -1) {
+ if (fallbackToLast && messages.length > 0) {
+ return navigateToMessage(messages[messages.length - 1].id);
+ }
+ return false;
+ }
+
+ const nextIndex = currentIndex + direction;
+ if (nextIndex < 0 || nextIndex >= messages.length) return false;
+
+ return navigateToMessage(messages[nextIndex].id);
+ };
+
+ const navigatePrevious = (): boolean => navigateByOffset(-1, true);
+ const navigateNext = (): boolean => navigateByOffset(1, false);
+
+ // Expose navigation methods to parent
+ onMount(() => {
+ props.onNavigationReady?.({
+ navigatePrevious,
+ navigateNext,
+ navigateToMessage,
+ });
+ });
+
const lastViewed = createMemo(() => {
return props?.latestActivity?.viewed_at;
});
@@ -372,15 +441,6 @@ function MessageListImpl(props: MessageListProps) {
setMessageListContext(reconcile(context));
}
- createEffect(() => {
- const activeElement_ = activeElement();
- const activeMessageId = activeElement_?.getAttribute(
- 'data-message-body-id'
- );
-
- props.setFocusedMessageId(activeMessageId ?? undefined);
- });
-
const isFocused = createSelector(props.focusedMessageId);
// Keep the message if:
@@ -807,7 +867,6 @@ function MessageListImpl(props: MessageListProps) {
virtualHandle={virtualHandle()!}
container={containerRef()}
listContext={messageListContext[row.id]}
- setLastMessageRef={props.setLastMessageRef}
isTarget={isActiveTargetMessage(row.message.id)}
/>
</Show>
diff --git a/js/app/packages/block-channel/component/ReactionSelector.tsx b/js/app/packages/block-channel/component/ReactionSelector.tsx
index 57b7e3666e..f00f5e1a13 100644
--- a/js/app/packages/block-channel/component/ReactionSelector.tsx
+++ b/js/app/packages/block-channel/component/ReactionSelector.tsx
@@ -227,7 +227,7 @@ export function ReactionSelector(props: ReactionSelectorProps) {
slide={true}
open={openPopover()}
>
- <Popover.Trigger>
+ <Popover.Trigger tabIndex={-1}>
<DeprecatedIconButton icon={SmileIcon} tabIndex={-1} />
</Popover.Trigger>
diff --git a/js/app/packages/core/component/BlockContainer.tsx b/js/app/packages/core/component/BlockContainer.tsx
index 5e1d9e817b..2d504af81d 100644
--- a/js/app/packages/core/component/BlockContainer.tsx
+++ b/js/app/packages/core/component/BlockContainer.tsx
@@ -91,13 +91,6 @@ export function BlockContainer(props: BlockContainerProps) {
resolved_.id = getBlockElementId(blockId);
resolved_.dataset.blockType = blockName;
setElement(resolved_);
- });
-
- onMount(() => {
- const resolved_ = resolved();
- if (!(resolved_ instanceof HTMLElement)) {
- return;
- }
attachHotkeys(resolved_);
});
diff --git a/js/app/packages/core/component/Message.tsx b/js/app/packages/core/component/Message.tsx
index 2ca067b48f..e2d4dc14a3 100644
--- a/js/app/packages/core/component/Message.tsx
+++ b/js/app/packages/core/component/Message.tsx
@@ -244,7 +244,7 @@ const Root: Component<MessageRootProps> = (props) => {
? `${replyHeight()}px`
: '0px',
}}
- hover={props.shouldHover || hover()}
+ hover={props.shouldHover}
>
{/* Message Wrapper w/ Main Connector Line */}
<div
@@ -336,11 +336,12 @@ const Root: Component<MessageRootProps> = (props) => {
<div
class="absolute right-0 -top-2 flex flex-col items-end z-tool-tip"
classList={{
- block: hover() || !!props.shouldHover,
- hidden: !(hover() || !!props.shouldHover),
+ block: props.focused || !!props.shouldHover,
+ hidden: !(props.focused || !!props.shouldHover),
}}
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
+ data-message-id={props.id}
>
<Show when={props.timestamp}>
<div class="absolute top-0 translate-y-[-100%] bg-panel pl-2 pt-2 text-xs text-ink-muted font-mono mb-0.5 select-text cursor-default">