UIBeginnerSSR-safehooks/ui/useMenu.ts

useMenu

Menu open state + highlight index.

Import

TypeScript
import { useMenu } from '@/hooks';

API

Signature

useMenu(options?) → menu + highlight API

When to use

Use useMenu instead of re-implementing the same pattern in multiple components.

SSR / Next.js

DOM extras (scroll lock, focus trap) run only in effects.

Live playground

Edit the example and see it run against the real @/hooks package in this app.

Live playgroundEdit · run with real @/hooks · useMenu
Editor
function Actions() {
  const triggerRef = useRef<HTMLButtonElement>(null);
  const contentRef = useRef<HTMLDivElement>(null);
  const menu = useMenu({ triggerRef, contentRef });
  return (
    <>
      <button
        ref={triggerRef}
        {...menu.getTriggerProps()}
        onKeyDown={(e) => menu.onKeyDown(e, 3)}
      >
        Actions
      </button>
      {menu.isOpen && (
        <div ref={contentRef}>
          <div data-active={menu.highlightedIndex === 0}>Edit</div>
          <div data-active={menu.highlightedIndex === 1}>Share</div>
          <div data-active={menu.highlightedIndex === 2}>Delete</div>
        </div>
      )}
    </>
  );
}
Preview

Implementation source

Full library source for useMenu — copy into your project when you are not installing a package yet.

hooks/ui/useMenu.ts
import { useCallback, useState } from 'react';
import { usePopover, type UsePopoverOptions } from './usePopover';

export interface UseMenuOptions extends UsePopoverOptions {
  defaultHighlightedIndex?: number;
}

/** Menu open state plus keyboard highlight index. */
export function useMenu(options: UseMenuOptions = {}) {
  const { defaultHighlightedIndex = -1, ...popoverOptions } = options;
  const popover = usePopover(popoverOptions);
  const [highlightedIndex, setHighlightedIndex] = useState(
    defaultHighlightedIndex,
  );

  const move = useCallback((delta: number, count: number) => {
    if (count <= 0) return;
    setHighlightedIndex((prev) => {
      const next = prev + delta;
      if (next < 0) return count - 1;
      if (next >= count) return 0;
      return next;
    });
  }, []);

  const onKeyDown = useCallback(
    (event: React.KeyboardEvent, itemCount: number) => {
      if (event.key === 'ArrowDown') {
        event.preventDefault();
        if (!popover.isOpen) popover.open();
        move(1, itemCount);
      } else if (event.key === 'ArrowUp') {
        event.preventDefault();
        if (!popover.isOpen) popover.open();
        move(-1, itemCount);
      } else if (event.key === 'Escape') {
        popover.close();
      }
    },
    [move, popover],
  );

  return { ...popover, highlightedIndex, setHighlightedIndex, onKeyDown };
}

Copy-paste example

useMenu.example.tsx
'use client';

import { useRef } from 'react';
import { useMenu } from '@/hooks';

export function Actions() {
  const triggerRef = useRef<HTMLButtonElement>(null);
  const contentRef = useRef<HTMLDivElement>(null);
  const menu = useMenu({ triggerRef, contentRef });
  return (
    <>
      <button
        ref={triggerRef}
        {...menu.getTriggerProps()}
        onKeyDown={(e) => menu.onKeyDown(e, 3)}
      >
        Actions
      </button>
      {menu.isOpen && (
        <div ref={contentRef}>
          <div data-active={menu.highlightedIndex === 0}>Edit</div>
          <div data-active={menu.highlightedIndex === 1}>Share</div>
          <div data-active={menu.highlightedIndex === 2}>Delete</div>
        </div>
      )}
    </>
  );
}

Production checklist

  • Called only from Client Components or other custom hooks
  • Type parameters filled in (e.g. useFetch<User[]>)
  • Tip: These are headless — you own the markup and styles.
  • Source of truth: open hooks/ui/useMenu.ts for implementation details