Skip to content

Pass id prop to the menu item's DOM element #7978

Open
@damianstasik

Description

@damianstasik

Provide a general summary of the feature here

Could you please consider adding a way to set an id attribute on the DOM element rendered by menu items?

🤔 Expected Behavior?

Currently, when you pass an id to MenuItem, it is not applied to the DOM element that the menu item renders. This is similar to the behavior of the Menu component, which accepts an id but does not use it as expected.

😯 Current Behavior

The id prop is filtered out of the DOM props and instead, the value from the custom useId hook is used.

💁 Possible Solution

From what I've seen, the current logic filters out the id prop due to special handling of the submenu triggers. A naive fix would be to fall back to user-provided ID if none is provided by the MenuItemContext:

  let {menuItemProps, labelProps, descriptionProps, keyboardShortcutProps, ...states} = useMenuItem({
    ...props,
-   id,
+   id: id ?? props.id,
    key: item.key,
    selectionManager
  }, state, ref);

🔦 Context

What are you trying to accomplish?

I was testing the Autocomplete component and needed to determine which menu item had virtual focus through AutocompleteStateContext. Unfortunately, the focusedNodeId property doesn't point to my provided id on the menu item, but instead to the internally generated one from useId.

💻 Examples

No response

🧢 Your Company/Team

No response

🕷 Tracking Issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    waitingWaiting on Issue Author

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions