Design an Action Menu – Frontend System Design

We’ll walk through the frontend design of a reusable dropdown menu — a trigger that opens a list of actions, not a select. Our goal is a component you can drop in a toolbar, a table row, or a nav, with click, optional hover, full keyboard support, nested submenus, and a public API that does not leak positioning math.
This is an accessibility interview that also happens to include a floating panel. Interviewers want roving focus, typeahead, focus restore, and flip/shift so the menu stays on screen. There is no meeting server and no CRDT. You’ll see item counts, the focus model, the positioner, and the edge cases that show up on touch and in overflow-hidden parents.
Problem Statement
Design a reusable dropdown menu component.
Requirements:
- A trigger button that opens a list of actions
- Click, hover (optional), and keyboard all work
- Nested submenus
- It can be used in a toolbar, a table row, or a nav
Focus on accessibility, positioning, and the public API.
Clarifying Questions
| # | Question | Interviewer’s Answer |
|---|---|---|
| 1 | Single select, multi-select, or actions? | Actions (a menu), not a select. |
| 2 | Mobile? | Yes. Long menus should scroll. Touch targets ≥ 44px. |
| 3 | Can it open upward if there is no room? | Yes. Flip and shift to stay on screen. |
Say the word “menu.” A select has a value, a listbox, and aria-activedescendant on a combobox. This component fires onSelect and closes. That choice drives roles: menu / menuitem, not listbox / option. Mobile means no hover-only submenus. Flip/shift means you need a positioner, not top: 100% in CSS.
Requirements
The clarifying answers become the contract:
- Action menu: items run a callback, they do not become a field value
- Open on click (and Enter/Space on the trigger). Hover-open is optional and never the only path
- Nested submenus, keyboard and (debounced) hover
- Works in a toolbar, a table row, or a nav — including inside overflow:hidden
- Long lists scroll; rows are at least 44px on touch
- Flip and shift so the panel stays in the viewport
Non-Functional Requirements
- One tab stop on the trigger. Arrows move inside the open menu.
- Focus returns to the trigger on close, including close-from-select and close-from-Escape.
- Do not render the list until first open, unless you have a measured reason to pre-render.
- No layout jump on the page. The menu is portaled; it does not expand the trigger’s parent.
Scalability Estimates
This is a component. The “scale” question is item count, submenu depth, and open cost — not DAU.
| Metric | Estimate | Calculation | Notes |
|---|---|---|---|
| Typical items | 5–20 | Toolbar / row actions | Render the full list on open |
| Long menu | 100–500 | Command palette–sized edge | Virtualize the list; keep typeahead |
| Submenu depth | 2 | Item → submenu | Deeper is rare; still use the same focus stack |
| Hover-open delay | 200–300 ms | Intentional pause | Cancels if the pointer leaves toward the parent |
| Typeahead window | ~500 ms | Buffer resets after idle | WAI-ARIA APG style |
| Touch row | ≥ 44px | From the clarifying answer | Do not shrink rows on iOS |
| Open cost | 1 portal + N items | Mount on first open | Unmount on close, or hide if you measured a flash |
If the interviewer asks “what if there are 10,000 actions?”, you virtualize the menu list the same way you virtualize any list: a window of rows, scroll the active item into view, typeahead jumps the window. You do not invent a backend for a toolbar menu.
Component API
type MenuItem =
| { type: "item"; id: string; label: string; shortcut?: string; disabled?: boolean; danger?: boolean; onSelect: () => void }
| { type: "submenu"; id: string; label: string; children: MenuItem[] }
| { type: "separator" };
type MenuProps = {
trigger: React.ReactNode;
items: MenuItem[];
align?: "start" | "end";
};
<Menu>
<MenuTrigger />
<MenuList>
<MenuItem />
<MenuSeparator />
<SubMenu />
</MenuList>
</Menu>
The compound tree is the nicer public API for a design system. The data array is the nicer interview API because you can talk about typeahead and disabled items without inventing ten child components. Offer both: a items prop for the common case, compound slots if the caller needs a custom row.
onSelect lives on the item, not on the menu, so a toolbar can reuse the same Menu shell with different actions. danger is a style + an extra confirm if you want it; it is not a different role. Separators have no id and no callback.
Architecture
This is a focus manager plus a floating panel. Keep the pieces small. There is no Network Manager, no SFU, and no document CRDT.
<Menu>
MenuController
FocusManager
Typeahead
Positioner
<MenuTrigger />
{open && createPortal(
<MenuList />,
document.body
)}
</Menu>
Component Structure and Responsibilities
- Menu: Owns
open, the items model, and the controller. Provides context to trigger and list. Unmounts the list when closed unless you opt into “keep mounted.” - MenuTrigger: The only tab stop when closed. Renders a button (or wraps one). Sets
aria-haspopup="menu",aria-expanded,aria-controls. - MenuList: The portaled panel.
role="menu". Handles keydown, pointer move, and outside click. Scrolls the active item into view. - MenuItem:
role="menuitem". Disabled items stay in the tree and are skipped by arrows. Danger items are still menuitems. - MenuSeparator:
role="separator". Not focusable. Arrows skip it. - SubMenu: A menuitem that owns a nested MenuList. Open on ArrowRight or after the hover delay. Close on ArrowLeft or parent close.
Controller, Focus, and Positioning
The controller is the “data manager” for a menu. It is all in-memory.
- FocusManager: Roving tabindex. Exactly one item has
tabIndex={0}; the rest are-1. Arrows, Home, End move that token. You talk about the pattern in the interview; you do not sprinkle tabindex attributes through static HTML. - Typeahead: A short string buffer. Printable keys append. Match prefix against enabled item labels, case-insensitive. Reset after ~500 ms of idle. Repeat the same letter cycles matches (“D” → Duplicate → Delete).
- Positioner: Floating-ui / Popper-style. Read trigger rect and menu rect. Try below-start (or
align="end"). If it overflows the viewport, flip to above. If it still overflows, shift on the inline axis. Recompute on scroll and resize. - Dismiss: Pointer down outside, Escape, select, tab-away, scroll of a clipping ancestor if you cannot follow it. Each path ends in
close()+ focus restore.
Portals to document.body so overflow:hidden parents don’t clip the menu. The positioner then has to use the trigger’s viewport coordinates, not offsetParent math inside the table cell.
Open / Close Workflow
- User clicks the trigger (or presses Enter/Space).
openbecomes true. The list portals. Positioner measures and writestop/left(or a transform). - Focus moves into the menu, onto the first enabled item (or the last remembered item if you add that).
- Arrows move the roving item. Typeahead jumps. Hover moves the visual active item only after a small delay so a stray pass does not steal keyboard focus.
- ArrowRight on a submenu item opens the child list and focuses its first enabled item. The parent item stays visually active.
- Enter/Space on an enabled item runs
onSelect, then closes the whole stack. Disabled items ignore select. - Escape closes the current list. If that list is a submenu, focus returns to the parent item. If it is the root, the menu closes and focus returns to the trigger.
- Outside click or Tab closes the stack and restores focus to the trigger (Tab then proceeds to the next page tab stop from there).
Pointer and keyboard share one “active index.” If you keep two, the highlight and the focused node drift, and that is the bug interviewers have seen a dozen times.
Behavior
- Click trigger toggles. Click outside or Escape closes.
- Arrow keys move; Enter/Space selects; Home/End jump.
- Typeahead: typing “de” highlights Delete.
- Submenus open on ArrowRight (or hover after a short delay) and close on ArrowLeft.
- Focus returns to the trigger on close.
Building the positioner

“Use Floating UI” is not the answer. Walk the function you would call after the menu mounts. The menu is portaled to document.body so an overflow: hidden toolbar cannot clip it. Then:
function place(trigger: DOMRect, menu: { w: number; h: number }, viewport: DOMRect, opts: { side: "bottom" | "top" | "right" | "left"; align: "start" | "center" | "end" }) {
// 1. preferred: bottom-start = (trigger.left, trigger.bottom + gap)
// 2. flip: if it overflows the viewport on that side, try the opposite side
// 3. shift: if it still overflows x or y, slide along the axis until it fits (min 8px inset)
// 4. size: if the menu is taller than the remaining space, set maxHeight and make the list scroll
}
Re-run that on scroll, resize, and when a submenu opens (the child measures itself). Use a ResizeObserver on the menu and an IntersectionObserver / scroll listeners on every scroll parent of the trigger, not just window. A positioner that only listens to the window is how a menu detaches inside a modal.
Submenus are a second place() with side: "right" relative to the row, not the trigger. If the right side does not fit, flip to left. The hover-bridge (Fitts-law triangle) is a hit-area between the parent row and the submenu you keep open while the pointer is inside that triangle; a 200–300 ms close delay is the cheap version of the same idea.
RTL: ArrowRight/ArrowLeft flip. The positioner flips align="start" too. Submenus open toward the remaining space, not always to the physical right.
Typeahead is a buffer with a 500ms reset: each character appends, you highlight the first enabled item whose label starts with the buffer (case-insensitive). A single letter cycles matches. Do not filter the list of an action menu — highlight, do not hide. A combobox filters; a menu does not.
Hover is a convenience, not a mode. On touch, ignore hover entirely — tap opens, tap outside closes.
State and Data Flow
State is local and small. Lift it only if a parent must know open (a toolbar that closes other menus).
- Trigger event →
setOpen(true)→ mount list → measure → place. - Items are a pure model. The list maps them to rows. Separators do not get an index in the focus order; they take a slot in the visual list.
- Keydown and pointermove both call
setActiveId. The focused node followsactiveId. - Select calls the item’s
onSelect, thencloseAll(). The menu does not wait for the callback to finish unless you add an async “pending” row. - Close clears the typeahead buffer, clears submenu timers, unmounts portals, and focuses the trigger.
If items are fetched (rare for an action menu), the parent passes items in. The menu can show a non-focusable “Loading” row. Do not hide the trigger while loading.
Data Model
Extend the public item union with the runtime state the controller needs. Do not put DOM nodes in the item model.
type MenuItem =
| { type: "item"; id: string; label: string; shortcut?: string; disabled?: boolean; danger?: boolean; onSelect: () => void }
| { type: "submenu"; id: string; label: string; children: MenuItem[] }
| { type: "separator" };
type MenuProps = {
trigger: React.ReactNode;
items: MenuItem[];
align?: "start" | "end";
};
type MenuState = {
open: boolean;
activeId: string | null;
submenuPath: string[];
typeahead: string;
};
type FocusableItem = Extract<MenuItem, { type: "item" | "submenu" }>;
function focusable(items: MenuItem[]): FocusableItem[] {
return items.filter(
(item): item is FocusableItem => item.type !== "separator" && !("disabled" in item && item.disabled)
);
}
submenuPath is the stack of open submenu ids. Close-all clears it. ArrowLeft pops one id. That is the whole nesting model.
API Design
The public surface is the component, plus a small imperative handle if a toolbar needs to close the menu from outside.
| Action | Method / Prop | Description |
|---|---|---|
| Render | <Menu items align trigger> | Controlled or uncontrolled open / onOpenChange. |
| Open | handle.open() | Mounts the list, places it, focuses first enabled item. |
| Close | handle.close() | Unmounts, restores focus to the trigger. |
| Select | item.onSelect() | Caller-supplied. Menu closes after invoke. |
| Update items | items= next | Rebuild focusable list. Clamp activeId if it vanished. |
Example: Consuming the Menu
const items: MenuItem[] = [
{ type: "item", id: "dup", label: "Duplicate", shortcut: "⌘D", onSelect: () => duplicate() },
{ type: "item", id: "del", label: "Delete", danger: true, onSelect: () => remove() },
{ type: "separator" },
{
type: "submenu",
id: "move",
label: "Move to",
children: folders.map((f) => ({
type: "item",
id: f.id,
label: f.name,
onSelect: () => moveTo(f.id)
}))
}
];
<Menu trigger={<button type="button">Actions</button>} items={items} align="end" />
The trigger you pass must be a focusable button. If the caller passes a <div>, the Menu should wrap it or warn — a non-button trigger fails the keyboard half of the interview.
Performance and Edge Cases
- Don’t render the list until first open (or keep it rendered and hidden if you need instant open)
- Disabled items are in the a11y tree but skipped by arrows
- Scroll the active item into view in a long menu
- iOS: 44px rows; don’t rely on hover
Virtualization only when N is large. A 12-item menu that uses a windowed list is harder to get right (height, typeahead, Home/End) and buys you nothing. Measure first.
function place(trigger: DOMRect, menu: DOMRect, viewW: number, viewH: number, align: "start" | "end") {
let top = trigger.bottom + 4;
if (top + menu.height > viewH) {
top = trigger.top - menu.height - 4;
}
let left = align === "end" ? trigger.right - menu.width : trigger.left;
if (left + menu.width > viewW) left = Math.max(8, viewW - menu.width - 8);
if (left < 8) left = 8;
if (top < 8) top = 8;
return { top, left };
}
function onMenuKeydown(
e: KeyboardEvent,
items: FocusableItem[],
activeId: string,
setActive: (id: string) => void,
close: () => void,
typeahead: (ch: string) => void
) {
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
e.preventDefault();
const i = items.findIndex((item) => item.id === activeId);
const next = e.key === "ArrowDown"
? items[(i + 1) % items.length]
: items[(i - 1 + items.length) % items.length];
if (next) setActive(next.id);
}
if (e.key === "Home") setActive(items[0]!.id);
if (e.key === "End") setActive(items[items.length - 1]!.id);
if (e.key === "Escape") close();
if (e.key.length === 1 && !e.ctrlKey && !e.metaKey) typeahead(e.key);
}
preventDefault on the arrows so the page does not scroll behind the menu. Home/End should not fall through to the document either while the menu is open.
Handling User Edge Cases
| Case | Strategy |
|---|---|
| ❌ Empty items | Do not open, or open a single disabled “No actions” row. Never mount an empty role="menu". |
| ❌ All items disabled | Open the menu, focus the list itself, arrows no-op. Escape still closes. |
| 🔄 Trigger unmounts while open | Close without focus restore. Do not search the document for a new tab stop. |
| 📶 Items array replaced | If activeId is gone, move to the nearest enabled neighbor. Submenus in the old path close. |
| 🖱️ Pointer leaves toward submenu | Keep the submenu open along the path; cancel the close timer if the pointer enters the child. |
| 📱 Touch + hover delay | No hover-open. First tap opens, second tap on an item selects. |
| ⌨️ Typeahead “de” | Buffer “d” then “e” within 500 ms. Highlight Delete, not Duplicate. |
| 🪟 Scroll / resize | Reposition. If the trigger leaves the viewport, close. |
Pro-Level Optimizations
| Area | Suggestion |
|---|---|
| Mount | Render on first open. If the first-open flash is visible, pre-render hidden once, then keep the instance. |
| Virtualize | Only above ~100 items. Active row must stay mounted so focus is not lost mid-arrow. |
| Submenu timers | One shared delay controller. Opening B closes A without a flicker gap. |
| Scroll into view | element.scrollIntoView({ block: "nearest" }) on activeId change. Do not jump the page. |
| Reduced motion | Skip the open animation when prefers-reduced-motion: reduce. Instant show/hide. |
| Toolbar sibling | ArrowLeft/Right on a closed trigger can move to the next toolbar control (APG toolbar pattern). |
Accessibility
This is the interview.
- Trigger:
aria-haspopup="menu",aria-expanded,aria-controls - List:
role="menu" - Items:
role="menuitem"(ormenuitemcheckbox) - Don’t put
tabindex="0"on every item. Roving tabindex: one tab stop, arrows movetabindex="0". - Separators are
role="separator"and skipped by the keyboard.
Name the trigger from its visible label. If it is an icon button in a table row, the name is “Actions for Order 1842”, not “•••”. Shortcuts are extra text on the row; they do not replace the label.
Focus restore is part of a11y, not a nice-to-have. Screen-reader users who select Delete and land at the document top have lost the row they were on. Restore to the trigger; the row’s next action is still there.
Do not toggle aria-hidden on the rest of the page unless you are building a modal menu. A toolbar menu is non-modal: the rest of the page stays readable. Outside click still dismisses.
Security
An action menu is a privilege boundary if those actions mutate data. The UI can hide Delete. The server still has to reject it. Labels come from your app, but if any label is user-generated (a folder name in “Move to”), render it as text, not HTML.
Shortcuts that fire without the menu open (⌘D) belong to the page’s keymap, not to a closed menu. Do not listen for those keys globally from an unmounted list.
Interview details people skip
These are the pieces that show up once you have a working menu. Interviewers use them to separate a styled list from a real Menu Button.
More than one menu can stay open. That is a product call, not a bug. If the interviewer allows it, a document-level coordinator registers every trigger and every portaled list — including menus that are currently closed. A completed click is “outside” only when event.composedPath() misses every registered branch. Clicking a sibling trigger is not an outside click. Attach that listener only while at least one menu is open. Use the click event, not pointerdown, so a canceled drag or a scroll gesture does not dismiss.
Tab and Escape are different close paths. Escape restores focus to the trigger. Tab / Shift+Tab close the menu and continue the page’s focus order from there — they do not bounce back to the trigger. An outside click leaves focus on whatever was clicked. Item activation restores the trigger unless the action itself moves you (Delete row, open a dialog). One close() function, four restore policies.
Build an item registry, then focus programmatically. Each item registers { id, ref, disabled, textValue } on mount and unregisters on removal. The root owns DOM order. Typeahead matches textValue, which matters when the row is an icon plus a badge, not a plain string. The APG Menu pattern puts tabindex="-1" on every menuitem and calls ref.focus() for the active one. aria-activedescendant is the listbox / combobox model. Do not mix them.
Disabled items stay in the arrow path and refuse onSelect. That is the APG rule. Skipping them with arrows is the simpler interview version — say which one you picked. If the active item unmounts, move to the nearest remaining id or clear it.
Hide means hide. A menu that is in the HTML from the start must use hidden or display: none. opacity: 0 still receives clicks and still sits in the accessibility tree. Pre-rendering the list is a tiny runtime win and a larger HTML cost; most action menus never open, so mounting on first open is the default. If you do pre-render, the closed list is not role="menu" until it is shown.
Placement uses one coordinate system. A portaled list should read getBoundingClientRect() and position with position: fixed (viewport) or add scroll offsets (document). offsetTop / offsetLeft are relative to an offsetParent, not the page. Recompute on ancestor scroll and resize, not just window. Default maxHeight around 200–300px so a long list scrolls inside the panel.
RTL inherits dir and uses logical CSS (inset-inline-start, align="start"). Do not mirror every icon and do not reverse Home / End / typeahead just because the page is RTL. ArrowLeft / ArrowRight for submenus do flip with the inline direction.
Conclusion: How to Design an Action Menu
A dropdown is an accessibility component that also happens to be a menu. If you skip roving tabindex and focus restore, you failed the hard part.
The rest of a senior answer is the positioner and the pointer/keyboard contract. Portal to document.body, flip and shift to stay on screen, one active id for both input devices, and a 200–300 ms submenu delay so hover does not fight the keyboard. Touch never depends on hover. Long lists scroll at 44px a row; very long lists virtualize without dropping the focused row.
Keep the public API boring: a trigger, an item union, align, and onSelect. The controller behind it — focus, typeahead, dismiss, place — is where the design work is. That is the interview.
