Skip to content

BUI Menu: support a controlled popover (triggerRef, isOpen, onOpenChange) #36077

Open

Description

@deiga

📜 Issue Labels

  • Please familiarize yourself with the issue labels used in this project: LABELS.md

🔎 Search Terms

BUI Menu triggerRef, Menu controlled isOpen, MenuTrigger custom anchor, Menu Popover controlled, react-aria Menu without MenuTrigger

🗃️ Project Area

Design System

🔖 Need

Menu from @backstage/ui can only be opened through MenuTrigger. It renders its own react-aria Popover internally and forwards only placement to it, so there is no way to pass triggerRef, isOpen or onOpenChange. Outside a MenuTrigger the popover has no state and no anchor, and renders nothing.

MenuTrigger requires the trigger to be its child and creates the trigger ref itself. That does not fit components where the anchor is only known at interaction time, or where the trigger is rendered by someone else. A concrete case is the MUI to BUI migration of @backstage-community/plugin-tech-insights-react (backstage/community-plugins#11473). Its ResultLinksMenu exposes a public open(element: Element) callback, and consumers call it from an arbitrary element, often inside their own wrapper component. MUI's Menu covered this with anchorEl and open.

Today the only options are:

  • Use BUI Popover, which does accept triggerRef and isOpen, with a hand-rolled <ul> of Links. This loses menu semantics and arrow-key navigation.
  • Use BUI Popover with react-aria's Menu and BUI MenuItems. This works, because react-aria Menu has an explicit fallback for rendering outside MenuTrigger. But it forces the plugin to depend on react-aria-components directly, pinned to exactly the range BUI uses. If the two ranges ever drift, two copies of react-aria end up in the app and MenuItem can no longer find the collection context of the outer Menu.

Plugins migrating off MUI's anchorEl pattern, and anyone wiring a menu to a context-menu or table-row trigger, would benefit.

📝 Proposal

Forward the react-aria Popover trigger props from Menu to its internal popover, so a controlled menu works like a controlled BUI Popover already does:

const anchorRef = useRef<Element | null>(null);
const [isOpen, setIsOpen] = useState(false);

<Menu
  triggerRef={anchorRef}
  isOpen={isOpen}
  onOpenChange={setIsOpen}
  placement="bottom end"
  onClose={() => setIsOpen(false)}
>
  <MenuItem href="/docs">Docs</MenuItem>
</Menu>

Implementation sketch for packages/ui/src/components/Menu/Menu.tsx:

  • Add triggerRef, isOpen, defaultOpen and onOpenChange to MenuPopoverOwnProps, typed from react-aria's PopoverProps.
  • Pass them through to RAPopover in Menu, MenuListBox, MenuAutocomplete and MenuAutocompleteListbox, next to the existing placement.
  • No behaviour change inside MenuTrigger: react-aria Popover only uses local state when isOpen or defaultOpen is set, and otherwise reads OverlayTriggerStateContext as today.

react-aria Menu already supports this shape. Its source falls back to a local useMenuTriggerState({}) when no MenuTrigger is present, and onClose on the menu is honoured, so item selection and Escape close the controlled popover.

The --trigger-width CSS variable that MenuTrigger provides would be absent in the controlled case. The menu content has min-width: 150px, so that is fine as a default.

🔄 Alternatives

- **Re-export react-aria's `Menu` (or `Popover` plus the inner menu styles) from `@backstage/ui`.** Avoids the direct `react-aria-components` dependency, but gives adopters an unstyled escape hatch rather than a first-class API.
- **Let `MenuTrigger` accept an external `triggerRef`.** react-aria's `MenuTrigger` creates its own ref and attaches press handling via `PressResponder`, so this would mean reimplementing `MenuTrigger` in BUI rather than wrapping it.
- **Restructure consumers so the trigger is a child of `MenuTrigger`.** Works for new code, but for existing public APIs such as `ResultLinksMenuInfo.open(element)` it is a breaking change, and it does not cover anchors that are not React elements the menu owns.

### Have you read the Code of Conduct?

- [x] I have read the [Code of Conduct](https://github.com/backstage/backstage/blob/master/CODE_OF_CONDUCT.md)

### Are you willing to submit a PR?

Yes, and I have enough information to get started

Activity

  1. reza686a commented on Oct 8, 2026

    @reza686a
  2. awanlin commented on Oct 15, 2026

    @awanlin
    Collaborator

    Thanks @deiga, there's some missing backticks in the proposal section if you could fix that would be handy 🙏

  3. awanlin commented on Oct 15, 2026

    @awanlin
    Collaborator

    @cdedreuille @Naycon, can you share your thoughts/feedback on this please 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:design-systemtype:suggestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions