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.

feat(ui): add mac-only ctrl-j/k menu nav to core menus (#4115)

Seamus004d6db94bac
diff --git a/js/app/packages/core/component/ContextMenu.tsx b/js/app/packages/core/component/ContextMenu.tsx
index e48f42641a..5167845f5c 100644
--- a/js/app/packages/core/component/ContextMenu.tsx
+++ b/js/app/packages/core/component/ContextMenu.tsx
@@ -3,7 +3,7 @@ import { isMobile } from '@core/mobile/isMobile';
import { ContextMenu } from '@kobalte/core/context-menu';
import CaretRight from '@phosphor/caret-right.svg?component-solid';
import CheckIcon from '@phosphor/check.svg?component-solid';
-import { cn, Layer } from '@ui';
+import { addCtrlJKMenuNavigation, cn, Layer } from '@ui';
import {
type Component,
createEffect,
@@ -391,6 +391,13 @@ export function ContextMenuContent(props: ParentProps<MenuContentProps>) {
observer?.disconnect();
});
});
+
+ createEffect(() => {
+ if (!contentRef) return;
+ const cleanup = addCtrlJKMenuNavigation(contentRef);
+ onCleanup(cleanup);
+ });
+
return (
<MobileConditionalOverlay mobileFullScreen={props.mobileFullScreen}>
<Show
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/menu/useMenuKeyboardNavigation.ts b/js/app/packages/core/component/LexicalMarkdown/component/menu/useMenuKeyboardNavigation.ts
index 70ceca4946..1f256fdacf 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/menu/useMenuKeyboardNavigation.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/component/menu/useMenuKeyboardNavigation.ts
@@ -7,10 +7,10 @@ type MenuKeyboardHandlers = {
/** Called when the user navigates down (ArrowDown, Ctrl+J, Ctrl+N, Tab) */
onDown?: (e: KeyboardEvent) => void;
- /** Called when the user navigates left (ArrowLeft) */
+ /** Called when the user navigates left (ArrowLeft, Ctrl+H) */
onLeft?: (e: KeyboardEvent) => void;
- /** Called when the user navigates right (ArrowRight) */
+ /** Called when the user navigates right (ArrowRight, Ctrl+L) */
onRight?: (e: KeyboardEvent) => void;
/** Called when the user confirms selection (Enter) */
@@ -54,8 +54,8 @@ type MenuKeyboardHandlers = {
* Key mappings:
* - Up: ArrowUp, Ctrl+K, Ctrl+P, Shift+Tab
* - Down: ArrowDown, Ctrl+J, Ctrl+N, Tab (without Shift)
- * - Left: ArrowLeft
- * - Right: ArrowRight
+ * - Left: ArrowLeft, Ctrl+H
+ * - Right: ArrowRight, Ctrl+L
* - Select: Enter
* - Close: Escape
* - Space: Space
@@ -127,6 +127,18 @@ function createMenuKeyboardNavigation(handlers: MenuKeyboardHandlers): {
}
break;
+ case 'h':
+ if (e.ctrlKey || e.metaKey) {
+ handler = onLeft;
+ }
+ break;
+
+ case 'l':
+ if (e.ctrlKey || e.metaKey) {
+ handler = onRight;
+ }
+ break;
+
case 'n':
if (e.ctrlKey) {
handler = onDown;
diff --git a/js/app/packages/ui/components/Dropdown.tsx b/js/app/packages/ui/components/Dropdown.tsx
index d2b25db3d0..1f094ddeb5 100644
--- a/js/app/packages/ui/components/Dropdown.tsx
+++ b/js/app/packages/ui/components/Dropdown.tsx
@@ -1,7 +1,8 @@
import { DropdownMenu as KobalteDropdownMenu } from '@kobalte/core/dropdown-menu';
import CheckIcon from '@phosphor/check.svg';
-import { type ComponentProps, splitProps } from 'solid-js';
+import { type ComponentProps, onCleanup, splitProps } from 'solid-js';
import { cn } from '../utils/classname';
+import { addCtrlJKMenuNavigation } from '../utils/menuKeyboardNavigation';
import { Button, type ButtonProps } from './Button';
import { Surface, type SurfaceProps } from './Surface';
@@ -82,6 +83,15 @@ function resolvePortalMount(
return searchRef?.closest<HTMLElement>('.portal-scope') ?? undefined;
}
+function installKeyboardNavigation(el: HTMLElement) {
+ const cleanup = addCtrlJKMenuNavigation(el);
+ onCleanup(cleanup);
+}
+
+function callRef<T>(ref: ((el: T) => void) | undefined, el: T) {
+ ref?.(el);
+}
+
function DropdownContent(props: DropdownContentProps) {
let searchRef: HTMLDivElement | undefined;
const [local, rest] = splitProps(props, [
@@ -90,7 +100,13 @@ function DropdownContent(props: DropdownContentProps) {
'mount',
'portalScope',
'children',
+ 'ref',
]);
+ const setContentRef = (el: HTMLElement) => {
+ installKeyboardNavigation(el);
+ callRef(local.ref, el);
+ };
+
return (
<>
<div class="hidden" ref={searchRef} />
@@ -102,6 +118,7 @@ function DropdownContent(props: DropdownContentProps) {
depth={local.depth ?? 2}
as={Surface}
{...rest}
+ ref={setContentRef}
>
<div class="flex flex-col gap-px bg-edge-muted size-full">
{local.children}
@@ -120,7 +137,13 @@ function DropdownSubContent(props: DropdownSubContentProps) {
'mount',
'portalScope',
'children',
+ 'ref',
]);
+ const setContentRef = (el: HTMLElement) => {
+ installKeyboardNavigation(el);
+ callRef(local.ref, el);
+ };
+
return (
<>
<div class="hidden" ref={searchRef} />
@@ -132,6 +155,7 @@ function DropdownSubContent(props: DropdownSubContentProps) {
depth={local.depth ?? 2}
as={Surface}
{...rest}
+ ref={setContentRef}
>
<div class="flex flex-col gap-px bg-edge-muted size-full">
{local.children}
diff --git a/js/app/packages/ui/index.ts b/js/app/packages/ui/index.ts
index 454d46cf96..eadbf2fd99 100644
--- a/js/app/packages/ui/index.ts
+++ b/js/app/packages/ui/index.ts
@@ -50,3 +50,9 @@ export { Tooltip } from './components/Tooltip';
/* utils */
export { cn } from './utils/classname';
+export type { CtrlJKMenuNavigationOptions } from './utils/menuKeyboardNavigation';
+export {
+ addCtrlJKMenuNavigation,
+ ctrlJKMenuNavigation,
+ handleCtrlJKMenuNavigation,
+} from './utils/menuKeyboardNavigation';
diff --git a/js/app/packages/ui/utils/menuKeyboardNavigation.ts b/js/app/packages/ui/utils/menuKeyboardNavigation.ts
new file mode 100644
index 0000000000..a2d9145472
--- /dev/null
+++ b/js/app/packages/ui/utils/menuKeyboardNavigation.ts
@@ -0,0 +1,96 @@
+import { isMac } from '@solid-primitives/platform';
+import { type Accessor, onCleanup } from 'solid-js';
+
+export type CtrlJKMenuNavigationOptions = {
+ enabled?: boolean;
+ macOnly?: boolean;
+};
+
+declare module 'solid-js' {
+ namespace JSX {
+ interface Directives {
+ ctrlJKMenuNavigation: CtrlJKMenuNavigationOptions | boolean | undefined;
+ }
+ }
+}
+
+function resolveOptions(
+ options: CtrlJKMenuNavigationOptions | boolean | undefined
+): Required<CtrlJKMenuNavigationOptions> {
+ if (typeof options === 'boolean') {
+ return { enabled: options, macOnly: true };
+ }
+
+ return {
+ enabled: options?.enabled ?? true,
+ macOnly: options?.macOnly ?? true,
+ };
+}
+
+function translatedArrowKey(e: KeyboardEvent, macOnly: boolean) {
+ if (macOnly && !isMac) return;
+ if (!e.ctrlKey || e.metaKey || e.altKey) return;
+
+ switch (e.key.toLowerCase()) {
+ case 'j':
+ return 'ArrowDown';
+ case 'k':
+ return 'ArrowUp';
+ case 'h':
+ return 'ArrowLeft';
+ case 'l':
+ return 'ArrowRight';
+ }
+}
+
+export function handleCtrlJKMenuNavigation(
+ e: KeyboardEvent,
+ options?: CtrlJKMenuNavigationOptions | boolean
+) {
+ const resolved = resolveOptions(options);
+ if (!resolved.enabled) return false;
+
+ const key = translatedArrowKey(e, resolved.macOnly);
+ if (!key) return false;
+
+ e.preventDefault();
+ e.stopPropagation();
+
+ const target = e.target instanceof Element ? e.target : e.currentTarget;
+ if (!(target instanceof Element)) return true;
+
+ target.dispatchEvent(
+ new KeyboardEvent('keydown', {
+ key,
+ code: key,
+ bubbles: true,
+ cancelable: true,
+ composed: true,
+ })
+ );
+
+ return true;
+}
+
+export function addCtrlJKMenuNavigation(
+ el: HTMLElement,
+ options?: Accessor<CtrlJKMenuNavigationOptions | boolean | undefined>
+) {
+ const handleKeyDown = (e: KeyboardEvent) => {
+ handleCtrlJKMenuNavigation(e, options?.());
+ };
+
+ el.addEventListener('keydown', handleKeyDown, { capture: true });
+
+ return () => {
+ el.removeEventListener('keydown', handleKeyDown, { capture: true });
+ };
+}
+
+export function ctrlJKMenuNavigation(
+ el: HTMLElement,
+ options: Accessor<CtrlJKMenuNavigationOptions | boolean | undefined>
+) {
+ const cleanup = addCtrlJKMenuNavigation(el, options);
+ onCleanup(cleanup);
+}